Skip to content

Commit 08947ca

Browse files
committed
[middleware] skipWillCallbacks
1 parent b474fe2 commit 08947ca

5 files changed

Lines changed: 96 additions & 57 deletions

File tree

‎coverage.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"tests":7636,"assertions":32830,"lines":{"total":2539,"covered":2539,"skipped":0,"pct":100},"statements":{"total":2750,"covered":2750,"skipped":0,"pct":100},"functions":{"total":1101,"covered":1101,"skipped":0,"pct":100},"branches":{"total":957,"covered":957,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
1+
{"tests":7636,"assertions":32830,"lines":{"total":2537,"covered":2537,"skipped":0,"pct":100},"statements":{"total":2748,"covered":2748,"skipped":0,"pct":100},"functions":{"total":1100,"covered":1100,"skipped":0,"pct":100},"branches":{"total":961,"covered":961,"skipped":0,"pct":100},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}

‎src/checkpoints/index.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
arrayShift,
2626
arrayUnshift,
2727
} from '../common/array.ts';
28-
import {setOrDelCell, setOrDelValue} from '../common/cell.ts';
28+
2929
import {collForEach, collHas, collIsEmpty, collSize2} from '../common/coll.ts';
3030
import {getCreateFunction} from '../common/definable.ts';
3131
import {getListenerFunctions} from '../common/listeners.ts';
@@ -76,18 +76,22 @@ export const createCheckpoints = getCreateFunction(
7676
collForEach(cellsDelta, (table, tableId) =>
7777
collForEach(table, (row, rowId) =>
7878
collForEach(row, (oldNew, cellId) =>
79-
setOrDelCell(
80-
store,
79+
(store as any).setOrDelCell(
8180
tableId,
8281
rowId,
8382
cellId,
8483
oldNew[oldOrNew] as CellOrUndefined,
84+
true,
8585
),
8686
),
8787
),
8888
);
8989
collForEach(valuesDelta, (oldNew, valueId) =>
90-
setOrDelValue(store, valueId, oldNew[oldOrNew] as ValueOrUndefined),
90+
(store as any).setOrDelValue(
91+
valueId,
92+
oldNew[oldOrNew] as ValueOrUndefined,
93+
true,
94+
),
9195
);
9296
});
9397
listening = 1;

