|
33 | 33 | */ |
34 | 34 | /// WillSetCellCallback |
35 | 35 | /** |
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. |
38 | 38 | * |
39 | 39 | * The callback receives the table Id, row Id, and the Row object that is about |
40 | 40 | * to be set. It can return the Row (possibly transformed) to allow the write, |
|
46 | 46 | * @param tableId The Id of the Table being written to. |
47 | 47 | * @param rowId The Id of the Row being set. |
48 | 48 | * @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. |
51 | 51 | * @category Callback |
52 | 52 | * @since v8.0.0 |
53 | 53 | */ |
|
71 | 71 | * @since v8.0.0 |
72 | 72 | */ |
73 | 73 | /// 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 |
74 | 92 | /** |
75 | 93 | * The WillDelCellCallback type describes a function that is called before a |
76 | 94 | * Cell is deleted from the Store. |
|
154 | 172 | */ |
155 | 173 | /// Middleware.getStore |
156 | 174 | /** |
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. |
159 | 177 | * |
160 | 178 | * The callback can transform the Cell value or return `undefined` to prevent |
161 | 179 | * the write. Multiple callbacks can be registered and they are called |
|
187 | 205 | * middleware.destroy(); |
188 | 206 | * ``` |
189 | 207 | * @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. |
192 | 210 | * |
193 | 211 | * ```js |
194 | 212 | * import {createMiddleware, createStore} from 'tinybase'; |
|
198 | 216 | * |
199 | 217 | * middleware.addWillSetCellCallback( |
200 | 218 | * (tableId, _rowId, _cellId, cell) => |
201 | | - * tableId === 'locked' ? undefined : cell, |
| 219 | + * tableId === 'species' ? undefined : cell, |
202 | 220 | * ); |
203 | 221 | * |
204 | | - * store.setCell('locked', 'r1', 'c1', 'value'); |
| 222 | + * store.setCell('species', 'dog', 'legs', 4); |
205 | 223 | * console.log(store.getTables()); |
206 | 224 | * // -> {} |
207 | 225 | * |
|
212 | 230 | */ |
213 | 231 | /// Middleware.addWillSetCellCallback |
214 | 232 | /** |
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. |
217 | 235 | * |
218 | 236 | * The callback can transform the Row or return `undefined` to prevent the |
219 | 237 | * write. Multiple callbacks can be registered and they are called |
|
249 | 267 | * middleware.destroy(); |
250 | 268 | * ``` |
251 | 269 | * @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. |
254 | 272 | * |
255 | 273 | * ```js |
256 | 274 | * import {createMiddleware, createStore} from 'tinybase'; |
|
259 | 277 | * const middleware = createMiddleware(store); |
260 | 278 | * |
261 | 279 | * middleware.addWillSetRowCallback((tableId, _rowId, row) => |
262 | | - * tableId === 'locked' ? undefined : row, |
| 280 | + * tableId === 'species' ? undefined : row, |
263 | 281 | * ); |
264 | 282 | * |
265 | | - * store.setRow('locked', 'r1', {c1: 'value'}); |
| 283 | + * store.setRow('species', 'dog', {legs: 4, sound: 'woof'}); |
266 | 284 | * console.log(store.getTables()); |
267 | 285 | * // -> {} |
268 | 286 | * |
|
283 | 301 | * @param callback The WillSetValueCallback to register. |
284 | 302 | * @returns A reference to the Middleware object, for chaining. |
285 | 303 | * @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. |
287 | 306 | * |
288 | 307 | * ```js |
289 | 308 | * import {createMiddleware, createStore} from 'tinybase'; |
|
292 | 311 | * const middleware = createMiddleware(store); |
293 | 312 | * |
294 | 313 | * 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)) |
297 | 316 | * : value, |
298 | 317 | * ); |
299 | 318 | * |
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 |
303 | 322 | * |
304 | 323 | * middleware.destroy(); |
305 | 324 | * ``` |
|
308 | 327 | */ |
309 | 328 | /// Middleware.addWillSetValueCallback |
310 | 329 | /** |
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. |
313 | 391 | * |
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. |
317 | 395 | * @param callback The WillDelCellCallback to register. |
318 | 396 | * @returns A reference to the Middleware object, for chaining. |
319 | 397 | * @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. |
322 | 400 | * |
323 | 401 | * ```js |
324 | 402 | * import {createMiddleware, createStore} from 'tinybase'; |
325 | 403 | * |
326 | 404 | * const store = createStore(); |
327 | 405 | * const middleware = createMiddleware(store); |
328 | 406 | * |
329 | | - * store.setCell('protected', 'r1', 'name', 'Alice'); |
| 407 | + * store.setCell('pets', 'fido', 'species', 'dog'); |
330 | 408 | * |
331 | 409 | * middleware.addWillDelCellCallback( |
332 | | - * (tableId) => tableId !== 'protected', |
| 410 | + * (tableId) => tableId !== 'pets', |
333 | 411 | * ); |
334 | 412 | * |
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' |
338 | 416 | * |
339 | 417 | * middleware.destroy(); |
340 | 418 | * ``` |
|
346 | 424 | * The addWillDelValueCallback method registers a WillDelValueCallback that |
347 | 425 | * will be called before any Value is deleted from the Store. |
348 | 426 | * |
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. |
352 | 430 | * @param callback The WillDelValueCallback to register. |
353 | 431 | * @returns A reference to the Middleware object, for chaining. |
354 | 432 | * @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. |
357 | 435 | * |
358 | 436 | * ```js |
359 | 437 | * import {createMiddleware, createStore} from 'tinybase'; |
360 | 438 | * |
361 | 439 | * const store = createStore(); |
362 | 440 | * const middleware = createMiddleware(store); |
363 | 441 | * |
364 | | - * store.setValue('theme', 'dark'); |
| 442 | + * store.setValue('storeName', 'happy pets'); |
365 | 443 | * |
366 | 444 | * middleware.addWillDelValueCallback( |
367 | | - * (valueId) => valueId !== 'theme', |
| 445 | + * (valueId) => valueId !== 'storeName', |
368 | 446 | * ); |
369 | 447 | * |
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' |
373 | 451 | * |
374 | 452 | * middleware.destroy(); |
375 | 453 | * ``` |
|
0 commit comments