@livequery/client 1.0.23 → 1.0.26
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/build/Collection.d.ts +7 -5
- package/build/Collection.js +57 -68
- package/package.json +2 -2
package/build/Collection.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ export declare class CollectionObservable<T extends {
|
|
|
28
28
|
private ref;
|
|
29
29
|
private collection_options;
|
|
30
30
|
private is_collection_ref;
|
|
31
|
+
private collection_ref;
|
|
32
|
+
private document_id;
|
|
31
33
|
constructor(ref: string, collection_options: CollectionOption<T>);
|
|
32
34
|
private push_item;
|
|
33
35
|
private sync;
|
|
@@ -36,11 +38,11 @@ export declare class CollectionObservable<T extends {
|
|
|
36
38
|
reset(): void;
|
|
37
39
|
fetch_more(): void;
|
|
38
40
|
filter(filters: Partial<QueryOption<T>>): void;
|
|
39
|
-
add(payload: T
|
|
40
|
-
remove(
|
|
41
|
-
update({ id, ...payload }: {
|
|
41
|
+
add(payload: T): Promise<T>;
|
|
42
|
+
remove(remove_document_id?: string): Promise<void>;
|
|
43
|
+
update({ id: update_payload_id, ...payload }: Partial<T & {
|
|
42
44
|
id: string;
|
|
43
|
-
}
|
|
44
|
-
trigger(name: string,
|
|
45
|
+
}>): Promise<T>;
|
|
46
|
+
trigger<T>(name: string, payload?: object, trigger_document_id?: string): Promise<T>;
|
|
45
47
|
}
|
|
46
48
|
export {};
|
package/build/Collection.js
CHANGED
|
@@ -8,18 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver,
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return value;
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
17
16
|
};
|
|
18
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver,
|
|
19
|
-
if (!
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return privateMap.get(receiver);
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
23
21
|
};
|
|
24
22
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
25
23
|
var t = {};
|
|
@@ -32,52 +30,54 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
32
30
|
}
|
|
33
31
|
return t;
|
|
34
32
|
};
|
|
35
|
-
var
|
|
33
|
+
var _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor;
|
|
36
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
35
|
exports.CollectionObservable = void 0;
|
|
38
36
|
const rxjs_1 = require("rxjs");
|
|
39
37
|
const get_sort_function_1 = require("./helpers/get_sort_function");
|
|
40
38
|
const operators_1 = require("rxjs/operators");
|
|
41
|
-
const uuid_1 = require("uuid");
|
|
42
39
|
class CollectionObservable extends rxjs_1.Observable {
|
|
43
40
|
constructor(ref, collection_options) {
|
|
44
41
|
super(o => {
|
|
45
|
-
__classPrivateFieldSet(this,
|
|
46
|
-
const subscription = __classPrivateFieldGet(this,
|
|
42
|
+
__classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
|
|
43
|
+
const subscription = __classPrivateFieldGet(this, _CollectionObservable_$state, "f").subscribe(o);
|
|
47
44
|
const auto_reload_interval = collection_options.reload_interval && setInterval(() => this.reload(), collection_options.reload_interval);
|
|
48
45
|
return () => {
|
|
49
|
-
__classPrivateFieldGet(this,
|
|
46
|
+
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
|
|
50
47
|
subscription.unsubscribe();
|
|
51
48
|
clearInterval(auto_reload_interval);
|
|
52
49
|
};
|
|
53
50
|
});
|
|
54
51
|
this.ref = ref;
|
|
55
52
|
this.collection_options = collection_options;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
_CollectionObservable_$state.set(this, new rxjs_1.Subject());
|
|
54
|
+
_CollectionObservable_subscriptions.set(this, new Set());
|
|
55
|
+
_CollectionObservable_state.set(this, void 0);
|
|
56
|
+
_CollectionObservable_next_cursor.set(this, null);
|
|
57
|
+
if (ref.startsWith('/') || ref.endsWith('/'))
|
|
58
|
+
throw 'INVAILD_REF_FORMAT';
|
|
60
59
|
const refs = ref.split('/');
|
|
61
60
|
this.is_collection_ref = refs.length % 2 == 1;
|
|
61
|
+
this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
|
|
62
|
+
this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
|
|
62
63
|
}
|
|
63
64
|
push_item(data) {
|
|
64
|
-
const { id }
|
|
65
|
-
|
|
66
|
-
__classPrivateFieldGet(this, _state).items.push(item);
|
|
65
|
+
const item = Object.assign(Object.assign({ __adding: false, __updating: false, __removing: false }, data), { __remove: () => this.remove(data === null || data === void 0 ? void 0 : data.id), __trigger: (name, payload) => this.trigger(name, payload, data === null || data === void 0 ? void 0 : data.id), __update: (payload) => this.update(Object.assign({ id: data === null || data === void 0 ? void 0 : data.id }, payload)) });
|
|
66
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(item);
|
|
67
67
|
}
|
|
68
68
|
sync(stream) {
|
|
69
69
|
var _a, _b, _c, _d;
|
|
70
70
|
for (const { data, error } of stream) {
|
|
71
71
|
// Error & paging
|
|
72
|
-
error && (__classPrivateFieldGet(this,
|
|
72
|
+
error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
|
|
73
73
|
if (((_a = data === null || data === void 0 ? void 0 : data.paging) === null || _a === void 0 ? void 0 : _a.n) == 0) {
|
|
74
|
-
__classPrivateFieldGet(this,
|
|
75
|
-
__classPrivateFieldSet(this,
|
|
76
|
-
__classPrivateFieldGet(this,
|
|
74
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = (_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.has_more;
|
|
75
|
+
__classPrivateFieldSet(this, _CollectionObservable_next_cursor, (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor, "f");
|
|
76
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
|
|
77
77
|
}
|
|
78
78
|
// Sync
|
|
79
79
|
for (const { data: payload, type } of (data === null || data === void 0 ? void 0 : data.changes) || []) {
|
|
80
|
-
const index = __classPrivateFieldGet(this,
|
|
80
|
+
const index = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.findIndex(item => item.id == payload.id);
|
|
81
81
|
if (index == -1 && type == 'added') {
|
|
82
82
|
if (
|
|
83
83
|
// Is first value from HTTP query
|
|
@@ -85,14 +85,14 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
85
85
|
|| (
|
|
86
86
|
// Is realtime update that match filters
|
|
87
87
|
Object
|
|
88
|
-
.keys(__classPrivateFieldGet(this,
|
|
88
|
+
.keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
|
|
89
89
|
.filter(key => !key.includes('_'))
|
|
90
90
|
.every(key => {
|
|
91
91
|
var _a;
|
|
92
92
|
try {
|
|
93
93
|
const [field, expression] = key.split(':');
|
|
94
94
|
const a = payload[field];
|
|
95
|
-
const b = (_a = __classPrivateFieldGet(this,
|
|
95
|
+
const b = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f").options) === null || _a === void 0 ? void 0 : _a[field];
|
|
96
96
|
if (!expression)
|
|
97
97
|
return a == b;
|
|
98
98
|
if (expression == 'ne')
|
|
@@ -124,69 +124,56 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
124
124
|
}
|
|
125
125
|
if (index >= 0) {
|
|
126
126
|
if (type == 'added' || type == 'modified') {
|
|
127
|
-
__classPrivateFieldGet(this,
|
|
127
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index]), { __adding: false, __updating: false, __removing: false }), payload);
|
|
128
128
|
}
|
|
129
129
|
if (type == 'removed') {
|
|
130
|
-
__classPrivateFieldGet(this,
|
|
130
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
-
const sort_function = get_sort_function_1.get_sort_function(__classPrivateFieldGet(this,
|
|
136
|
-
__classPrivateFieldGet(this,
|
|
137
|
-
__classPrivateFieldGet(this,
|
|
135
|
+
const sort_function = (0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], __classPrivateFieldGet(this, _CollectionObservable_state, "f").options._order_by || 'created_at', __classPrivateFieldGet(this, _CollectionObservable_state, "f").options._sort || 'desc');
|
|
136
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort(sort_function);
|
|
137
|
+
__classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
|
|
138
138
|
}
|
|
139
139
|
fetch_data(options = {}, flush = false) {
|
|
140
|
-
console.log({ ref: this.ref, options });
|
|
141
140
|
if (!this.ref)
|
|
142
141
|
return;
|
|
143
142
|
if (flush) {
|
|
144
|
-
__classPrivateFieldGet(this,
|
|
145
|
-
__classPrivateFieldGet(this,
|
|
143
|
+
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
|
|
144
|
+
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
|
|
146
145
|
}
|
|
147
|
-
__classPrivateFieldSet(this,
|
|
148
|
-
__classPrivateFieldGet(this,
|
|
146
|
+
__classPrivateFieldSet(this, _CollectionObservable_state, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f")), { items: flush ? [] : __classPrivateFieldGet(this, _CollectionObservable_state, "f").items, error: null, loading: true, options }), "f");
|
|
147
|
+
__classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
|
|
149
148
|
const query = this.collection_options.transporter.query(this.ref, options);
|
|
150
149
|
const sub = Object.assign(query
|
|
151
|
-
.pipe(operators_1.bufferTime(this.collection_options.sync_delay || 500), operators_1.filter(stream => stream.length > 0))
|
|
150
|
+
.pipe((0, operators_1.bufferTime)(this.collection_options.sync_delay || 500), (0, operators_1.filter)(stream => stream.length > 0))
|
|
152
151
|
.subscribe(data => this.sync(data)), { reload: query.reload });
|
|
153
|
-
__classPrivateFieldGet(this,
|
|
152
|
+
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(sub);
|
|
154
153
|
}
|
|
155
154
|
reload() {
|
|
156
|
-
__classPrivateFieldGet(this,
|
|
155
|
+
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
|
|
157
156
|
}
|
|
158
157
|
reset() {
|
|
159
158
|
this.fetch_data({}, true);
|
|
160
159
|
}
|
|
161
160
|
fetch_more() {
|
|
162
161
|
var _a;
|
|
163
|
-
const options = (_a = __classPrivateFieldGet(this,
|
|
164
|
-
__classPrivateFieldGet(this,
|
|
162
|
+
const options = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f")) === null || _a === void 0 ? void 0 : _a.options;
|
|
163
|
+
__classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f") && (options._cursor = __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f"));
|
|
165
164
|
this.fetch_data(options);
|
|
166
165
|
}
|
|
167
166
|
filter(filters) {
|
|
168
167
|
this.fetch_data(filters, true);
|
|
169
168
|
}
|
|
170
|
-
add(payload
|
|
169
|
+
add(payload) {
|
|
171
170
|
return __awaiter(this, void 0, void 0, function* () {
|
|
172
|
-
|
|
173
|
-
const data = Object.assign(Object.assign({ id: uuid_1.v4() }, payload), { __adding: true });
|
|
174
|
-
this.sync([{
|
|
175
|
-
data: {
|
|
176
|
-
changes: [{
|
|
177
|
-
data: Object.assign(Object.assign({}, payload), { __adding: true }),
|
|
178
|
-
ref: this.ref,
|
|
179
|
-
type: 'added'
|
|
180
|
-
}]
|
|
181
|
-
}
|
|
182
|
-
}]);
|
|
183
|
-
return yield this.collection_options.transporter.add(`${this.ref}`, data);
|
|
184
|
-
}
|
|
185
|
-
return yield this.collection_options.transporter.add(`${this.ref}`, payload);
|
|
171
|
+
return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
|
|
186
172
|
});
|
|
187
173
|
}
|
|
188
|
-
remove(
|
|
174
|
+
remove(remove_document_id) {
|
|
189
175
|
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
const id = remove_document_id || this.document_id;
|
|
190
177
|
this.sync([{
|
|
191
178
|
data: {
|
|
192
179
|
changes: [{
|
|
@@ -197,13 +184,14 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
197
184
|
}
|
|
198
185
|
}]);
|
|
199
186
|
// Trigger
|
|
200
|
-
const ref = `${this.
|
|
201
|
-
|
|
187
|
+
const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
|
|
188
|
+
yield this.collection_options.transporter.remove(ref);
|
|
202
189
|
});
|
|
203
190
|
}
|
|
204
191
|
update(_a) {
|
|
205
|
-
var { id } = _a, payload = __rest(_a, ["id"]);
|
|
192
|
+
var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
|
|
206
193
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
const id = update_payload_id || this.document_id;
|
|
207
195
|
// Trigger local update
|
|
208
196
|
this.sync([{
|
|
209
197
|
data: {
|
|
@@ -214,16 +202,17 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
214
202
|
}]
|
|
215
203
|
}
|
|
216
204
|
}]);
|
|
217
|
-
const ref = `${this.
|
|
205
|
+
const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
|
|
218
206
|
return yield this.collection_options.transporter.update(ref, payload);
|
|
219
207
|
});
|
|
220
208
|
}
|
|
221
|
-
trigger(name,
|
|
209
|
+
trigger(name, payload, trigger_document_id) {
|
|
222
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
-
const
|
|
211
|
+
const id = trigger_document_id || this.document_id;
|
|
212
|
+
const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
|
|
224
213
|
return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
|
|
225
214
|
});
|
|
226
215
|
}
|
|
227
216
|
}
|
|
228
217
|
exports.CollectionObservable = CollectionObservable;
|
|
229
|
-
|
|
218
|
+
_CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap();
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"repository": {
|
|
4
4
|
"url": "https://github.com/livequery/client"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.26",
|
|
7
7
|
"description": "",
|
|
8
8
|
"main": "build/index.js",
|
|
9
9
|
"types": "build/index.d.ts",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"uuid": "^8.3.2"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@livequery/types": "^1.0.
|
|
18
|
+
"@livequery/types": "^1.0.31"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "echo \"Error: no test specified\" && exit 1",
|