@livequery/client 1.0.26 → 1.0.27
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.js +44 -42
- package/package.json +1 -1
package/build/Collection.js
CHANGED
|
@@ -8,16 +8,18 @@ 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
|
-
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
|
|
12
|
+
if (!privateMap.has(receiver)) {
|
|
13
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
14
|
+
}
|
|
15
|
+
privateMap.set(receiver, value);
|
|
16
|
+
return value;
|
|
16
17
|
};
|
|
17
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver,
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
|
|
19
|
+
if (!privateMap.has(receiver)) {
|
|
20
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
21
|
+
}
|
|
22
|
+
return privateMap.get(receiver);
|
|
21
23
|
};
|
|
22
24
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
23
25
|
var t = {};
|
|
@@ -30,7 +32,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
30
32
|
}
|
|
31
33
|
return t;
|
|
32
34
|
};
|
|
33
|
-
var
|
|
35
|
+
var _$state, _subscriptions, _state, _next_cursor;
|
|
34
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
37
|
exports.CollectionObservable = void 0;
|
|
36
38
|
const rxjs_1 = require("rxjs");
|
|
@@ -39,21 +41,21 @@ const operators_1 = require("rxjs/operators");
|
|
|
39
41
|
class CollectionObservable extends rxjs_1.Observable {
|
|
40
42
|
constructor(ref, collection_options) {
|
|
41
43
|
super(o => {
|
|
42
|
-
__classPrivateFieldSet(this,
|
|
43
|
-
const subscription = __classPrivateFieldGet(this,
|
|
44
|
+
__classPrivateFieldSet(this, _state, { items: [], options: collection_options.filters, has_more: false });
|
|
45
|
+
const subscription = __classPrivateFieldGet(this, _$state).subscribe(o);
|
|
44
46
|
const auto_reload_interval = collection_options.reload_interval && setInterval(() => this.reload(), collection_options.reload_interval);
|
|
45
47
|
return () => {
|
|
46
|
-
__classPrivateFieldGet(this,
|
|
48
|
+
__classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
|
|
47
49
|
subscription.unsubscribe();
|
|
48
50
|
clearInterval(auto_reload_interval);
|
|
49
51
|
};
|
|
50
52
|
});
|
|
51
53
|
this.ref = ref;
|
|
52
54
|
this.collection_options = collection_options;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
_$state.set(this, new rxjs_1.Subject());
|
|
56
|
+
_subscriptions.set(this, new Set());
|
|
57
|
+
_state.set(this, void 0);
|
|
58
|
+
_next_cursor.set(this, null);
|
|
57
59
|
if (ref.startsWith('/') || ref.endsWith('/'))
|
|
58
60
|
throw 'INVAILD_REF_FORMAT';
|
|
59
61
|
const refs = ref.split('/');
|
|
@@ -62,22 +64,22 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
62
64
|
this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
|
|
63
65
|
}
|
|
64
66
|
push_item(data) {
|
|
65
|
-
const item = Object.assign(Object.assign({ __adding: false, __updating: false, __removing: false
|
|
66
|
-
__classPrivateFieldGet(this,
|
|
67
|
+
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 })) });
|
|
68
|
+
__classPrivateFieldGet(this, _state).items.push(item);
|
|
67
69
|
}
|
|
68
70
|
sync(stream) {
|
|
69
71
|
var _a, _b, _c, _d;
|
|
70
72
|
for (const { data, error } of stream) {
|
|
71
73
|
// Error & paging
|
|
72
|
-
error && (__classPrivateFieldGet(this,
|
|
74
|
+
error && (__classPrivateFieldGet(this, _state).error = error);
|
|
73
75
|
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,
|
|
76
|
+
__classPrivateFieldGet(this, _state).has_more = (_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.has_more;
|
|
77
|
+
__classPrivateFieldSet(this, _next_cursor, (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor);
|
|
78
|
+
__classPrivateFieldGet(this, _state).loading = false;
|
|
77
79
|
}
|
|
78
80
|
// Sync
|
|
79
81
|
for (const { data: payload, type } of (data === null || data === void 0 ? void 0 : data.changes) || []) {
|
|
80
|
-
const index = __classPrivateFieldGet(this,
|
|
82
|
+
const index = __classPrivateFieldGet(this, _state).items.findIndex(item => item.id == payload.id);
|
|
81
83
|
if (index == -1 && type == 'added') {
|
|
82
84
|
if (
|
|
83
85
|
// Is first value from HTTP query
|
|
@@ -85,14 +87,14 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
85
87
|
|| (
|
|
86
88
|
// Is realtime update that match filters
|
|
87
89
|
Object
|
|
88
|
-
.keys(__classPrivateFieldGet(this,
|
|
90
|
+
.keys(__classPrivateFieldGet(this, _state).options || {})
|
|
89
91
|
.filter(key => !key.includes('_'))
|
|
90
92
|
.every(key => {
|
|
91
93
|
var _a;
|
|
92
94
|
try {
|
|
93
95
|
const [field, expression] = key.split(':');
|
|
94
96
|
const a = payload[field];
|
|
95
|
-
const b = (_a = __classPrivateFieldGet(this,
|
|
97
|
+
const b = (_a = __classPrivateFieldGet(this, _state).options) === null || _a === void 0 ? void 0 : _a[field];
|
|
96
98
|
if (!expression)
|
|
97
99
|
return a == b;
|
|
98
100
|
if (expression == 'ne')
|
|
@@ -124,43 +126,43 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
124
126
|
}
|
|
125
127
|
if (index >= 0) {
|
|
126
128
|
if (type == 'added' || type == 'modified') {
|
|
127
|
-
__classPrivateFieldGet(this,
|
|
129
|
+
__classPrivateFieldGet(this, _state).items[index] = Object.assign(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state).items[index]), payload), { __adding: false, __updating: false, __removing: false });
|
|
128
130
|
}
|
|
129
131
|
if (type == 'removed') {
|
|
130
|
-
__classPrivateFieldGet(this,
|
|
132
|
+
__classPrivateFieldGet(this, _state).items.splice(index, 1);
|
|
131
133
|
}
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
}
|
|
135
|
-
const sort_function =
|
|
136
|
-
__classPrivateFieldGet(this,
|
|
137
|
-
__classPrivateFieldGet(this,
|
|
137
|
+
const sort_function = get_sort_function_1.get_sort_function(__classPrivateFieldGet(this, _state).items[0], __classPrivateFieldGet(this, _state).options._order_by || 'created_at', __classPrivateFieldGet(this, _state).options._sort || 'desc');
|
|
138
|
+
__classPrivateFieldGet(this, _state).items = __classPrivateFieldGet(this, _state).items.sort(sort_function);
|
|
139
|
+
__classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
|
|
138
140
|
}
|
|
139
141
|
fetch_data(options = {}, flush = false) {
|
|
140
142
|
if (!this.ref)
|
|
141
143
|
return;
|
|
142
144
|
if (flush) {
|
|
143
|
-
__classPrivateFieldGet(this,
|
|
144
|
-
__classPrivateFieldGet(this,
|
|
145
|
+
__classPrivateFieldGet(this, _subscriptions).forEach(s => s.unsubscribe());
|
|
146
|
+
__classPrivateFieldGet(this, _subscriptions).clear();
|
|
145
147
|
}
|
|
146
|
-
__classPrivateFieldSet(this,
|
|
147
|
-
__classPrivateFieldGet(this,
|
|
148
|
+
__classPrivateFieldSet(this, _state, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _state)), { items: flush ? [] : __classPrivateFieldGet(this, _state).items, error: null, loading: true, options }));
|
|
149
|
+
__classPrivateFieldGet(this, _$state).next(__classPrivateFieldGet(this, _state));
|
|
148
150
|
const query = this.collection_options.transporter.query(this.ref, options);
|
|
149
151
|
const sub = Object.assign(query
|
|
150
|
-
.pipe(
|
|
152
|
+
.pipe(operators_1.bufferTime(this.collection_options.sync_delay || 500), operators_1.filter(stream => stream.length > 0))
|
|
151
153
|
.subscribe(data => this.sync(data)), { reload: query.reload });
|
|
152
|
-
__classPrivateFieldGet(this,
|
|
154
|
+
__classPrivateFieldGet(this, _subscriptions).add(sub);
|
|
153
155
|
}
|
|
154
156
|
reload() {
|
|
155
|
-
__classPrivateFieldGet(this,
|
|
157
|
+
__classPrivateFieldGet(this, _subscriptions).forEach(s => s.reload());
|
|
156
158
|
}
|
|
157
159
|
reset() {
|
|
158
160
|
this.fetch_data({}, true);
|
|
159
161
|
}
|
|
160
162
|
fetch_more() {
|
|
161
163
|
var _a;
|
|
162
|
-
const options = (_a = __classPrivateFieldGet(this,
|
|
163
|
-
__classPrivateFieldGet(this,
|
|
164
|
+
const options = (_a = __classPrivateFieldGet(this, _state)) === null || _a === void 0 ? void 0 : _a.options;
|
|
165
|
+
__classPrivateFieldGet(this, _next_cursor) && (options._cursor = __classPrivateFieldGet(this, _next_cursor));
|
|
164
166
|
this.fetch_data(options);
|
|
165
167
|
}
|
|
166
168
|
filter(filters) {
|
|
@@ -196,7 +198,7 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
196
198
|
this.sync([{
|
|
197
199
|
data: {
|
|
198
200
|
changes: [{
|
|
199
|
-
data: Object.assign({ id, __updating: true }
|
|
201
|
+
data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
|
|
200
202
|
ref: this.ref,
|
|
201
203
|
type: 'modified'
|
|
202
204
|
}]
|
|
@@ -215,4 +217,4 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
215
217
|
}
|
|
216
218
|
}
|
|
217
219
|
exports.CollectionObservable = CollectionObservable;
|
|
218
|
-
|
|
220
|
+
_$state = new WeakMap(), _subscriptions = new WeakMap(), _state = new WeakMap(), _next_cursor = new WeakMap();
|