@@ -45,16 +45,16 @@ value. The meaning of this return value depends on the type of callback:
4545
4646The full list of ` willSet* ` callbacks you can register is as follows:
4747
48- | Callback | Parameters | Called | Return |
49- | ---------------- | ---------------------------- | ---------------------------- | -------------------- |
50- | willSetContent | content | When setContent is called. | Content or undefined |
51- | willSetTables | tables | When setTables is called. | Tables or undefined |
52- | willSetTable | tableId, table | When setTable is called. | Table or undefined |
53- | willSetRow | tableId, rowId, row | When setRow is called. | Row or undefined |
54- | willSetCell | tableId, rowId, cellId, cell | When setCell is called. | Cell or undefined |
55- | willSetValues | values | When setValues is called. | Values or undefined |
56- | willSetValue | valueId, value | When setValue is called. | Value or undefined |
57- | willApplyChanges | changes | When applyChanges is called. | Changes or undefined |
48+ | Callback | Parameters | Called | Return |
49+ | ---------------- | ---------------------------- | --------------------------------- | -------------------- |
50+ | willSetContent | content | When setContent is called. | Content or undefined |
51+ | willSetTables | tables | When setTables is called. | Tables or undefined |
52+ | willSetTable | tableId, table | When setTable is called. | Table or undefined |
53+ | willSetRow | tableId, rowId, row | When setRow or setCell is called. | Row or undefined |
54+ | willSetCell | tableId, rowId, cellId, cell | When setCell is called. | Cell or undefined |
55+ | willSetValues | values | When setValues is called. | Values or undefined |
56+ | willSetValue | valueId, value | When setValue is called. | Value or undefined |
57+ | willApplyChanges | changes | When applyChanges is called. | Changes or undefined |
5858
5959Finally, the full list of ` willDel* ` callbacks you can register is as follows:
6060
@@ -89,42 +89,51 @@ callback cancels the operation, subsequent `willSet*` callbacks will not be
8989called.
9090
9191``` js
92- middleware .addWillSetRowCallback ((tableId , rowId , row ) => {
93- console .log (' Timestamp row' );
94- return {... row, timestamp: Date .now ()};
95- });
96- middleware .addWillSetRowCallback ((tableId , rowId , row ) => {
97- console .log (' Cancel setting row' );
98- return undefined ;
99- });
100- middleware .addWillSetRowCallback ((tableId , rowId , row ) => {
101- console .log (' Defaulting pet to be alive' );
102- return {... row, alive: true };
103- });
92+ middleware
93+ .addWillSetRowCallback ((_tableId , _rowId , row ) => ({... row, step1: true }))
94+ .addWillSetRowCallback ((_tableId , _rowId , row ) => ({... row, step2: true }));
10495
105- store .setRow (' pets' , ' fido' , {' species' : ' dog' });
106- // -> 'Timestamp row'
107- // -> 'Cancel setting row'
108- // (Callback 3 is not called because Callback 2 cancels the operation)
96+ store .setRow (' pets' , ' fido' , {species: ' dog' });
97+ console . log ( store . getRow ( ' pets ' , ' fido ' ));
98+ // -> {species: 'dog', step1: true, step2: true}
99+ ```
109100
110- console .log (store .getTable (' pets' ));
101+ Returning ` undefined ` from a callback cancels the entire operation, and
102+ subsequent callbacks will not be called:
103+
104+ ``` js
105+ middleware .addWillSetRowCallback ((tableId , _rowId , row ) =>
106+ tableId === ' readonly' ? undefined : row,
107+ );
108+
109+ store .setRow (' employees' , ' alice' , {role: ' admin' });
110+ console .log (store .getRow (' employees' , ' alice' ));
111+ // -> {role: 'admin', step1: true, step2: true}
112+
113+ store .setRow (' readonly' , ' r1' , {data: ' test' });
114+ console .log (store .getTable (' readonly' ));
111115// -> {}
112116```
113117
114118Similarly, if a ` willDel* ` callback cancels the delete operation, subsequent
115119` willDel* ` callbacks will not be called. In other words, a callback cannot
116120re-enable a delete operation that has been cancelled by a previous callback.
117121
118- A less granular operation on the Store (e.g. setting a Table, which will call
119- ` willSetTable ` ) will also then call more granular callbacks (e.g. ` willSetRow ` ,
120- ` willSetCell ` ) for each relevant Row and Cell. BUT a more granular operation
121- (e.g. setting a Cell, which will call ` willSetCell ` ) will NOT call less granular
122- callbacks (e.g. ` willSetRow ` , ` willSetTable ` , ` willSetContent ` and so on).
122+ In terms of cascading, a less granular operation on the Store (e.g. setting a
123+ Table, which will call ` willSetTable ` ) will also then call more granular
124+ callbacks (e.g. ` willSetRow ` , ` willSetCell ` ) for each relevant Row and Cell.
125+
126+ BUT there is one important exception to this rule though! Calling ` setCell `
127+ will also fire ` willSetRow ` (receiving existing cells merged with the new cell),
128+ in addition to the ` willSetCell ` calls for each Cell in the Row. In other words,
129+ The entire resulting Row is applied as though ` setRow ` had been called with it.
130+
131+ This is to allow for row- or schema-level validation and transformation to be
132+ applied even when only ` setCell ` calls are being made.
123133
124- This might seem strange since, in a way, the Row, Table, and Content were
125- technically being updated. But the key is to think about the actual method that
126- was called on the Store, and then expect callbacks for only more granular
127- elements from there.
134+ Note that ` setCell ` does not fire ` willSetTable ` , ` willSetTables ` , or
135+ ` willSetContent ` though. The 'upwards' cascade only goes as far as ` willSetRow `
136+ for ` setCell ` calls.
128137
129138## Complex Object Callbacks
130139
0 commit comments