Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/@types/store/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ export interface Store {
/// Store.transaction
transaction<Return>(actions: () => Return, doRollback?: DoRollback): Return;

internalTransaction<Return>(
actions: () => Return,
doRollback?: DoRollback,
): Return;

/// Store.startTransaction
startTransaction(): this;

Expand Down
14 changes: 7 additions & 7 deletions src/checkpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const createCheckpoints = getCreateFunction(

const updateStore = (oldOrNew: 0 | 1, checkpointId: Id) => {
listening = 0;
store.transaction(() => {
store.internalTransaction(() => {
const [cellsDelta, valuesDelta] = mapGet(deltas, checkpointId) as [
CellsDelta,
ValuesDelta,
Expand Down Expand Up @@ -96,7 +96,7 @@ export const createCheckpoints = getCreateFunction(
const clearCheckpointId = (checkpointId: Id): void => {
mapSet(deltas, checkpointId);
mapSet(labels, checkpointId);
callListeners(checkpointListeners, [checkpointId]);
callListeners(false, checkpointListeners, [checkpointId]);
};

const clearCheckpointIds = (checkpointIds: Ids, to?: number): void =>
Expand Down Expand Up @@ -157,7 +157,7 @@ export const createCheckpoints = getCreateFunction(

const callListenersIfChanged = (): void => {
if (checkpointsChanged) {
callListeners(checkpointIdsListeners);
callListeners(false, checkpointIdsListeners);
checkpointsChanged = 0;
}
};
Expand All @@ -180,7 +180,7 @@ export const createCheckpoints = getCreateFunction(
mapGet(labels, checkpointId) !== label
) {
mapSet(labels, checkpointId, label);
callListeners(checkpointListeners, [checkpointId]);
callListeners(false, checkpointListeners, [checkpointId]);
}
return checkpoints;
};
Expand Down Expand Up @@ -227,12 +227,12 @@ export const createCheckpoints = getCreateFunction(
};

const addCheckpointIdsListener = (listener: CheckpointIdsListener): Id =>
addListener(listener, checkpointIdsListeners);
addListener(false, listener, checkpointIdsListeners);

const addCheckpointListener = (
checkpointId: IdOrNull,
listener: CheckpointListener,
): Id => addListener(listener, checkpointListeners, [checkpointId]);
): Id => addListener(false, listener, checkpointListeners, [checkpointId]);

const delListener = (listenerId: Id): Checkpoints => {
delListenerImpl(listenerId);
Expand All @@ -254,7 +254,7 @@ export const createCheckpoints = getCreateFunction(
const clearForward = (): Checkpoints => {
if (!arrayIsEmpty(forwardIds)) {
clearCheckpointIds(forwardIds);
callListeners(checkpointIdsListeners);
callListeners(false, checkpointIdsListeners);
}
return checkpoints;
};
Expand Down
17 changes: 11 additions & 6 deletions src/common/definable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const getDefinableFunctions = <Thing, RowValue>(
mapSet(things, id, getDefaultThing());
mapSet(allRowValues, id, mapNew());
mapSet(allSortKeys, id, mapNew());
callListeners(thingIdListeners);
callListeners(true, thingIdListeners);
}
};

Expand Down Expand Up @@ -206,10 +206,15 @@ export const getDefinableFunctions = <Thing, RowValue>(
addStoreListeners(
id,
0,
store.addRowListener(tableId, null, (_store, _tableId, rowId) =>
processRow(rowId),
store.addRowListener.call(
{isInternal: true},
tableId,
null,
(_store, _tableId, rowId) => processRow(rowId),
),
store.addTableListener.call({isInternal: true}, tableId, () =>
processTable(),
),
store.addTableListener(tableId, () => processTable()),
);
};

Expand All @@ -219,11 +224,11 @@ export const getDefinableFunctions = <Thing, RowValue>(
mapSet(allRowValues, id);
mapSet(allSortKeys, id);
delStoreListeners(id);
callListeners(thingIdListeners);
callListeners(false, thingIdListeners);
};

const addThingIdsListener = (listener: () => void) =>
addListener(listener, thingIdListeners);
addListener(false, listener, thingIdListeners);

const destroy = (): void => mapForEach(storeListenerIds, delDefinition);

Expand Down
18 changes: 15 additions & 3 deletions src/common/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ export type ListenerArgument = IdOrNull | boolean | number | undefined;
export type PathGetters = ((...ids: Ids) => Ids)[];
export type ExtraArgsGetter = (ids: Ids) => any[];
export type AddListener = (
isInternal: boolean,
listener: Listener,
idSetNode: IdSetNode,
path?: ListenerArgument[],
pathGetters?: PathGetters,
extraArgsGetter?: ExtraArgsGetter,
) => Id;
export type CallListeners = (
isInternal: boolean,
idSetNode: IdSetNode,
ids?: Ids,
...extra: any[]
Expand Down Expand Up @@ -159,8 +161,10 @@ export const getListenerFunctions = (
const allListeners: IdMap<
[Listener, IdSetNode, ListenerArgument[], PathGetters, ExtraArgsGetter]
> = mapNew();
const internalListenerIds: IdSet = setNew();

const addListener = (
isInternal: boolean,
listener: Listener,
idSetNode: IdSetNode,
path?: ListenerArgument[],
Expand All @@ -184,22 +188,29 @@ export const getListenerFunctions = (
),
id,
) as IdSet;
if (isInternal) {
internalListenerIds.add(id);
}
return id;
};

const callListeners = (
isInternal: boolean,
idSetNode: IdSetNode,
ids?: Ids,
...extraArgs: any[]
): void =>
arrayForEach(getWildcardedLeaves(idSetNode, ids), (set) =>
collForEach(set, (id: Id) =>
collForEach(set, (id: Id) => {
if (internalListenerIds.has(id) !== isInternal) {
return;
}
(mapGet(allListeners, id) as any)[0](
thing,
...(ids ?? []),
...extraArgs,
),
),
);
}),
);

const delListener = (id: Id): Ids =>
Expand All @@ -215,6 +226,7 @@ export const getListenerFunctions = (
);
mapSet(allListeners, id);
releaseId(id);
internalListenerIds.delete(id);
return idOrNulls;
}) as Ids;

Expand Down
9 changes: 5 additions & 4 deletions src/indexes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ export const createIndexes = getCreateFunction((store: Store): Indexes => {
}

if (sliceIdsChanged) {
callListeners(sliceIdsListeners, [indexId]);
callListeners(false, sliceIdsListeners, [indexId]);
}
collForEach(changedSlices, (sliceId) =>
callListeners(sliceRowIdsListeners, [indexId, sliceId]),
callListeners(false, sliceRowIdsListeners, [indexId, sliceId]),
);
},
getRowCellFunction(getSliceIdOrIds),
Expand Down Expand Up @@ -256,13 +256,14 @@ export const createIndexes = getCreateFunction((store: Store): Indexes => {
const addSliceIdsListener = (
indexId: IdOrNull,
listener: SliceIdsListener,
): Id => addListener(listener, sliceIdsListeners, [indexId]);
): Id => addListener(false, listener, sliceIdsListeners, [indexId]);

const addSliceRowIdsListener = (
indexId: IdOrNull,
sliceId: IdOrNull,
listener: SliceRowIdsListener,
): Id => addListener(listener, sliceRowIdsListeners, [indexId, sliceId]);
): Id =>
addListener(false, listener, sliceRowIdsListeners, [indexId, sliceId]);

const delListener = (listenerId: Id): Indexes => {
delListenerImpl(listenerId);
Expand Down
10 changes: 8 additions & 2 deletions src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ export const createMetrics = getCreateFunction((store: Store): Metrics => {

if (newMetric != oldMetric) {
setMetric(metricId, newMetric);
callListeners(metricListeners, [metricId], newMetric, oldMetric);
callListeners(
false,
metricListeners,
[metricId],
newMetric,
oldMetric,
);
}
},
getRowCellFunction(getNumber, 1),
Expand All @@ -132,7 +138,7 @@ export const createMetrics = getCreateFunction((store: Store): Metrics => {
const addMetricListener = (
metricId: IdOrNull,
listener: MetricListener,
): Id => addListener(listener, metricListeners, [metricId]);
): Id => addListener(false, listener, metricListeners, [metricId]);

const delListener = (listenerId: Id): Metrics => {
delListenerImpl(listenerId);
Expand Down
4 changes: 2 additions & 2 deletions src/persisters/common/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const createCustomPersister = <
const setStatus = (newStatus: StatusValues): void => {
if (newStatus != status) {
status = newStatus;
callListeners(statusListeners, undefined, status);
callListeners(false, statusListeners, undefined, status);
}
};

Expand Down Expand Up @@ -332,7 +332,7 @@ export const createCustomPersister = <
const getStatus = (): StatusValues => status;

const addStatusListener = (listener: StatusListener): Id =>
addListener(listener, statusListeners);
addListener(false, listener, statusListeners);

const delListener = (listenerId: Id): Store => {
delListenerImpl(listenerId);
Expand Down
4 changes: 2 additions & 2 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
arg2,
]) as [Id, Id, Id]),
);
selectJoinWhereStore.transaction(() =>
selectJoinWhereStore.internalTransaction(() =>
arrayEvery(wheres, (where) => where(getTableCell))
? mapForEach(selects, (asCellId, tableCellGetter) =>
setOrDelCell(
Expand Down Expand Up @@ -543,7 +543,7 @@ export const createQueries = getCreateFunction((store: Store): Queries => {
};

const {3: joinedTableIds} = mapGet(joins, undefined) as JoinClause;
selectJoinWhereStore.transaction(() =>
selectJoinWhereStore.internalTransaction(() =>
addStoreListeners(
queryId,
1,
Expand Down
24 changes: 18 additions & 6 deletions src/relationships/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,23 @@ export const createRelationships = getCreateFunction(
change();

collForEach(changedLocalRows, (localRowId) =>
callListeners(remoteRowIdListeners, [relationshipId, localRowId]),
callListeners(false, remoteRowIdListeners, [
relationshipId,
localRowId,
]),
);
collForEach(changedRemoteRows, (remoteRowId) =>
callListeners(localRowIdsListeners, [relationshipId, remoteRowId]),
callListeners(false, localRowIdsListeners, [
relationshipId,
remoteRowId,
]),
);
collForEach(changedLinkedRows, (firstRowId) => {
delLinkedRowIdsCache(relationshipId, firstRowId);
callListeners(linkedRowIdsListeners, [relationshipId, firstRowId]);
callListeners(false, linkedRowIdsListeners, [
relationshipId,
firstRowId,
]);
});
},
getRowCellFunction(getRemoteRowId),
Expand Down Expand Up @@ -219,14 +228,17 @@ export const createRelationships = getCreateFunction(
localRowId: IdOrNull,
listener: RemoteRowIdListener,
): Id =>
addListener(listener, remoteRowIdListeners, [relationshipId, localRowId]);
addListener(false, listener, remoteRowIdListeners, [
relationshipId,
localRowId,
]);

const addLocalRowIdsListener = (
relationshipId: IdOrNull,
remoteRowId: IdOrNull,
listener: LocalRowIdsListener,
): Id =>
addListener(listener, localRowIdsListeners, [
addListener(false, listener, localRowIdsListeners, [
relationshipId,
remoteRowId,
]);
Expand All @@ -237,7 +249,7 @@ export const createRelationships = getCreateFunction(
listener: LinkedRowIdsListener,
): Id => {
getLinkedRowIdsCache(relationshipId, firstRowId);
return addListener(listener, linkedRowIdsListeners, [
return addListener(false, listener, linkedRowIdsListeners, [
relationshipId,
firstRowId,
]);
Expand Down
Loading