@eleven-am/pondsocket 0.1.10 → 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 -83
  52. package/pondBase/pubSub.js +0 -150
  53. package/pondBase/pubSub.test.js +0 -323
  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 -66
  60. package/pondClient/channel.js +0 -139
  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,83 +0,0 @@
1
- export declare class Subscription {
2
- unsubscribe(): void;
3
- }
4
-
5
- export declare class Broadcast<T, A> {
6
-
7
- /**
8
- * @desc Gets the number of subscribers
9
- */
10
- get subscriberCount(): number;
11
-
12
- /**
13
- * @desc Subscribe to the broadcast
14
- * @param handler - The handler to call when the broadcast is published
15
- */
16
- subscribe(handler: (data: T) => A): Subscription;
17
-
18
- /**
19
- * @desc Publish to the broadcast
20
- * @param data - The data to publish
21
- */
22
- publish(data: T): A | undefined;
23
-
24
- /**
25
- * @dec clears all subscribers
26
- */
27
- clear(): void;
28
- }
29
-
30
- export declare class Subject<T, A> extends Broadcast<T, A> {
31
- constructor(value: T);
32
-
33
- /**
34
- * @desc Get the current value of the subject
35
- */
36
- get value(): T;
37
-
38
- /**
39
- * @desc Subscribe to the subject
40
- * @param handler - The handler to call when the subject is published
41
- */
42
- subscribe(handler: (data: T) => A): Subscription;
43
-
44
- /**
45
- * @desc Publish to the subject
46
- * @param data - The data to publish
47
- */
48
- publish(data: T): A | undefined;
49
- }
50
-
51
- export declare class EventPubSub<T, A> {
52
-
53
- /**
54
- * @desc Subscribe to the event subject
55
- * @param event - The event to subscribe to
56
- * @param handler - The handler to call when the event subject is published
57
- */
58
- subscribe(event: string, handler: (data: T) => A): Subscription;
59
-
60
- /**
61
- * @desc Publish to the event subject
62
- * @param event - The event to publish
63
- * @param data - The data to publish
64
- */
65
- publish(event: string, data: T): void;
66
-
67
- /**
68
- * @desc Subscribe to all events
69
- * @param handler - The handler to call when the event subject is published
70
- */
71
- subscribeAll(handler: (event: T) => A): Subscription;
72
-
73
- /**
74
- * @desc Complete the event subject
75
- */
76
- complete(): void;
77
-
78
- /**
79
- * @desc Subscribe to the event subject completion
80
- * @param handler - The handler to call when the event subject is completed
81
- */
82
- onComplete(handler: () => void): void;
83
- }
@@ -1,150 +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 Gets the number of subscribers
25
- */
26
- get subscriberCount() {
27
- return this._subscribers.size;
28
- }
29
- /**
30
- * @desc Publish to the broadcast
31
- * @param data - The data to publish
32
- */
33
- publish(data) {
34
- let result;
35
- for (const subscriber of this._subscribers) {
36
- result = subscriber(data);
37
- if (result)
38
- break;
39
- }
40
- return result;
41
- }
42
- /**
43
- * @dec clears all subscribers
44
- */
45
- clear() {
46
- this._subscribers.clear();
47
- }
48
- }
49
- exports.Broadcast = Broadcast;
50
- class Subject extends Broadcast {
51
- constructor(value) {
52
- super();
53
- this._value = value;
54
- }
55
- /**
56
- * @desc Get the current value of the subject
57
- */
58
- get value() {
59
- return this._value;
60
- }
61
- /**
62
- * @desc Subscribe to the subject
63
- * @param handler - The handler to call when the subject is published
64
- */
65
- subscribe(handler) {
66
- void handler(this._value);
67
- return super.subscribe(handler);
68
- }
69
- /**
70
- * @desc Publish to the subject
71
- * @param data - The data to publish
72
- */
73
- publish(data) {
74
- if (this._value !== data) {
75
- this._value = data;
76
- return super.publish(data);
77
- }
78
- }
79
- }
80
- exports.Subject = Subject;
81
- class EventPubSub {
82
- constructor() {
83
- this._subscribers = new Set();
84
- }
85
- /**
86
- * @desc Subscribe to the event subject
87
- * @param event - The event to subscribe to
88
- * @param handler - The handler to call when the event subject is published
89
- */
90
- subscribe(event, handler) {
91
- const subscriber = (eventData) => {
92
- if (eventData.type === event)
93
- return handler(eventData.data);
94
- return undefined;
95
- };
96
- this._subscribers.add(subscriber);
97
- return {
98
- /**
99
- * @desc Unsubscribe from the event subject
100
- */
101
- unsubscribe: () => {
102
- this._subscribers.delete(subscriber);
103
- }
104
- };
105
- }
106
- /**
107
- * @desc Publish to the event subject
108
- * @param event - The event to publish
109
- * @param data - The data to publish
110
- */
111
- publish(event, data) {
112
- for (const subscriber of this._subscribers) {
113
- void subscriber({ type: event, data });
114
- }
115
- }
116
- /**
117
- * @desc Subscribe to all events
118
- * @param handler - The handler to call when the event subject is published
119
- */
120
- subscribeAll(handler) {
121
- const subscriber = (eventData) => {
122
- return handler(eventData.data);
123
- };
124
- this._subscribers.add(subscriber);
125
- return {
126
- /**
127
- * @desc Unsubscribe from the event subject
128
- */
129
- unsubscribe: () => {
130
- this._subscribers.delete(subscriber);
131
- }
132
- };
133
- }
134
- /**
135
- * @desc Complete the event subject
136
- */
137
- complete() {
138
- this._subscribers.clear();
139
- if (this._onComplete)
140
- this._onComplete();
141
- }
142
- /**
143
- * @desc Subscribe to the event subject completion
144
- * @param handler - The handler to call when the event subject is completed
145
- */
146
- onComplete(handler) {
147
- this._onComplete = handler;
148
- }
149
- }
150
- exports.EventPubSub = EventPubSub;
@@ -1,323 +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
- });
167
- describe('EventSubject', () => {
168
- it('should be defined', () => {
169
- expect(pubSub_1.EventPubSub).toBeDefined();
170
- });
171
- it('should be a class', () => {
172
- expect(pubSub_1.EventPubSub).toBeInstanceOf(Function);
173
- });
174
- // Functionality tests
175
- it('should publish a message to all subscribers', () => {
176
- const subject = new pubSub_1.EventPubSub();
177
- const subscriber = jest.fn();
178
- subject.subscribe('test', subscriber); // test with one subscriber
179
- subject.publish('test', 'Hello');
180
- expect(subscriber).toHaveBeenCalledWith('Hello');
181
- });
182
- it('should publish a message to all subscribers', () => {
183
- const subject = new pubSub_1.EventPubSub();
184
- const subscriber1 = jest.fn();
185
- const subscriber2 = jest.fn();
186
- subject.subscribe('test', subscriber1); // test with two subscribers
187
- subject.subscribe('test', subscriber2);
188
- subject.publish('test', 'Hello');
189
- expect(subscriber1).toHaveBeenCalledWith('Hello');
190
- expect(subscriber2).toHaveBeenCalledWith('Hello');
191
- });
192
- it('should publish a message to all subscribers', () => {
193
- // including subscribers that subscribe after the first message is published
194
- const subject = new pubSub_1.EventPubSub();
195
- const subscriber1 = jest.fn();
196
- const subscriber2 = jest.fn();
197
- subject.subscribe('test', subscriber1);
198
- subject.publish('test', 'Hello');
199
- subject.subscribe('test', subscriber2);
200
- subject.publish('test', 'Hello Again');
201
- expect(subscriber1).toHaveBeenCalledWith('Hello');
202
- expect(subscriber1).toHaveBeenCalledWith('Hello Again');
203
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
204
- });
205
- it('should not call a function if the event name does not match', () => {
206
- const subject = new pubSub_1.EventPubSub();
207
- const subscriber = jest.fn();
208
- subject.subscribe('test', subscriber);
209
- subject.publish('test2', 'Hello');
210
- expect(subscriber).not.toHaveBeenCalled();
211
- });
212
- it('should not call a function if the event name does not match', () => {
213
- // including subscribers that subscribe after the first message is published
214
- const subject = new pubSub_1.EventPubSub();
215
- const subscriber1 = jest.fn();
216
- const subscriber2 = jest.fn();
217
- subject.subscribe('test', subscriber1);
218
- subject.publish('test', 'Hello');
219
- expect(subscriber1).toHaveBeenCalledWith('Hello');
220
- expect(subscriber2).not.toHaveBeenCalled();
221
- subscriber1.mockClear();
222
- subscriber2.mockClear();
223
- subject.subscribe('test2', subscriber2);
224
- subject.publish('test2', 'Hello Again');
225
- expect(subscriber1).not.toHaveBeenCalled();
226
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
227
- });
228
- it('should accept a generic subscriber', () => {
229
- const subject = new pubSub_1.EventPubSub();
230
- const subscriber = jest.fn();
231
- subject.subscribeAll(subscriber);
232
- subject.publish('test', 'Hello');
233
- expect(subscriber).toHaveBeenCalledWith('Hello');
234
- });
235
- it('should accept a generic subscriber', () => {
236
- const subject = new pubSub_1.EventPubSub();
237
- const subscriber = jest.fn();
238
- subject.subscribeAll(subscriber);
239
- subject.publish('test', 'Hello');
240
- subject.publish('test2', 'Hello Again');
241
- expect(subscriber).toHaveBeenCalledWith('Hello');
242
- expect(subscriber).toHaveBeenCalledWith('Hello Again');
243
- });
244
- it('should be able to unsubscribe from a specific event', () => {
245
- const subject = new pubSub_1.EventPubSub();
246
- const subscriber = jest.fn();
247
- const sub = subject.subscribe('test', subscriber);
248
- subject.publish('test', 'Hello');
249
- expect(subscriber).toHaveBeenCalledWith('Hello');
250
- subscriber.mockClear();
251
- sub.unsubscribe();
252
- subject.publish('test', 'Hello Again');
253
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
254
- });
255
- it('should complete the subscription when unsubscribed', () => {
256
- const subject = new pubSub_1.EventPubSub();
257
- const subscriber = jest.fn();
258
- const subscriber2 = jest.fn();
259
- const subscriber3 = jest.fn();
260
- subject.subscribe('test', subscriber);
261
- subject.subscribe('test1', subscriber2);
262
- subject.subscribe('test2', subscriber3);
263
- subject.publish('test', 'Hello');
264
- expect(subscriber).toHaveBeenCalledWith('Hello');
265
- subscriber.mockClear();
266
- subject.publish('test1', 'Hello Again');
267
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
268
- expect(subscriber2).toHaveBeenCalledWith('Hello Again');
269
- subscriber2.mockClear();
270
- subject.publish('test2', 'Hello Again');
271
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
272
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
273
- expect(subscriber3).toHaveBeenCalledWith('Hello Again');
274
- subscriber3.mockClear();
275
- subject.complete(); // complete all subscriptions
276
- subject.publish('test', 'Hello Again');
277
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
278
- subject.publish('test1', 'Hello Again');
279
- expect(subscriber2).not.toHaveBeenCalledWith('Hello Again');
280
- subject.publish('test2', 'Hello Again');
281
- expect(subscriber3).not.toHaveBeenCalledWith('Hello Again');
282
- });
283
- it('should throw an error if one of its subscribers throw an error', () => {
284
- const subject = new pubSub_1.EventPubSub();
285
- const subscriber1 = jest.fn();
286
- const subscriber2 = jest.fn(() => {
287
- throw new Error('Something went wrong');
288
- });
289
- const subscriber3 = jest.fn();
290
- subject.subscribeAll(subscriber1);
291
- subject.subscribeAll(subscriber2);
292
- subject.subscribe('event', subscriber3);
293
- expect(() => subject.publish('event', 'Hello')).toThrowError('Something went wrong');
294
- expect(subscriber1).toHaveBeenCalledWith('Hello');
295
- expect(subscriber2).toHaveBeenCalledWith('Hello');
296
- expect(subscriber3).not.toHaveBeenCalled();
297
- });
298
- it('should be possible to unsubscribe from a generic subscriber', () => {
299
- const subject = new pubSub_1.EventPubSub();
300
- const subscriber = jest.fn();
301
- const sub = subject.subscribeAll(subscriber);
302
- subject.publish('test', 'Hello');
303
- expect(subscriber).toHaveBeenCalledWith('Hello');
304
- subscriber.mockClear();
305
- sub.unsubscribe();
306
- subject.publish('test', 'Hello Again');
307
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
308
- });
309
- it('should be able to add an oncomplete callback', () => {
310
- const subject = new pubSub_1.EventPubSub();
311
- const subscriber = jest.fn();
312
- const subscriber2 = jest.fn();
313
- subject.onComplete(subscriber2);
314
- subject.subscribe('test', subscriber);
315
- subject.publish('test', 'Hello');
316
- expect(subscriber).toHaveBeenCalledWith('Hello');
317
- subscriber.mockClear();
318
- subject.complete();
319
- expect(subscriber2).toHaveBeenCalled();
320
- subject.publish('test', 'Hello Again');
321
- expect(subscriber).not.toHaveBeenCalledWith('Hello Again');
322
- });
323
- });