@eleven-am/pondsocket 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/client/channel.js +200 -0
  2. package/dist/client/index.d.ts +122 -0
  3. package/{pondClient/socket.js → dist/client/index.js} +30 -44
  4. package/dist/express/index.d.ts +36 -0
  5. package/dist/express/index.js +16 -0
  6. package/dist/index.d.ts +340 -0
  7. package/dist/index.js +4 -0
  8. package/dist/server/abstracts/abstractRequest.js +40 -0
  9. package/dist/server/abstracts/abstractRequest.test.js +41 -0
  10. package/dist/server/abstracts/abstractResponse.js +6 -0
  11. package/dist/server/abstracts/middleware.js +38 -0
  12. package/dist/server/abstracts/middleware.test.js +70 -0
  13. package/dist/server/channel/channelEngine.js +279 -0
  14. package/dist/server/channel/channelEngine.test.js +377 -0
  15. package/dist/server/channel/channelRequest.test.js +29 -0
  16. package/dist/server/channel/channelResponse.test.js +134 -0
  17. package/dist/server/channel/eventRequest.js +18 -0
  18. package/dist/server/channel/eventResponse.js +141 -0
  19. package/dist/server/endpoint/connectionResponse.js +50 -0
  20. package/dist/server/endpoint/endpoint.js +269 -0
  21. package/dist/server/endpoint/endpoint.test.js +406 -0
  22. package/dist/server/endpoint/endpointResponse.test.js +43 -0
  23. package/dist/server/pondChannel/joinRequest.js +29 -0
  24. package/dist/server/pondChannel/joinResponse.js +96 -0
  25. package/dist/server/pondChannel/pondChannel.js +161 -0
  26. package/dist/server/pondChannel/pondChannelResponse.test.js +108 -0
  27. package/dist/server/presence/presenceEngine.js +112 -0
  28. package/dist/server/presence/presenceEngine.test.js +104 -0
  29. package/dist/server/server/pondSocket.js +122 -0
  30. package/{pondSocket → dist/server/server}/server.test.js +7 -21
  31. package/{pondBase/baseClass.js → dist/server/utils/matchPattern.js} +33 -43
  32. package/{pondBase/baseClass.test.js → dist/server/utils/matchPattern.test.js} +16 -25
  33. package/dist/server/utils/subjectUtils.js +68 -0
  34. package/package.json +28 -12
  35. package/.eslintrc.js +0 -28
  36. package/base.d.ts +0 -1
  37. package/base.js +0 -17
  38. package/client.d.ts +0 -1
  39. package/client.js +0 -17
  40. package/index.d.ts +0 -1
  41. package/index.js +0 -17
  42. package/jest.config.js +0 -11
  43. package/pondBase/baseClass.d.ts +0 -55
  44. package/pondBase/enums.d.ts +0 -9
  45. package/pondBase/enums.js +0 -14
  46. package/pondBase/index.d.ts +0 -6
  47. package/pondBase/index.js +0 -22
  48. package/pondBase/pondBase.d.ts +0 -41
  49. package/pondBase/pondBase.js +0 -60
  50. package/pondBase/pondBase.test.js +0 -101
  51. package/pondBase/pubSub.d.ts +0 -82
  52. package/pondBase/pubSub.js +0 -158
  53. package/pondBase/pubSub.test.js +0 -332
  54. package/pondBase/simpleBase.d.ts +0 -126
  55. package/pondBase/simpleBase.js +0 -211
  56. package/pondBase/simpleBase.test.js +0 -153
  57. package/pondBase/types.d.ts +0 -2
  58. package/pondBase/types.js +0 -2
  59. package/pondClient/channel.d.ts +0 -77
  60. package/pondClient/channel.js +0 -167
  61. package/pondClient/index.d.ts +0 -2
  62. package/pondClient/index.js +0 -18
  63. package/pondClient/socket.d.ts +0 -41
  64. package/pondSocket/channel.d.ts +0 -129
  65. package/pondSocket/channel.js +0 -287
  66. package/pondSocket/channel.test.js +0 -377
  67. package/pondSocket/channelMiddleWare.d.ts +0 -28
  68. package/pondSocket/channelMiddleWare.js +0 -36
  69. package/pondSocket/endpoint.d.ts +0 -90
  70. package/pondSocket/endpoint.js +0 -320
  71. package/pondSocket/endpoint.test.js +0 -490
  72. package/pondSocket/enums.d.ts +0 -19
  73. package/pondSocket/enums.js +0 -25
  74. package/pondSocket/index.d.ts +0 -7
  75. package/pondSocket/index.js +0 -23
  76. package/pondSocket/pondChannel.d.ts +0 -79
  77. package/pondSocket/pondChannel.js +0 -211
  78. package/pondSocket/pondChannel.test.js +0 -430
  79. package/pondSocket/pondResponse.d.ts +0 -25
  80. package/pondSocket/pondResponse.js +0 -120
  81. package/pondSocket/pondSocket.d.ts +0 -47
  82. package/pondSocket/pondSocket.js +0 -94
  83. package/pondSocket/socketMiddleWare.d.ts +0 -6
  84. package/pondSocket/socketMiddleWare.js +0 -32
  85. package/pondSocket/types.d.ts +0 -74
  86. package/pondSocket/types.js +0 -2
  87. package/socket.d.ts +0 -1
  88. package/socket.js +0 -17
  89. package/tsconfig.eslint.json +0 -5
  90. package/tsconfig.json +0 -90
