@livequery/client 1.0.30 → 1.0.32
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
CHANGED
|
@@ -5,6 +5,7 @@ export type CollectionOption<T = any> = {
|
|
|
5
5
|
sync_delay?: number;
|
|
6
6
|
filters?: Partial<QueryOption<T>>;
|
|
7
7
|
reload_interval?: number;
|
|
8
|
+
realtime?: boolean;
|
|
8
9
|
};
|
|
9
10
|
type CollectionStream<T> = {
|
|
10
11
|
items: SmartQueryItem<T>[];
|
|
@@ -31,8 +32,8 @@ export declare class CollectionObservable<T extends {
|
|
|
31
32
|
private collection_ref;
|
|
32
33
|
private document_id;
|
|
33
34
|
readonly $changes: Subject<UpdatedData<T>>;
|
|
35
|
+
set_realtime(realtime: boolean): void;
|
|
34
36
|
constructor(ref: string, collection_options: CollectionOption<T>);
|
|
35
|
-
private push_item;
|
|
36
37
|
private sync;
|
|
37
38
|
private fetch_data;
|
|
38
39
|
reload(): void;
|
package/build/Collection.js
CHANGED
|
@@ -34,8 +34,12 @@ var _CollectionObservable_$state, _CollectionObservable_subscriptions, _Collecti
|
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.CollectionObservable = void 0;
|
|
36
36
|
const rxjs_1 = require("rxjs");
|
|
37
|
+
const get_sort_function_1 = require("./helpers/get_sort_function");
|
|
37
38
|
const operators_1 = require("rxjs/operators");
|
|
38
39
|
class CollectionObservable extends rxjs_1.Observable {
|
|
40
|
+
set_realtime(realtime) {
|
|
41
|
+
this.collection_options.realtime = realtime;
|
|
42
|
+
}
|
|
39
43
|
constructor(ref, collection_options) {
|
|
40
44
|
super(o => {
|
|
41
45
|
__classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
|
|
@@ -62,33 +66,30 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
62
66
|
this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
|
|
63
67
|
this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
|
|
64
68
|
}
|
|
65
|
-
push_item(data) {
|
|
66
|
-
const item = Object.assign(Object.assign({}, data), { __adding: false, __updating: false, __removing: false, __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(Object.assign({}, payload), { id: data === null || data === void 0 ? void 0 : data.id })) });
|
|
67
|
-
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(item);
|
|
68
|
-
__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.length - 1);
|
|
69
|
-
}
|
|
70
69
|
sync(stream) {
|
|
71
|
-
var _a, _b, _c, _d, _e;
|
|
70
|
+
var _a, _b, _c, _d, _e, _f;
|
|
71
|
+
const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
|
|
72
|
+
const actions = { update: false, reindex: false };
|
|
72
73
|
for (const { data, error } of stream) {
|
|
73
74
|
// Error & paging
|
|
74
75
|
error && (__classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error);
|
|
75
|
-
if (((
|
|
76
|
-
__classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = (
|
|
77
|
-
__classPrivateFieldSet(this, _CollectionObservable_next_cursor, (
|
|
76
|
+
if (((_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.n) == 0) {
|
|
77
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.has_more;
|
|
78
|
+
__classPrivateFieldSet(this, _CollectionObservable_next_cursor, (_d = data === null || data === void 0 ? void 0 : data.paging) === null || _d === void 0 ? void 0 : _d.next_cursor, "f");
|
|
78
79
|
__classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
|
|
79
80
|
}
|
|
80
81
|
// Sync
|
|
81
82
|
for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
|
|
82
83
|
const { data: payload, type } = change;
|
|
83
84
|
this.$changes.next(change);
|
|
84
|
-
const index = (
|
|
85
|
+
const index = (_e = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _e !== void 0 ? _e : -1;
|
|
85
86
|
if (index == -1 && type == 'added') {
|
|
86
87
|
if (
|
|
87
88
|
// Is first value from HTTP query
|
|
88
|
-
((
|
|
89
|
+
((_f = data === null || data === void 0 ? void 0 : data.paging) === null || _f === void 0 ? void 0 : _f.n) == 0
|
|
89
90
|
|| (
|
|
90
91
|
// Is realtime update that match filters
|
|
91
|
-
Object
|
|
92
|
+
realtime && Object
|
|
92
93
|
.keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
|
|
93
94
|
.filter(key => !key.includes('_'))
|
|
94
95
|
.every(key => {
|
|
@@ -123,14 +124,22 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
123
124
|
catch (e) { }
|
|
124
125
|
return false;
|
|
125
126
|
}))) {
|
|
126
|
-
this.
|
|
127
|
+
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.push(Object.assign(Object.assign({}, payload), { __adding: false, __updating: false, __removing: false, __remove: () => this.remove(payload === null || payload === void 0 ? void 0 : payload.id), __trigger: (name, payload) => this.trigger(name, payload, payload === null || payload === void 0 ? void 0 : payload.id), __update: (payload) => this.update(Object.assign(Object.assign({}, payload), { id: payload === null || payload === void 0 ? void 0 : payload.id })) }));
|
|
128
|
+
actions.reindex = true;
|
|
129
|
+
actions.update = true;
|
|
127
130
|
}
|
|
128
131
|
}
|
|
129
|
-
if (index >= 0) {
|
|
132
|
+
if (index >= 0 && realtime) {
|
|
130
133
|
if (type == 'added' || type == 'modified') {
|
|
134
|
+
actions.update = true;
|
|
135
|
+
if (Object.keys(payload).some(key => ['created_at', this.collection_options.filters._order_by].includes(key))) {
|
|
136
|
+
actions.reindex = true;
|
|
137
|
+
}
|
|
131
138
|
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[index]), payload), { __adding: false, __updating: false, __removing: false });
|
|
132
139
|
}
|
|
133
140
|
if (type == 'removed') {
|
|
141
|
+
actions.reindex = true;
|
|
142
|
+
actions.update = true;
|
|
134
143
|
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
|
|
135
144
|
for (const [document_id, i] of __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f")) {
|
|
136
145
|
i == index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").delete(document_id);
|
|
@@ -140,7 +149,9 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
140
149
|
}
|
|
141
150
|
}
|
|
142
151
|
}
|
|
143
|
-
__classPrivateFieldGet(this,
|
|
152
|
+
actions.reindex && __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort((0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], this.collection_options.filters._order_by || 'created_at', this.collection_options.filters._sort));
|
|
153
|
+
actions.reindex && (__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear(), __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.map((item, index) => __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, index)));
|
|
154
|
+
actions.update && __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
|
|
144
155
|
}
|
|
145
156
|
fetch_data(options = {}, flush = false) {
|
|
146
157
|
if (!this.ref)
|
|
@@ -5,15 +5,16 @@ function get_sort_function(data, key, order = 'desc') {
|
|
|
5
5
|
const type = typeof (data === null || data === void 0 ? void 0 : data[key]);
|
|
6
6
|
if (type == 'bigint' || type == 'number') {
|
|
7
7
|
if (order == 'asc')
|
|
8
|
-
return (a, b) => a[key] - b[key];
|
|
8
|
+
return (a, b) => a[key] - b[key] || ((a.created_at || 1) - (b.created_at || 0));
|
|
9
9
|
if (order == 'desc')
|
|
10
|
-
return (a, b) => b[key] - a[key];
|
|
10
|
+
return (a, b) => b[key] - a[key] || ((a.created_at || 1) - (b.created_at || 0));
|
|
11
11
|
}
|
|
12
12
|
if (type == 'string') {
|
|
13
13
|
if (order == 'asc')
|
|
14
|
-
return (a, b) => a[key].localeCompare(b[key]);
|
|
14
|
+
return (a, b) => a[key].localeCompare(b[key]) || ((a.created_at || 1) - (b.created_at || 0));
|
|
15
15
|
if (order == 'desc')
|
|
16
|
-
return (a, b) => b[key].localeCompare(a[key]);
|
|
16
|
+
return (a, b) => b[key].localeCompare(a[key]) || ((a.created_at || 1) - (b.created_at || 0));
|
|
17
17
|
}
|
|
18
|
+
return () => 1;
|
|
18
19
|
}
|
|
19
20
|
exports.get_sort_function = get_sort_function;
|