@eleven-am/pondsocket 0.1.0
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.js +28 -0
- package/.idea/modules.xml +8 -0
- package/.idea/pondsocket.iml +12 -0
- package/LICENSE +674 -0
- package/base.d.ts +1 -0
- package/base.js +17 -0
- package/client.d.ts +1 -0
- package/client.js +17 -0
- package/index.d.ts +1 -0
- package/index.js +17 -0
- package/jest.config.js +11 -0
- package/package.json +48 -0
- package/pondBase/baseClass.d.ts +37 -0
- package/pondBase/baseClass.js +111 -0
- package/pondBase/baseClass.test.js +73 -0
- package/pondBase/enums.d.ts +9 -0
- package/pondBase/enums.js +14 -0
- package/pondBase/index.d.ts +6 -0
- package/pondBase/index.js +22 -0
- package/pondBase/pondBase.d.ts +41 -0
- package/pondBase/pondBase.js +60 -0
- package/pondBase/pondBase.test.js +101 -0
- package/pondBase/pubSub.d.ts +73 -0
- package/pondBase/pubSub.js +138 -0
- package/pondBase/pubSub.test.js +309 -0
- package/pondBase/simpleBase.d.ts +131 -0
- package/pondBase/simpleBase.js +211 -0
- package/pondBase/simpleBase.test.js +153 -0
- package/pondBase/types.d.ts +2 -0
- package/pondBase/types.js +2 -0
- package/pondClient/channel.d.ts +66 -0
- package/pondClient/channel.js +152 -0
- package/pondClient/index.d.ts +2 -0
- package/pondClient/index.js +18 -0
- package/pondClient/socket.d.ts +42 -0
- package/pondClient/socket.js +116 -0
- package/pondSocket/channel.d.ts +134 -0
- package/pondSocket/channel.js +287 -0
- package/pondSocket/channel.test.js +377 -0
- package/pondSocket/channelMiddleWare.d.ts +26 -0
- package/pondSocket/channelMiddleWare.js +36 -0
- package/pondSocket/endpoint.d.ts +90 -0
- package/pondSocket/endpoint.js +323 -0
- package/pondSocket/endpoint.test.js +513 -0
- package/pondSocket/enums.d.ts +19 -0
- package/pondSocket/enums.js +25 -0
- package/pondSocket/index.d.ts +7 -0
- package/pondSocket/index.js +23 -0
- package/pondSocket/pondChannel.d.ts +79 -0
- package/pondSocket/pondChannel.js +219 -0
- package/pondSocket/pondChannel.test.js +430 -0
- package/pondSocket/pondResponse.d.ts +25 -0
- package/pondSocket/pondResponse.js +120 -0
- package/pondSocket/pondSocket.d.ts +47 -0
- package/pondSocket/pondSocket.js +94 -0
- package/pondSocket/server.test.js +136 -0
- package/pondSocket/socketMiddleWare.d.ts +6 -0
- package/pondSocket/socketMiddleWare.js +32 -0
- package/pondSocket/types.d.ts +74 -0
- package/pondSocket/types.js +2 -0
- package/socket.d.ts +1 -0
- package/socket.js +17 -0
- package/tsconfig.eslint.json +5 -0
- package/tsconfig.json +90 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleBase = exports.PondDocument = void 0;
|
|
4
|
+
class PondDocument {
|
|
5
|
+
constructor(id, removeDoc, updateDoc, getDoc) {
|
|
6
|
+
this._removeDoc = removeDoc;
|
|
7
|
+
this._updateDoc = updateDoc;
|
|
8
|
+
this._getDoc = getDoc;
|
|
9
|
+
this._id = id;
|
|
10
|
+
}
|
|
11
|
+
get id() {
|
|
12
|
+
return this._id;
|
|
13
|
+
}
|
|
14
|
+
get doc() {
|
|
15
|
+
return this._getDoc();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @desc Removes the document from the collection
|
|
19
|
+
*/
|
|
20
|
+
removeDoc() {
|
|
21
|
+
const doc = this._getDoc();
|
|
22
|
+
this._removeDoc();
|
|
23
|
+
return doc;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @desc Updates the document in the collection
|
|
27
|
+
* @param value - the new value of the document
|
|
28
|
+
*/
|
|
29
|
+
updateDoc(value) {
|
|
30
|
+
this._updateDoc(value);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.PondDocument = PondDocument;
|
|
35
|
+
class SimpleBase {
|
|
36
|
+
constructor(callbacks) {
|
|
37
|
+
this._db = {};
|
|
38
|
+
this._update = callbacks || null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @desc Get the number of documents
|
|
42
|
+
*/
|
|
43
|
+
get size() {
|
|
44
|
+
return Object.keys(this._db).length;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @desc Get all the documents in the database
|
|
48
|
+
*/
|
|
49
|
+
get all() {
|
|
50
|
+
return Object.keys(this._db).map(this._createPondDocument.bind(this));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @desc Makes the database iterable
|
|
54
|
+
*/
|
|
55
|
+
[Symbol.iterator]() {
|
|
56
|
+
return this.all[Symbol.iterator]();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @desc Create a generator for the pond
|
|
60
|
+
*/
|
|
61
|
+
*generator() {
|
|
62
|
+
for (const key in this._db) {
|
|
63
|
+
yield this._createPondDocument(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* @desc Get a document by key
|
|
68
|
+
* @param key - The key of the document
|
|
69
|
+
*/
|
|
70
|
+
get(key) {
|
|
71
|
+
const doc = this._db[key];
|
|
72
|
+
if (doc)
|
|
73
|
+
return this._createPondDocument(key);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @desc getOrCreate a document in the database
|
|
78
|
+
* @param key - The key of the document
|
|
79
|
+
* @param creator - The function to create the document
|
|
80
|
+
*/
|
|
81
|
+
getOrCreate(key, creator) {
|
|
82
|
+
const doc = this.get(key);
|
|
83
|
+
if (doc)
|
|
84
|
+
return doc;
|
|
85
|
+
return this.set(key, creator(this._createPondDocument(key)));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @desc Upsert a document in the database
|
|
89
|
+
* @param key - The key of the document
|
|
90
|
+
* @param value - The value of the document
|
|
91
|
+
*/
|
|
92
|
+
upsert(key, value) {
|
|
93
|
+
const doc = this.get(key);
|
|
94
|
+
if (doc)
|
|
95
|
+
return doc.updateDoc(value);
|
|
96
|
+
return this.set(key, value);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @desc checks if a document exists
|
|
100
|
+
* @param key - The key of the document
|
|
101
|
+
*/
|
|
102
|
+
has(key) {
|
|
103
|
+
return this._db[key] !== undefined;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @desc Set a document to the database
|
|
107
|
+
* @param key - The key of the document
|
|
108
|
+
* @param value - The value of the document
|
|
109
|
+
*/
|
|
110
|
+
set(key, value) {
|
|
111
|
+
var _a;
|
|
112
|
+
const oldValue = this._db[key] || null;
|
|
113
|
+
this._db[key] = value;
|
|
114
|
+
(_a = this._update) === null || _a === void 0 ? void 0 : _a.call(this, { oldValue, currentValue: value });
|
|
115
|
+
return this._createPondDocument(key);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @desc Find a document by a query function
|
|
119
|
+
* @param query - The query function
|
|
120
|
+
*/
|
|
121
|
+
find(query) {
|
|
122
|
+
for (const key in this._db) {
|
|
123
|
+
const doc = this._db[key];
|
|
124
|
+
if (query(doc, key))
|
|
125
|
+
return this._createPondDocument(key);
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @desc Map the pond to a new array
|
|
131
|
+
* @param mapper - The mapper function
|
|
132
|
+
*/
|
|
133
|
+
map(mapper) {
|
|
134
|
+
return Object.keys(this._db).map(key => mapper(this._db[key], key));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @desc Filters the pond using a query function
|
|
138
|
+
* @param query - The query function
|
|
139
|
+
*/
|
|
140
|
+
filter(query) {
|
|
141
|
+
return Object.keys(this._db)
|
|
142
|
+
.filter(key => query(this._db[key], key))
|
|
143
|
+
.map(this._createPondDocument.bind(this));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @desc Reduce the pond to a single value
|
|
147
|
+
* @param reducer - The reducer function
|
|
148
|
+
* @param initialValue - The initial value of the reducer
|
|
149
|
+
*/
|
|
150
|
+
reduce(reducer, initialValue) {
|
|
151
|
+
return Object.values(this._db).reduce(reducer, initialValue);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @desc Gets all the keys of the database
|
|
155
|
+
*/
|
|
156
|
+
get keys() {
|
|
157
|
+
return Object.keys(this._db);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @desc Gets all the values of the database
|
|
161
|
+
*/
|
|
162
|
+
get values() {
|
|
163
|
+
return Object.values(this._db);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @desc Generate a generic pond document
|
|
167
|
+
* @param id - The id of the document
|
|
168
|
+
*/
|
|
169
|
+
createGenericDocument(id) {
|
|
170
|
+
const idValue = id || Math.random().toString(36).substr(2, 9);
|
|
171
|
+
return this._createPondDocument(idValue);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @desc Delete a document by key
|
|
175
|
+
*/
|
|
176
|
+
_delete(key) {
|
|
177
|
+
var _a;
|
|
178
|
+
const oldValue = this._db[key];
|
|
179
|
+
delete this._db[key];
|
|
180
|
+
(_a = this._update) === null || _a === void 0 ? void 0 : _a.call(this, { oldValue, currentValue: null });
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @desc Retrieve a document from the database
|
|
184
|
+
* @param key - The key of the document
|
|
185
|
+
*/
|
|
186
|
+
_getDocument(key) {
|
|
187
|
+
const data = this._db[key];
|
|
188
|
+
if (!data)
|
|
189
|
+
return null;
|
|
190
|
+
return data;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @desc Create a pond document
|
|
194
|
+
* @param id - The id of the document
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
_createPondDocument(id) {
|
|
198
|
+
const removeDoc = this._delete.bind(this, id);
|
|
199
|
+
const updateDoc = this.set.bind(this, id);
|
|
200
|
+
const getDoc = this._getDocument.bind(this, id);
|
|
201
|
+
return new PondDocument(id, removeDoc, updateDoc, getDoc);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @desc Gets the raw database
|
|
205
|
+
* @protected
|
|
206
|
+
*/
|
|
207
|
+
_getDB() {
|
|
208
|
+
return this._db;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.SimpleBase = SimpleBase;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const simpleBase_1 = require("./simpleBase");
|
|
4
|
+
describe('SimpleBase', () => {
|
|
5
|
+
it('should ba instantiated', () => {
|
|
6
|
+
const base = new simpleBase_1.SimpleBase();
|
|
7
|
+
expect(base).toBeTruthy();
|
|
8
|
+
});
|
|
9
|
+
it('should set a value', () => {
|
|
10
|
+
const base = new simpleBase_1.SimpleBase();
|
|
11
|
+
base.set('test', { name: 'test' });
|
|
12
|
+
expect(base.size).toBe(1);
|
|
13
|
+
});
|
|
14
|
+
it('should get a value', () => {
|
|
15
|
+
var _a;
|
|
16
|
+
const base = new simpleBase_1.SimpleBase();
|
|
17
|
+
base.set('test', { name: 'test' });
|
|
18
|
+
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toStrictEqual({ name: 'test' });
|
|
19
|
+
});
|
|
20
|
+
it('should upsert a value', () => {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const base = new simpleBase_1.SimpleBase();
|
|
23
|
+
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toBeUndefined();
|
|
24
|
+
expect(base.size).toBe(0);
|
|
25
|
+
base.getOrCreate('test', () => ({ name: 'test' }));
|
|
26
|
+
expect(base.size).toBe(1);
|
|
27
|
+
expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toStrictEqual({ name: 'test' });
|
|
28
|
+
});
|
|
29
|
+
it('should tell if a document exists', () => {
|
|
30
|
+
const base = new simpleBase_1.SimpleBase();
|
|
31
|
+
expect(base.has('test')).toBe(false);
|
|
32
|
+
base.set('test', { name: 'test' });
|
|
33
|
+
expect(base.has('test')).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
it('should return an array of filtered documents', () => {
|
|
36
|
+
const base = new simpleBase_1.SimpleBase();
|
|
37
|
+
base.set('test', { name: 'test' });
|
|
38
|
+
base.set('test2', { name: 'test2' });
|
|
39
|
+
base.set('test3', { name: 'test3' });
|
|
40
|
+
expect(base.filter(doc => doc.name === 'test').map(doc => doc.doc.name)).toEqual(['test']);
|
|
41
|
+
});
|
|
42
|
+
it('should return an array of mapped documents', () => {
|
|
43
|
+
const base = new simpleBase_1.SimpleBase();
|
|
44
|
+
base.set('test', { name: 'test' });
|
|
45
|
+
base.set('test2', { name: 'test2' });
|
|
46
|
+
base.set('test3', { name: 'test3' });
|
|
47
|
+
expect(base.map(doc => doc.name)).toEqual(['test', 'test2', 'test3']);
|
|
48
|
+
});
|
|
49
|
+
it('should return an empty array of documents', () => {
|
|
50
|
+
const base = new simpleBase_1.SimpleBase();
|
|
51
|
+
expect(base.map(doc => doc.name)).toEqual([]);
|
|
52
|
+
expect(base.filter(doc => doc.name === 'test').map(doc => doc.doc.name)).toEqual([]);
|
|
53
|
+
});
|
|
54
|
+
it('should return a document by a query function', () => {
|
|
55
|
+
var _a;
|
|
56
|
+
const base = new simpleBase_1.SimpleBase();
|
|
57
|
+
base.set('test', { name: 'test' });
|
|
58
|
+
base.set('test2', { name: 'test2' });
|
|
59
|
+
base.set('test3', { name: 'test3' });
|
|
60
|
+
expect((_a = base.find(doc => doc.name === 'test2')) === null || _a === void 0 ? void 0 : _a.doc.name).toBe('test2');
|
|
61
|
+
});
|
|
62
|
+
it('should return null if no document is found by a query function', () => {
|
|
63
|
+
const base = new simpleBase_1.SimpleBase();
|
|
64
|
+
base.set('test', { name: 'test' });
|
|
65
|
+
base.set('test2', { name: 'test2' });
|
|
66
|
+
base.set('test3', { name: 'test3' });
|
|
67
|
+
expect(base.find(doc => doc.name === 'test4')).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
it('should reduce the pond to a single value', () => {
|
|
70
|
+
const base = new simpleBase_1.SimpleBase();
|
|
71
|
+
base.set('test', { name: 'test' });
|
|
72
|
+
base.set('test2', { name: 'test2' });
|
|
73
|
+
base.set('test3', { name: 'test3' });
|
|
74
|
+
expect(base.reduce((acc, doc) => acc + doc.name, '')).toBe('testtest2test3');
|
|
75
|
+
});
|
|
76
|
+
it('should return the size of the pond', () => {
|
|
77
|
+
const base = new simpleBase_1.SimpleBase();
|
|
78
|
+
base.set('test', { name: 'test' });
|
|
79
|
+
base.set('test2', { name: 'test2' });
|
|
80
|
+
base.set('test3', { name: 'test3' });
|
|
81
|
+
expect(base.size).toBe(3);
|
|
82
|
+
});
|
|
83
|
+
it('should provide an empty document', () => {
|
|
84
|
+
const base = new simpleBase_1.SimpleBase();
|
|
85
|
+
const test = base.createGenericDocument();
|
|
86
|
+
expect(test).toBeInstanceOf(simpleBase_1.PondDocument);
|
|
87
|
+
expect(test.doc).toBeNull();
|
|
88
|
+
});
|
|
89
|
+
it('should provide a document with a value', () => {
|
|
90
|
+
const base = new simpleBase_1.SimpleBase();
|
|
91
|
+
const test = base.createGenericDocument('test');
|
|
92
|
+
expect(base.size).toBe(0);
|
|
93
|
+
expect(test).toBeInstanceOf(simpleBase_1.PondDocument);
|
|
94
|
+
expect(test.id).toBe('test');
|
|
95
|
+
});
|
|
96
|
+
it('should be iterable', () => {
|
|
97
|
+
const base = new simpleBase_1.SimpleBase();
|
|
98
|
+
base.set('test', { name: 'test' });
|
|
99
|
+
base.set('test2', { name: 'test2' });
|
|
100
|
+
base.set('test3', { name: 'test3' });
|
|
101
|
+
expect([...base].map(doc => doc.doc.name)).toEqual(['test', 'test2', 'test3']);
|
|
102
|
+
});
|
|
103
|
+
it('should be iterable with a for of loop', () => {
|
|
104
|
+
const base = new simpleBase_1.SimpleBase();
|
|
105
|
+
base.set('test', { name: 'test' });
|
|
106
|
+
base.set('test2', { name: 'test2' });
|
|
107
|
+
base.set('test3', { name: 'test3' });
|
|
108
|
+
const docs = [];
|
|
109
|
+
for (const doc of base) {
|
|
110
|
+
docs.push(doc.doc.name);
|
|
111
|
+
}
|
|
112
|
+
expect(docs).toEqual(['test', 'test2', 'test3']);
|
|
113
|
+
});
|
|
114
|
+
it('should be have a generator', () => {
|
|
115
|
+
const base = new simpleBase_1.SimpleBase();
|
|
116
|
+
base.set('test', { name: 'test' });
|
|
117
|
+
base.set('test2', { name: 'test2' });
|
|
118
|
+
base.set('test3', { name: 'test3' });
|
|
119
|
+
const docs = [];
|
|
120
|
+
for (const doc of base.generator()) {
|
|
121
|
+
docs.push(doc.doc.name);
|
|
122
|
+
}
|
|
123
|
+
expect(docs).toEqual(['test', 'test2', 'test3']);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('PondDocument', () => {
|
|
127
|
+
it('should be able to set a value', () => {
|
|
128
|
+
var _a;
|
|
129
|
+
const base = new simpleBase_1.SimpleBase();
|
|
130
|
+
const test = base.createGenericDocument('test');
|
|
131
|
+
test.updateDoc({ name: 'test' });
|
|
132
|
+
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toStrictEqual({ name: 'test' });
|
|
133
|
+
});
|
|
134
|
+
it('should be able to get a value', () => {
|
|
135
|
+
const base = new simpleBase_1.SimpleBase();
|
|
136
|
+
const test = base.createGenericDocument('test');
|
|
137
|
+
test.updateDoc({ name: 'test' });
|
|
138
|
+
expect(test.doc).toStrictEqual({ name: 'test' });
|
|
139
|
+
});
|
|
140
|
+
it('should be able to delete a value', () => {
|
|
141
|
+
const base = new simpleBase_1.SimpleBase();
|
|
142
|
+
const test = base.createGenericDocument('test');
|
|
143
|
+
test.updateDoc({ name: 'test' });
|
|
144
|
+
expect(base.size).toBe(1);
|
|
145
|
+
test.removeDoc();
|
|
146
|
+
expect(base.size).toBe(0);
|
|
147
|
+
});
|
|
148
|
+
it('should be able to get the id', () => {
|
|
149
|
+
const base = new simpleBase_1.SimpleBase();
|
|
150
|
+
const test = base.createGenericDocument('test');
|
|
151
|
+
expect(test.id).toBe('test');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {Subscription} from "rxjs";
|
|
2
|
+
import {PondAssigns, PondMessage, PondPresence} from "../pondSocket";
|
|
3
|
+
|
|
4
|
+
export declare type ChannelParams = PondAssigns;
|
|
5
|
+
|
|
6
|
+
export declare class Channel {
|
|
7
|
+
readonly channel: string;
|
|
8
|
+
|
|
9
|
+
get isActive(): boolean;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @desc Connects to the channel.
|
|
13
|
+
*/
|
|
14
|
+
join(): this;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @desc Disconnects from the channel.
|
|
18
|
+
*/
|
|
19
|
+
leave(): void;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @desc Monitors the presence state of the channel.
|
|
23
|
+
* @param callback - The callback to call when the presence state changes.
|
|
24
|
+
*/
|
|
25
|
+
onPresenceUpdate(callback: (presence: PondPresence[]) => void): Subscription;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @desc Monitors the channel for messages.
|
|
29
|
+
* @param callback - The callback to call when a message is received.
|
|
30
|
+
*/
|
|
31
|
+
onMessage(callback: (event: string, message: PondMessage) => void): Subscription;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @desc Broadcasts a message to the channel, including yourself.
|
|
35
|
+
* @param event - The event to send.
|
|
36
|
+
* @param payload - The message to send.
|
|
37
|
+
*/
|
|
38
|
+
broadcast(event: string, payload: PondMessage): void;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
42
|
+
* @param event - The event to send.
|
|
43
|
+
* @param payload - The message to send.
|
|
44
|
+
*/
|
|
45
|
+
broadcastFrom(event: string, payload: PondMessage): void;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @desc Updates the presence state of the current client in the channel.
|
|
49
|
+
* @param presence - The presence state to update.
|
|
50
|
+
*/
|
|
51
|
+
updatePresence(presence: PondPresence): void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @desc Sends a message to specific clients in the channel.
|
|
55
|
+
* @param event - The event to send.
|
|
56
|
+
* @param payload - The message to send.
|
|
57
|
+
* @param recipient - The clients to send the message to.
|
|
58
|
+
*/
|
|
59
|
+
sendMessage(event: string, payload: PondMessage, recipient: string[]): void;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @desc Listens for the connections state of the channel.
|
|
63
|
+
* @param callback - The callback to call when the connection state changes.
|
|
64
|
+
*/
|
|
65
|
+
onConnectionChange(callback: (connected: boolean) => void): import("../pondBase").Subscription;
|
|
66
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Channel = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const operators_1 = require("rxjs/operators");
|
|
6
|
+
const pondSocket_1 = require("../pondSocket");
|
|
7
|
+
const pondBase_1 = require("../pondBase");
|
|
8
|
+
class Channel {
|
|
9
|
+
constructor(channel, params, socket) {
|
|
10
|
+
this._subscriptions = [];
|
|
11
|
+
this._presenceSubject = new rxjs_1.Subject();
|
|
12
|
+
this.channel = channel;
|
|
13
|
+
this._params = params;
|
|
14
|
+
this._socket = socket;
|
|
15
|
+
this._subject = new rxjs_1.Subject();
|
|
16
|
+
this._connectedSubject = new pondBase_1.Subject(false);
|
|
17
|
+
}
|
|
18
|
+
get isActive() {
|
|
19
|
+
return this._connectedSubject.value;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @desc Connects to the channel.
|
|
23
|
+
*/
|
|
24
|
+
join() {
|
|
25
|
+
if (this._connectedSubject.value)
|
|
26
|
+
return this;
|
|
27
|
+
const observable = this._init();
|
|
28
|
+
const subscription = observable
|
|
29
|
+
.subscribe(message => {
|
|
30
|
+
this._connectedSubject.publish(true);
|
|
31
|
+
if (message.action === "PRESENCE")
|
|
32
|
+
this._presenceSubject.next(message.payload.presence);
|
|
33
|
+
else if (message.action === "MESSAGE")
|
|
34
|
+
this._subject.next(message);
|
|
35
|
+
else if (message.event === "KICKED_FROM_CHANNEL")
|
|
36
|
+
this.leave();
|
|
37
|
+
});
|
|
38
|
+
this._subscriptions.push(subscription);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @desc Disconnects from the channel.
|
|
43
|
+
*/
|
|
44
|
+
leave() {
|
|
45
|
+
void this._connectedSubject.publish(false);
|
|
46
|
+
this._presenceSubject.complete();
|
|
47
|
+
this._subscriptions.forEach(subscription => subscription.unsubscribe());
|
|
48
|
+
this._subscriptions = [];
|
|
49
|
+
this._subject.complete();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @desc Monitors the presence state of the channel.
|
|
53
|
+
* @param callback - The callback to call when the presence state changes.
|
|
54
|
+
*/
|
|
55
|
+
onPresenceUpdate(callback) {
|
|
56
|
+
const sub = this._presenceSubject.subscribe(callback);
|
|
57
|
+
this._subscriptions.push(sub);
|
|
58
|
+
return sub;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @desc Monitors the channel for messages.
|
|
62
|
+
* @param callback - The callback to call when a message is received.
|
|
63
|
+
*/
|
|
64
|
+
onMessage(callback) {
|
|
65
|
+
const sub = this._subject
|
|
66
|
+
.pipe((0, operators_1.filter)((message) => message.action === "MESSAGE"))
|
|
67
|
+
.subscribe(message => callback(message.event, message.payload));
|
|
68
|
+
this._subscriptions.push(sub);
|
|
69
|
+
return sub;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @desc Broadcasts a message to the channel, including yourself.
|
|
73
|
+
* @param event - The event to send.
|
|
74
|
+
* @param payload - The message to send.
|
|
75
|
+
*/
|
|
76
|
+
broadcast(event, payload) {
|
|
77
|
+
const message = {
|
|
78
|
+
channelName: this.channel,
|
|
79
|
+
payload, event,
|
|
80
|
+
action: pondSocket_1.ClientActions.BROADCAST
|
|
81
|
+
};
|
|
82
|
+
this._socket.next(message);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
86
|
+
* @param event - The event to send.
|
|
87
|
+
* @param payload - The message to send.
|
|
88
|
+
*/
|
|
89
|
+
broadcastFrom(event, payload) {
|
|
90
|
+
const message = {
|
|
91
|
+
channelName: this.channel,
|
|
92
|
+
payload, event,
|
|
93
|
+
action: pondSocket_1.ClientActions.BROADCAST_FROM
|
|
94
|
+
};
|
|
95
|
+
this._socket.next(message);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @desc Updates the presence state of the current client in the channel.
|
|
99
|
+
* @param presence - The presence state to update.
|
|
100
|
+
*/
|
|
101
|
+
updatePresence(presence) {
|
|
102
|
+
this._socket.next({
|
|
103
|
+
action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
|
|
104
|
+
channelName: this.channel,
|
|
105
|
+
event: "PRESENCE",
|
|
106
|
+
payload: presence
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @desc Sends a message to specific clients in the channel.
|
|
111
|
+
* @param event - The event to send.
|
|
112
|
+
* @param payload - The message to send.
|
|
113
|
+
* @param recipient - The clients to send the message to.
|
|
114
|
+
*/
|
|
115
|
+
sendMessage(event, payload, recipient) {
|
|
116
|
+
const addresses = Array.isArray(recipient) ? recipient : [recipient];
|
|
117
|
+
const message = {
|
|
118
|
+
channelName: this.channel,
|
|
119
|
+
payload, event, addresses,
|
|
120
|
+
action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER
|
|
121
|
+
};
|
|
122
|
+
this._socket.next(message);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @desc Listens for the connections state of the channel.
|
|
126
|
+
* @param callback - The callback to call when the connection state changes.
|
|
127
|
+
*/
|
|
128
|
+
onConnectionChange(callback) {
|
|
129
|
+
const sub = this._connectedSubject.subscribe(callback);
|
|
130
|
+
this._subscriptions.push(sub);
|
|
131
|
+
return sub;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @desc Initializes the channel.
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
_init() {
|
|
138
|
+
const observable = this._socket.multiplex(() => ({
|
|
139
|
+
action: "JOIN_CHANNEL",
|
|
140
|
+
channelName: this.channel,
|
|
141
|
+
event: "JOIN_CHANNEL",
|
|
142
|
+
payload: this._params
|
|
143
|
+
}), () => ({
|
|
144
|
+
action: "LEAVE_CHANNEL",
|
|
145
|
+
channelName: this.channel,
|
|
146
|
+
event: "LEAVE_CHANNEL",
|
|
147
|
+
payload: this._params
|
|
148
|
+
}), message => message.channelName === this.channel);
|
|
149
|
+
return observable;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.Channel = Channel;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./socket"), exports);
|
|
18
|
+
__exportStar(require("./channel"), exports);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {Channel, ChannelParams} from "./channel";
|
|
2
|
+
import {default_t} from "../pondBase";
|
|
3
|
+
|
|
4
|
+
declare type PondParams = {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
};
|
|
7
|
+
declare type PondState = "CONNECTING" | "OPEN" | "CLOSING" | "CLOSED";
|
|
8
|
+
|
|
9
|
+
export declare class PondClient {
|
|
10
|
+
|
|
11
|
+
constructor(endpoint: string, params?: PondParams);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @desc Connects to the server and returns the socket.
|
|
15
|
+
*/
|
|
16
|
+
connect(): this | undefined;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @desc Returns the current state of the socket.
|
|
20
|
+
*/
|
|
21
|
+
getState(): PondState;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @desc Creates a channel with the given name and params.
|
|
25
|
+
* @param channel - The name of the channel.
|
|
26
|
+
* @param params - The params to send to the server.
|
|
27
|
+
*/
|
|
28
|
+
createChannel(channel: string, params?: ChannelParams): Channel | null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @desc An event that is triggered when the socket receives a message.
|
|
32
|
+
* @param callback - The callback to be called when the event is triggered.
|
|
33
|
+
*/
|
|
34
|
+
onMessage(callback: (event: string, message: default_t) => void): void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @desc Disconnects the socket from the server.
|
|
38
|
+
*/
|
|
39
|
+
disconnect(): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export {};
|