@base-framework/base 3.9.17 → 3.9.19

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.
@@ -0,0 +1,214 @@
1
+ /**
2
+ * ResourceCacheStore
3
+ *
4
+ * This is a small LRU cache for request results. Each key holds
5
+ * the last value, the time it was stored, its ttl, and the shared
6
+ * in-flight promise so identical requests can be deduped.
7
+ *
8
+ * @class
9
+ */
10
+ export class ResourceCacheStore {
11
+ /**
12
+ * This will create a resource cache.
13
+ *
14
+ * @constructor
15
+ * @param {object} [settings]
16
+ * @param {number} [settings.max=100] The max number of entries.
17
+ * @param {number} [settings.ttl=30000] The default ttl in ms.
18
+ */
19
+ constructor({ max, ttl }?: {
20
+ max?: number;
21
+ ttl?: number;
22
+ });
23
+ /**
24
+ * @type {Map<string, CacheEntry>} entries
25
+ */
26
+ entries: Map<string, CacheEntry>;
27
+ /**
28
+ * @type {number} max
29
+ */
30
+ max: number;
31
+ /**
32
+ * @type {number} defaultTtl
33
+ */
34
+ defaultTtl: number;
35
+ /**
36
+ * This will get the number of entries.
37
+ *
38
+ * @returns {number}
39
+ */
40
+ get size(): number;
41
+ /**
42
+ * This will set the max number of entries and evict the
43
+ * oldest entries if the cache is over the cap.
44
+ *
45
+ * @param {number} max
46
+ * @returns {void}
47
+ */
48
+ setMax(max: number): void;
49
+ /**
50
+ * This will get an entry and mark it as recently used.
51
+ *
52
+ * @param {string} key
53
+ * @returns {CacheEntry|null}
54
+ */
55
+ getEntry(key: string): CacheEntry | null;
56
+ /**
57
+ * This will create an empty entry.
58
+ *
59
+ * @protected
60
+ * @param {string} key
61
+ * @returns {CacheEntry}
62
+ */
63
+ protected createEntry(key: string): CacheEntry;
64
+ /**
65
+ * This will remove the least recently used entries
66
+ * until the cache is under the cap.
67
+ *
68
+ * @protected
69
+ * @returns {void}
70
+ */
71
+ protected evict(): void;
72
+ /**
73
+ * This will check if an entry has a fresh value.
74
+ *
75
+ * @param {CacheEntry|null} entry
76
+ * @returns {boolean}
77
+ */
78
+ isFresh(entry: CacheEntry | null): boolean;
79
+ /**
80
+ * This will check if the key has a fresh value.
81
+ *
82
+ * @param {string} key
83
+ * @returns {boolean}
84
+ */
85
+ has(key: string): boolean;
86
+ /**
87
+ * This will get a fresh value from the cache.
88
+ *
89
+ * @param {string} key
90
+ * @returns {*} The value or undefined if missing or expired.
91
+ */
92
+ get(key: string): any;
93
+ /**
94
+ * This will set a value in the cache.
95
+ *
96
+ * @param {string} key
97
+ * @param {*} value
98
+ * @param {number} [ttl]
99
+ * @param {*} [meta]
100
+ * @returns {void}
101
+ */
102
+ set(key: string, value: any, ttl?: number, meta?: any): void;
103
+ /**
104
+ * This will remove entries by key, key prefix, regular
105
+ * expression, predicate, or an array of any of these.
106
+ *
107
+ * @param {CacheMatch} match
108
+ * @returns {number} The number of removed entries.
109
+ */
110
+ invalidate(match: CacheMatch): number;
111
+ /**
112
+ * This will remove all entries.
113
+ *
114
+ * @returns {void}
115
+ */
116
+ clear(): void;
117
+ /**
118
+ * This will start a request for an entry and share its
119
+ * promise with every caller that asks for the same key.
120
+ *
121
+ * @protected
122
+ * @param {string} key
123
+ * @param {CacheEntry} entry
124
+ * @param {function} start
125
+ * @param {number} ttl
126
+ * @returns {*} The value returned by start.
127
+ */
128
+ protected fetch(key: string, entry: CacheEntry, start: Function, ttl: number): any;
129
+ /**
130
+ * This will resolve a key from the cache or by starting a
131
+ * request. The deliver callback is called asynchronously with
132
+ * the result. With swr it can be called a second time when a
133
+ * background refresh changes the value.
134
+ *
135
+ * @param {string} key
136
+ * @param {function(function(boolean, *, *):void):*} start Starts the request and calls settle(ok, value, meta).
137
+ * @param {CacheOptions} options
138
+ * @param {function(*, *, boolean):void} deliver Called with (value, meta, ok).
139
+ * @returns {*} The value returned by start, or null if no new request was started for this caller.
140
+ */
141
+ resolve(key: string, start: (arg0: (arg0: boolean, arg1: any, arg2: any) => void) => any, options: CacheOptions, deliver: (arg0: any, arg1: any, arg2: boolean) => void): any;
142
+ /**
143
+ * This will load a value using a promise fetcher. Identical keys
144
+ * share one fetch, fresh values skip the fetcher, and stale values
145
+ * with swr resolve right away and refresh in the background.
146
+ *
147
+ * @param {string} key
148
+ * @param {function(): (Promise<*>|*)} fetcher
149
+ * @param {CacheOptions} [options]
150
+ * @returns {Promise<*>}
151
+ */
152
+ load(key: string, fetcher: () => (Promise<any> | any), options?: CacheOptions): Promise<any>;
153
+ }
154
+ /**
155
+ * The shared resource cache used by model requests.
156
+ *
157
+ * @type {ResourceCacheStore}
158
+ */
159
+ export const ResourceCache: ResourceCacheStore;
160
+ export type CacheEntry = {
161
+ /**
162
+ * The cached value.
163
+ */
164
+ value: any;
165
+ /**
166
+ * Extra data stored with the value (e.g. the xhr).
167
+ */
168
+ meta: any;
169
+ /**
170
+ * The time the value was stored.
171
+ */
172
+ time: number;
173
+ /**
174
+ * How long the value is fresh in ms.
175
+ */
176
+ ttl: number;
177
+ /**
178
+ * Whether a value has been stored.
179
+ */
180
+ hasValue: boolean;
181
+ /**
182
+ * The shared in-flight request.
183
+ */
184
+ promise: Promise<CacheResult> | null;
185
+ };
186
+ export type CacheResult = {
187
+ /**
188
+ * Whether the request succeeded.
189
+ */
190
+ ok: boolean;
191
+ /**
192
+ * The response value.
193
+ */
194
+ value: any;
195
+ /**
196
+ * Extra data returned by the request.
197
+ */
198
+ meta: any;
199
+ };
200
+ export type CacheOptions = {
201
+ /**
202
+ * How long a value stays fresh in ms.
203
+ */
204
+ ttl?: number;
205
+ /**
206
+ * Serve stale values while revalidating.
207
+ */
208
+ swr?: boolean;
209
+ /**
210
+ * Called when a swr refresh changes the value.
211
+ */
212
+ onUpdate?: Function;
213
+ };
214
+ export type CacheMatch = string | RegExp | ((arg0: string, arg1: CacheEntry) => boolean | Array<string | RegExp | Function>);
@@ -0,0 +1,6 @@
1
+ /**
2
+ * MountQueue
3
+ *
4
+ * @type {object}
5
+ */
6
+ export const MountQueue: object;
@@ -111,10 +111,16 @@ export class Unit {
111
111
  /**
112
112
  * This will remove a persisted child by its token.
113
113
  *
114
+ * When the child is passed, the slot is only removed if it still
115
+ * holds that child. A newer child can take over the same token
116
+ * (e.g. a keyed rebuild), and removing the old child must not
117
+ * drop the newer one.
118
+ *
114
119
  * @param {string} token
120
+ * @param {object} [child]
115
121
  * @returns {void}
116
122
  */
117
- removePersistedChild(token: string): void;
123
+ removePersistedChild(token: string, child?: object): void;
118
124
  /**
119
125
  * This will setup the component props.
120
126
  *
@@ -313,6 +319,36 @@ export class Unit {
313
319
  * @returns {void}
314
320
  */
315
321
  protected afterSetup(): void;
322
+ /**
323
+ * Optional lifecycle method: define `afterMount()` on a unit,
324
+ * component, Jot or Pod to run code once the component panel is
325
+ * connected to the document (e.g. to measure layout, focus an
326
+ * input or start an observer).
327
+ *
328
+ * `afterSetup()` runs while the layout is still inside the
329
+ * detached build fragment, so measurements read 0 there.
330
+ * `afterMount()` is called exactly once per render of the
331
+ * instance, in a microtask after the outermost build returns and
332
+ * the panel is connected. Content built detached and inserted
333
+ * later is mounted by the next flush after it is connected. A
334
+ * unit destroyed before it is connected is never called.
335
+ *
336
+ * It is intentionally not defined on the prototype so units
337
+ * that do not use it pay nothing.
338
+ *
339
+ * @name Unit#afterMount
340
+ * @function
341
+ * @returns {void}
342
+ *
343
+ * @example
344
+ * class Chart extends Component
345
+ * {
346
+ * afterMount()
347
+ * {
348
+ * this.width = this.panel.offsetWidth;
349
+ * }
350
+ * }
351
+ */
316
352
  /**
317
353
  * This will setup and render the component.
318
354
  *
@@ -1,3 +1,14 @@
1
+ /**
2
+ * @typedef {object} CacheRequestOptions
3
+ * @property {string} [key] The cache key. Defaults to method + url + params.
4
+ * @property {number} [ttl] How long the response stays fresh in ms.
5
+ * @property {boolean} [swr] Return a stale response and refresh it in the background.
6
+ */
7
+ /**
8
+ * @typedef {object} RequestOptions
9
+ * @property {CacheRequestOptions|boolean} [cache] Opt-in response caching for GET requests.
10
+ * @property {import('../../../ajax/resource-cache.js').CacheMatch} [invalidate] Cache keys or prefixes to clear after a successful request.
11
+ */
1
12
  /**
2
13
  * ModelService
3
14
  *
@@ -36,6 +47,10 @@ export class ModelService {
36
47
  * @type {function|null} validateCallBack
37
48
  */
38
49
  validateCallBack: Function | null;
50
+ /**
51
+ * @type {RequestOptions|null} requestOptions
52
+ */
53
+ requestOptions: RequestOptions | null;
39
54
  /**
40
55
  * This will initialize the model service.
41
56
  *
@@ -174,6 +189,30 @@ export class ModelService {
174
189
  * @returns {string}
175
190
  */
176
191
  protected getUrl(url: string): string;
192
+ /**
193
+ * This will return a copy of the service that adds the request
194
+ * options to every request it makes. The original service is
195
+ * not changed.
196
+ *
197
+ * @example
198
+ * model.xhr.withOptions({ cache: { ttl: 30000, swr: true } }).all('', callBack);
199
+ * model.xhr.withOptions({ invalidate: '/api/users' }).add();
200
+ *
201
+ * @param {RequestOptions} options
202
+ * @returns {this}
203
+ */
204
+ withOptions(options: RequestOptions): this;
205
+ /**
206
+ * This will return a copy of the service that caches its
207
+ * read requests.
208
+ *
209
+ * @example
210
+ * model.xhr.withCache({ ttl: 30000 }).get('', callBack);
211
+ *
212
+ * @param {CacheRequestOptions|boolean} [cache]
213
+ * @returns {this}
214
+ */
215
+ withCache(cache?: CacheRequestOptions | boolean): this;
177
216
  /**
178
217
  * This will make an ajax request.
179
218
  *
@@ -183,9 +222,37 @@ export class ModelService {
183
222
  * @param {(string|object)} params
184
223
  * @param {function} [callBack]
185
224
  * @param {function} [requestCallBack]
225
+ * @param {RequestOptions} [options]
226
+ * @returns {XMLHttpRequest?} The xhr, or null if the request was served
227
+ * by the resource cache without a new network call.
228
+ */
229
+ protected setupRequest(url: string, method: string, params: (string | object), callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest | null;
230
+ /**
231
+ * This will check if a request method can read from the cache.
232
+ * Mutating requests never read from the cache.
233
+ *
234
+ * @protected
235
+ * @param {string} method
236
+ * @returns {boolean}
237
+ */
238
+ protected isCacheableMethod(method: string): boolean;
239
+ /**
240
+ * This will get the default cache key for a request.
241
+ *
242
+ * @protected
243
+ * @param {object} settings
244
+ * @returns {string}
245
+ */
246
+ protected getCacheKey(settings: object): string;
247
+ /**
248
+ * This will make a request through the resource cache.
249
+ *
250
+ * @protected
251
+ * @param {object} settings
252
+ * @param {CacheRequestOptions} cache
186
253
  * @returns {XMLHttpRequest?}
187
254
  */
188
- protected setupRequest(url: string, method: string, params: (string | object), callBack?: Function, requestCallBack?: Function): XMLHttpRequest | null;
255
+ protected cachedRequest(settings: object, cache: CacheRequestOptions): XMLHttpRequest | null;
189
256
  /**
190
257
  * Set up an EventSource for real-time activity updates with auto-reconnection.
191
258
  *
@@ -220,9 +287,10 @@ export class ModelService {
220
287
  * @param {string} instanceParams
221
288
  * @param {function} callBack
222
289
  * @param {function} [requestCallBack]
290
+ * @param {RequestOptions} [options]
223
291
  * @returns {XMLHttpRequest}
224
292
  */
225
- protected request(params: (string | object), instanceParams: string, callBack: Function, requestCallBack?: Function): XMLHttpRequest;
293
+ protected request(params: (string | object), instanceParams: string, callBack: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
226
294
  /**
227
295
  * This will make a GET request.
228
296
  *
@@ -232,9 +300,10 @@ export class ModelService {
232
300
  * @param {string} [instanceParams]
233
301
  * @param {function} [callBack]
234
302
  * @param {function} [requestCallBack]
303
+ * @param {RequestOptions} [options]
235
304
  * @returns {XMLHttpRequest}
236
305
  */
237
- protected _get(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
306
+ protected _get(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
238
307
  /**
239
308
  * This will make a POST request.
240
309
  *
@@ -244,9 +313,10 @@ export class ModelService {
244
313
  * @param {string} [instanceParams]
245
314
  * @param {function} [callBack]
246
315
  * @param {function} [requestCallBack]
316
+ * @param {RequestOptions} [options]
247
317
  * @returns {XMLHttpRequest}
248
318
  */
249
- protected _post(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
319
+ protected _post(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
250
320
  /**
251
321
  * This will make a PUT request.
252
322
  *
@@ -256,9 +326,10 @@ export class ModelService {
256
326
  * @param {string} [instanceParams]
257
327
  * @param {function} [callBack]
258
328
  * @param {function} [requestCallBack]
329
+ * @param {RequestOptions} [options]
259
330
  * @returns {XMLHttpRequest}
260
331
  */
261
- protected _put(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
332
+ protected _put(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
262
333
  /**
263
334
  * This will make a PATCH request.
264
335
  *
@@ -268,9 +339,10 @@ export class ModelService {
268
339
  * @param {string} [instanceParams]
269
340
  * @param {function} [callBack]
270
341
  * @param {function} [requestCallBack]
342
+ * @param {RequestOptions} [options]
271
343
  * @returns {XMLHttpRequest}
272
344
  */
273
- protected _patch(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
345
+ protected _patch(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
274
346
  /**
275
347
  * This will make a DELETE request.
276
348
  *
@@ -280,9 +352,10 @@ export class ModelService {
280
352
  * @param {string} [instanceParams]
281
353
  * @param {function} [callBack]
282
354
  * @param {function} [requestCallBack]
355
+ * @param {RequestOptions} [options]
283
356
  * @returns {XMLHttpRequest}
284
357
  */
285
- protected _delete(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
358
+ protected _delete(url: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
286
359
  /**
287
360
  * This will make an ajax request.
288
361
  *
@@ -293,9 +366,10 @@ export class ModelService {
293
366
  * @param {string} [instanceParams]
294
367
  * @param {function} [callBack]
295
368
  * @param {function} [requestCallBack]
369
+ * @param {RequestOptions} [options]
296
370
  * @returns {XMLHttpRequest}
297
371
  */
298
- protected _request(url: string, method: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function): XMLHttpRequest;
372
+ protected _request(url: string, method: string, params: (string | object), instanceParams?: string, callBack?: Function, requestCallBack?: Function, options?: RequestOptions): XMLHttpRequest;
299
373
  /**
300
374
  * This will get the response.
301
375
  *
@@ -307,3 +381,27 @@ export class ModelService {
307
381
  */
308
382
  protected getResponse(response: object, callBack?: Function, xhr?: object): void;
309
383
  }
384
+ export type CacheRequestOptions = {
385
+ /**
386
+ * The cache key. Defaults to method + url + params.
387
+ */
388
+ key?: string;
389
+ /**
390
+ * How long the response stays fresh in ms.
391
+ */
392
+ ttl?: number;
393
+ /**
394
+ * Return a stale response and refresh it in the background.
395
+ */
396
+ swr?: boolean;
397
+ };
398
+ export type RequestOptions = {
399
+ /**
400
+ * Opt-in response caching for GET requests.
401
+ */
402
+ cache?: CacheRequestOptions | boolean;
403
+ /**
404
+ * Cache keys or prefixes to clear after a successful request.
405
+ */
406
+ invalidate?: import("../../../ajax/resource-cache.js").CacheMatch;
407
+ };
@@ -23,4 +23,15 @@ export class LayoutManager {
23
23
  * @return {object|null}
24
24
  */
25
25
  static render(layout: object, element: object, parent: object): object | null;
26
+ /**
27
+ * This will render the layout to the DOM and return every
28
+ * root node so layouts with more than one root (arrays) can
29
+ * be fully removed.
30
+ *
31
+ * @param {object} layout
32
+ * @param {object} element
33
+ * @param {object} parent
34
+ * @return {Array<object>}
35
+ */
36
+ static renderNodes(layout: object, element: object, parent: object): Array<object>;
26
37
  }
@@ -6,12 +6,34 @@
6
6
  * @class
7
7
  */
8
8
  export class BrowserHistory extends History {
9
+ /**
10
+ * The key of the history entry that is showing.
11
+ *
12
+ * @type {string|null} currentKey
13
+ */
14
+ currentKey: string | null;
15
+ /**
16
+ * The last scroll position of each visited entry. Entries
17
+ * left by back / forward can't be updated with replaceState,
18
+ * so their position is kept here.
19
+ *
20
+ * @type {LRUCache} positions
21
+ */
22
+ positions: LRUCache;
23
+ /**
24
+ * This will save the scroll position of the showing entry.
25
+ *
26
+ * @protected
27
+ * @returns {object}
28
+ */
29
+ protected saveCurrentPosition(): object;
9
30
  /**
10
31
  * This will add the events.
11
32
  *
12
33
  * @returns {this} a reference to the object.
13
34
  */
14
35
  addEvent(): this;
36
+ previousScrollRestoration: ScrollRestoration;
15
37
  pageShowCallBack: any;
16
38
  /**
17
39
  * This will remove the events.
@@ -51,3 +73,4 @@ export class BrowserHistory extends History {
51
73
  addState(uri: string, data: object | null, replace?: boolean): this;
52
74
  }
53
75
  import { History } from "./history.js";
76
+ import { LRUCache } from "../../../shared/lru-cache.js";