@@ -1,101 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const pondBase_1 = require("./pondBase");
4
- const enums_1 = require("./enums");
5
- describe('PondBase', () => {
6
- it('should be able to take in a subscriber', () => {
7
- const base = new pondBase_1.PondBase();
8
- expect(base['_broadcast']['_subscribers'].size).toBe(0);
9
- const mockSubscriber = jest.fn();
10
- base.subscribe(mockSubscriber);
11
- expect(base['_broadcast']['_subscribers'].size).toBe(1);
12
- });
13
- it('should be able to remove a subscriber', () => {
14
- const base = new pondBase_1.PondBase();
15
- expect(base['_broadcast']['_subscribers'].size).toBe(0);
16
- const mockSubscriber = jest.fn();
17
- const subscription = base.subscribe(mockSubscriber);
18
- expect(base['_broadcast']['_subscribers'].size).toBe(1);
19
- subscription.unsubscribe();
20
- expect(base['_broadcast']['_subscribers'].size).toBe(0);
21
- });
22
- it('should fire the subscriber when a document is added', () => {
23
- const base = new pondBase_1.PondBase();
24
- const mockSubscriber = jest.fn();
25
- base.subscribe(mockSubscriber);
26
- base.set('test', { name: 'test' });
27
- expect(mockSubscriber).toBeCalled();
28
- expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
29
- });
30
- it('should fire the subscriber when a document is removed', () => {
31
- const base = new pondBase_1.PondBase();
32
- const mockSubscriber = jest.fn();
33
- base.subscribe(mockSubscriber);
34
- const data = base.set('test', { name: 'test' });
35
- expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
36
- mockSubscriber.mockClear();
37
- data.removeDoc();
38
- expect(mockSubscriber).toBeCalledWith([], { name: "test" }, enums_1.PondBaseActions.REMOVE_FROM_POND);
39
- });
40
- it('should fire the subscriber when a document is updated', () => {
41
- const base = new pondBase_1.PondBase();
42
- const mockSubscriber = jest.fn();
43
- base.subscribe(mockSubscriber);
44
- const data = base.set('test', { name: 'test' });
45
- expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
46
- mockSubscriber.mockClear();
47
- data.updateDoc({ name: 'test2' });
48
- expect(mockSubscriber).toBeCalledWith([{ name: "test2" }], { name: "test2" }, enums_1.PondBaseActions.UPDATE_IN_POND);
49
- });
50
- it('should add a document to the database', () => {
51
- var _a, _b;
52
- const base = new pondBase_1.PondBase();
53
- const data = base.addDoc({ name: 'test' });
54
- expect(data.id).toBeDefined();
55
- expect((_a = base.get(data.id)) === null || _a === void 0 ? void 0 : _a.doc).toEqual(data.doc);
56
- expect((_b = base.get(data.id)) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
57
- });
58
- it('should left join with another pond', () => {
59
- const owners = new pondBase_1.PondBase();
60
- const pets = new pondBase_1.PondBase();
61
- owners.addDoc({ name: 'test', age: 10 });
62
- owners.set('test2', { name: 'test2', age: 12 });
63
- pets.addDoc({ name: 'test', owner: 'test' });
64
- pets.addDoc({ name: 'test2', owner: 'test2' });
65
- pets.set('test3', { name: 'test3', owner: 'test3' });
66
- const joined = pets.leftJoin(owners, 'owner', 'name');
67
- expect([...joined].map(({ doc }) => doc)).toEqual([
68
- { name: 'test', owner: { name: 'test', age: 10 } },
69
- { name: 'test2', owner: { name: 'test2', age: 12 } },
70
- { name: 'test3', owner: null }
71
- ]);
72
- });
73
- it('should be able to get the keys', () => {
74
- const base = new pondBase_1.PondBase();
75
- base.set('test', { name: 'test' });
76
- base.set('test2', { name: 'test2' });
77
- expect(base.keys).toEqual(['test', 'test2']);
78
- });
79
- it('should be able to get the values', () => {
80
- const base = new pondBase_1.PondBase();
81
- base.addDoc({ name: 'test' });
82
- base.set('test2', { name: 'test2' });
83
- expect(base.values).toEqual([{ name: 'test' }, { name: 'test2' }]);
84
- });
85
- it('should be able to upsert a document', () => {
86
- var _a, _b;
87
- const base = new pondBase_1.PondBase();
88
- base.upsert('test', { name: 'test' });
89
- expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
90
- base.upsert('test', { name: 'test2' });
91
- expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test2' });
92
- });
93
- it('should be able to getOrCreate a document with a function', () => {
94
- var _a, _b;
95
- const base = new pondBase_1.PondBase();
96
- base.getOrCreate('test', () => ({ name: 'test' }));
97
- expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
98
- base.getOrCreate('test', () => ({ name: 'test2' }));
99
- expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
100
- });
101
- });
@@ -1,82 +0,0 @@
1
- export declare class Subscription {
2
- unsubscribe(): void;
3
- }
4
-
5
- export declare class Broadcast<T, A> {
6
-
7
- /**
8
- * @desc Subscribe to the broadcast
9
- * @param handler - The handler to call when the broadcast is published
10
- */
11
- subscribe(handler: (data: T) => A): Subscription;
12
-
13
- /**
14
- * @desc Publish to the broadcast
15
- * @param data - The data to publish
16
- */
17
- publish(data: T): A | undefined;
18
-
19
- /**
20
- * @dec clears all subscribers
21
- */
22
- clear(): void;
23
- }
24
-
25
- export declare class Subject<T, A> extends Broadcast<T, A> {
26
- constructor(value: T);
27
-
28
- /**
29
- * @desc Get the current value of the subject
30
- */
31
- get value(): T;
32
-
33
- /**
34
- * @desc Get the list of observers
35
- * @returns The list of observers
36
- */
37
- get observers(): Set<(data: T) => Anything<A>>;
38
-
39
- /**
40
- * @desc Subscribe to the subject
41
- */
42
- subscribe(handler: (data: T) => A): Subscription;
43
-
44
- /**
45
- * @desc Publish to the subject
46
- */
47
- publish(data: T): Anything<A>;
48
- }
49
-
50
- export declare class EventPubSub<T, A> {
51
-
52
- /**
53
- * @desc Subscribe to the event subject
54
- * @param event - The event to subscribe to
55
- * @param handler - The handler to call when the event subject is published
56
- */
57
- subscribe(event: string, handler: (data: T) => Anything<A>): Subscription;
58
-
59
- /**
60
- * @desc Publish to the event subject
61
- * @param event - The event to publish
62
- * @param data - The data to publish
63
- */
64
- publish(event: string, data: T): void;
65
-
66
- /**
67
- * @desc Subscribe to all events
68
- * @param handler - The handler to call when the event subject is published
69
- */
70
- subscribeAll(handler: (event: T) => A): Subscription;
71
-
72
- /**
73
- * @desc Complete the event subject
74
- */
75
- complete(): void;
76
-
77
- /**
78
- * @desc Subscribe to the event subject completion
79
- * @param handler - The handler to call when the event subject is completed
80
- */
81
- onComplete(handler: () => void): void;
82
- }
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventPubSub = exports.Subject = exports.Broadcast = void 0;
4
- class Broadcast {
5
- constructor() {
6
- this._subscribers = new Set();
7
- }
8
- /**
9
- * @desc Subscribe to the broadcast
10
- * @param handler - The handler to call when the broadcast is published
11
- */
12
- subscribe(handler) {
13
- this._subscribers.add(handler);
14
- return {
15
- /**
16
- * @desc Unsubscribe from the broadcast
17
- */
18
- unsubscribe: () => {
19
- this._subscribers.delete(handler);
20
- }
21
- };
22
- }
23
- /**
24
- * @desc Publish to the broadcast
25
- * @param data - The data to publish
26
- */
27
- publish(data) {
28
- let result;
29
- for (const subscriber of this._subscribers) {
30
- try {
31
- result = subscriber(data);
32
- if (result)
33
- break;
34
- }
35
- catch (e) {
36
- throw e;
37
- }
38
- }
39
- return result;
40
- }
41
- /**
42
- * @dec clears all subscribers
43
- */
44
- clear() {
45
- this._subscribers.clear();
46
- }
47
- }
48
- exports.Broadcast = Broadcast;
49
- class Subject extends Broadcast {
50
- constructor(value) {
51
- super();
52
- this._value = value;
53
- }
54
- /**
55
- * @desc Get the current value of the subject
56
- */
57
- get value() {
58
- return this._value;
59
- }
60
- /**
61
- * @desc Get the list of observers
62
- * @returns The list of observers
63
- */
64
- get observers() {
65
- return this._subscribers;
66
- }
67
- /**
68
- * @desc Subscribe to the subject
69
- */
70
- subscribe(handler) {
71
- handler(this._value);
72
- return super.subscribe(handler);
73
- }
74
- /**
75
- * @desc Publish to the subject
76
- */
77
- publish(data) {
78
- if (this._value !== data) {
79
- this._value = data;
80
- return super.publish(data);
81
- }
82
- }
83
- }
84
- exports.Subject = Subject;
85
- class EventPubSub {
86
- constructor() {
87
- this._subscribers = new Set();
88
- }
89
- /**
90
- * @desc Subscribe to the event subject
91
- * @param event - The event to subscribe to
92
- * @param handler - The handler to call when the event subject is published
93
- */
94
- subscribe(event, handler) {
95
- const subscriber = (eventData) => {
96
- if (eventData.type === event)
97
- return handler(eventData.data);
98
- };
99
- this._subscribers.add(subscriber);
100
- return {
101
- /**
102
- * @desc Unsubscribe from the event subject
103
- */
104
- unsubscribe: () => {
105
- this._subscribers.delete(subscriber);
106
- }
107
- };
108
- }
109
- /**
110
- * @desc Publish to the event subject
111
- * @param event - The event to publish
112
- * @param data - The data to publish
113
- */
114
- publish(event, data) {
115
- for (const subscriber of this._subscribers) {
116
- try {
117
- subscriber({ type: event, data });
118
- }
119
- catch (e) {
120
- throw e;
121
- }
122
- }
123
- }
124
- /**
125
- * @desc Subscribe to all events
126
- * @param handler - The handler to call when the event subject is published
127
- */
128
- subscribeAll(handler) {
129
- const subscriber = (eventData) => {
130
- return handler(eventData.data);
131
- };
132
- this._subscribers.add(subscriber);
133
- return {
134
- /**
135
- * @desc Unsubscribe from the event subject
136
- */
137
- unsubscribe: () => {
138
- this._subscribers.delete(subscriber);
139
- }
140
- };
141
- }
142
- /**
143
- * @desc Complete the event subject
144
- */
145
- complete() {
146
- this._subscribers.clear();
147
- if (this._onComplete)
148
- this._onComplete();
149
- }
150
- /**
151
- * @desc Subscribe to the event subject completion
152
- * @param handler - The handler to call when the event subject is completed
153
- */
154
- onComplete(handler) {
155
- this._onComplete = handler;
156
- }
157
- }
158
- exports.EventPubSub = EventPubSub;
@@ -1,332 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const pubSub_1 = require("./pubSub");
4
- describe('Broadcast', () => {
5
- it('should be defined', () => {
6
- expect(pubSub_1.Broadcast).toBeDefined();
7
- });
8
- it('should be a class', () => {
9
- expect(pubSub_1.Broadcast).toBeInstanceOf(Function);
10
- });
11
- // Functionality tests
12
- it('should publish a message to all subscribers', () => {
13
- const broadcast = new pubSub_1.Broadcast();
14
- const subscriber = jest.fn();
15
- broadcast.subscribe(subscriber); // test with one subscriber
16
- broadcast.publish('Hello');
17
- expect(subscriber).toHaveBeenCalledWith('Hello');
18
- });
19
- it('should publish a message to all subscribers', () => {
20
- const broadcast = new pubSub_1.Broadcast();
21
- const subscriber1 = jest.fn();
22
- const subscriber2 = jest.fn();
23
- broadcast.subscribe(subscriber1); // test with two subscribers
24
- broadcast.subscribe(subscriber2);
25
- broadcast.publish('Hello');
26
- expect(subscriber1).toHaveBeenCalledWith('Hello');
27
- expect(subscriber2).toHaveBeenCalledWith('Hello');
28
- });
29
- it('should publish a message to all subscribers', () => {
30
- // including subscribers that subscribe after the first message is published
31
- const broadcast = new pubSub_1.Broadcast();
32
- const subscriber1 = jest.fn();
33
- const subscriber2 = jest.fn();
34
- broadcast.subscribe(subscriber1);
35
- broadcast.publish('Hello');
36
- broadcast.subscribe(subscriber2);
37
- broadcast.publish('Hello Again');
38
- expect(subscriber1).toHaveBeenCalledWith('Hello');
39
- expect(subscriber1).toHaveBeenCalledWith('Hello Again');
40
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
41
- });
42
- it('should unsubscribe a subscriber and thus not receive new message', () => {
43
- const broadcast = new pubSub_1.Broadcast();
44
- const subscriber1 = jest.fn();
45
- const subscriber2 = jest.fn();
46
- broadcast.subscribe(subscriber1);
47
- broadcast.subscribe(subscriber2);
48
- broadcast.publish('Hello');
49
- const subscription = broadcast.subscribe(subscriber2);
50
- subscription.unsubscribe();
51
- broadcast.publish('Hello Again');
52
- expect(subscriber1).toHaveBeenCalledWith('Hello');
53
- expect(subscriber1).toHaveBeenCalledWith('Hello Again');
54
- expect(subscriber2).toHaveBeenCalledWith('Hello');
55
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
56
- });
57
- it('should stop the broadcast midway if a subscription returns a value', () => {
58
- const broadcast = new pubSub_1.Broadcast();
59
- const subscriber1 = jest.fn();
60
- const subscriber2 = jest.fn(() => true);
61
- const subscriber3 = jest.fn();
62
- broadcast.subscribe(subscriber1);
63
- broadcast.subscribe(subscriber2);
64
- broadcast.subscribe(subscriber3);
65
- broadcast.publish('Hello');
66
- expect(subscriber1).toHaveBeenCalledWith('Hello');
67
- expect(subscriber2).toHaveBeenCalledWith('Hello');
68
- expect(subscriber3).not.toHaveBeenCalled();
69
- });
70
- it('should throw an error if one of its subscribers throws an error', () => {
71
- const broadcast = new pubSub_1.Broadcast();
72
- const subscriber1 = jest.fn();
73
- const subscriber2 = jest.fn(() => {
74
- throw new Error('Something went wrong');
75
- });
76
- const subscriber3 = jest.fn();
77
- broadcast.subscribe(subscriber1);
78
- broadcast.subscribe(subscriber2);
79
- broadcast.subscribe(subscriber3);
80
- expect(() => broadcast.publish('Hello')).toThrowError('Something went wrong');
81
- expect(subscriber1).toHaveBeenCalledWith('Hello');
82
- expect(subscriber2).toHaveBeenCalledWith('Hello');
83
- expect(subscriber3).not.toHaveBeenCalled();
84
- });
85
- it('should clear all subscribers', () => {
86
- const broadcast = new pubSub_1.Broadcast();
87
- const subscriber1 = jest.fn();
88
- const subscriber2 = jest.fn();
89
- broadcast.subscribe(subscriber1);
90
- broadcast.subscribe(subscriber2);
91
- broadcast.publish('Hello');
92
- broadcast.clear();
93
- broadcast.publish('Hello Again');
94
- expect(subscriber1).toHaveBeenCalledWith('Hello');
95
- expect(subscriber1).not.toHaveBeenCalledWith('Hello Again');
96
- expect(subscriber2).toHaveBeenCalledWith('Hello');
97
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
98
- });
99
- });
100
- describe('Subject', () => {
101
- it('should be defined', () => {
102
- expect(pubSub_1.Subject).toBeDefined();
103
- });
104
- it('should be a class', () => {
105
- expect(pubSub_1.Subject).toBeInstanceOf(Function);
106
- });
107
- // Functionality tests
108
- it('should publish a message to all subscribers', () => {
109
- const subject = new pubSub_1.Subject('hi');
110
- const subscriber = jest.fn();
111
- subject.subscribe(subscriber); // test with one subscriber
112
- subject.publish('Hello');
113
- expect(subscriber).toHaveBeenCalledWith('Hello');
114
- });
115
- it('should publish a message to all subscribers', () => {
116
- const subject = new pubSub_1.Subject('hi');
117
- const subscriber1 = jest.fn();
118
- const subscriber2 = jest.fn();
119
- subject.subscribe(subscriber1); // test with two subscribers
120
- subject.subscribe(subscriber2);
121
- subject.publish('Hello');
122
- expect(subscriber1).toHaveBeenCalledWith('Hello');
123
- expect(subscriber2).toHaveBeenCalledWith('Hello');
124
- });
125
- it('should provide the initial value to new subscribers', () => {
126
- const subject = new pubSub_1.Subject('hi');
127
- const subscriber = jest.fn();
128
- subject.subscribe(subscriber); // test with one subscriber
129
- expect(subscriber).toHaveBeenCalledWith('hi');
130
- });
131
- it('should publish a message to all subscribers', () => {
132
- // including subscribers that subscribe after the first message is published
133
- const subject = new pubSub_1.Subject('hi');
134
- const subscriber1 = jest.fn();
135
- const subscriber2 = jest.fn();
136
- subject.subscribe(subscriber1);
137
- subject.publish('Hello');
138
- subject.subscribe(subscriber2);
139
- subject.publish('Hello Again');
140
- expect(subscriber1).toHaveBeenCalledWith('Hello');
141
- expect(subscriber1).toHaveBeenCalledWith('Hello Again');
142
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
143
- });
144
- it('should return the current value when the getter is called', () => {
145
- const subject = new pubSub_1.Subject('hi');
146
- expect(subject.value).toEqual('hi');
147
- });
148
- it('should throw an error if one of its subscribers throws an error', () => {
149
- const subject = new pubSub_1.Subject('hi');
150
- const subscriber1 = jest.fn();
151
- const subscriber2 = jest.fn(() => {
152
- throw new Error('Something went wrong');
153
- });
154
- const subscriber3 = jest.fn();
155
- subject.subscribe(subscriber1);
156
- // because the subject immediately calls the subscriber with the initial value
157
- expect(() => subject.subscribe(subscriber2)).toThrowError('Something went wrong');
158
- subscriber2.mockClear();
159
- subject.subscribe(subscriber3);
160
- // since ths subscriber throws an error, the subject should not call it
161
- subject.publish('Hello'); // this should not call subscriber2 and should not throw an error
162
- expect(subscriber1).toHaveBeenCalledWith('Hello');
163
- expect(subscriber3).toHaveBeenCalledWith('Hello');
164
- expect(subscriber2).not.toHaveBeenCalled();
165
- });
166
- it('should provide the current list of observers', () => {
167
- const subject = new pubSub_1.Subject('hi');
168
- const subscriber1 = jest.fn();
169
- const subscriber2 = jest.fn();
170
- subject.subscribe(subscriber1);
171
- subject.subscribe(subscriber2);
172
- const set = new Set([subscriber1, subscriber2]);
173
- expect(subject.observers).toEqual(set);
174
- });
175
- });
176
- describe('EventSubject', () => {
177
- it('should be defined', () => {
178
- expect(pubSub_1.EventPubSub).toBeDefined();
179
- });
180
- it('should be a class', () => {
181
- expect(pubSub_1.EventPubSub).toBeInstanceOf(Function);
182
- });
183
- // Functionality tests
184
- it('should publish a message to all subscribers', () => {
185
- const subject = new pubSub_1.EventPubSub();
186
- const subscriber = jest.fn();
187
- subject.subscribe('test', subscriber); // test with one subscriber
188
- subject.publish('test', 'Hello');
189
- expect(subscriber).toHaveBeenCalledWith('Hello');
190
- });
191
- it('should publish a message to all subscribers', () => {
192
- const subject = new pubSub_1.EventPubSub();
193
- const subscriber1 = jest.fn();
194
- const subscriber2 = jest.fn();
195
- subject.subscribe('test', subscriber1); // test with two subscribers
196
- subject.subscribe('test', subscriber2);
197
- subject.publish('test', 'Hello');
198
- expect(subscriber1).toHaveBeenCalledWith('Hello');
199
- expect(subscriber2).toHaveBeenCalledWith('Hello');
200
- });
201
- it('should publish a message to all subscribers', () => {
202
- // including subscribers that subscribe after the first message is published
203
- const subject = new pubSub_1.EventPubSub();
204
- const subscriber1 = jest.fn();
205
- const subscriber2 = jest.fn();
206
- subject.subscribe('test', subscriber1);
207
- subject.publish('test', 'Hello');
208
- subject.subscribe('test', subscriber2);
209
- subject.publish('test', 'Hello Again');
210
- expect(subscriber1).toHaveBeenCalledWith('Hello');
211
- expect(subscriber1).toHaveBeenCalledWith('Hello Again');
212
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
213
- });
214
- it('should not call a function if the event name does not match', () => {
215
- const subject = new pubSub_1.EventPubSub();
216
- const subscriber = jest.fn();
217
- subject.subscribe('test', subscriber);
218
- subject.publish('test2', 'Hello');
219
- expect(subscriber).not.toHaveBeenCalled();
220
- });
221
- it('should not call a function if the event name does not match', () => {
222
- // including subscribers that subscribe after the first message is published
223
- const subject = new pubSub_1.EventPubSub();
224
- const subscriber1 = jest.fn();
225
- const subscriber2 = jest.fn();
226
- subject.subscribe('test', subscriber1);
227
- subject.publish('test', 'Hello');
228
- expect(subscriber1).toHaveBeenCalledWith('Hello');
229
- expect(subscriber2).not.toHaveBeenCalled();
230
- subscriber1.mockClear();
231
- subscriber2.mockClear();
232
- subject.subscribe('test2', subscriber2);
233
- subject.publish('test2', 'Hello Again');
234
- expect(subscriber1).not.toHaveBeenCalled();
235
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
236
- });
237
- it('should accept a generic subscriber', () => {
238
- const subject = new pubSub_1.EventPubSub();
239
- const subscriber = jest.fn();
240
- subject.subscribeAll(subscriber);
241
- subject.publish('test', 'Hello');
242
- expect(subscriber).toHaveBeenCalledWith('Hello');
243
- });
244
- it('should accept a generic subscriber', () => {
245
- const subject = new pubSub_1.EventPubSub();
246
- const subscriber = jest.fn();
247
- subject.subscribeAll(subscriber);
248
- subject.publish('test', 'Hello');
249
- subject.publish('test2', 'Hello Again');
250
- expect(subscriber).toHaveBeenCalledWith('Hello');
251
- expect(subscriber).toHaveBeenCalledWith('Hello Again');
252
- });
253
- it('should be able to unsubscribe from a specific event', () => {
254
- const subject = new pubSub_1.EventPubSub();
255
- const subscriber = jest.fn();
256
- const sub = subject.subscribe('test', subscriber);
257
- subject.publish('test', 'Hello');
258
- expect(subscriber).toHaveBeenCalledWith('Hello');
259
- subscriber.mockClear();
260
- sub.unsubscribe();
261
- subject.publish('test', 'Hello Again');
262
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
263
- });
264
- it('should complete the subscription when unsubscribed', () => {
265
- const subject = new pubSub_1.EventPubSub();
266
- const subscriber = jest.fn();
267
- const subscriber2 = jest.fn();
268
- const subscriber3 = jest.fn();
269
- subject.subscribe('test', subscriber);
270
- subject.subscribe('test1', subscriber2);
271
- subject.subscribe('test2', subscriber3);
272
- subject.publish('test', 'Hello');
273
- expect(subscriber).toHaveBeenCalledWith('Hello');
274
- subscriber.mockClear();
275
- subject.publish('test1', 'Hello Again');
276
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
277
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
278
- subscriber2.mockClear();
279
- subject.publish('test2', 'Hello Again');
280
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
281
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
282
- expect(subscriber3).toHaveBeenCalledWith('Hello Again');
283
- subscriber3.mockClear();
284
- subject.complete(); // complete all subscriptions
285
- subject.publish('test', 'Hello Again');
286
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
287
- subject.publish('test1', 'Hello Again');
288
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
289
- subject.publish('test2', 'Hello Again');
290
- expect(subscriber3).not.toHaveBeenCalledWith('Hello Again');
291
- });
292
- it('should throw an error if one of its subscribers throw an error', () => {
293
- const subject = new pubSub_1.EventPubSub();
294
- const subscriber1 = jest.fn();
295
- const subscriber2 = jest.fn(() => {
296
- throw new Error('Something went wrong');
297
- });
298
- const subscriber3 = jest.fn();
299
- subject.subscribeAll(subscriber1);
300
- subject.subscribeAll(subscriber2);
301
- subject.subscribe('event', subscriber3);
302
- expect(() => subject.publish('event', 'Hello')).toThrowError('Something went wrong');
303
- expect(subscriber1).toHaveBeenCalledWith('Hello');
304
- expect(subscriber2).toHaveBeenCalledWith('Hello');
305
- expect(subscriber3).not.toHaveBeenCalled();
306
- });
307
- it('should be possible to unsubscribe from a generic subscriber', () => {
308
- const subject = new pubSub_1.EventPubSub();
309
- const subscriber = jest.fn();
310
- const sub = subject.subscribeAll(subscriber);
311
- subject.publish('test', 'Hello');
312
- expect(subscriber).toHaveBeenCalledWith('Hello');
313
- subscriber.mockClear();
314
- sub.unsubscribe();
315
- subject.publish('test', 'Hello Again');
316
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
317
- });
318
- it('should be able to add an oncomplete callback', () => {
319
- const subject = new pubSub_1.EventPubSub();
320
- const subscriber = jest.fn();
321
- const subscriber2 = jest.fn();
322
- subject.onComplete(subscriber2);
323
- subject.subscribe('test', subscriber);
324
- subject.publish('test', 'Hello');
325
- expect(subscriber).toHaveBeenCalledWith('Hello');
326
- subscriber.mockClear();
327
- subject.complete();
328
- expect(subscriber2).toHaveBeenCalled();
329
- subject.publish('test', 'Hello Again');
330
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
331
- });
332
- });