@livequery/client 1.0.28 → 1.0.29
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 +3 -2
- package/build/Collection.js +14 -4
- package/package.json +1 -1
package/build/Collection.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Observable } from 'rxjs';
|
|
2
|
-
import { ErrorInfo, QueryOption, Transporter } from '@livequery/types';
|
|
1
|
+
import { Subject, Observable } from 'rxjs';
|
|
2
|
+
import { ErrorInfo, QueryOption, Transporter, UpdatedData } from '@livequery/types';
|
|
3
3
|
export type CollectionOption<T = any> = {
|
|
4
4
|
transporter: Transporter;
|
|
5
5
|
sync_delay?: number;
|
|
@@ -30,6 +30,7 @@ export declare class CollectionObservable<T extends {
|
|
|
30
30
|
private is_collection_ref;
|
|
31
31
|
private collection_ref;
|
|
32
32
|
private document_id;
|
|
33
|
+
readonly $changes: Subject<UpdatedData<T>>;
|
|
33
34
|
constructor(ref: string, collection_options: CollectionOption<T>);
|
|
34
35
|
private push_item;
|
|
35
36
|
private sync;
|
package/build/Collection.js
CHANGED
|
@@ -30,7 +30,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
30
30
|
}
|
|
31
31
|
return t;
|
|
32
32
|
};
|
|
33
|
-
var _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor;
|
|
33
|
+
var _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor, _CollectionObservable_IdMap;
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.CollectionObservable = void 0;
|
|
36
36
|
const rxjs_1 = require("rxjs");
|
|
@@ -53,6 +53,8 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
53
53
|
_CollectionObservable_subscriptions.set(this, new Set());
|
|
54
54
|
_CollectionObservable_state.set(this, void 0);
|
|
55
55
|
_CollectionObservable_next_cursor.set(this, null);
|
|
56
|
+
this.$changes = new rxjs_1.Subject();
|
|
57
|
+
_CollectionObservable_IdMap.set(this, new Map());
|
|
56
58
|
if (ref.startsWith('/') || ref.endsWith('/'))
|
|
57
59
|
throw 'INVAILD_REF_FORMAT';
|
|
58
60
|
const refs = ref.split('/');
|
|
@@ -63,6 +65,7 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
63
65
|
push_item(data) {
|
|
64
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 })) });
|
|
65
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);
|
|
66
69
|
}
|
|
67
70
|
sync(stream) {
|
|
68
71
|
var _a, _b, _c, _d;
|
|
@@ -75,8 +78,10 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
75
78
|
__classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
|
|
76
79
|
}
|
|
77
80
|
// Sync
|
|
78
|
-
for (const
|
|
79
|
-
const
|
|
81
|
+
for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
|
|
82
|
+
const { data: payload, type } = change;
|
|
83
|
+
this.$changes.next(change);
|
|
84
|
+
const index = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id) || -1;
|
|
80
85
|
if (index == -1 && type == 'added') {
|
|
81
86
|
if (
|
|
82
87
|
// Is first value from HTTP query
|
|
@@ -127,6 +132,10 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
127
132
|
}
|
|
128
133
|
if (type == 'removed') {
|
|
129
134
|
__classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
|
|
135
|
+
for (const [document_id, i] of __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f")) {
|
|
136
|
+
i == index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").delete(document_id);
|
|
137
|
+
i > index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(document_id, i - 1);
|
|
138
|
+
}
|
|
130
139
|
}
|
|
131
140
|
}
|
|
132
141
|
}
|
|
@@ -140,6 +149,7 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
140
149
|
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
|
|
141
150
|
__classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
|
|
142
151
|
}
|
|
152
|
+
flush && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
|
|
143
153
|
__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");
|
|
144
154
|
__classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
|
|
145
155
|
const query = this.collection_options.transporter.query(this.ref, options);
|
|
@@ -212,4 +222,4 @@ class CollectionObservable extends rxjs_1.Observable {
|
|
|
212
222
|
}
|
|
213
223
|
}
|
|
214
224
|
exports.CollectionObservable = CollectionObservable;
|
|
215
|
-
_CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap();
|
|
225
|
+
_CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap(), _CollectionObservable_IdMap = new WeakMap();
|