‎src/common/cell.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import type {Id} from '../@types/common/index.d.ts';
2-
import type {
3-
CellOrUndefined,
4-
Store,
5-
ValueOrUndefined,
6-
} from '../@types/store/index.d.ts';
71
import {
82
isFiniteNumber,
93
isNull,
@@ -30,24 +24,6 @@ export const getCellOrValueType = (
3024
export const isCellOrValueOrUndefined = (cellOrValue: any): boolean =>
3125
isUndefined(cellOrValue) || !isUndefined(getCellOrValueType(cellOrValue));
3226

33-
export const setOrDelCell = (
34-
store: Store,
35-
tableId: Id,
36-
rowId: Id,
37-
cellId: Id,
38-
cell: CellOrUndefined,
39-
) =>
40-
isUndefined(cell)
41-
? store.delCell(tableId, rowId, cellId, true)
42-
: store.setCell(tableId, rowId, cellId, cell);
43-
44-
export const setOrDelValue = (
45-
store: Store,
46-
valueId: Id,
47-
value: ValueOrUndefined,
48-
) =>
49-
isUndefined(value) ? store.delValue(valueId) : store.setValue(valueId, value);
50-
5127
export const getTypeCase = <IfStringReturn, IfNumberReturn, IfBooleanReturn>(
5228
type: CellOrValueType | undefined,
5329
stringCase: IfStringReturn,

‎src/queries/index.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
arrayOrValueEqual,
3838
arrayPush,
3939
} from '../common/array.ts';
40-
import {getCellOrValueType, setOrDelCell} from '../common/cell.ts';
40+
import {getCellOrValueType} from '../common/cell.ts';
4141
import {
4242
collClear,
4343
collDel,
@@ -516,8 +516,7 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
516516
selectJoinWhereStore.transaction(() =>
517517
arrayEvery(wheres, (where) => where(getTableCell))
518518
? mapForEach(selects, (asCellId, tableCellGetter) =>
519-
setOrDelCell(
520-
selectJoinWhereStore,
519+
(selectJoinWhereStore as any).setOrDelCell(
521520
queryId,
522521
rootRowId,
523522
asCellId,

‎src/store/index.ts‎

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ import {
4444
arrayPush,
4545
arraySort,
4646
} from '../common/array.ts';
47-
import {
48-
getCellOrValueType,
49-
setOrDelCell,
50-
setOrDelValue,
51-
} from '../common/cell.ts';
47+
import {getCellOrValueType} from '../common/cell.ts';
5248
import {
5349
collClear,
5450
collDel,
@@ -466,6 +462,29 @@ export const createStore: typeof createStoreDecl = (): Store => {
466462
const setOrDelTables = (tables: Tables) =>
467463
objIsEmpty(tables) ? delTables() : setTables(tables);
468464

465+
const setOrDelCell = (
466+
tableId: Id,
467+
rowId: Id,
468+
cellId: Id,
469+
cell: CellOrUndefined,
470+
skipWillCallbacks?: boolean,
471+
) =>
472+
isUndefined(cell)
473+
? delCell(tableId, rowId, cellId, true, skipWillCallbacks)
474+
: setCell(tableId, rowId, cellId, cell, skipWillCallbacks);
475+
476+
const setOrDelValues = (values: Values) =>
477+
objIsEmpty(values) ? delValues() : setValues(values);
478+
479+
const setOrDelValue = (
480+
valueId: Id,
481+
value: ValueOrUndefined,
482+
skipWillCallbacks?: boolean,
483+
) =>
484+
isUndefined(value)
485+
? delValue(valueId, skipWillCallbacks)
486+
: setValue(valueId, value, skipWillCallbacks);
487+
469488
const setValidContent = (content: Content): void =>
470489
ifNotUndefined(
471490
ifNotUndefined(
@@ -559,10 +578,11 @@ export const createStore: typeof createStoreDecl = (): Store => {
559578
rowMap: RowMap,
560579
cellId: Id,
561580
cell: Cell,
581+
skipWillCallbacks?: boolean,
562582
): void =>
563583
ifNotUndefined(
564584
ifNotUndefined(
565-
willCallbacks[4],
585+
skipWillCallbacks ? undefined : willCallbacks[4],
566586
(willSetCell) => willSetCell(tableId, rowId, cellId, cell),
567587
() => cell,
568588
),
@@ -584,25 +604,38 @@ export const createStore: typeof createStoreDecl = (): Store => {
584604
rowId: Id,
585605
cellId: Id,
586606
validCell: Cell,
607+
skipWillCallbacks?: boolean,
587608
): void =>
588609
ifNotUndefined(
589610
mapGet(tableMap, rowId),
590-
(rowMap): any => setValidCell(tableId, rowId, rowMap, cellId, validCell),
611+
(rowMap): any =>
612+
setValidCell(
613+
tableId,
614+
rowId,
615+
rowMap,
616+
cellId,
617+
validCell,
618+
skipWillCallbacks,
619+
),
591620
() => {
592621
const rowMap: RowMap = mapNew();
593622
mapSet(tableMap, rowId, rowMap);
594623
rowIdsChanged(tableId, rowId, 1);
595624
objMap(
596625
addDefaultsToRow({[cellId]: validCell}, tableId, rowId),
597626
(cell, cellId) =>
598-
setValidCell(tableId, rowId, rowMap, cellId, cell as Cell),
627+
setValidCell(
628+
tableId,
629+
rowId,
630+
rowMap,
631+
cellId,
632+
cell as Cell,
633+
skipWillCallbacks,
634+
),
599635
);
600636
},
601637
);
602638

603-
const setOrDelValues = (values: Values) =>
604-
objIsEmpty(values) ? delValues() : setValues(values);
605-
606639
const setValidValues = (
607640
values: Values,
608641
forceDel?: boolean,
@@ -624,10 +657,14 @@ export const createStore: typeof createStoreDecl = (): Store => {
624657
),
625658
);
626659

627-
const setValidValue = (valueId: Id, value: Value): void =>
660+
const setValidValue = (
661+
valueId: Id,
662+
value: Value,
663+
skipWillCallbacks?: boolean,
664+
): void =>
628665
ifNotUndefined(
629666
ifNotUndefined(
630-
willCallbacks[6],
667+
skipWillCallbacks ? undefined : willCallbacks[6],
631668
(willSetValue) => willSetValue(valueId, value),
632669
() => value,
633670
),
@@ -680,6 +717,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
680717
row: RowMap,
681718
cellId: Id,
682719
forceDel?: boolean,
720+
skipWillCallbacks?: boolean,
683721
): void => {
684722
const defaultCell = mapGet(
685723
mapGet(tablesSchemaRowCache, tableId)?.[0],
@@ -688,7 +726,10 @@ export const createStore: typeof createStoreDecl = (): Store => {
688726
if (!isUndefined(defaultCell) && !forceDel) {
689727
return setValidCell(tableId, rowId, row, cellId, defaultCell);
690728
}
691-
if (willCallbacks[10]?.(tableId, rowId, cellId) ?? true) {
729+
if (
730+
skipWillCallbacks ||
731+
(willCallbacks[10]?.(tableId, rowId, cellId) ?? true)
732+
) {
692733
const delCell = (cellId: Id) => {
693734
cellChanged(tableId, rowId, cellId, mapGet(row, cellId));
694735
cellIdsChanged(tableId, rowId, cellId, -1);
@@ -711,12 +752,12 @@ export const createStore: typeof createStoreDecl = (): Store => {
711752
}
712753
};
713754

714-
const delValidValue = (valueId: Id): void => {
755+
const delValidValue = (valueId: Id, skipWillCallbacks?: boolean): void => {
715756
const defaultValue = mapGet(valuesDefaulted, valueId);
716757
if (!isUndefined(defaultValue)) {
717758
return setValidValue(valueId, defaultValue);
718759
}
719-
if (willCallbacks[12]?.(valueId) ?? true) {
760+
if (skipWillCallbacks || (willCallbacks[12]?.(valueId) ?? true)) {
720761
valueChanged(valueId, mapGet(valuesMap, valueId));
721762
valueIdsChanged(valueId, -1);
722763
mapSet(valuesMap, valueId);
@@ -1328,6 +1369,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
13281369
rowId: Id,
13291370
cellId: Id,
13301371
cell: Cell | MapCell,
1372+
skipWillCallbacks?: boolean,
13311373
): Store =>
13321374
fluentTransaction(
13331375
(tableId, rowId, cellId) =>
@@ -1345,6 +1387,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
13451387
rowId,
13461388
cellId,
13471389
validCell,
1390+
skipWillCallbacks,
13481391
),
13491392
),
13501393
tableId,
@@ -1366,15 +1409,19 @@ export const createStore: typeof createStoreDecl = (): Store => {
13661409
: 0,
13671410
);
13681411

1369-
const setValue = (valueId: Id, value: Value): Store =>
1412+
const setValue = (
1413+
valueId: Id,
1414+
value: Value,
1415+
skipWillCallbacks?: boolean,
1416+
): Store =>
13701417
fluentTransaction(
13711418
(valueId) =>
13721419
ifNotUndefined(
13731420
getValidatedValue(
13741421
valueId,
13751422
isFunction(value) ? value(getValue(valueId)) : value,
13761423
),
1377-
(validValue) => setValidValue(valueId, validValue),
1424+
(validValue) => setValidValue(valueId, validValue, skipWillCallbacks),
13781425
),
13791426
valueId,
13801427
);
@@ -1396,7 +1443,6 @@ export const createStore: typeof createStoreDecl = (): Store => {
13961443
? delRow(tableId, rowId)
13971444
: objMap(row, (cell, cellId) =>
13981445
setOrDelCell(
1399-
store,
14001446
tableId,
14011447
rowId,
14021448
cellId,
@@ -1406,7 +1452,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
14061452
),
14071453
);
14081454
objMap(changes[1], (value, valueId) =>
1409-
setOrDelValue(store, valueId, value as ValueOrUndefined),
1455+
setOrDelValue(valueId, value as ValueOrUndefined),
14101456
);
14111457
},
14121458
),
@@ -1493,13 +1539,22 @@ export const createStore: typeof createStoreDecl = (): Store => {
14931539
rowId: Id,
14941540
cellId: Id,
14951541
forceDel?: boolean,
1542+
skipWillCallbacks?: boolean,
14961543
): Store =>
14971544
fluentTransaction(
14981545
(tableId, rowId, cellId) =>
14991546
ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
15001547
ifNotUndefined(mapGet(tableMap, rowId), (rowMap) =>
15011548
collHas(rowMap, cellId)
1502-
? delValidCell(tableId, tableMap, rowId, rowMap, cellId, forceDel)
1549+
? delValidCell(
1550+
tableId,
1551+
tableMap,
1552+
rowId,
1553+
rowMap,
1554+
cellId,
1555+
forceDel,
1556+
skipWillCallbacks,
1557+
)
15031558
: 0,
15041559
),
15051560
),
@@ -1513,9 +1568,12 @@ export const createStore: typeof createStoreDecl = (): Store => {
15131568
(willCallbacks[11]?.() ?? true) ? setValidValues({}, true) : 0,
15141569
);
15151570

1516-
const delValue = (valueId: Id): Store =>
1571+
const delValue = (valueId: Id, skipWillCallbacks?: boolean): Store =>
15171572
fluentTransaction(
1518-
(valueId) => (collHas(valuesMap, valueId) ? delValidValue(valueId) : 0),
1573+
(valueId) =>
1574+
collHas(valuesMap, valueId)
1575+
? delValidValue(valueId, skipWillCallbacks)
1576+
: 0,
15191577
valueId,
15201578
);
15211579

@@ -1627,13 +1685,13 @@ export const createStore: typeof createStoreDecl = (): Store => {
16271685
collForEach(changedCells, (table, tableId) =>
16281686
collForEach(table, (row, rowId) =>
16291687
collForEach(row, ([oldCell], cellId) =>
1630-
setOrDelCell(store, tableId, rowId, cellId, oldCell),
1688+
setOrDelCell(tableId, rowId, cellId, oldCell, true),
16311689
),
16321690
),
16331691
);
16341692
collClear(changedCells);
16351693
collForEach(changedValues, ([oldValue], valueId) =>
1636-
setOrDelValue(store, valueId, oldValue),
1694+
setOrDelValue(valueId, oldValue, true),
16371695
);
16381696
collClear(changedValues);
16391697
}
@@ -1940,6 +1998,8 @@ export const createStore: typeof createStoreDecl = (): Store => {
19401998
callListeners,
19411999
setInternalListeners,
19422000
setWillCallbacks,
2001+
setOrDelCell,
2002+
setOrDelValue,
19432003
};
19442004

19452005
// and now for some gentle meta-programming

0 commit comments

Comments
 (0)