@livequery/client 1.0.51 → 1.0.52

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.
@@ -1,6 +1,6 @@
1
1
  import { Subject, Observable } from 'rxjs';
2
- import { ErrorInfo, QueryOption, Transporter, UpdatedData } from '@livequery/types';
3
- export type CollectionOption<T = any> = {
2
+ import { ErrorInfo, LivequeryBaseEntity, QueryOption, Transporter, UpdatedData } from '@livequery/types';
3
+ export type CollectionOption<T extends LivequeryBaseEntity = LivequeryBaseEntity> = {
4
4
  transporter: Transporter;
5
5
  sync_delay?: number;
6
6
  filters?: Partial<QueryOption<T>>;
@@ -14,18 +14,16 @@ export type SmartQueryItem<T> = T & {
14
14
  __remove: Function;
15
15
  __update: (data: Partial<T>) => any;
16
16
  __trigger: (name: string, payload?: any) => any;
17
- __collection_ref: string;
17
+ __ref: string;
18
18
  };
19
- type CollectionStream<T> = {
19
+ type CollectionStream<T extends LivequeryBaseEntity = LivequeryBaseEntity> = {
20
20
  items: SmartQueryItem<T>[];
21
21
  error?: ErrorInfo;
22
22
  has_more: boolean;
23
23
  loading?: boolean;
24
24
  options: Partial<QueryOption<T>>;
25
25
  };
26
- export declare class CollectionObservable<T extends {
27
- id: string;
28
- }> extends Observable<CollectionStream<T>> {
26
+ export declare class CollectionObservable<T extends LivequeryBaseEntity = LivequeryBaseEntity> extends Observable<CollectionStream<T>> {
29
27
  #private;
30
28
  private ref;
31
29
  private collection_options;
@@ -38,14 +36,12 @@ export declare class CollectionObservable<T extends {
38
36
  reset(): void;
39
37
  fetch_more(): void;
40
38
  filter(filters: Partial<QueryOption<T>>): void;
41
- add(payload: T): Promise<{
39
+ add(payload: Partial<T>): Promise<{
42
40
  data: {
43
41
  item: T;
44
42
  };
45
43
  }>;
46
- update({ id: update_payload_id, ...payload }: Partial<T & {
47
- id: string;
48
- }>): Promise<any>;
44
+ update({ id: update_payload_id, ...payload }: Partial<T>): Promise<any>;
49
45
  remove(remove_document_id?: string): Promise<void>;
50
46
  trigger<T>(name: string, payload?: object, trigger_document_id?: string): Promise<T>;
51
47
  }
@@ -1,108 +1,85 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
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;
16
- };
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);
21
- };
22
- var __rest = (this && this.__rest) || function (s, e) {
23
- var t = {};
24
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
25
- t[p] = s[p];
26
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
27
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
28
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
29
- t[p[i]] = s[p[i]];
30
- }
31
- return t;
32
- };
33
- var _CollectionObservable_instances, _CollectionObservable_$state, _CollectionObservable_subscriptions, _CollectionObservable_state, _CollectionObservable_next_cursor, _CollectionObservable_IdMap, _CollectionObservable_refs, _CollectionObservable_ref_parser, _CollectionObservable_find_ref_by_id;
34
- Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.CollectionObservable = void 0;
36
- const rxjs_1 = require("rxjs");
37
- const get_sort_function_1 = require("./helpers/get_sort_function");
38
- const operators_1 = require("rxjs/operators");
39
- class CollectionObservable extends rxjs_1.Observable {
1
+ import { Subject, Observable, merge } from 'rxjs';
2
+ import { get_sort_function } from './helpers/get_sort_function.js';
3
+ import { bufferTime, filter, map } from 'rxjs/operators';
4
+ export class CollectionObservable extends Observable {
5
+ ref;
6
+ collection_options;
7
+ $changes = new Subject();
8
+ #$state = new Subject();
9
+ #subscriptions = new Set();
10
+ #state;
11
+ #next_cursor = {};
12
+ #IdMap = new Map();
13
+ #refs = [];
40
14
  constructor(ref, collection_options) {
41
15
  super(o => {
42
- __classPrivateFieldSet(this, _CollectionObservable_state, { items: [], options: collection_options.filters, has_more: false }, "f");
43
- const subscription = __classPrivateFieldGet(this, _CollectionObservable_$state, "f").subscribe(o);
16
+ this.#state = { items: [], options: collection_options.filters, has_more: false };
17
+ const subscription = this.#$state.subscribe(o);
44
18
  const auto_reload_interval = collection_options.reload_interval && setInterval(() => this.reload(), collection_options.reload_interval);
45
19
  return () => {
46
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
20
+ this.#subscriptions.forEach(s => s.unsubscribe());
47
21
  subscription.unsubscribe();
48
22
  clearInterval(auto_reload_interval);
49
23
  };
50
24
  });
51
25
  this.ref = ref;
52
26
  this.collection_options = collection_options;
53
- _CollectionObservable_instances.add(this);
54
- this.$changes = new rxjs_1.Subject();
55
- _CollectionObservable_$state.set(this, new rxjs_1.Subject());
56
- _CollectionObservable_subscriptions.set(this, new Set());
57
- _CollectionObservable_state.set(this, void 0);
58
- _CollectionObservable_next_cursor.set(this, {});
59
- _CollectionObservable_IdMap.set(this, new Map());
60
- _CollectionObservable_refs.set(this, []);
61
27
  if (ref.startsWith('/') || ref.endsWith('/'))
62
28
  throw 'INVAILD_REF_FORMAT';
63
- __classPrivateFieldSet(this, _CollectionObservable_refs, __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_ref_parser).call(this, ref), "f");
29
+ this.#refs = this.#ref_parser(ref);
30
+ }
31
+ #ref_parser(path) {
32
+ if (!path)
33
+ return [];
34
+ const refs_builder = (paths) => {
35
+ const [a, b, ...c] = paths;
36
+ if (!b)
37
+ return paths;
38
+ const r = a.map(aa => b.map(bb => `${aa}/${bb}`)).flat(2);
39
+ const d = [r, ...c];
40
+ return refs_builder(d);
41
+ };
42
+ return path.split(',').map(f => refs_builder(f.trim().split('/').map(l => l.split('|')))).flat(2);
64
43
  }
65
44
  set_realtime(realtime) {
66
45
  this.collection_options.realtime = realtime;
67
46
  }
68
47
  sync(stream, from_local = false) {
69
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
70
- const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
48
+ const realtime = this.collection_options.realtime ?? true;
71
49
  const actions = { update: false, reindex: false };
72
50
  for (const { data, error, ref } of stream) {
73
51
  // Error & paging
74
52
  if (error) {
75
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").error = error;
53
+ this.#state.error = error;
76
54
  actions.update = true;
77
55
  }
78
- if (((_b = data === null || data === void 0 ? void 0 : data.paging) === null || _b === void 0 ? void 0 : _b.n) == 0) {
79
- __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] = (_c = data === null || data === void 0 ? void 0 : data.paging) === null || _c === void 0 ? void 0 : _c.next_cursor;
80
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").has_more = Object.values(__classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")).some(v => v && v != '#');
81
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading = false;
56
+ if (data?.paging?.n == 0) {
57
+ this.#next_cursor[ref] = data?.paging?.next_cursor;
58
+ this.#state.has_more = Object.values(this.#next_cursor).some(v => v && v != '#');
59
+ this.#state.loading = false;
82
60
  actions.update = true;
83
61
  }
84
62
  // Sync
85
- for (const change of (data === null || data === void 0 ? void 0 : data.changes) || []) {
86
- if (!((_d = change === null || change === void 0 ? void 0 : change.data) === null || _d === void 0 ? void 0 : _d.id))
63
+ for (const change of data?.changes || []) {
64
+ if (!change?.data?.id)
87
65
  continue;
88
66
  const { data: payload, type } = change;
89
67
  this.$changes.next(change);
90
- const index = (_e = __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(payload.id)) !== null && _e !== void 0 ? _e : -1;
68
+ const index = this.#IdMap.get(payload.id) ?? -1;
91
69
  if (index == -1 && type == 'added') {
92
70
  if (
93
71
  // Is first value from HTTP query
94
- ((_f = data === null || data === void 0 ? void 0 : data.paging) === null || _f === void 0 ? void 0 : _f.n) == 0
72
+ data?.paging?.n == 0
95
73
  || (
96
74
  // Is realtime update that match filters
97
75
  (realtime || from_local) && Object
98
- .keys(__classPrivateFieldGet(this, _CollectionObservable_state, "f").options || {})
76
+ .keys(this.#state.options || {})
99
77
  .filter(key => !key.includes('_'))
100
78
  .every(key => {
101
- var _a;
102
79
  try {
103
80
  const [field, expression] = key.split(':');
104
81
  const a = payload[field];
105
- const b = (_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f").options) === null || _a === void 0 ? void 0 : _a[field];
82
+ const b = this.#state.options?.[field];
106
83
  if (!expression)
107
84
  return a == b;
108
85
  if (expression == 'ne')
@@ -116,11 +93,11 @@ class CollectionObservable extends rxjs_1.Observable {
116
93
  if (expression == 'gte')
117
94
  return a >= b;
118
95
  if (expression == 'in-array')
119
- return a === null || a === void 0 ? void 0 : a.includes(b);
96
+ return a?.includes(b);
120
97
  if (expression == 'contains')
121
- return a === null || a === void 0 ? void 0 : a.some(e => b === null || b === void 0 ? void 0 : b.includes(e));
98
+ return a?.some(e => b?.includes(e));
122
99
  if (expression == 'not-contains')
123
- return a === null || a === void 0 ? void 0 : a.every(e => !(b === null || b === void 0 ? void 0 : b.includes(e)));
100
+ return a?.every(e => !b?.includes(e));
124
101
  if (expression == 'between')
125
102
  return (b[0] <= a && a <= b[1]);
126
103
  if (expression == 'like')
@@ -129,7 +106,16 @@ class CollectionObservable extends rxjs_1.Observable {
129
106
  catch (e) { }
130
107
  return false;
131
108
  }))) {
132
- __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, input) => this.trigger(name, input, payload === null || payload === void 0 ? void 0 : payload.id), __update: (input) => this.update(Object.assign(Object.assign({}, input), { id: payload === null || payload === void 0 ? void 0 : payload.id })), __collection_ref: change.ref }));
109
+ this.#state.items.push({
110
+ ...payload,
111
+ __adding: false,
112
+ __updating: false,
113
+ __removing: false,
114
+ __remove: () => this.remove(payload?.id),
115
+ __trigger: (name, input) => this.trigger(name, input, payload?.id),
116
+ __update: (input) => this.update({ ...input, id: payload?.id }),
117
+ __ref: change.ref
118
+ });
133
119
  actions.reindex = true;
134
120
  actions.update = true;
135
121
  }
@@ -137,165 +123,155 @@ class CollectionObservable extends rxjs_1.Observable {
137
123
  if (index >= 0 && (realtime || from_local)) {
138
124
  if (type == 'added' || type == 'modified') {
139
125
  actions.update = true;
140
- if (Object.keys(payload).some(key => { var _a, _b; return ['created_at', (_b = (_a = this.collection_options) === null || _a === void 0 ? void 0 : _a.filters) === null || _b === void 0 ? void 0 : _b._order_by].includes(key); })) {
126
+ if (Object.keys(payload).some(key => ['created_at', this.collection_options?.filters?._order_by].includes(key))) {
141
127
  actions.reindex = true;
142
128
  }
143
- __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);
129
+ this.#state.items[index] = {
130
+ ...this.#state.items[index],
131
+ __adding: false,
132
+ __updating: false,
133
+ __removing: false,
134
+ ...payload
135
+ };
144
136
  }
145
137
  if (type == 'removed') {
146
138
  actions.reindex = true;
147
139
  actions.update = true;
148
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.splice(index, 1);
149
- for (const [document_id, i] of __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f")) {
150
- i == index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").delete(document_id);
151
- i > index && __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(document_id, i - 1);
140
+ this.#state.items.splice(index, 1);
141
+ for (const [document_id, i] of this.#IdMap) {
142
+ i == index && this.#IdMap.delete(document_id);
143
+ i > index && this.#IdMap.set(document_id, i - 1);
152
144
  }
153
145
  }
154
146
  }
155
147
  }
156
148
  }
157
149
  if (actions.reindex) {
158
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").items = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.sort((0, get_sort_function_1.get_sort_function)(__classPrivateFieldGet(this, _CollectionObservable_state, "f").items[0], ((_h = (_g = this.collection_options) === null || _g === void 0 ? void 0 : _g.filters) === null || _h === void 0 ? void 0 : _h._order_by) || 'created_at', (_k = (_j = this.collection_options) === null || _j === void 0 ? void 0 : _j.filters) === null || _k === void 0 ? void 0 : _k._sort));
159
- __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
160
- __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.map((item, index) => __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, index));
150
+ this.#state.items = this.#state.items.sort(get_sort_function(this.#state.items[0], this.collection_options?.filters?._order_by || 'created_at', this.collection_options?.filters?._sort));
151
+ this.#IdMap.clear();
152
+ this.#state.items.map((item, index) => this.#IdMap.set(item.id, index));
161
153
  }
162
- actions.update && __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
154
+ actions.update && this.#$state.next(this.#state);
163
155
  }
164
156
  fetch_data(options = {}, flush = false) {
165
- if (__classPrivateFieldGet(this, _CollectionObservable_refs, "f").length == 0)
157
+ if (this.#refs.length == 0)
166
158
  return;
167
159
  if (flush) {
168
- __classPrivateFieldSet(this, _CollectionObservable_next_cursor, {}, "f");
169
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.unsubscribe());
170
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").clear();
171
- __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
160
+ this.#next_cursor = {};
161
+ this.#subscriptions.forEach(s => s.unsubscribe());
162
+ this.#subscriptions.clear();
163
+ this.#IdMap.clear();
172
164
  }
173
- const has_more_data_refs = __classPrivateFieldGet(this, _CollectionObservable_refs, "f").filter(ref => __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] === undefined || (__classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] && __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] != '#'));
165
+ const has_more_data_refs = this.#refs.filter(ref => this.#next_cursor[ref] === undefined || (this.#next_cursor[ref] && this.#next_cursor[ref] != '#'));
174
166
  // Load more but no more data || loading
