@eleven-am/pondsocket 0.1.9 → 0.1.10
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/package.json +1 -1
- package/pondBase/baseClass.js +32 -51
- package/pondBase/baseClass.test.js +34 -34
- package/pondBase/pondBase.js +34 -95
- package/pondBase/pondBase.test.js +39 -67
- package/pondBase/pubSub.js +60 -124
- package/pondBase/pubSub.test.js +110 -110
- package/pondBase/simpleBase.js +92 -166
- package/pondBase/simpleBase.test.js +66 -124
- package/pondClient/channel.js +37 -40
- package/pondClient/socket.js +35 -38
- package/pondSocket/channel.js +121 -174
- package/pondSocket/channel.test.js +78 -89
- package/pondSocket/channelMiddleWare.js +16 -43
- package/pondSocket/endpoint.js +93 -188
- package/pondSocket/endpoint.test.js +447 -632
- package/pondSocket/pondChannel.js +81 -142
- package/pondSocket/pondChannel.test.js +40 -40
- package/pondSocket/pondResponse.js +52 -87
- package/pondSocket/pondSocket.js +35 -55
- package/pondSocket/server.test.js +88 -142
- package/pondSocket/socketMiddleWare.js +14 -16
package/package.json
CHANGED
package/pondBase/baseClass.js
CHANGED
|
@@ -1,71 +1,53 @@
|
|
|
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.BaseClass = void 0;
|
|
20
|
-
|
|
21
|
-
function BaseClass() {
|
|
22
|
-
}
|
|
4
|
+
class BaseClass {
|
|
23
5
|
/**
|
|
24
6
|
* @desc checks if the pattern is matchable
|
|
25
7
|
* @param pattern - the pattern to check
|
|
26
8
|
*/
|
|
27
|
-
|
|
9
|
+
static isPatternMatchable(pattern) {
|
|
28
10
|
return typeof pattern === 'string' && pattern.includes(':');
|
|
29
|
-
}
|
|
11
|
+
}
|
|
30
12
|
/**
|
|
31
13
|
* @desc compares string to string | regex
|
|
32
14
|
* @param string - the string to compare to the pattern
|
|
33
15
|
* @param pattern - the pattern to compare to the string
|
|
34
16
|
*/
|
|
35
|
-
|
|
17
|
+
compareStringToPattern(string, pattern) {
|
|
36
18
|
if (typeof pattern === 'string')
|
|
37
19
|
return string.split('?')[0] === pattern;
|
|
38
20
|
else
|
|
39
21
|
return pattern.test(string);
|
|
40
|
-
}
|
|
22
|
+
}
|
|
41
23
|
/**
|
|
42
24
|
* @desc Checks if the given object is empty
|
|
43
25
|
* @param obj - the object to check
|
|
44
26
|
*/
|
|
45
|
-
|
|
27
|
+
isObjectEmpty(obj) {
|
|
46
28
|
return Object.keys(obj).length === 0;
|
|
47
|
-
}
|
|
29
|
+
}
|
|
48
30
|
/**
|
|
49
31
|
* @desc Generates a pond request resolver object
|
|
50
32
|
* @param path - the path to resolve
|
|
51
33
|
* @param address - the address to resolve
|
|
52
34
|
*/
|
|
53
|
-
|
|
54
|
-
|
|
35
|
+
generateEventRequest(path, address) {
|
|
36
|
+
const match = this._matchStringToPattern(address, path);
|
|
55
37
|
if (match)
|
|
56
38
|
return {
|
|
57
39
|
params: match, query: this._parseQueries(address), address: address
|
|
58
40
|
};
|
|
59
41
|
return null;
|
|
60
|
-
}
|
|
42
|
+
}
|
|
61
43
|
/**
|
|
62
44
|
* @desc Compares if two objects are equal
|
|
63
45
|
* @param obj1 - the first object
|
|
64
46
|
* @param obj2 - the second object
|
|
65
47
|
*/
|
|
66
|
-
|
|
48
|
+
areEqual(obj1, obj2) {
|
|
67
49
|
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
|
68
|
-
}
|
|
50
|
+
}
|
|
69
51
|
/**
|
|
70
52
|
* @desc Creates an object from the params of a path
|
|
71
53
|
* @param path - the path to create the object from
|
|
@@ -74,17 +56,17 @@ var BaseClass = /** @class */ (function () {
|
|
|
74
56
|
* /api/id?name=abc should return { name: 'abc' }
|
|
75
57
|
* /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
|
|
76
58
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
59
|
+
_parseQueries(path) {
|
|
60
|
+
const obj = {};
|
|
61
|
+
const params = path.split('?')[1];
|
|
80
62
|
if (params) {
|
|
81
|
-
params.split('&').forEach(
|
|
82
|
-
|
|
63
|
+
params.split('&').forEach(param => {
|
|
64
|
+
const [key, value] = param.split('=');
|
|
83
65
|
obj[key] = value;
|
|
84
66
|
});
|
|
85
67
|
}
|
|
86
68
|
return obj;
|
|
87
|
-
}
|
|
69
|
+
}
|
|
88
70
|
/**
|
|
89
71
|
* @desc Returns the {key: value} matches of a string
|
|
90
72
|
* @param string - the string to create the regex from
|
|
@@ -96,35 +78,34 @@ var BaseClass = /** @class */ (function () {
|
|
|
96
78
|
* hello:id should match hello:123 and return { id: 123 }
|
|
97
79
|
* @private
|
|
98
80
|
*/
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
81
|
+
_matchString(string, pattern) {
|
|
82
|
+
const replace = pattern.replace(/:[^/]+/g, '([^/]+)');
|
|
83
|
+
const regExp = new RegExp(`^${replace}$`);
|
|
84
|
+
const matches = string.split('?')[0].match(regExp);
|
|
103
85
|
if (matches) {
|
|
104
|
-
|
|
86
|
+
const keys = pattern.match(/:[^/]+/g);
|
|
105
87
|
if (keys) {
|
|
106
|
-
|
|
107
|
-
keys.forEach(
|
|
108
|
-
|
|
88
|
+
const obj = {};
|
|
89
|
+
keys.forEach((key, index) => {
|
|
90
|
+
obj[key.replace(':', '')] = matches[index + 1].replace(/\?.*$/, '');
|
|
109
91
|
});
|
|
110
|
-
return
|
|
92
|
+
return obj;
|
|
111
93
|
}
|
|
112
94
|
}
|
|
113
95
|
return null;
|
|
114
|
-
}
|
|
96
|
+
}
|
|
115
97
|
/**
|
|
116
98
|
* @desc matches a string to a pattern and returns its params if any
|
|
117
99
|
* @param string - the string to match
|
|
118
100
|
* @param pattern - the pattern to match to
|
|
119
101
|
*/
|
|
120
|
-
|
|
102
|
+
_matchStringToPattern(string, pattern) {
|
|
121
103
|
if (BaseClass.isPatternMatchable(pattern))
|
|
122
104
|
return this._matchString(string, pattern);
|
|
123
|
-
|
|
105
|
+
const valid = this.compareStringToPattern(string, pattern);
|
|
124
106
|
if (valid)
|
|
125
107
|
return {};
|
|
126
108
|
return null;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
}());
|
|
109
|
+
}
|
|
110
|
+
}
|
|
130
111
|
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
|
+
const baseClass_1 = require("./baseClass");
|
|
4
|
+
describe('BaseClass', () => {
|
|
5
|
+
const baseClass = new baseClass_1.BaseClass();
|
|
6
|
+
it('should return true when object is empty', () => {
|
|
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', () => {
|
|
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', () => {
|
|
13
|
+
const regex = new RegExp(/^test/);
|
|
14
14
|
expect(baseClass.compareStringToPattern('test', regex)).toBe(true);
|
|
15
|
-
|
|
15
|
+
const 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', () => {
|
|
19
|
+
const regex = new RegExp(/^test$/);
|
|
20
20
|
expect(baseClass.compareStringToPattern('test2', regex)).toBe(false);
|
|
21
|
-
|
|
21
|
+
const 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', () => {
|
|
25
|
+
const pattern = '/test/:id';
|
|
26
|
+
const secondPattern = '/test/:id/:id2';
|
|
27
|
+
const string = '/test/5';
|
|
28
|
+
const 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', function () {
|
|
|
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
|
+
const thirdPattern = 'test:id';
|
|
37
|
+
const 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', () => {
|
|
41
|
+
const string = '/test/5?test=5';
|
|
42
|
+
const 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', () => {
|
|
47
|
+
const object = { test: 5 };
|
|
48
|
+
const 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', () => {
|
|
53
|
+
const pattern = 'pondSocket';
|
|
54
|
+
const 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', () => {
|
|
58
|
+
const pattern = 'pondSocket';
|
|
59
|
+
const string = 'pondSocket';
|
|
60
60
|
expect(baseClass['_matchStringToPattern'](string, pattern)).toEqual({});
|
|
61
61
|
});
|
|
62
|
-
it('should generateEventRequest',
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
it('should generateEventRequest', () => {
|
|
63
|
+
const pattern = 'pondSocket:test';
|
|
64
|
+
const 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
|
+
const 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,121 +1,60 @@
|
|
|
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
|
-
};
|
|
39
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
3
|
exports.PondBase = void 0;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
4
|
+
const pubSub_1 = require("./pubSub");
|
|
5
|
+
const simpleBase_1 = require("./simpleBase");
|
|
6
|
+
const enums_1 = require("./enums");
|
|
7
|
+
class PondBase extends simpleBase_1.SimpleBase {
|
|
8
|
+
constructor() {
|
|
9
|
+
const broadcast = new pubSub_1.Broadcast();
|
|
10
|
+
super((data) => broadcast.publish(data));
|
|
11
|
+
this._broadcast = broadcast;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @des Generate a key for a new document
|
|
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;
|
|
52
23
|
}
|
|
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
|
-
});
|
|
68
24
|
/**
|
|
69
25
|
* @desc Subscribe to the database
|
|
70
26
|
* @param handler - The handler to call when the database is updated
|
|
71
27
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var change = enums_1.PondBaseActions.UPDATE_IN_POND;
|
|
28
|
+
subscribe(handler) {
|
|
29
|
+
return this._broadcast.subscribe((data) => {
|
|
30
|
+
let change = enums_1.PondBaseActions.UPDATE_IN_POND;
|
|
76
31
|
if (data.oldValue === null)
|
|
77
32
|
change = enums_1.PondBaseActions.ADD_TO_POND;
|
|
78
33
|
else if (data.currentValue === null)
|
|
79
34
|
change = enums_1.PondBaseActions.REMOVE_FROM_POND;
|
|
80
|
-
handler(Object.values(
|
|
35
|
+
handler(Object.values(this._getDB()), data.currentValue || data.oldValue, change);
|
|
81
36
|
});
|
|
82
|
-
}
|
|
37
|
+
}
|
|
83
38
|
/**
|
|
84
39
|
* @desc Add a document to the database
|
|
85
40
|
* @param doc - The document to add
|
|
86
41
|
*/
|
|
87
|
-
|
|
88
|
-
return
|
|
89
|
-
}
|
|
42
|
+
addDoc(doc) {
|
|
43
|
+
return super.set(this._nanoid, doc);
|
|
44
|
+
}
|
|
90
45
|
/**
|
|
91
46
|
* @desc Left join two ponds on a key on this pond and a foreign key on the other pond
|
|
92
47
|
* @param pond - The pond to join with
|
|
93
48
|
* @param key - The key to join on
|
|
94
49
|
* @param foreignKey - The foreign key to join on
|
|
95
50
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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; }
|
|
51
|
+
leftJoin(pond, key, foreignKey) {
|
|
52
|
+
const newPond = new PondBase();
|
|
53
|
+
for (const doc of this) {
|
|
54
|
+
const foreignDoc = pond.find((d) => d[foreignKey] === doc.doc[key]);
|
|
55
|
+
newPond.set(doc.id, Object.assign(Object.assign({}, doc.doc), { [key]: (foreignDoc === null || foreignDoc === void 0 ? void 0 : foreignDoc.doc) || null }));
|
|
116
56
|
}
|
|
117
57
|
return newPond;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
}(simpleBase_1.SimpleBase));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
121
60
|
exports.PondBase = PondBase;
|
|
@@ -1,129 +1,101 @@
|
|
|
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
|
-
};
|
|
27
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
describe('PondBase',
|
|
31
|
-
it('should be able to take in a subscriber',
|
|
32
|
-
|
|
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();
|
|
33
8
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
34
|
-
|
|
9
|
+
const mockSubscriber = jest.fn();
|
|
35
10
|
base.subscribe(mockSubscriber);
|
|
36
11
|
expect(base['_broadcast']['_subscribers'].size).toBe(1);
|
|
37
12
|
});
|
|
38
|
-
it('should be able to remove a subscriber',
|
|
39
|
-
|
|
13
|
+
it('should be able to remove a subscriber', () => {
|
|
14
|
+
const base = new pondBase_1.PondBase();
|
|
40
15
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
41
|
-
|
|
42
|
-
|
|
16
|
+
const mockSubscriber = jest.fn();
|
|
17
|
+
const subscription = base.subscribe(mockSubscriber);
|
|
43
18
|
expect(base['_broadcast']['_subscribers'].size).toBe(1);
|
|
44
19
|
subscription.unsubscribe();
|
|
45
20
|
expect(base['_broadcast']['_subscribers'].size).toBe(0);
|
|
46
21
|
});
|
|
47
|
-
it('should fire the subscriber when a document is added',
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
it('should fire the subscriber when a document is added', () => {
|
|
23
|
+
const base = new pondBase_1.PondBase();
|
|
24
|
+
const mockSubscriber = jest.fn();
|
|
50
25
|
base.subscribe(mockSubscriber);
|
|
51
26
|
base.set('test', { name: 'test' });
|
|
52
27
|
expect(mockSubscriber).toBeCalled();
|
|
53
28
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
54
29
|
});
|
|
55
|
-
it('should fire the subscriber when a document is removed',
|
|
56
|
-
|
|
57
|
-
|
|
30
|
+
it('should fire the subscriber when a document is removed', () => {
|
|
31
|
+
const base = new pondBase_1.PondBase();
|
|
32
|
+
const mockSubscriber = jest.fn();
|
|
58
33
|
base.subscribe(mockSubscriber);
|
|
59
|
-
|
|
34
|
+
const data = base.set('test', { name: 'test' });
|
|
60
35
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
61
36
|
mockSubscriber.mockClear();
|
|
62
37
|
data.removeDoc();
|
|
63
38
|
expect(mockSubscriber).toBeCalledWith([], { name: "test" }, enums_1.PondBaseActions.REMOVE_FROM_POND);
|
|
64
39
|
});
|
|
65
|
-
it('should fire the subscriber when a document is updated',
|
|
66
|
-
|
|
67
|
-
|
|
40
|
+
it('should fire the subscriber when a document is updated', () => {
|
|
41
|
+
const base = new pondBase_1.PondBase();
|
|
42
|
+
const mockSubscriber = jest.fn();
|
|
68
43
|
base.subscribe(mockSubscriber);
|
|
69
|
-
|
|
44
|
+
const data = base.set('test', { name: 'test' });
|
|
70
45
|
expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
|
|
71
46
|
mockSubscriber.mockClear();
|
|
72
47
|
data.updateDoc({ name: 'test2' });
|
|
73
48
|
expect(mockSubscriber).toBeCalledWith([{ name: "test2" }], { name: "test2" }, enums_1.PondBaseActions.UPDATE_IN_POND);
|
|
74
49
|
});
|
|
75
|
-
it('should add a document to the database',
|
|
50
|
+
it('should add a document to the database', () => {
|
|
76
51
|
var _a, _b;
|
|
77
|
-
|
|
78
|
-
|
|
52
|
+
const base = new pondBase_1.PondBase();
|
|
53
|
+
const data = base.addDoc({ name: 'test' });
|
|
79
54
|
expect(data.id).toBeDefined();
|
|
80
55
|
expect((_a = base.get(data.id)) === null || _a === void 0 ? void 0 : _a.doc).toEqual(data.doc);
|
|
81
56
|
expect((_b = base.get(data.id)) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
|
|
82
57
|
});
|
|
83
|
-
it('should left join with another pond',
|
|
84
|
-
|
|
85
|
-
|
|
58
|
+
it('should left join with another pond', () => {
|
|
59
|
+
const owners = new pondBase_1.PondBase();
|
|
60
|
+
const pets = new pondBase_1.PondBase();
|
|
86
61
|
owners.addDoc({ name: 'test', age: 10 });
|
|
87
62
|
owners.set('test2', { name: 'test2', age: 12 });
|
|
88
63
|
pets.addDoc({ name: 'test', owner: 'test' });
|
|
89
64
|
pets.addDoc({ name: 'test2', owner: 'test2' });
|
|
90
65
|
pets.set('test3', { name: 'test3', owner: 'test3' });
|
|
91
|
-
|
|
92
|
-
expect(
|
|
93
|
-
var doc = _a.doc;
|
|
94
|
-
return doc;
|
|
95
|
-
})).toEqual([
|
|
66
|
+
const joined = pets.leftJoin(owners, 'owner', 'name');
|
|
67
|
+
expect([...joined].map(({ doc }) => doc)).toEqual([
|
|
96
68
|
{ name: 'test', owner: { name: 'test', age: 10 } },
|
|
97
69
|
{ name: 'test2', owner: { name: 'test2', age: 12 } },
|
|
98
70
|
{ name: 'test3', owner: null }
|
|
99
71
|
]);
|
|
100
72
|
});
|
|
101
|
-
it('should be able to get the keys',
|
|
102
|
-
|
|
73
|
+
it('should be able to get the keys', () => {
|
|
74
|
+
const base = new pondBase_1.PondBase();
|
|
103
75
|
base.set('test', { name: 'test' });
|
|
104
76
|
base.set('test2', { name: 'test2' });
|
|
105
77
|
expect(base.keys).toEqual(['test', 'test2']);
|
|
106
78
|
});
|
|
107
|
-
it('should be able to get the values',
|
|
108
|
-
|
|
79
|
+
it('should be able to get the values', () => {
|
|
80
|
+
const base = new pondBase_1.PondBase();
|
|
109
81
|
base.addDoc({ name: 'test' });
|
|
110
82
|
base.set('test2', { name: 'test2' });
|
|
111
83
|
expect(base.values).toEqual([{ name: 'test' }, { name: 'test2' }]);
|
|
112
84
|
});
|
|
113
|
-
it('should be able to upsert a document',
|
|
85
|
+
it('should be able to upsert a document', () => {
|
|
114
86
|
var _a, _b;
|
|
115
|
-
|
|
87
|
+
const base = new pondBase_1.PondBase();
|
|
116
88
|
base.upsert('test', { name: 'test' });
|
|
117
89
|
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
|
|
118
90
|
base.upsert('test', { name: 'test2' });
|
|
119
91
|
expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test2' });
|
|
120
92
|
});
|
|
121
|
-
it('should be able to getOrCreate a document with a function',
|
|
93
|
+
it('should be able to getOrCreate a document with a function', () => {
|
|
122
94
|
var _a, _b;
|
|
123
|
-
|
|
124
|
-
base.getOrCreate('test',
|
|
95
|
+
const base = new pondBase_1.PondBase();
|
|
96
|
+
base.getOrCreate('test', () => ({ name: 'test' }));
|
|
125
97
|
expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
|
|
126
|
-
base.getOrCreate('test',
|
|
98
|
+
base.getOrCreate('test', () => ({ name: 'test2' }));
|
|
127
99
|
expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
|
|
128
100
|
});
|
|
129
101
|
});
|