Skip to content

Commit

Permalink
[hooks] Persisters in ui-react
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgpearce committed Sep 13, 2024
1 parent b2d3c74 commit 9c688bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/ui-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ export const Provider: typeof ProviderDecl = ({
queriesById,
checkpoints,
checkpointsById,
persister,
persistersById,
children,
}: ProviderProps & {readonly children: React.ReactNode}): any => {
type ThingsById<ThingsByOffset> = {
Expand All @@ -251,6 +253,7 @@ export const Provider: typeof ProviderDecl = ({
{},
{},
{},
{},
]);
const addExtraThingById = useCallback(
<Offset extends Offsets>(
Expand Down Expand Up @@ -299,6 +302,8 @@ export const Provider: typeof ProviderDecl = ({
{...parentValue[9], ...queriesById, ...extraThingsById[4]},
checkpoints ?? parentValue[10],
{...parentValue[11], ...checkpointsById, ...extraThingsById[5]},
persister ?? parentValue[12],
{...parentValue[13], ...persistersById, ...extraThingsById[6]},
addExtraThingById,
delExtraThingById,
],
Expand All @@ -316,6 +321,8 @@ export const Provider: typeof ProviderDecl = ({
queriesById,
checkpoints,
checkpointsById,
persister,
persistersById,
parentValue,
addExtraThingById,
delExtraThingById,
Expand Down
30 changes: 26 additions & 4 deletions src/ui-react/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import type {
CheckpointsOrCheckpointsId,
IndexesOrIndexesId,
MetricsOrMetricsId,
PersisterOrPersisterId,
QueriesOrQueriesId,
RelationshipsOrRelationshipsId,
StoreOrStoreId,
useCheckpoints as useCheckpointsDecl,
useIndexes as useIndexesDecl,
useMetrics as useMetricsDecl,
usePersister as usePersisterDecl,
useQueries as useQueriesDecl,
useRelationships as useRelationshipsDecl,
useStore as useStoreDecl,
Expand All @@ -18,6 +20,7 @@ import {IdObj, objEnsure, objGet, objIds} from '../common/obj.ts';
import type {Checkpoints} from '../@types/checkpoints/index.d.ts';
import type {Indexes} from '../@types/indexes/index.d.ts';
import type {Metrics} from '../@types/metrics/index.d.ts';
import type {Persister} from '../@types/persisters/index.d.ts';
import type {Queries} from '../@types/queries/index.d.ts';
import React from 'react';
import type {Relationships} from '../@types/relationships/index.d.ts';
Expand All @@ -32,16 +35,19 @@ export type Thing =
| Indexes
| Relationships
| Queries
| Checkpoints;
| Checkpoints
| Persister;

export type ThingsByOffset = [
Store,
Metrics,
Indexes,
Relationships,
Queries,
Checkpoints,
Persister,
];
export type Offsets = 0 | 1 | 2 | 3 | 4 | 5;
export type Offsets = 0 | 1 | 2 | 3 | 4 | 5 | 6;

type ContextValue = [
store?: Store,
Expand All @@ -56,6 +62,8 @@ type ContextValue = [
queriesById?: {[queriesId: Id]: Queries},
checkpoints?: Checkpoints,
checkpointsById?: {[checkpointsId: Id]: Checkpoints},
persister?: Persister,
persistersById?: {[persisterId: Id]: Persister},
addExtraThingById?: <Offset extends Offsets>(
offset: Offset,
id: string,
Expand Down Expand Up @@ -91,7 +99,8 @@ const useThingOrThingById = <
| Indexes
| Relationships
| Queries
| Checkpoints,
| Checkpoints
| Persister,
>(
thingOrThingId: Thing | Id | undefined,
offset: number,
Expand All @@ -107,7 +116,7 @@ const useProvideThing = <Offset extends Offsets>(
thing: ThingsByOffset[Offset],
offset: Offset,
): void => {
const {12: addExtraThingById, 13: delExtraThingById} = useContext(Context);
const {14: addExtraThingById, 15: delExtraThingById} = useContext(Context);
useEffect(() => {
addExtraThingById?.(offset, thingId, thing);
return () => delExtraThingById?.(offset, thingId);
Expand Down Expand Up @@ -187,3 +196,16 @@ export const useProvideCheckpoints = (
checkpointsId: Id,
checkpoints: Checkpoints,
): void => useProvideThing(checkpointsId, checkpoints, 5);

export const usePersister: typeof usePersisterDecl = (
id?: Id,
): Persister | undefined => useThing(id, 12);

export const usePersisterOrPersisterById = (
persisterOrPersisterId?: PersisterOrPersisterId,
): Persister | undefined => useThingOrThingById(persisterOrPersisterId, 12);

export const useProvidePersister = (
persisterId: Id,
persister: Persister,
): void => useProvideThing(persisterId, persister, 6);
7 changes: 7 additions & 0 deletions src/ui-react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import type {
useMetricIds as useMetricIdsDecl,
useMetricListener as useMetricListenerDecl,
useMetricsIds as useMetricsIdsDecl,
usePersisterIds as usePersisterIdsDecl,
useQueriesIds as useQueriesIdsDecl,
useQueryIds as useQueryIdsDecl,
useRedoInformation as useRedoInformationDecl,
Expand Down Expand Up @@ -248,9 +249,12 @@ export {
useIndexesOrIndexesById,
useMetrics,
useMetricsOrMetricsById,
usePersister,
usePersisterOrPersisterById,
useProvideCheckpoints,
useProvideIndexes,
useProvideMetrics,
useProvidePersister,
useProvideQueries,
useProvideRelationships,
useProvideStore,
Expand Down Expand Up @@ -1909,6 +1913,9 @@ export const useCreatePersister: typeof useCreatePersisterDecl = <
return persister;
};

export const usePersisterIds: typeof usePersisterIdsDecl = () =>
useThingIds(13);

export const useCreateSynchronizer: typeof useCreateSynchronizerDecl = <
SynchronizerOrUndefined extends Synchronizer | undefined,
>(
Expand Down

0 comments on commit 9c688bd

Please sign in to comment.