@livequery/client 1.0.31 → 1.0.33

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.
@@ -34,6 +34,7 @@ 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 {
39
40
  set_realtime(realtime) {
@@ -66,7 +67,7 @@ class CollectionObservable extends rxjs_1.Observable {
66
67
  this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
67
68
  }
68
69
  sync(stream) {
69
- var _a, _b, _c, _d, _e, _f;
70
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
70
71
  const realtime = (_a = this.collection_options.realtime) !== null && _a !== void 0 ? _a : true;
71
72
  const actions = { update: false, reindex: false };
72
73
  for (const { data, error } of stream) {
@@ -131,7 +132,7 @@ class CollectionObservable extends rxjs_1.Observable {
131
132
  if (index >= 0 && realtime) {
132
133
  if (type == 'added' || type == 'modified') {
133
134
  actions.update = true;
134
- if (Object.keys(payload).some(key => ['created_at', this.collection_options.filters._order_by].includes(key))) {
135
+ 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); })) {
135
136
  actions.reindex = true;
136
137
  }
137
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 });
@@ -148,7 +149,11 @@ class CollectionObservable extends rxjs_1.Observable {
148
149
  }
149
150
  }
150
151
  }
151
- 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)));
152
+ if (actions.reindex) {
153
+ __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));
154
+ __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").clear();
155
+ __classPrivateFieldGet(this, _CollectionObservable_state, "f").items.map((item, index) => __classPrivateFieldGet(this, _CollectionObservable_IdMap, "f").set(item.id, index));
156
+ }
152
157
  actions.update && __classPrivateFieldGet(this, _CollectionObservable_$state, "f").next(__classPrivateFieldGet(this, _CollectionObservable_state, "f"));
153
158
  }
154
159
  fetch_data(options = {}, flush = false) {
@@ -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;
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.31",
6
+ "version": "1.0.33",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",