175
- if (!flush && (has_more_data_refs.length == 0 || __classPrivateFieldGet(this, _CollectionObservable_state, "f").loading))
167
+ if (!flush && (has_more_data_refs.length == 0 || this.#state.loading))
176
168
  return;
177
- __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");
178
- __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
169
+ this.#state = {
170
+ ...this.#state,
171
+ items: flush ? [] : this.#state.items,
172
+ error: null,
173
+ loading: true,
174
+ options
175
+ };
176
+ this.#$state.next(this.#state);
179
177
  const queries = has_more_data_refs.map(ref => (this
180
178
  .collection_options
181
179
  .transporter
182
- .query(ref, Object.assign(Object.assign({}, options), { _cursor: __classPrivateFieldGet(this, _CollectionObservable_next_cursor, "f")[ref] }))));
180
+ .query(ref, { ...options, _cursor: this.#next_cursor[ref] })));
183
181
  const reload = () => queries.map(q => q.reload());
184
- const $ = (0, rxjs_1.merge)(...queries.map((q, index) => q.pipe((0, operators_1.map)(data => (Object.assign(Object.assign({}, data), { ref: has_more_data_refs[index] })))))).pipe((0, operators_1.bufferTime)(500), (0, operators_1.filter)(list => list.length > 0), (0, operators_1.map)(data => this.sync(data)));
182
+ const $ = merge(...queries.map((q, index) => q.pipe(map(data => ({ ...data, ref: has_more_data_refs[index] }))))).pipe(bufferTime(500), filter(list => list.length > 0), map(data => this.sync(data)));
185
183
  const subscription = Object.assign($.subscribe(), { reload });
186
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").add(subscription);
184
+ this.#subscriptions.add(subscription);
187
185
  }
188
186
  reload() {
189
- __classPrivateFieldGet(this, _CollectionObservable_subscriptions, "f").forEach(s => s.reload());
187
+ this.#subscriptions.forEach(s => s.reload());
190
188
  }
191
189
  reset() {
192
190
  this.fetch_data({}, true);
193
191
  }
194
192
  fetch_more() {
195
- var _a;
196
- this.fetch_data((_a = __classPrivateFieldGet(this, _CollectionObservable_state, "f")) === null || _a === void 0 ? void 0 : _a.options);
193
+ this.fetch_data(this.#state?.options);
197
194
  }
198
195
  filter(filters) {
199
196
  this.fetch_data(filters, true);
200
197
  }
201
- add(payload) {
202
- return __awaiter(this, void 0, void 0, function* () {
203
- if (this.ref.includes('.'))
204
- throw 'INVAILD_COLLECTION_REF_FOR_ADDING';
205
- return yield this.collection_options.transporter.add(`${this.ref}`, payload);
206
- });
198
+ async add(payload) {
199
+ if (this.ref.includes('.'))
200
+ throw 'INVAILD_COLLECTION_REF_FOR_ADDING';
201
+ return await this.collection_options.transporter.add(`${this.ref}`, payload);
207
202
  }
208
- update(_a) {
209
- var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
210
- return __awaiter(this, void 0, void 0, function* () {
211
- const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, update_payload_id);
212
- // Trigger local update
203
+ #find_ref_by_id(id) {
204
+ if (!id)
205
+ return { ref: this.ref, collection_ref: this.ref };
206
+ const ref = this.#state.items[this.#IdMap.get(id)].__ref;
207
+ if (!ref)
208
+ throw 'COLLECTION_REF_NOT_FOUND';
209
+ const refs = ref.split('/');
210
+ const collection_ref = refs.slice(0, refs.length - (refs.length % 2 == 1 ? 0 : 1)).join('/');
211
+ return { ref, id, collection_ref };
212
+ }
213
+ async update({ id: update_payload_id, ...payload }) {
214
+ const { id, ref } = this.#find_ref_by_id(update_payload_id);
215
+ // Trigger local update
216
+ this.sync([{
217
+ ref,
218
+ data: {
219
+ changes: [{
220
+ data: { ...payload, id, __updating: true },
221
+ ref,
222
+ type: 'modified'
223
+ }]
224
+ }
225
+ }], true);
226
+ try {
227
+ return await this.collection_options.transporter.update(ref, payload);
228
+ }
229
+ catch (e) {
213
230
  this.sync([{
214
231
  ref,
215
232
  data: {
216
233
  changes: [{
217
- data: Object.assign(Object.assign({}, payload), { id, __updating: true }),
234
+ data: { id, __updating: false },
218
235
  ref,
219
236
  type: 'modified'
220
237
  }]
221
238
  }
222
239
  }], true);
223
- try {
224
- return yield this.collection_options.transporter.update(ref, payload);
225
- }
226
- catch (e) {
227
- this.sync([{
228
- ref,
229
- data: {
230
- changes: [{
231
- data: { id, __updating: false },
232
- ref,
233
- type: 'modified'
234
- }]
235
- }
236
- }], true);
237
- throw e;
238
- }
239
- });
240
+ throw e;
241
+ }
240
242
  }
241
- remove(remove_document_id) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- const { id, ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, remove_document_id);
243
+ async remove(remove_document_id) {
244
+ const { id, ref } = this.#find_ref_by_id(remove_document_id);
245
+ this.sync([{
246
+ ref,
247
+ data: {
248
+ changes: [{
249
+ data: { id, __removing: true },
250
+ ref,
251
+ type: 'modified'
252
+ }]
253
+ }
254
+ }], true);
255
+ // Trigger
256
+ try {
257
+ return await this.collection_options.transporter.remove(ref);
258
+ }
259
+ catch (e) {
244
260
  this.sync([{
245
261
  ref,
246
262
  data: {
247
263
  changes: [{
248
- data: { id, __removing: true },
264
+ data: { id, __removing: false },
249
265
  ref,
250
266
  type: 'modified'
251
267
  }]
252
268
  }
253
269
  }], true);
254
- // Trigger
255
- try {
256
- return yield this.collection_options.transporter.remove(ref);
257
- }
258
- catch (e) {
259
- this.sync([{
260
- ref,
261
- data: {
262
- changes: [{
263
- data: { id, __removing: false },
264
- ref,
265
- type: 'modified'
266
- }]
267
- }
268
- }], true);
269
- throw e;
270
- }
271
- });
270
+ throw e;
271
+ }
272
272
  }
273
- trigger(name, payload, trigger_document_id) {
274
- return __awaiter(this, void 0, void 0, function* () {
275
- const { ref } = __classPrivateFieldGet(this, _CollectionObservable_instances, "m", _CollectionObservable_find_ref_by_id).call(this, trigger_document_id);
276
- return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
277
- });
273
+ async trigger(name, payload, trigger_document_id) {
274
+ const { ref } = this.#find_ref_by_id(trigger_document_id);
275
+ return await this.collection_options.transporter.trigger(ref, name, {}, payload);
278
276
  }
279
277
  }
280
- exports.CollectionObservable = CollectionObservable;
281
- _CollectionObservable_$state = new WeakMap(), _CollectionObservable_subscriptions = new WeakMap(), _CollectionObservable_state = new WeakMap(), _CollectionObservable_next_cursor = new WeakMap(), _CollectionObservable_IdMap = new WeakMap(), _CollectionObservable_refs = new WeakMap(), _CollectionObservable_instances = new WeakSet(), _CollectionObservable_ref_parser = function _CollectionObservable_ref_parser(path) {
282
- if (!path)
283
- return [];
284
- const refs_builder = (paths) => {
285
- const [a, b, ...c] = paths;
286
- if (!b)
287
- return paths;
288
- const r = a.map(aa => b.map(bb => `${aa}/${bb}`)).flat(2);
289
- const d = [r, ...c];
290
- return refs_builder(d);
291
- };
292
- return path.split(',').map(f => refs_builder(f.trim().split('/').map(l => l.split('|')))).flat(2);
293
- }, _CollectionObservable_find_ref_by_id = function _CollectionObservable_find_ref_by_id(id) {
294
- if (!id)
295
- throw 'ID_NOT_FOUND';
296
- const collection_ref = __classPrivateFieldGet(this, _CollectionObservable_state, "f").items[__classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").get(id)].__collection_ref;
297
- if (!collection_ref)
298
- throw 'COLLECTION_REF_NOT_FOUND';
299
- const ref = `${collection_ref}${id ? `/${id}` : ''}`;
300
- return { ref, id, collection_ref };
301
- };
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.get_sort_function = void 0;
4
- function get_sort_function(data, key, order = 'desc') {
5
- const type = typeof (data === null || data === void 0 ? void 0 : data[key]);
1
+ export function get_sort_function(data, key, order = 'desc') {
2
+ const type = typeof data?.[key];
6
3
  if (type == 'bigint' || type == 'number') {
7
4
  if (order == 'asc')
8
5
  return (a, b) => a[key] - b[key] || ((a.created_at || 1) - (b.created_at || 0));
@@ -17,4 +14,3 @@ function get_sort_function(data, key, order = 'desc') {
17
14
  }
18
15
  return () => 1;
19
16
  }
20
- exports.get_sort_function = get_sort_function;
package/build/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './Collection';
1
+ export * from './Collection.js';
package/build/index.js CHANGED
@@ -1,17 +1 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Collection"), exports);
1
+ export * from './Collection.js';
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@livequery/client",
3
+ "type": "module",
3
4
  "repository": {
4
5
  "url": "https://github.com/livequery/client"
5
6
  },
6
- "version": "1.0.51",
7
+ "version": "1.0.52",
7
8
  "description": "",
8
9
  "main": "build/index.js",
9
10
  "types": "build/index.d.ts",
@@ -16,7 +17,7 @@
16
17
  "uuid": "^8.3.2"
17
18
  },
18
19
  "devDependencies": {
19
- "@livequery/types": "^1.0.31"
20
+ "@livequery/types": "^1.0.79"
20
21
  },
21
22
  "scripts": {
22
23
  "test": "echo \"Error: no test specified\" && exit 1",