@eleven-am/pondsocket 0.1.7 → 0.1.9
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/README.md +3 -5
- package/package.json +1 -2
- package/pondBase/baseClass.d.ts +3 -1
- package/pondBase/baseClass.js +51 -32
- package/pondBase/baseClass.test.js +34 -34
- package/pondBase/pondBase.js +95 -34
- package/pondBase/pondBase.test.js +67 -39
- package/pondBase/pubSub.d.ts +10 -5
- package/pondBase/pubSub.js +127 -57
- package/pondBase/pubSub.test.js +120 -106
- package/pondBase/simpleBase.d.ts +0 -5
- package/pondBase/simpleBase.js +166 -92
- package/pondBase/simpleBase.test.js +124 -66
- package/pondClient/channel.d.ts +19 -19
- package/pondClient/channel.js +79 -100
- package/pondClient/socket.d.ts +13 -14
- package/pondClient/socket.js +58 -89
- package/pondSocket/channel.d.ts +2 -7
- package/pondSocket/channel.js +174 -121
- package/pondSocket/channel.test.js +89 -78
- package/pondSocket/channelMiddleWare.d.ts +2 -0
- package/pondSocket/channelMiddleWare.js +43 -16
- package/pondSocket/endpoint.d.ts +2 -2
- package/pondSocket/endpoint.js +188 -96
- package/pondSocket/endpoint.test.js +641 -479
- package/pondSocket/pondChannel.d.ts +2 -2
- package/pondSocket/pondChannel.js +142 -89
- package/pondSocket/pondChannel.test.js +40 -40
- package/pondSocket/pondResponse.js +87 -52
- package/pondSocket/pondSocket.d.ts +2 -2
- package/pondSocket/pondSocket.js +55 -35
- package/pondSocket/server.test.js +151 -88
- package/pondSocket/socketMiddleWare.js +16 -14
package/README.md
CHANGED
|
@@ -19,13 +19,11 @@ Multiple endpoints can be created but every endpoint is independent of the other
|
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
21
|
import { PondSocket } from "@eleven-am/pondsocket";
|
|
22
|
-
import parse from "url";
|
|
23
22
|
|
|
24
23
|
const pond = new PondSocket();
|
|
25
24
|
|
|
26
25
|
const endpoint = pond.createEndpoint('/api/socket', (req, res, _endpoint) => {
|
|
27
|
-
const
|
|
28
|
-
const { token } = query;
|
|
26
|
+
const token = req.query.token;
|
|
29
27
|
if (!token)
|
|
30
28
|
return res.reject('No token provided');
|
|
31
29
|
res.accept({
|
|
@@ -69,7 +67,7 @@ It can be anything from a boolean to an instance of a class. This data cannot be
|
|
|
69
67
|
|
|
70
68
|
```js
|
|
71
69
|
channel.on('hello', (req, res, channel) => {
|
|
72
|
-
const users = channel.
|
|
70
|
+
const users = channel.presence;
|
|
73
71
|
res.assign({
|
|
74
72
|
assign: {
|
|
75
73
|
pingDate: new Date(),
|
|
@@ -91,7 +89,7 @@ In case there is no *on* function, the message will be sent without any action b
|
|
|
91
89
|
```js
|
|
92
90
|
import { PondClient } from "@eleven-am/pondsocket/client";
|
|
93
91
|
|
|
94
|
-
export const socket = new
|
|
92
|
+
export const socket = new PondClient('/api/socket', {});
|
|
95
93
|
socket.connect();
|
|
96
94
|
```
|
|
97
95
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
"url": "git+https://github.com/Eleven-am/pondSocket.git"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"rxjs": "^7.5.6",
|
|
32
31
|
"ws": "^8.8.1"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
package/pondBase/baseClass.d.ts
CHANGED
|
@@ -49,5 +49,7 @@ export declare class BaseClass {
|
|
|
49
49
|
* /api/id?name=abc should return { name: 'abc' }
|
|
50
50
|
* /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
|
|
51
51
|
*/
|
|
52
|
-
protected _parseQueries(path: string):
|
|
52
|
+
protected _parseQueries(path: string): {
|
|
53
|
+
[p: string]: string;
|
|
54
|
+
};
|
|
53
55
|
}
|
package/pondBase/baseClass.js
CHANGED
|
@@ -1,53 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
2
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
19
|
exports.BaseClass = void 0;
|
|
4
|
-
|
|
20
|
+
var BaseClass = /** @class */ (function () {
|
|
21
|
+
function BaseClass() {
|
|
22
|
+
}
|
|
5
23
|
/**
|
|
6
24
|
* @desc checks if the pattern is matchable
|
|
7
25
|
* @param pattern - the pattern to check
|
|
8
26
|
*/
|
|
9
|
-
|
|
27
|
+
BaseClass.isPatternMatchable = function (pattern) {
|
|
10
28
|
return typeof pattern === 'string' && pattern.includes(':');
|
|
11
|
-
}
|
|
29
|
+
};
|
|
12
30
|
/**
|
|
13
31
|
* @desc compares string to string | regex
|
|
14
32
|
* @param string - the string to compare to the pattern
|
|
15
33
|
* @param pattern - the pattern to compare to the string
|
|
16
34
|
*/
|
|
17
|
-
compareStringToPattern(string, pattern) {
|
|
35
|
+
BaseClass.prototype.compareStringToPattern = function (string, pattern) {
|
|
18
36
|
if (typeof pattern === 'string')
|
|
19
37
|
return string.split('?')[0] === pattern;
|
|
20
38
|
else
|
|
21
39
|
return pattern.test(string);
|
|
22
|
-
}
|
|
40
|
+
};
|
|
23
41
|
/**
|
|
24
42
|
* @desc Checks if the given object is empty
|
|
25
43
|
* @param obj - the object to check
|
|
26
44
|
*/
|
|
27
|
-
isObjectEmpty(obj) {
|
|
45
|
+
BaseClass.prototype.isObjectEmpty = function (obj) {
|
|
28
46
|
return Object.keys(obj).length === 0;
|
|
29
|
-
}
|
|
47
|
+
};
|
|
30
48
|
/**
|
|
31
49
|
* @desc Generates a pond request resolver object
|
|
32
50
|
* @param path - the path to resolve
|
|
33
51
|
* @param address - the address to resolve
|
|
34
52
|
*/
|
|
35
|
-
generateEventRequest(path, address) {
|
|
36
|
-
|
|
53
|
+
BaseClass.prototype.generateEventRequest = function (path, address) {
|
|
54
|
+
var match = this._matchStringToPattern(address, path);
|
|
37
55
|
if (match)
|
|
38
56
|
return {
|
|
39
57
|
params: match, query: this._parseQueries(address), address: address
|
|
40
58
|
};
|
|
41
59
|
return null;
|
|
42
|
-
}
|
|
60
|
+
};
|
|
43
61
|
/**
|
|
44
62
|
* @desc Compares if two objects are equal
|
|
45
63
|
* @param obj1 - the first object
|
|
46
64
|
* @param obj2 - the second object
|
|
47
65
|
*/
|
|
48
|
-
areEqual(obj1, obj2) {
|
|
66
|
+
BaseClass.prototype.areEqual = function (obj1, obj2) {
|
|
49
67
|
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
50
|
-
}
|
|
68
|
+
};
|
|
51
69
|
/**
|
|
52
70
|
* @desc Creates an object from the params of a path
|
|
53
71
|
* @param path - the path to create the object from
|
|
@@ -56,17 +74,17 @@ class BaseClass {
|
|
|
56
74
|
* /api/id?name=abc should return { name: 'abc' }
|
|
57
75
|
* /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
|
|
58
76
|
*/
|
|
59
|
-
_parseQueries(path) {
|
|
60
|
-
|
|
61
|
-
|
|
77
|
+
BaseClass.prototype._parseQueries = function (path) {
|
|
78
|
+
var obj = {};
|
|
79
|
+
var params = path.split('?')[1];
|
|
62
80
|
if (params) {
|
|
63
|
-
params.split('&').forEach(param
|
|
64
|
-
|
|
81
|
+
params.split('&').forEach(function (param) {
|
|
82
|
+
var _a = __read(param.split('='), 2), key = _a[0], value = _a[1];
|
|
65
83
|
obj[key] = value;
|
|
66
84
|
});
|
|
67
85
|
}
|
|
68
86
|
return obj;
|
|
69
|
-
}
|
|
87
|
+
};
|
|
70
88
|
/**
|
|
71
89
|
* @desc Returns the {key: value} matches of a string
|
|
72
90
|
* @param string - the string to create the regex from
|
|
@@ -78,34 +96,35 @@ class BaseClass {
|
|
|
78
96
|
* hello:id should match hello:123 and return { id: 123 }
|
|
79
97
|
* @private
|
|
80
98
|
*/
|
|
81
|
-
_matchString(string, pattern) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
99
|
+
BaseClass.prototype._matchString = function (string, pattern) {
|
|
100
|
+
var replace = pattern.replace(/:[^/]+/g, '([^/]+)');
|
|
101
|
+
var regExp = new RegExp("^".concat(replace, "$"));
|
|
102
|
+
var matches = string.split('?')[0].match(regExp);
|
|
85
103
|
if (matches) {
|
|
86
|
-
|
|
104
|
+
var keys = pattern.match(/:[^/]+/g);
|
|
87
105
|
if (keys) {
|
|
88
|
-
|
|
89
|
-
keys.forEach((key, index)
|
|
90
|
-
|
|
106
|
+
var obj_1 = {};
|
|
107
|
+
keys.forEach(function (key, index) {
|
|
108
|
+
obj_1[key.replace(':', '')] = matches[index + 1].replace(/\?.*$/, '');
|
|
91
109
|
});
|
|
92
|
-
return
|
|
110
|
+
return obj_1;
|
|
93
111
|
}
|
|
94
112
|
}
|
|
95
113
|
return null;
|
|
96
|
-
}
|
|
114
|
+
};
|
|
97
115
|
/**
|
|
98
116
|
* @desc matches a string to a pattern and returns its params if any
|
|
99
117
|
* @param string - the string to match
|
|
100
118
|
* @param pattern - the pattern to match to
|
|
101
119
|
*/
|
|
102
|
-
_matchStringToPattern(string, pattern) {
|
|
120
|
+
BaseClass.prototype._matchStringToPattern = function (string, pattern) {
|
|
103
121
|
if (BaseClass.isPatternMatchable(pattern))
|
|
104
122
|
return this._matchString(string, pattern);
|
|
105
|
-
|
|
123
|
+
var valid = this.compareStringToPattern(string, pattern);
|
|
106
124
|
if (valid)
|
|
107
125
|
return {};
|
|
108
126
|
return null;
|
|
109
|
-
}
|
|
110
|
-
|
|
127
|
+
};
|
|
128
|
+
return BaseClass;
|
|
129
|
+
}());
|
|
111
130
|
exports.BaseClass = BaseClass;
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
describe('BaseClass', ()
|
|
5
|
-
|
|
6
|
-
it('should return true when object is empty', ()
|
|
3
|
+
var baseClass_1 = require("./baseClass");
|
|
4
|
+
describe('BaseClass', function () {
|
|
5
|
+
var baseClass = new baseClass_1.BaseClass();
|
|
6
|
+
it('should return true when object is empty', function () {
|
|
7
7
|
expect(baseClass.isObjectEmpty({})).toBe(true);
|
|
8
8
|
});
|
|
9
|
-
it('should return false when object is not empty', ()
|
|
9
|
+
it('should return false when object is not empty', function () {
|
|
10
10
|
expect(baseClass.isObjectEmpty({ test: 5 })).toBe(false);
|
|
11
11
|
});
|
|
12
|
-
it('should return true if a string matches a regex | string', ()
|
|
13
|
-
|
|
12
|
+
it('should return true if a string matches a regex | string', function () {
|
|
13
|
+
var regex = new RegExp(/^test/);
|
|
14
14
|
expect(baseClass.compareStringToPattern('test', regex)).toBe(true);
|
|
15
|
-
|
|
15
|
+
var string = 'test';
|
|
16
16
|
expect(baseClass.compareStringToPattern('test', string)).toBe(true);
|
|
17
17
|
});
|
|
18
|
-
it('should return false if a string does not match a regex | string', ()
|
|
19
|
-
|
|
18
|
+
it('should return false if a string does not match a regex | string', function () {
|
|
19
|
+
var regex = new RegExp(/^test$/);
|
|
20
20
|
expect(baseClass.compareStringToPattern('test2', regex)).toBe(false);
|
|
21
|
-
|
|
21
|
+
var string = 'test';
|
|
22
22
|
expect(baseClass.compareStringToPattern('test2', string)).toBe(false);
|
|
23
23
|
});
|
|
24
|
-
it('should return the params of a string matching the pattern', ()
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
it('should return the params of a string matching the pattern', function () {
|
|
25
|
+
var pattern = '/test/:id';
|
|
26
|
+
var secondPattern = '/test/:id/:id2';
|
|
27
|
+
var string = '/test/5';
|
|
28
|
+
var secondString = '/test/5/6';
|
|
29
29
|
expect(baseClass['_matchString'](string, pattern)).toEqual({ id: '5' });
|
|
30
30
|
expect(baseClass['_matchString'](secondString, secondPattern)).toEqual({ id: '5', id2: '6' });
|
|
31
31
|
// this function fails if the pattern is not a string or regex
|
|
@@ -33,41 +33,41 @@ describe('BaseClass', () => {
|
|
|
33
33
|
// But will return null if the string is smaller than the pattern
|
|
34
34
|
expect(baseClass['_matchString'](string, secondPattern)).toEqual(null);
|
|
35
35
|
//it should also match patterns without the slash
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
var thirdPattern = 'test:id';
|
|
37
|
+
var thirdString = 'test5';
|
|
38
38
|
expect(baseClass['_matchString'](thirdString, thirdPattern)).toEqual({ id: '5' });
|
|
39
39
|
});
|
|
40
|
-
it('should return the query of string', ()
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
it('should return the query of string', function () {
|
|
41
|
+
var string = '/test/5?test=5';
|
|
42
|
+
var secondString = '/test/5?test=5&test2=6';
|
|
43
43
|
expect(baseClass['_parseQueries'](string)).toEqual({ test: '5' });
|
|
44
44
|
expect(baseClass['_parseQueries'](secondString)).toEqual({ test: '5', test2: '6' });
|
|
45
45
|
});
|
|
46
|
-
it('should return true if an object matches another object', ()
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
it('should return true if an object matches another object', function () {
|
|
47
|
+
var object = { test: 5 };
|
|
48
|
+
var secondObject = { test: 5, test2: 6 };
|
|
49
49
|
expect(baseClass.areEqual(object, object)).toBe(true);
|
|
50
50
|
expect(baseClass.areEqual(object, secondObject)).toBe(false);
|
|
51
51
|
});
|
|
52
|
-
it('should return null if the string does not match the pattern', ()
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
it('should return null if the string does not match the pattern', function () {
|
|
53
|
+
var pattern = 'pondSocket';
|
|
54
|
+
var string = '/test2/5';
|
|
55
55
|
expect(baseClass['_matchStringToPattern'](string, pattern)).toBe(null);
|
|
56
56
|
});
|
|
57
|
-
it('should return the params of a string matching the pattern', ()
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
it('should return the params of a string matching the pattern', function () {
|
|
58
|
+
var pattern = 'pondSocket';
|
|
59
|
+
var string = 'pondSocket';
|
|
60
60
|
expect(baseClass['_matchStringToPattern'](string, pattern)).toEqual({});
|
|
61
61
|
});
|
|
62
|
-
it('should generateEventRequest', ()
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
it('should generateEventRequest', function () {
|
|
63
|
+
var pattern = 'pondSocket:test';
|
|
64
|
+
var string = 'pondSockethello?test=5&test2=6';
|
|
65
65
|
expect(baseClass.generateEventRequest(pattern, string)).toEqual({
|
|
66
66
|
address: string,
|
|
67
67
|
params: { test: 'hello' },
|
|
68
68
|
query: { test: '5', test2: '6' }
|
|
69
69
|
});
|
|
70
|
-
|
|
70
|
+
var unMatchingString = 'pondXocket2hello?test=5&test2=6';
|
|
71
71
|
expect(baseClass.generateEventRequest(pattern, unMatchingString)).toEqual(null);
|
|
72
72
|
});
|
|
73
73
|
});
|
package/pondBase/pondBase.js
CHANGED
|
@@ -1,60 +1,121 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __values = (this && this.__values) || function(o) {
|
|
29
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
30
|
+
if (m) return m.call(o);
|
|
31
|
+
if (o && typeof o.length === "number") return {
|
|
32
|
+
next: function () {
|
|
33
|
+
if (o && i >= o.length) o = void 0;
|
|
34
|
+
return { value: o && o[i++], done: !o };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
38
|
+
};
|
|
2
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
40
|
exports.PondBase = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
get _nanoid() {
|
|
17
|
-
let id = '';
|
|
18
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
19
|
-
for (let i = 0; i < 21; i++) {
|
|
20
|
-
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
21
|
-
}
|
|
22
|
-
return id;
|
|
41
|
+
var pubSub_1 = require("./pubSub");
|
|
42
|
+
var simpleBase_1 = require("./simpleBase");
|
|
43
|
+
var enums_1 = require("./enums");
|
|
44
|
+
var PondBase = /** @class */ (function (_super) {
|
|
45
|
+
__extends(PondBase, _super);
|
|
46
|
+
function PondBase() {
|
|
47
|
+
var _this = this;
|
|
48
|
+
var broadcast = new pubSub_1.Broadcast();
|
|
49
|
+
_this = _super.call(this, function (data) { return broadcast.publish(data); }) || this;
|
|
50
|
+
_this._broadcast = broadcast;
|
|
51
|
+
return _this;
|
|
23
52
|
}
|
|
53
|
+
Object.defineProperty(PondBase.prototype, "_nanoid", {
|
|
54
|
+
/**
|
|
55
|
+
* @des Generate a key for a new document
|
|
56
|
+
*/
|
|
57
|
+
get: function () {
|
|
58
|
+
var id = '';
|
|
59
|
+
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
60
|
+
for (var i = 0; i < 21; i++) {
|
|
61
|
+
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
62
|
+
}
|
|
63
|
+
return id;
|
|
64
|
+
},
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: true
|
|
67
|
+
});
|
|
24
68
|
/**
|
|
25
69
|
* @desc Subscribe to the database
|
|
26
70
|
* @param handler - The handler to call when the database is updated
|
|
27
71
|
*/
|
|
28
|
-
subscribe(handler) {
|
|
29
|
-
|
|
30
|
-
|
|
72
|
+
PondBase.prototype.subscribe = function (handler) {
|
|
73
|
+
var _this = this;
|
|
74
|
+
return this._broadcast.subscribe(function (data) {
|
|
75
|
+
var change = enums_1.PondBaseActions.UPDATE_IN_POND;
|
|
31
76
|
if (data.oldValue === null)
|
|
32
77
|
change = enums_1.PondBaseActions.ADD_TO_POND;
|
|
33
78
|
else if (data.currentValue === null)
|
|
34
79
|
change = enums_1.PondBaseActions.REMOVE_FROM_POND;
|
|
35
|
-
handler(Object.values(
|
|
80
|
+
handler(Object.values(_this._getDB()), data.currentValue || data.oldValue, change);
|
|
36
81
|
});
|
|
37
|
-
}
|
|
82
|
+
};
|
|
38
83
|
/**
|
|
39
84
|
* @desc Add a document to the database
|
|
40
85
|
* @param doc - The document to add
|
|
41
86
|
*/
|
|
42
|
-
addDoc(doc) {
|
|
43
|
-
return
|
|
44
|
-
}
|
|
87
|
+
PondBase.prototype.addDoc = function (doc) {
|
|
88
|
+
return _super.prototype.set.call(this, this._nanoid, doc);
|
|
89
|
+
};
|
|
45
90
|
/**
|
|
46
91
|
* @desc Left join two ponds on a key on this pond and a foreign key on the other pond
|
|
47
92
|
* @param pond - The pond to join with
|
|
48
93
|
* @param key - The key to join on
|
|
49
94
|
* @param foreignKey - The foreign key to join on
|
|
50
95
|
*/
|
|
51
|
-
leftJoin(pond, key, foreignKey) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
96
|
+
PondBase.prototype.leftJoin = function (pond, key, foreignKey) {
|
|
97
|
+
var e_1, _a;
|
|
98
|
+
var newPond = new PondBase();
|
|
99
|
+
var _loop_1 = function (doc) {
|
|
100
|
+
var _d;
|
|
101
|
+
var foreignDoc = pond.find(function (d) { return d[foreignKey] === doc.doc[key]; });
|
|
102
|
+
newPond.set(doc.id, __assign(__assign({}, doc.doc), (_d = {}, _d[key] = (foreignDoc === null || foreignDoc === void 0 ? void 0 : foreignDoc.doc) || null, _d)));
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
106
|
+
var doc = _c.value;
|
|
107
|
+
_loop_1(doc);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
111
|
+
finally {
|
|
112
|
+
try {
|
|
113
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
114
|
+
}
|
|
115
|
+
finally { if (e_1) throw e_1.error; }
|
|
56
116
|
}
|
|
57
117
|
return newPond;
|
|
58
|
-
}
|
|
59
|
-
|
|
118
|
+
};
|
|
119
|
+
return PondBase;
|
|
120
|
+
}(simpleBase_1.SimpleBase));
|
|
60
121
|
exports.PondBase = PondBase;
|
|
@@ -1,101 +1,129 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
2
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe('PondBase', ()
|
|
6
|
-
it('should be able to take in a subscriber', ()
|
|
7
|
-
|
|
28
|
+
var pondBase_1 = require("./pondBase");
|
|
29
|
+
var enums_1 = require("./enums");
|
|
30
|
+
describe('PondBase', function () {
|
|
31
|
+
it('should be able to take in a subscriber', function () {
|
|
32
|
+
var base = new pondBase_1.PondBase();
|
|
8
33
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
9
|
-
|
|
34
|
+
var mockSubscriber = jest.fn();
|
|
10
35
|
base.subscribe(mockSubscriber);
|
|
11
36
|
expect(base['_broadcast']['_subscribers'].size).toBe(1);
|
|
12
37
|
});
|
|
13
|
-
it('should be able to remove a subscriber', ()
|
|
14
|
-
|
|
38
|
+
it('should be able to remove a subscriber', function () {
|
|
39
|
+
var base = new pondBase_1.PondBase();
|
|
15
40
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
16
|
-
|
|
17
|
-
|
|
41
|
+
var mockSubscriber = jest.fn();
|
|
42
|
+
var subscription = base.subscribe(mockSubscriber);
|
|
18
43
|
expect(base['_broadcast']['_subscribers'].size).toBe(1);
|
|
19
44
|
subscription.unsubscribe();
|
|
20
45
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
21
46
|
});
|
|
22
|
-
it('should fire the subscriber when a document is added', ()
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
it('should fire the subscriber when a document is added', function () {
|
|
48
|
+
var base = new pondBase_1.PondBase();
|
|
49
|
+
var mockSubscriber = jest.fn();
|
|
25
50
|
base.subscribe(mockSubscriber);
|
|
26
51
|
base.set('test', { name: 'test' });
|
|
27
52
|
expect(mockSubscriber).toBeCalled();
|
|
28
53
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
29
54
|
});
|
|
30
|
-
it('should fire the subscriber when a document is removed', ()
|
|
31
|
-
|
|
32
|
-
|
|
55
|
+
it('should fire the subscriber when a document is removed', function () {
|
|
56
|
+
var base = new pondBase_1.PondBase();
|
|
57
|
+
var mockSubscriber = jest.fn();
|
|
33
58
|
base.subscribe(mockSubscriber);
|
|
34
|
-
|
|
59
|
+
var data = base.set('test', { name: 'test' });
|
|
35
60
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
36
61
|
mockSubscriber.mockClear();
|
|
37
62
|
data.removeDoc();
|
|
38
63
|
expect(mockSubscriber).toBeCalledWith([], { name: "test" }, enums_1.PondBaseActions.REMOVE_FROM_POND);
|
|
39
64
|
});
|
|
40
|
-
it('should fire the subscriber when a document is updated', ()
|
|
41
|
-
|
|
42
|
-
|
|
65
|
+
it('should fire the subscriber when a document is updated', function () {
|
|
66
|
+
var base = new pondBase_1.PondBase();
|
|
67
|
+
var mockSubscriber = jest.fn();
|
|
43
68
|
base.subscribe(mockSubscriber);
|
|
44
|
-
|
|
69
|
+
var data = base.set('test', { name: 'test' });
|
|
45
70
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
46
71
|
mockSubscriber.mockClear();
|
|
47
72
|
data.updateDoc({ name: 'test2' });
|
|
48
73
|
expect(mockSubscriber).toBeCalledWith([{ name: "test2" }], { name: "test2" }, enums_1.PondBaseActions.UPDATE_IN_POND);
|
|
49
74
|
});
|
|
50
|
-
it('should add a document to the database', ()
|
|
75
|
+
it('should add a document to the database', function () {
|
|
51
76
|
var _a, _b;
|
|
52
|
-
|
|
53
|
-
|
|
77
|
+
var base = new pondBase_1.PondBase();
|
|
78
|
+
var data = base.addDoc({ name: 'test' });
|
|
54
79
|
expect(data.id).toBeDefined();
|
|
55
80
|
expect((_a = base.get(data.id)) === null || _a === void 0 ? void 0 : _a.doc).toEqual(data.doc);
|
|
56
81
|
expect((_b = base.get(data.id)) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
|
|
57
82
|
});
|
|
58
|
-
it('should left join with another pond', ()
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
it('should left join with another pond', function () {
|
|
84
|
+
var owners = new pondBase_1.PondBase();
|
|
85
|
+
var pets = new pondBase_1.PondBase();
|
|
61
86
|
owners.addDoc({ name: 'test', age: 10 });
|
|
62
87
|
owners.set('test2', { name: 'test2', age: 12 });
|
|
63
88
|
pets.addDoc({ name: 'test', owner: 'test' });
|
|
64
89
|
pets.addDoc({ name: 'test2', owner: 'test2' });
|
|
65
90
|
pets.set('test3', { name: 'test3', owner: 'test3' });
|
|
66
|
-
|
|
67
|
-
expect([
|
|
91
|
+
var joined = pets.leftJoin(owners, 'owner', 'name');
|
|
92
|
+
expect(__spreadArray([], __read(joined), false).map(function (_a) {
|
|
93
|
+
var doc = _a.doc;
|
|
94
|
+
return doc;
|
|
95
|
+
})).toEqual([
|
|
68
96
|
{ name: 'test', owner: { name: 'test', age: 10 } },
|
|
69
97
|
{ name: 'test2', owner: { name: 'test2', age: 12 } },
|
|
70
98
|
{ name: 'test3', owner: null }
|
|
71
99
|
]);
|
|
72
100
|
});
|
|
73
|
-
it('should be able to get the keys', ()
|
|
74
|
-
|
|
101
|
+
it('should be able to get the keys', function () {
|
|
102
|
+
var base = new pondBase_1.PondBase();
|
|
75
103
|
base.set('test', { name: 'test' });
|
|
76
104
|
base.set('test2', { name: 'test2' });
|
|
77
105
|
expect(base.keys).toEqual(['test', 'test2']);
|
|
78
106
|
});
|
|
79
|
-
it('should be able to get the values', ()
|
|
80
|
-
|
|
107
|
+
it('should be able to get the values', function () {
|
|
108
|
+
var base = new pondBase_1.PondBase();
|
|
81
109
|
base.addDoc({ name: 'test' });
|
|
82
110
|
base.set('test2', { name: 'test2' });
|
|
83
111
|
expect(base.values).toEqual([{ name: 'test' }, { name: 'test2' }]);
|
|
84
112
|
});
|
|
85
|
-
it('should be able to upsert a document', ()
|
|
113
|
+
it('should be able to upsert a document', function () {
|
|
86
114
|
var _a, _b;
|
|
87
|
-
|
|
115
|
+
var base = new pondBase_1.PondBase();
|
|
88
116
|
base.upsert('test', { name: 'test' });
|
|
89
117
|
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
|
|
90
118
|
base.upsert('test', { name: 'test2' });
|
|
91
119
|
expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test2' });
|
|
92
120
|
});
|
|
93
|
-
it('should be able to getOrCreate a document with a function', ()
|
|
121
|
+
it('should be able to getOrCreate a document with a function', function () {
|
|
94
122
|
var _a, _b;
|
|
95
|
-
|
|
96
|
-
base.getOrCreate('test', ()
|
|
123
|
+
var base = new pondBase_1.PondBase();
|
|
124
|
+
base.getOrCreate('test', function () { return ({ name: 'test' }); });
|
|
97
125
|
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
|
|
98
|
-
base.getOrCreate('test', ()
|
|
126
|
+
base.getOrCreate('test', function () { return ({ name: 'test2' }); });
|
|
99
127
|
expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
|
|
100
128
|
});
|
|
101
129
|
});
|