Skip to content

Commit ee239ab

Browse files
committed
[middleware] WillSetValuesCallback
1 parent a32e5bd commit ee239ab

8 files changed

Lines changed: 441 additions & 73 deletions

File tree

src/@types/middleware/docs.js

Lines changed: 122 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
*/
3434
/// WillSetCellCallback
3535
/**
36-
* The WillSetRowCallback type describes a function that is called before a
37-
* Row is set in the Store.
36+
* The WillSetRowCallback type describes a function that is called before a Row
37+
* is set in the Store.
3838
*
3939
* The callback receives the table Id, row Id, and the Row object that is about
4040
* to be set. It can return the Row (possibly transformed) to allow the write,
@@ -46,8 +46,8 @@
4646
* @param tableId The Id of the Table being written to.
4747
* @param rowId The Id of the Row being set.
4848
* @param row The Row object about to be set.
49-
* @returns The Row to use (possibly transformed), or `undefined` to prevent
50-
* the write.
49+
* @returns The Row to use (possibly transformed), or `undefined` to prevent the
50+
* write.
5151
* @category Callback
5252
* @since v8.0.0
5353
*/
@@ -71,6 +71,24 @@
7171
* @since v8.0.0
7272
*/
7373
/// WillSetValueCallback
74+
/**
75+
* The WillSetValuesCallback type describes a function that is called before
76+
* Values are set in the Store.
77+
*
78+
* The callback receives the Values object that is about to be set. It can
79+
* return the Values (possibly transformed) to allow the write, or `undefined`
80+
* to prevent the Values from being set.
81+
*
82+
* Multiple WillSetValuesCallback functions can be registered and they will be
83+
* called sequentially, the Values being updated successively. If any callback
84+
* returns `undefined`, the chain short-circuits and the Values will not be set.
85+
* @param values The Values object about to be set.
86+
* @returns The Values to use (possibly transformed), or `undefined` to prevent
87+
* the write.
88+
* @category Callback
89+
* @since v8.0.0
90+
*/
91+
/// WillSetValuesCallback
7492
/**
7593
* The WillDelCellCallback type describes a function that is called before a
7694
* Cell is deleted from the Store.
@@ -154,8 +172,8 @@
154172
*/
155173
/// Middleware.getStore
156174
/**
157-
* The addWillSetCellCallback method registers a WillSetCellCallback that
158-
* will be called before any Cell is set in the Store.
175+
* The addWillSetCellCallback method registers a WillSetCellCallback that will
176+
* be called before any Cell is set in the Store.
159177
*
160178
* The callback can transform the Cell value or return `undefined` to prevent
161179
* the write. Multiple callbacks can be registered and they are called
@@ -187,8 +205,8 @@
187205
* middleware.destroy();
188206
* ```
189207
* @example
190-
* This example registers a callback that prevents writes to a 'locked'
191-
* table.
208+
* This example registers a callback that prevents writes to the
209+
* 'species' table.
192210
*
193211
* ```js
194212
* import {createMiddleware, createStore} from 'tinybase';
@@ -198,10 +216,10 @@
198216
*
199217
* middleware.addWillSetCellCallback(
200218
* (tableId, _rowId, _cellId, cell) =>
201-
* tableId === 'locked' ? undefined : cell,
219+
* tableId === 'species' ? undefined : cell,
202220
* );
203221
*
204-
* store.setCell('locked', 'r1', 'c1', 'value');
222+
* store.setCell('species', 'dog', 'legs', 4);
205223
* console.log(store.getTables());
206224
* // -> {}
207225
*
@@ -212,8 +230,8 @@
212230
*/
213231
/// Middleware.addWillSetCellCallback
214232
/**
215-
* The addWillSetRowCallback method registers a WillSetRowCallback that
216-
* will be called before any Row is set in the Store.
233+
* The addWillSetRowCallback method registers a WillSetRowCallback that will
234+
* be called before any Row is set in the Store.
217235
*
218236
* The callback can transform the Row or return `undefined` to prevent the
219237
* write. Multiple callbacks can be registered and they are called
@@ -249,8 +267,8 @@
249267
* middleware.destroy();
250268
* ```
251269
* @example
252-
* This example registers a callback that prevents writes to a 'locked'
253-
* table.
270+
* This example registers a callback that prevents writes to the
271+
* 'species' table.
254272
*
255273
* ```js
256274
* import {createMiddleware, createStore} from 'tinybase';
@@ -259,10 +277,10 @@
259277
* const middleware = createMiddleware(store);
260278
*
261279
* middleware.addWillSetRowCallback((tableId, _rowId, row) =>
262-
* tableId === 'locked' ? undefined : row,
280+
* tableId === 'species' ? undefined : row,
263281
* );
264282
*
265-
* store.setRow('locked', 'r1', {c1: 'value'});
283+
* store.setRow('species', 'dog', {legs: 4, sound: 'woof'});
266284
* console.log(store.getTables());
267285
* // -> {}
268286
*
@@ -283,7 +301,8 @@
283301
* @param callback The WillSetValueCallback to register.
284302
* @returns A reference to the Middleware object, for chaining.
285303
* @example
286-
* This example registers a callback that clamps a numeric Value.
304+
* This example registers a callback that clamps the 'limit' Value to
305+
* the maximum capacity of the pet store.
287306
*
288307
* ```js
289308
* import {createMiddleware, createStore} from 'tinybase';
@@ -292,14 +311,14 @@
292311
* const middleware = createMiddleware(store);
293312
*
294313
* middleware.addWillSetValueCallback((valueId, value) =>
295-
* valueId === 'score' && typeof value === 'number'
296-
* ? Math.min(100, Math.max(0, value))
314+
* valueId === 'limit' && typeof value === 'number'
315+
* ? Math.min(50, Math.max(0, value))
297316
* : value,
298317
* );
299318
*
300-
* store.setValue('score', 150);
301-
* console.log(store.getValue('score'));
302-
* // -> 100
319+
* store.setValue('limit', 100);
320+
* console.log(store.getValue('limit'));
321+
* // -> 50
303322
*
304323
* middleware.destroy();
305324
* ```
@@ -308,33 +327,92 @@
308327
*/
309328
/// Middleware.addWillSetValueCallback
310329
/**
311-
* The addWillDelCellCallback method registers a WillDelCellCallback that
312-
* will be called before any Cell is deleted from the Store.
330+
* The addWillSetValuesCallback method registers a WillSetValuesCallback that
331+
* will be called before Values are set in the Store.
332+
*
333+
* The callback can transform the Values or return `undefined` to prevent the
334+
* write. Multiple callbacks can be registered and they are called
335+
* sequentially, each receiving the (possibly transformed) values from the
336+
* previous callback.
337+
* @param callback The WillSetValuesCallback to register.
338+
* @returns A reference to the Middleware object, for chaining.
339+
* @example
340+
* This example registers a callback that upper-cases all string Values
341+
* in the pet store's settings.
342+
*
343+
* ```js
344+
* import {createMiddleware, createStore} from 'tinybase';
345+
*
346+
* const store = createStore();
347+
* const middleware = createMiddleware(store);
348+
*
349+
* middleware.addWillSetValuesCallback((values) =>
350+
* Object.fromEntries(
351+
* Object.entries(values).map(([k, v]) => [
352+
* k,
353+
* typeof v === 'string' ? v.toUpperCase() : v,
354+
* ]),
355+
* ),
356+
* );
357+
*
358+
* store.setValues({storeName: 'happy pets', limit: 50});
359+
* console.log(store.getValues());
360+
* // -> {storeName: 'HAPPY PETS', limit: 50}
361+
*
362+
* middleware.destroy();
363+
* ```
364+
* @example
365+
* This example registers a callback that prevents setting Values when
366+
* the pet store is 'closed'.
367+
*
368+
* ```js
369+
* import {createMiddleware, createStore} from 'tinybase';
370+
*
371+
* const store = createStore();
372+
* const middleware = createMiddleware(store);
373+
*
374+
* middleware.addWillSetValuesCallback((values) =>
375+
* 'closed' in values ? undefined : values,
376+
* );
377+
*
378+
* store.setValues({closed: true, storeName: 'happy pets'});
379+
* console.log(store.getValues());
380+
* // -> {}
381+
*
382+
* middleware.destroy();
383+
* ```
384+
* @category Configuration
385+
* @since v8.0.0
386+
*/
387+
/// Middleware.addWillSetValuesCallback
388+
/**
389+
* The addWillDelCellCallback method registers a WillDelCellCallback that will
390+
* be called before any Cell is deleted from the Store.
313391
*
314-
* The callback returns `true` to allow the deletion or `false` to prevent
315-
* it. Multiple callbacks can be registered and they are called sequentially.
316-
* If any callback returns `false`, the deletion is prevented.
392+
* The callback returns `true` to allow the deletion or `false` to prevent it.
393+
* Multiple callbacks can be registered and they are called sequentially. If
394+
* any callback returns `false`, the deletion is prevented.
317395
* @param callback The WillDelCellCallback to register.
318396
* @returns A reference to the Middleware object, for chaining.
319397
* @example
320-
* This example registers a callback that prevents deleting cells from the
321-
* 'protected' table.
398+
* This example registers a callback that prevents deleting cells from
399+
* the 'pets' table.
322400
*
323401
* ```js
324402
* import {createMiddleware, createStore} from 'tinybase';
325403
*
326404
* const store = createStore();
327405
* const middleware = createMiddleware(store);
328406
*
329-
* store.setCell('protected', 'r1', 'name', 'Alice');
407+
* store.setCell('pets', 'fido', 'species', 'dog');
330408
*
331409
* middleware.addWillDelCellCallback(
332-
* (tableId) => tableId !== 'protected',
410+
* (tableId) => tableId !== 'pets',
333411
* );
334412
*
335-
* store.delCell('protected', 'r1', 'name', true);
336-
* console.log(store.getCell('protected', 'r1', 'name'));
337-
* // -> 'Alice'
413+
* store.delCell('pets', 'fido', 'species', true);
414+
* console.log(store.getCell('pets', 'fido', 'species'));
415+
* // -> 'dog'
338416
*
339417
* middleware.destroy();
340418
* ```
@@ -346,30 +424,30 @@
346424
* The addWillDelValueCallback method registers a WillDelValueCallback that
347425
* will be called before any Value is deleted from the Store.
348426
*
349-
* The callback returns `true` to allow the deletion or `false` to prevent
350-
* it. Multiple callbacks can be registered and they are called sequentially.
351-
* If any callback returns `false`, the deletion is prevented.
427+
* The callback returns `true` to allow the deletion or `false` to prevent it.
428+
* Multiple callbacks can be registered and they are called sequentially. If
429+
* any callback returns `false`, the deletion is prevented.
352430
* @param callback The WillDelValueCallback to register.
353431
* @returns A reference to the Middleware object, for chaining.
354432
* @example
355-
* This example registers a callback that prevents deleting a specific
356-
* Value.
433+
* This example registers a callback that prevents deleting the
434+
* 'storeName' Value from the pet store.
357435
*
358436
* ```js
359437
* import {createMiddleware, createStore} from 'tinybase';
360438
*
361439
* const store = createStore();
362440
* const middleware = createMiddleware(store);
363441
*
364-
* store.setValue('theme', 'dark');
442+
* store.setValue('storeName', 'happy pets');
365443
*
366444
* middleware.addWillDelValueCallback(
367-
* (valueId) => valueId !== 'theme',
445+
* (valueId) => valueId !== 'storeName',
368446
* );
369447
*
370-
* store.delValue('theme');
371-
* console.log(store.getValue('theme'));
372-
* // -> 'dark'
448+
* store.delValue('storeName');
449+
* console.log(store.getValue('storeName'));
450+
* // -> 'happy pets'
373451
*
374452
* middleware.destroy();
375453
* ```

src/@types/middleware/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Store,
88
Value,
99
ValueOrUndefined,
10+
Values,
1011
} from '../store/index.d.ts';
1112

1213
/// WillSetCellCallback
@@ -30,6 +31,9 @@ export type WillSetValueCallback = (
3031
value: Value,
3132
) => ValueOrUndefined;
3233

34+
/// WillSetValuesCallback
35+
export type WillSetValuesCallback = (values: Values) => Values | undefined;
36+
3337
/// WillDelCellCallback
3438
export type WillDelCellCallback = (
3539
tableId: Id,
@@ -54,6 +58,9 @@ export interface Middleware {
5458
/// Middleware.addWillSetValueCallback
5559
addWillSetValueCallback(callback: WillSetValueCallback): Middleware;
5660

61+
/// Middleware.addWillSetValuesCallback
62+
addWillSetValuesCallback(callback: WillSetValuesCallback): Middleware;
63+
5764
/// Middleware.addWillDelCellCallback
5865
addWillDelCellCallback(callback: WillDelCellCallback): Middleware;
5966

src/@types/middleware/with-schemas/index.d.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
Row,
1414
Store,
1515
Value,
16+
Values,
1617
} from '../../store/with-schemas/index.d.ts';
1718

1819
/// WillSetCellCallback
@@ -41,11 +42,7 @@ export type WillSetRowCallback<
4142
Schema extends OptionalTablesSchema,
4243
Params extends any[] = TableIdFromSchema<Schema> extends infer TableId
4344
? TableId extends TableIdFromSchema<Schema>
44-
? [
45-
tableId: TableId,
46-
rowId: Id,
47-
row: Row<Schema, TableId>,
48-
]
45+
? [tableId: TableId, rowId: Id, row: Row<Schema, TableId>]
4946
: never
5047
: never,
5148
> = (
@@ -64,6 +61,11 @@ export type WillSetValueCallback<
6461
...params: Params | [valueId: never, value: never]
6562
) => Params[1] | undefined;
6663

64+
/// WillSetValuesCallback
65+
export type WillSetValuesCallback<Schema extends OptionalValuesSchema> = (
66+
values: Values<Schema>,
67+
) => Values<Schema> | undefined;
68+
6769
/// WillDelCellCallback
6870
export type WillDelCellCallback<
6971
Schema extends OptionalTablesSchema,
@@ -110,6 +112,11 @@ export interface Middleware<in out Schemas extends OptionalSchemas> {
110112
callback: WillSetValueCallback<Schemas[1]>,
111113
): Middleware<Schemas>;
112114

115+
/// Middleware.addWillSetValuesCallback
116+
addWillSetValuesCallback(
117+
callback: WillSetValuesCallback<Schemas[1]>,
118+
): Middleware<Schemas>;
119+
113120
/// Middleware.addWillDelCellCallback
114121
addWillDelCellCallback(
115122
callback: WillDelCellCallback<Schemas[0]>,

0 commit comments

Comments
 (0)