@eleven-am/pondsocket 0.1.55 → 0.1.57
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.
- package/.eslintrc.json +387 -0
- package/dist/LICENSE +674 -0
- package/dist/README.md +139 -0
- package/{channel → dist/channel}/channel.js +5 -7
- package/{lobby → dist/lobby}/joinResponse.js +2 -2
- package/dist/package.json +51 -0
- package/{presence → dist/presence}/presence.js +16 -18
- package/{server → dist/server}/pondSocket.js +1 -1
- package/{subjects → dist/subjects}/subject.js +44 -0
- package/{types.d.ts → dist/types.d.ts} +0 -5
- package/jest.config.js +11 -0
- package/package.json +3 -3
- package/src/abstracts/abstractRequest.test.ts +49 -0
- package/src/abstracts/abstractRequest.ts +56 -0
- package/src/abstracts/abstractResponse.ts +26 -0
- package/src/abstracts/middleware.test.ts +75 -0
- package/src/abstracts/middleware.ts +50 -0
- package/src/channel/channel.test.ts +501 -0
- package/src/channel/channel.ts +305 -0
- package/src/channel/eventRequest.test.ts +37 -0
- package/src/channel/eventRequest.ts +27 -0
- package/src/channel/eventResponse.test.ts +249 -0
- package/src/channel/eventResponse.ts +172 -0
- package/src/client/channel.test.ts +799 -0
- package/src/client/channel.ts +342 -0
- package/src/client.ts +124 -0
- package/src/endpoint/endpoint.test.ts +825 -0
- package/src/endpoint/endpoint.ts +304 -0
- package/src/endpoint/response.ts +106 -0
- package/src/enums.ts +52 -0
- package/src/errors/pondError.ts +32 -0
- package/src/express.ts +58 -0
- package/src/index.ts +3 -0
- package/src/lobby/JoinRequest.test.ts +48 -0
- package/src/lobby/JoinResponse.test.ts +162 -0
- package/src/lobby/joinRequest.ts +32 -0
- package/src/lobby/joinResponse.ts +146 -0
- package/src/lobby/lobby.ts +182 -0
- package/src/matcher/matcher.test.ts +103 -0
- package/src/matcher/matcher.ts +105 -0
- package/src/node.ts +33 -0
- package/src/presence/presence.ts +127 -0
- package/src/presence/presenceEngine.test.ts +143 -0
- package/src/server/pondSocket.ts +153 -0
- package/src/subjects/subject.test.ts +163 -0
- package/src/subjects/subject.ts +137 -0
- package/src/typedefs.d.ts +451 -0
- package/src/types.d.ts +89 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +12 -0
- /package/{abstracts → dist/abstracts}/abstractRequest.js +0 -0
- /package/{abstracts → dist/abstracts}/abstractResponse.js +0 -0
- /package/{abstracts → dist/abstracts}/middleware.js +0 -0
- /package/{channel → dist/channel}/eventRequest.js +0 -0
- /package/{channel → dist/channel}/eventResponse.js +0 -0
- /package/{client → dist/client}/channel.js +0 -0
- /package/{client.d.ts → dist/client.d.ts} +0 -0
- /package/{client.js → dist/client.js} +0 -0
- /package/{endpoint → dist/endpoint}/endpoint.js +0 -0
- /package/{endpoint → dist/endpoint}/response.js +0 -0
- /package/{enums.js → dist/enums.js} +0 -0
- /package/{errors → dist/errors}/pondError.js +0 -0
- /package/{express.d.ts → dist/express.d.ts} +0 -0
- /package/{express.js → dist/express.js} +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{lobby → dist/lobby}/joinRequest.js +0 -0
- /package/{lobby → dist/lobby}/lobby.js +0 -0
- /package/{matcher → dist/matcher}/matcher.js +0 -0
- /package/{node.d.ts → dist/node.d.ts} +0 -0
- /package/{node.js → dist/node.js} +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Subject, BehaviorSubject, SimpleSubject, SimpleBehaviorSubject } from './subject';
|
|
2
|
+
|
|
3
|
+
describe('Subject', () => {
|
|
4
|
+
let testSubject: Subject<number>;
|
|
5
|
+
let observer1: jest.Mock;
|
|
6
|
+
let observer2: jest.Mock;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
testSubject = new Subject<number>();
|
|
10
|
+
observer1 = jest.fn();
|
|
11
|
+
observer2 = jest.fn();
|
|
12
|
+
testSubject.subscribeWith('observer1', observer1);
|
|
13
|
+
testSubject.subscribeWith('observer2', observer2);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
testSubject.unsubscribe('observer1');
|
|
18
|
+
testSubject.unsubscribe('observer2');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should notify all subscribers when next is called', () => {
|
|
22
|
+
const message = 10;
|
|
23
|
+
|
|
24
|
+
testSubject.publish(message);
|
|
25
|
+
expect(observer1).toHaveBeenCalledWith(message);
|
|
26
|
+
expect(observer2).toHaveBeenCalledWith(message);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should unsubscribe an observer when unsubscribe is called', () => {
|
|
30
|
+
const identifier = 'observer1';
|
|
31
|
+
|
|
32
|
+
testSubject.unsubscribe(identifier);
|
|
33
|
+
expect(testSubject.has(identifier)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('BehaviorSubject', () => {
|
|
38
|
+
let testSubject: BehaviorSubject<number>;
|
|
39
|
+
let observer1: jest.Mock;
|
|
40
|
+
let observer2: jest.Mock;
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
testSubject = new BehaviorSubject<number>();
|
|
44
|
+
observer1 = jest.fn();
|
|
45
|
+
observer2 = jest.fn();
|
|
46
|
+
testSubject.subscribeWith('observer1', observer1);
|
|
47
|
+
testSubject.subscribeWith('observer2', observer2);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
afterEach(() => {
|
|
51
|
+
testSubject.unsubscribe('observer1');
|
|
52
|
+
testSubject.unsubscribe('observer2');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should notify all subscribers when next is called', () => {
|
|
56
|
+
const message = 10;
|
|
57
|
+
|
|
58
|
+
testSubject.publish(message);
|
|
59
|
+
expect(observer1).toHaveBeenCalledWith(message);
|
|
60
|
+
expect(observer2).toHaveBeenCalledWith(message);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should unsubscribe an observer when unsubscribe is called', () => {
|
|
64
|
+
const identifier = 'observer1';
|
|
65
|
+
|
|
66
|
+
testSubject.unsubscribe(identifier);
|
|
67
|
+
expect(testSubject.has(identifier)).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should notify new subscribers with the last message when subscribe is called', () => {
|
|
71
|
+
const message = 10;
|
|
72
|
+
|
|
73
|
+
testSubject.publish(message);
|
|
74
|
+
const newObserver: jest.Mock = jest.fn();
|
|
75
|
+
|
|
76
|
+
testSubject.subscribeWith('newObserver', newObserver);
|
|
77
|
+
expect(newObserver).toHaveBeenCalledWith(message);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('SimpleSubject', () => {
|
|
82
|
+
let testSubject: SimpleSubject<number>;
|
|
83
|
+
let observer1: jest.Mock;
|
|
84
|
+
let observer2: jest.Mock;
|
|
85
|
+
|
|
86
|
+
beforeEach(() => {
|
|
87
|
+
testSubject = new SimpleSubject<number>();
|
|
88
|
+
observer1 = jest.fn();
|
|
89
|
+
observer2 = jest.fn();
|
|
90
|
+
testSubject.subscribe(observer1);
|
|
91
|
+
testSubject.subscribe(observer2);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
afterEach(() => {
|
|
95
|
+
testSubject.publish(0);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should notify all subscribers when publish is called', () => {
|
|
99
|
+
const message = 10;
|
|
100
|
+
|
|
101
|
+
testSubject.publish(message);
|
|
102
|
+
expect(observer1).toHaveBeenCalledWith(message);
|
|
103
|
+
expect(observer2).toHaveBeenCalledWith(message);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should unsubscribe an observer when unsubscribe is called', () => {
|
|
107
|
+
const unsubscribe = testSubject.subscribe(observer1);
|
|
108
|
+
|
|
109
|
+
unsubscribe();
|
|
110
|
+
expect(testSubject.size).toBe(1);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('SimpleBehaviorSubject', () => {
|
|
115
|
+
let testSubject: SimpleBehaviorSubject<number>;
|
|
116
|
+
let observer1: jest.Mock;
|
|
117
|
+
let observer2: jest.Mock;
|
|
118
|
+
|
|
119
|
+
beforeEach(() => {
|
|
120
|
+
testSubject = new SimpleBehaviorSubject<number>(0);
|
|
121
|
+
observer1 = jest.fn();
|
|
122
|
+
observer2 = jest.fn();
|
|
123
|
+
testSubject.subscribe(observer1);
|
|
124
|
+
testSubject.subscribe(observer2);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
afterEach(() => {
|
|
128
|
+
testSubject.publish(0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should notify all subscribers when publish is called', () => {
|
|
132
|
+
const message = 10;
|
|
133
|
+
|
|
134
|
+
testSubject.publish(message);
|
|
135
|
+
expect(observer1).toHaveBeenCalledWith(message);
|
|
136
|
+
expect(observer2).toHaveBeenCalledWith(message);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should unsubscribe an observer when unsubscribe is called', () => {
|
|
140
|
+
const unsubscribe = testSubject.subscribe(observer1);
|
|
141
|
+
|
|
142
|
+
unsubscribe();
|
|
143
|
+
expect(testSubject.size).toBe(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should notify new subscribers with the last message when subscribe is called', () => {
|
|
147
|
+
const message = 10;
|
|
148
|
+
|
|
149
|
+
testSubject.publish(message);
|
|
150
|
+
const newObserver: jest.Mock = jest.fn();
|
|
151
|
+
|
|
152
|
+
testSubject.subscribe(newObserver);
|
|
153
|
+
expect(newObserver).toHaveBeenCalledWith(message);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should return the last message when value is called', () => {
|
|
157
|
+
const message = 10;
|
|
158
|
+
|
|
159
|
+
testSubject.publish(message);
|
|
160
|
+
expect(testSubject.value).toBe(message);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-unresolved
|
|
2
|
+
import { Unsubscribe } from '../types';
|
|
3
|
+
|
|
4
|
+
type Subscriber<T> = (message: T) => void;
|
|
5
|
+
|
|
6
|
+
export class SimpleSubject<T> {
|
|
7
|
+
readonly #observers: Set<Subscriber<T>>;
|
|
8
|
+
|
|
9
|
+
constructor () {
|
|
10
|
+
this.#observers = new Set<Subscriber<T>>();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @desc Subscribes to a subject
|
|
15
|
+
* @param observer - The observer to subscribe
|
|
16
|
+
*/
|
|
17
|
+
subscribe (observer: Subscriber<T>): Unsubscribe {
|
|
18
|
+
this.#observers.add(observer);
|
|
19
|
+
|
|
20
|
+
return () => this.#observers.delete(observer);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @desc Publishes a message to all subscribers
|
|
25
|
+
* @param message - The message to publish
|
|
26
|
+
*/
|
|
27
|
+
publish (message: T) {
|
|
28
|
+
this.#observers.forEach((observer) => observer(message));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @desc Returns the number of subscribers
|
|
33
|
+
*/
|
|
34
|
+
get size () {
|
|
35
|
+
return this.#observers.size;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class SimpleBehaviorSubject<T> extends SimpleSubject<T> {
|
|
40
|
+
#lastMessage: T | undefined;
|
|
41
|
+
|
|
42
|
+
constructor (initialValue?: T) {
|
|
43
|
+
super();
|
|
44
|
+
|
|
45
|
+
this.#lastMessage = initialValue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @desc Returns the last message published
|
|
50
|
+
*/
|
|
51
|
+
public get value () {
|
|
52
|
+
return this.#lastMessage;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @desc Publishes a message to all subscribers
|
|
57
|
+
* @param message - The message to publish
|
|
58
|
+
*/
|
|
59
|
+
publish (message: T) {
|
|
60
|
+
this.#lastMessage = message;
|
|
61
|
+
super.publish(message);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @desc Subscribes to a subject
|
|
66
|
+
* @param observer - The observer to subscribe
|
|
67
|
+
*/
|
|
68
|
+
subscribe (observer: Subscriber<T>): Unsubscribe {
|
|
69
|
+
if (this.#lastMessage) {
|
|
70
|
+
observer(this.#lastMessage);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return super.subscribe(observer);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class Subject<T> extends SimpleSubject<T> {
|
|
78
|
+
readonly #subscriptions: Record<string, Unsubscribe> = {};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @desc Subscribes to a subject
|
|
82
|
+
* @param identifier - The identifier of the subscription
|
|
83
|
+
* @param observer - The observer to subscribe
|
|
84
|
+
*/
|
|
85
|
+
subscribeWith (identifier: string, observer: Subscriber<T>): void {
|
|
86
|
+
this.#subscriptions[identifier] = super.subscribe(observer);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @desc Unsubscribes from a subject
|
|
91
|
+
* @param identifier - The identifier of the subscription
|
|
92
|
+
*/
|
|
93
|
+
unsubscribe (identifier: string) {
|
|
94
|
+
this.#subscriptions[identifier]?.();
|
|
95
|
+
delete this.#subscriptions[identifier];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @desc Checks if a subscription exists
|
|
100
|
+
* @param identifier - The identifier of the subscription
|
|
101
|
+
*/
|
|
102
|
+
has (identifier: string) {
|
|
103
|
+
return Boolean(this.#subscriptions[identifier]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class BehaviorSubject<T> extends Subject<T> {
|
|
108
|
+
#lastMessage: T | undefined;
|
|
109
|
+
|
|
110
|
+
constructor (initialValue?: T) {
|
|
111
|
+
super();
|
|
112
|
+
|
|
113
|
+
this.#lastMessage = initialValue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @desc Subscribes to a subject
|
|
118
|
+
* @param identifier - The identifier of the subscription
|
|
119
|
+
* @param observer - The observer to subscribe
|
|
120
|
+
*/
|
|
121
|
+
subscribeWith (identifier: string, observer: Subscriber<T>): void {
|
|
122
|
+
if (this.#lastMessage) {
|
|
123
|
+
observer(this.#lastMessage);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
super.subscribeWith(identifier, observer);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @desc Publishes a message to all subscribers
|
|
131
|
+
* @param message - The message to publish
|
|
132
|
+
*/
|
|
133
|
+
publish (message: T) {
|
|
134
|
+
this.#lastMessage = message;
|
|
135
|
+
super.publish(message);
|
|
136
|
+
}
|
|
137
|
+
}
|