1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | const invariant = require('invariant');
|
14 |
|
15 | import type EventSubscription from 'EventSubscription';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class EventSubscriptionVendor {
|
22 | _subscriptionsForType: Object;
|
23 | _currentSubscription: ?EventSubscription;
|
24 |
|
25 | constructor() {
|
26 | this._subscriptionsForType = {};
|
27 | this._currentSubscription = null;
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | addSubscription(
|
37 | eventType: string,
|
38 | subscription: EventSubscription,
|
39 | ): EventSubscription {
|
40 | invariant(
|
41 | subscription.subscriber === this,
|
42 | 'The subscriber of the subscription is incorrectly set.',
|
43 | );
|
44 | if (!this._subscriptionsForType[eventType]) {
|
45 | this._subscriptionsForType[eventType] = [];
|
46 | }
|
47 | const key = this._subscriptionsForType[eventType].length;
|
48 | this._subscriptionsForType[eventType].push(subscription);
|
49 | subscription.eventType = eventType;
|
50 | subscription.key = key;
|
51 | return subscription;
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | removeAllSubscriptions(eventType: ?string) {
|
61 | if (eventType === undefined) {
|
62 | this._subscriptionsForType = {};
|
63 | } else {
|
64 | delete this._subscriptionsForType[eventType];
|
65 | }
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | removeSubscription(subscription: Object) {
|
75 | const eventType = subscription.eventType;
|
76 | const key = subscription.key;
|
77 |
|
78 | const subscriptionsForType = this._subscriptionsForType[eventType];
|
79 | if (subscriptionsForType) {
|
80 | delete subscriptionsForType[key];
|
81 | }
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | getSubscriptionsForType(eventType: string): ?[EventSubscription] {
|
97 | return this._subscriptionsForType[eventType];
|
98 | }
|
99 | }
|
100 |
|
101 | module.exports = EventSubscriptionVendor;
|