@koolbase/core 12.27.0 → 12.28.0

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.
@@ -46,6 +46,15 @@ export interface CollectionState {
46
46
  readonly searching: boolean;
47
47
  /** Why the server could not answer the latest search, or null. The records are not its answer. */
48
48
  readonly searchError: unknown;
49
+ /**
50
+ * True while the search is not run on the server, so the records are the
51
+ * list unsearched and an app filters them itself: the server refused this
52
+ * list's search (a field_search it will not take -- the collection's
53
+ * fields changed since the app was built; it is not asked again while
54
+ * this controller lives), or the search is wider than the server takes
55
+ * (a Choice matching more than 100 values, text over 100 characters).
56
+ */
57
+ readonly searchLoadedOnly: boolean;
49
58
  }
50
59
  export declare const initialCollectionState: CollectionState;
51
60
  /** One string per distinct query, whatever the order of its `where` keys. */
@@ -67,7 +76,13 @@ export declare class KoolbaseCollectionController {
67
76
  private liveTimer;
68
77
  private search;
69
78
  private searchTimer;
79
+ private searchRefused;
80
+ private answered;
81
+ private refreshToken;
70
82
  constructor(db: QueryReader, collection: string, query?: CollectionQuery);
83
+ /** The search the server runs: none when it is refused or too wide. */
84
+ private serverSearch;
85
+ private loadedOnly;
71
86
  /**
72
87
  * Search on the server (#160), once the typing pauses (searchDelayMs): page
73
88
  * one of the matches replaces what is shown, never merged with it, and
@@ -78,6 +93,11 @@ export declare class KoolbaseCollectionController {
78
93
  */
79
94
  setSearch(search?: FieldSearch | null): void;
80
95
  private runSearch;
96
+ /**
97
+ * After the server refused the search: it is not sent again while this
98
+ * controller lives, and the list is read unsearched for the app to filter.
99
+ */
100
+ private refuse;
81
101
  getState(): CollectionState;
82
102
  /** Called on every change. Returns the unsubscribe. */
83
103
  subscribe(listener: () => void): () => void;
@@ -27,6 +27,7 @@ exports.KoolbaseCollectionController = exports.initialCollectionState = void 0;
27
27
  exports.collectionQueryKey = collectionQueryKey;
28
28
  const live_source_js_1 = require("./live-source.js");
29
29
  const query_refresh_js_1 = require("./query-refresh.js");
30
+ const errors_js_1 = require("./errors.js");
30
31
  exports.initialCollectionState = Object.freeze({
31
32
  status: 'loading',
32
33
  records: Object.freeze([]),
@@ -37,6 +38,7 @@ exports.initialCollectionState = Object.freeze({
37
38
  hasMore: true,
38
39
  searching: false,
39
40
  searchError: null,
41
+ searchLoadedOnly: false,
40
42
  });
41
43
  /** A search with text to search, or null: none. */
42
44
  function normalizedSearch(search) {
@@ -48,6 +50,17 @@ function normalizedSearch(search) {
48
50
  choices: (search.choices ?? []).map((c) => ({ field: c.field, values: [...c.values], listed: [...c.listed] })),
49
51
  };
50
52
  }
53
+ /** The server's limits on a search (#160): past them it refuses it. */
54
+ const SEARCH_MAX_TEXT = 100;
55
+ const SEARCH_MAX_VALUES = 100;
56
+ /** A search the server takes, by its size: text and each Choice's matching values. */
57
+ function withinServerLimits(search) {
58
+ return Array.from(search.text).length <= SEARCH_MAX_TEXT && (search.choices ?? []).every((c) => c.values.length <= SEARCH_MAX_VALUES);
59
+ }
60
+ /** The server refusing a field_search (400): this search, as sent, will never be answered. */
61
+ function isSearchRefusal(error) {
62
+ return error instanceof errors_js_1.KoolbaseError && error.message.startsWith('field_search:');
63
+ }
51
64
  function sortedWhere(where) {
52
65
  const out = {};
53
66
  for (const key of Object.keys(where ?? {}).sort())
@@ -95,10 +108,22 @@ class KoolbaseCollectionController {
95
108
  this.unlive = null;
96
109
  this.liveTimer = null;
97
110
  this.searchTimer = null;
111
+ // The server refused this list's search: it is not asked again.
112
+ this.searchRefused = false;
113
+ // The latest refresh(): only it ends `refreshing`.
114
+ this.refreshToken = 0;
98
115
  this.db = db;
99
116
  this.collection = collection;
100
117
  this.spec = query;
101
118
  this.search = normalizedSearch(query.search);
119
+ this.state = Object.freeze({ ...this.state, searchLoadedOnly: this.loadedOnly() });
120
+ }
121
+ /** The search the server runs: none when it is refused or too wide. */
122
+ serverSearch() {
123
+ return this.search && !this.searchRefused && withinServerLimits(this.search) ? this.search : null;
124
+ }
125
+ loadedOnly() {
126
+ return this.search !== null && this.serverSearch() === null;
102
127
  }
103
128
  /**
104
129
  * Search on the server (#160), once the typing pauses (searchDelayMs): page
@@ -115,11 +140,22 @@ class KoolbaseCollectionController {
115
140
  if (JSON.stringify(next) === JSON.stringify(this.search))
116
141
  return;
117
142
  this.search = next;
143
+ // What is shown already answers the server's search (both not sent, say):
144
+ // nothing to read again, and nothing still waiting is wanted.
145
+ if (this.answered !== undefined && JSON.stringify(this.serverSearch()) === this.answered && this.state.status === 'loaded') {
146
+ if (this.searchTimer) {
147
+ clearTimeout(this.searchTimer);
148
+ this.searchTimer = null;
149
+ }
150
+ this.generation++;
151
+ this.set({ searching: false, searchError: null, loadingMore: false, searchLoadedOnly: this.loadedOnly() });
152
+ return;
153
+ }
118
154
  // Whatever is in flight was for the old search.
119
155
  this.generation++;
120
156
  if (this.searchTimer)
121
157
  clearTimeout(this.searchTimer);
122
- this.set({ searching: true, searchError: null, loadingMore: false });
158
+ this.set({ searching: true, searchError: null, loadingMore: false, searchLoadedOnly: this.loadedOnly() });
123
159
  this.searchTimer = setTimeout(() => {
124
160
  this.searchTimer = null;
125
161
  void this.runSearch();
@@ -132,22 +168,38 @@ class KoolbaseCollectionController {
132
168
  const gen = ++this.generation;
133
169
  if (this.state.loadingMore)
134
170
  this.set({ loadingMore: false });
171
+ const asking = JSON.stringify(this.serverSearch());
135
172
  try {
136
173
  const result = await this.db.query(this.collection, this.options(0, 'network-only'));
137
174
  if (this.isStale(gen))
138
175
  return;
176
+ this.answered = asking;
139
177
  this.showPageOne(result, false, false);
140
178
  this.set({ searching: false, searchError: null });
141
179
  }
142
180
  catch (error) {
143
181
  if (this.isStale(gen))
144
182
  return;
183
+ if (this.refuse(error))
184
+ return;
145
185
  // Nothing loaded yet to show under it: the list's own error.
146
186
  this.set(this.state.status === 'loaded'
147
187
  ? { searching: false, searchError: error }
148
188
  : { searching: false, searchError: error, status: 'error', error });
149
189
  }
150
190
  }
191
+ /**
192
+ * After the server refused the search: it is not sent again while this
193
+ * controller lives, and the list is read unsearched for the app to filter.
194
+ */
195
+ refuse(error) {
196
+ if (!isSearchRefusal(error) || this.serverSearch() === null)
197
+ return false;
198
+ this.searchRefused = true;
199
+ this.set({ searching: true, searchError: null, searchLoadedOnly: true });
200
+ void this.runSearch();
201
+ return true;
202
+ }
151
203
  getState() {
152
204
  return this.state;
153
205
  }
@@ -174,9 +226,11 @@ class KoolbaseCollectionController {
174
226
  async refresh() {
175
227
  if (this.disposed)
176
228
  return;
229
+ const token = ++this.refreshToken;
177
230
  this.set({ refreshing: true });
178
- const gen = await this.firstPage('network-only');
179
- if (!this.isStale(gen))
231
+ await this.firstPage('network-only');
232
+ // Ended by the latest refresh, whatever superseded its read.
233
+ if (token === this.refreshToken)
180
234
  this.set({ refreshing: false });
181
235
  }
182
236
  /**
@@ -218,7 +272,7 @@ class KoolbaseCollectionController {
218
272
  const offset = this.nextOffset;
219
273
  this.set({ loadingMore: true });
220
274
  try {
221
- const result = await this.db.query(this.collection, this.options(offset, this.search ? 'network-only' : 'default'));
275
+ const result = await this.db.query(this.collection, this.options(offset, this.serverSearch() ? 'network-only' : 'default'));
222
276
  if (this.isStale(gen))
223
277
  return;
224
278
  this.laterPages = [...this.laterPages, ...result.records];
@@ -227,8 +281,11 @@ class KoolbaseCollectionController {
227
281
  const total = result.records.length === 0 ? this.nextOffset : result.total;
228
282
  this.publish({}, total);
229
283
  }
230
- catch {
231
- // Keep what is shown; the caller can try again.
284
+ catch (error) {
285
+ // Refused (the schema changed since): the list unsearched. Otherwise
286
+ // keep what is shown; the caller can try again.
287
+ if (!this.isStale(gen))
288
+ this.refuse(error);
232
289
  }
233
290
  finally {
234
291
  if (!this.isStale(gen))
@@ -258,14 +315,18 @@ class KoolbaseCollectionController {
258
315
  // will not clear its own flag.
259
316
  if (this.state.loadingMore)
260
317
  this.set({ loadingMore: false });
318
+ const asking = JSON.stringify(this.serverSearch());
261
319
  try {
262
320
  const result = await this.db.query(this.collection,
263
321
  // A search's answer is the server's, never a cached one (#160).
264
- this.options(0, this.search ? 'network-only' : cache, (fresh) => {
265
- if (!this.isStale(gen))
322
+ this.options(0, this.serverSearch() ? 'network-only' : cache, (fresh) => {
323
+ if (!this.isStale(gen)) {
324
+ this.answered = asking;
266
325
  this.showPageOne(fresh, false, true);
326
+ }
267
327
  }));
268
328
  if (!this.isStale(gen)) {
329
+ this.answered = asking;
269
330
  this.showPageOne(result, result.isFromCache === true, false);
270
331
  // A search this fetch superseded is answered by it -- unless a newer
271
332
  // one is still waiting for the typing to pause.
@@ -275,6 +336,13 @@ class KoolbaseCollectionController {
275
336
  }
276
337
  }
277
338
  catch (error) {
339
+ // The server refused the search: read unsearched, at once -- the
340
+ // generation this returns is that read's, so refresh() ends with it.
341
+ if (!this.isStale(gen) && isSearchRefusal(error) && this.serverSearch() !== null) {
342
+ this.searchRefused = true;
343
+ this.set({ searchError: null, searchLoadedOnly: true });
344
+ return this.firstPage(cache);
345
+ }
278
346
  if (!this.isStale(gen) && this.state.searching && !this.searchTimer) {
279
347
  this.set({ searching: false, searchError: error });
280
348
  }
@@ -310,7 +378,7 @@ class KoolbaseCollectionController {
310
378
  offset,
311
379
  orderBy: this.spec.orderBy,
312
380
  orderDesc: this.spec.orderDesc,
313
- ...(this.search ? { fieldSearch: this.search } : {}),
381
+ ...(this.serverSearch() ? { fieldSearch: this.serverSearch() } : {}),
314
382
  cache,
315
383
  onRefresh,
316
384
  };
@@ -46,6 +46,15 @@ export interface CollectionState {
46
46
  readonly searching: boolean;
47
47
  /** Why the server could not answer the latest search, or null. The records are not its answer. */
48
48
  readonly searchError: unknown;
49
+ /**
50
+ * True while the search is not run on the server, so the records are the
51
+ * list unsearched and an app filters them itself: the server refused this
52
+ * list's search (a field_search it will not take -- the collection's
53
+ * fields changed since the app was built; it is not asked again while
54
+ * this controller lives), or the search is wider than the server takes
55
+ * (a Choice matching more than 100 values, text over 100 characters).
56
+ */
57
+ readonly searchLoadedOnly: boolean;
49
58
  }
50
59
  export declare const initialCollectionState: CollectionState;
51
60
  /** One string per distinct query, whatever the order of its `where` keys. */
@@ -67,7 +76,13 @@ export declare class KoolbaseCollectionController {
67
76
  private liveTimer;
68
77
  private search;
69
78
  private searchTimer;
79
+ private searchRefused;
80
+ private answered;
81
+ private refreshToken;
70
82
  constructor(db: QueryReader, collection: string, query?: CollectionQuery);
83
+ /** The search the server runs: none when it is refused or too wide. */
84
+ private serverSearch;
85
+ private loadedOnly;
71
86
  /**
72
87
  * Search on the server (#160), once the typing pauses (searchDelayMs): page
73
88
  * one of the matches replaces what is shown, never merged with it, and
@@ -78,6 +93,11 @@ export declare class KoolbaseCollectionController {
78
93
  */
79
94
  setSearch(search?: FieldSearch | null): void;
80
95
  private runSearch;
96
+ /**
97
+ * After the server refused the search: it is not sent again while this
98
+ * controller lives, and the list is read unsearched for the app to filter.
99
+ */
100
+ private refuse;
81
101
  getState(): CollectionState;
82
102
  /** Called on every change. Returns the unsubscribe. */
83
103
  subscribe(listener: () => void): () => void;
@@ -23,6 +23,7 @@
23
23
  // from page one. Duplicates are removed; a skipped row cannot be detected.
24
24
  import { liveSource } from './live-source.js';
25
25
  import { registerQueryRefresher } from './query-refresh.js';
26
+ import { KoolbaseError } from './errors.js';
26
27
  export const initialCollectionState = Object.freeze({
27
28
  status: 'loading',
28
29
  records: Object.freeze([]),
@@ -33,6 +34,7 @@ export const initialCollectionState = Object.freeze({
33
34
  hasMore: true,
34
35
  searching: false,
35
36
  searchError: null,
37
+ searchLoadedOnly: false,
36
38
  });
37
39
  /** A search with text to search, or null: none. */
38
40
  function normalizedSearch(search) {
@@ -44,6 +46,17 @@ function normalizedSearch(search) {
44
46
  choices: (search.choices ?? []).map((c) => ({ field: c.field, values: [...c.values], listed: [...c.listed] })),
45
47
  };
46
48
  }
49
+ /** The server's limits on a search (#160): past them it refuses it. */
50
+ const SEARCH_MAX_TEXT = 100;
51
+ const SEARCH_MAX_VALUES = 100;
52
+ /** A search the server takes, by its size: text and each Choice's matching values. */
53
+ function withinServerLimits(search) {
54
+ return Array.from(search.text).length <= SEARCH_MAX_TEXT && (search.choices ?? []).every((c) => c.values.length <= SEARCH_MAX_VALUES);
55
+ }
56
+ /** The server refusing a field_search (400): this search, as sent, will never be answered. */
57
+ function isSearchRefusal(error) {
58
+ return error instanceof KoolbaseError && error.message.startsWith('field_search:');
59
+ }
47
60
  function sortedWhere(where) {
48
61
  const out = {};
49
62
  for (const key of Object.keys(where ?? {}).sort())
@@ -91,10 +104,22 @@ export class KoolbaseCollectionController {
91
104
  this.unlive = null;
92
105
  this.liveTimer = null;
93
106
  this.searchTimer = null;
107
+ // The server refused this list's search: it is not asked again.
108
+ this.searchRefused = false;
109
+ // The latest refresh(): only it ends `refreshing`.
110
+ this.refreshToken = 0;
94
111
  this.db = db;
95
112
  this.collection = collection;
96
113
  this.spec = query;
97
114
  this.search = normalizedSearch(query.search);
115
+ this.state = Object.freeze({ ...this.state, searchLoadedOnly: this.loadedOnly() });
116
+ }
117
+ /** The search the server runs: none when it is refused or too wide. */
118
+ serverSearch() {
119
+ return this.search && !this.searchRefused && withinServerLimits(this.search) ? this.search : null;
120
+ }
121
+ loadedOnly() {
122
+ return this.search !== null && this.serverSearch() === null;
98
123
  }
99
124
  /**
100
125
  * Search on the server (#160), once the typing pauses (searchDelayMs): page
@@ -111,11 +136,22 @@ export class KoolbaseCollectionController {
111
136
  if (JSON.stringify(next) === JSON.stringify(this.search))
112
137
  return;
113
138
  this.search = next;
139
+ // What is shown already answers the server's search (both not sent, say):
140
+ // nothing to read again, and nothing still waiting is wanted.
141
+ if (this.answered !== undefined && JSON.stringify(this.serverSearch()) === this.answered && this.state.status === 'loaded') {
142
+ if (this.searchTimer) {
143
+ clearTimeout(this.searchTimer);
144
+ this.searchTimer = null;
145
+ }
146
+ this.generation++;
147
+ this.set({ searching: false, searchError: null, loadingMore: false, searchLoadedOnly: this.loadedOnly() });
148
+ return;
149
+ }
114
150
  // Whatever is in flight was for the old search.
115
151
  this.generation++;
116
152
  if (this.searchTimer)
117
153
  clearTimeout(this.searchTimer);
118
- this.set({ searching: true, searchError: null, loadingMore: false });
154
+ this.set({ searching: true, searchError: null, loadingMore: false, searchLoadedOnly: this.loadedOnly() });
119
155
  this.searchTimer = setTimeout(() => {
120
156
  this.searchTimer = null;
121
157
  void this.runSearch();
@@ -128,22 +164,38 @@ export class KoolbaseCollectionController {
128
164
  const gen = ++this.generation;
129
165
  if (this.state.loadingMore)
130
166
  this.set({ loadingMore: false });
167
+ const asking = JSON.stringify(this.serverSearch());
131
168
  try {
132
169
  const result = await this.db.query(this.collection, this.options(0, 'network-only'));
133
170
  if (this.isStale(gen))
134
171
  return;
172
+ this.answered = asking;
135
173
  this.showPageOne(result, false, false);
136
174
  this.set({ searching: false, searchError: null });
137
175
  }
138
176
  catch (error) {
139
177
  if (this.isStale(gen))
140
178
  return;
179
+ if (this.refuse(error))
180
+ return;
141
181
  // Nothing loaded yet to show under it: the list's own error.
142
182
  this.set(this.state.status === 'loaded'
143
183
  ? { searching: false, searchError: error }
144
184
  : { searching: false, searchError: error, status: 'error', error });
145
185
  }
146
186
  }
187
+ /**
188
+ * After the server refused the search: it is not sent again while this
189
+ * controller lives, and the list is read unsearched for the app to filter.
190
+ */
191
+ refuse(error) {
192
+ if (!isSearchRefusal(error) || this.serverSearch() === null)
193
+ return false;
194
+ this.searchRefused = true;
195
+ this.set({ searching: true, searchError: null, searchLoadedOnly: true });
196
+ void this.runSearch();
197
+ return true;
198
+ }
147
199
  getState() {
148
200
  return this.state;
149
201
  }
@@ -170,9 +222,11 @@ export class KoolbaseCollectionController {
170
222
  async refresh() {
171
223
  if (this.disposed)
172
224
  return;
225
+ const token = ++this.refreshToken;
173
226
  this.set({ refreshing: true });
174
- const gen = await this.firstPage('network-only');
175
- if (!this.isStale(gen))
227
+ await this.firstPage('network-only');
228
+ // Ended by the latest refresh, whatever superseded its read.
229
+ if (token === this.refreshToken)
176
230
  this.set({ refreshing: false });
177
231
  }
178
232
  /**
@@ -214,7 +268,7 @@ export class KoolbaseCollectionController {
214
268
  const offset = this.nextOffset;
215
269
  this.set({ loadingMore: true });
216
270
  try {
217
- const result = await this.db.query(this.collection, this.options(offset, this.search ? 'network-only' : 'default'));
271
+ const result = await this.db.query(this.collection, this.options(offset, this.serverSearch() ? 'network-only' : 'default'));
218
272
  if (this.isStale(gen))
219
273
  return;
220
274
  this.laterPages = [...this.laterPages, ...result.records];
@@ -223,8 +277,11 @@ export class KoolbaseCollectionController {
223
277
  const total = result.records.length === 0 ? this.nextOffset : result.total;
224
278
  this.publish({}, total);
225
279
  }
226
- catch {
227
- // Keep what is shown; the caller can try again.
280
+ catch (error) {
281
+ // Refused (the schema changed since): the list unsearched. Otherwise
282
+ // keep what is shown; the caller can try again.
283
+ if (!this.isStale(gen))
284
+ this.refuse(error);
228
285
  }
229
286
  finally {
230
287
  if (!this.isStale(gen))
@@ -254,14 +311,18 @@ export class KoolbaseCollectionController {
254
311
  // will not clear its own flag.
255
312
  if (this.state.loadingMore)
256
313
  this.set({ loadingMore: false });
314
+ const asking = JSON.stringify(this.serverSearch());
257
315
  try {
258
316
  const result = await this.db.query(this.collection,
259
317
  // A search's answer is the server's, never a cached one (#160).
260
- this.options(0, this.search ? 'network-only' : cache, (fresh) => {
261
- if (!this.isStale(gen))
318
+ this.options(0, this.serverSearch() ? 'network-only' : cache, (fresh) => {
319
+ if (!this.isStale(gen)) {
320
+ this.answered = asking;
262
321
  this.showPageOne(fresh, false, true);
322
+ }
263
323
  }));
264
324
  if (!this.isStale(gen)) {
325
+ this.answered = asking;
265
326
  this.showPageOne(result, result.isFromCache === true, false);
266
327
  // A search this fetch superseded is answered by it -- unless a newer
267
328
  // one is still waiting for the typing to pause.
@@ -271,6 +332,13 @@ export class KoolbaseCollectionController {
271
332
  }
272
333
  }
273
334
  catch (error) {
335
+ // The server refused the search: read unsearched, at once -- the
336
+ // generation this returns is that read's, so refresh() ends with it.
337
+ if (!this.isStale(gen) && isSearchRefusal(error) && this.serverSearch() !== null) {
338
+ this.searchRefused = true;
339
+ this.set({ searchError: null, searchLoadedOnly: true });
340
+ return this.firstPage(cache);
341
+ }
274
342
  if (!this.isStale(gen) && this.state.searching && !this.searchTimer) {
275
343
  this.set({ searching: false, searchError: error });
276
344
  }
@@ -306,7 +374,7 @@ export class KoolbaseCollectionController {
306
374
  offset,
307
375
  orderBy: this.spec.orderBy,
308
376
  orderDesc: this.spec.orderDesc,
309
- ...(this.search ? { fieldSearch: this.search } : {}),
377
+ ...(this.serverSearch() ? { fieldSearch: this.serverSearch() } : {}),
310
378
  cache,
311
379
  onRefresh,
312
380
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.27.0",
3
+ "version": "12.28.0",
4
4
  "description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",