Skip to content

Commit 933c6a2

Browse files
authored
Merge pull request particle-iot#50 from AntonPuko/events
move events filtering to EventPublisher
2 parents 06ff6d5 + 255097d commit 933c6a2

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

src/controllers/EventsController.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class EventsController extends Controller {
6363
const subscriptionID = this._eventManager.subscribe(
6464
eventName,
6565
this._pipeEvent.bind(this),
66-
this.user.id,
66+
{ userID: this.user.id },
6767
);
6868

6969
await this._closeStream(subscriptionID);
@@ -77,8 +77,10 @@ class EventsController extends Controller {
7777
const subscriptionID = this._eventManager.subscribe(
7878
eventName,
7979
this._pipeEvent.bind(this),
80-
this.user.id,
81-
deviceID,
80+
{
81+
deviceID,
82+
userID: this.user.id,
83+
},
8284
);
8385

8486
await this._closeStream(subscriptionID);

src/managers/EventManager.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,27 @@
33
import type { EventPublisher } from 'spark-protocol';
44
import type { Event, EventData } from '../types';
55

6+
type FilterOptions = {
7+
deviceID?: string,
8+
userID?: string,
9+
};
10+
611
class EventManager {
712
_eventPublisher: EventPublisher;
813

914
constructor(eventPublisher: EventPublisher) {
1015
this._eventPublisher = eventPublisher;
1116
}
1217

13-
_filterEvents = (
14-
eventHandler: (event: Event) => void,
15-
userID?: string,
16-
deviceID?: string,
17-
): (event: Event) => void =>
18-
(event: Event) => {
19-
if (
20-
event.deviceID &&
21-
userID && userID !== event.userID
22-
) {
23-
return;
24-
}
25-
26-
if (deviceID && deviceID !== event.deviceID) {
27-
return;
28-
}
29-
30-
eventHandler(event);
31-
};
32-
3318
subscribe = (
3419
eventName: ?string,
3520
eventHandler: (event: Event) => void,
36-
userID?: string,
37-
deviceID?: string,
21+
filterOptions?: FilterOptions,
3822
): string =>
3923
this._eventPublisher.subscribe(
4024
eventName,
41-
this._filterEvents(eventHandler, userID, deviceID),
42-
deviceID,
25+
eventHandler,
26+
filterOptions,
4327
);
4428

4529
unsubscribe = (subscriptionID: string): void =>

0 commit comments

Comments
 (0)