import type React from 'react'; import type {Checkpoints} from '../@types/checkpoints/index.d.ts'; import type {Id, Ids} from '../@types/common/index.d.ts'; import type {Indexes} from '../@types/indexes/index.d.ts'; import type {Metrics} from '../@types/metrics/index.d.ts'; import type {AnyPersister} from '../@types/persisters/index.d.ts'; import type {Queries} from '../@types/queries/index.d.ts'; import type {Relationships} from '../@types/relationships/index.d.ts'; import type {Store} from '../@types/store/index.d.ts'; import type {Synchronizer} from '../@types/synchronizers/index.d.ts'; import {IdObj, objGet, objIds} from '../common/obj.ts'; import {GLOBAL, isString, isUndefined} from '../common/other.ts'; import {createContext, useContext, useEffect, useRef} from '../common/react.ts'; import {TINYBASE} from '../common/strings.ts'; import type {Offsets} from './Provider.tsx'; export type Thing = | Store | Metrics | Indexes | Relationships | Queries | Checkpoints | AnyPersister | Synchronizer; export type ThingsByOffset = [ Store, Metrics, Indexes, Relationships, Queries, Checkpoints, AnyPersister, Synchronizer, ]; export type ContextValue = [ store?: Store, storesById?: {[storeId: Id]: Store}, metrics?: Metrics, metricsById?: {[metricsId: Id]: Metrics}, indexes?: Indexes, indexesById?: {[indexesId: Id]: Indexes}, relationships?: Relationships, relationshipsById?: {[relationshipsId: Id]: Relationships}, queries?: Queries, queriesById?: {[queriesId: Id]: Queries}, checkpoints?: Checkpoints, checkpointsById?: {[checkpointsId: Id]: Checkpoints}, persister?: AnyPersister, persistersById?: { [persisterId: Id]: AnyPersister; }, synchronizer?: Synchronizer, synchronizersById?: {[synchronizerId: Id]: Synchronizer}, addExtraThingById?: ( offset: Offset, id: string, thing: ThingsByOffset[Offset], owner: object, ) => void, delExtraThingById?: (offset: Offsets, id: string, owner: object) => void, ]; const TINYBASE_CONTEXT = TINYBASE + '_uirc'; export const Context: React.Context = (GLOBAL as any)[ TINYBASE_CONTEXT ] ? /*! istanbul ignore next */ (GLOBAL as any)[TINYBASE_CONTEXT] : ((GLOBAL as any)[TINYBASE_CONTEXT] = createContext([])); export const useThing = ( id: Id | undefined, offset: Offsets, ): UsedThing | undefined => { const contextValue = useContext(Context); return ( isUndefined(id) ? contextValue[offset * 2] : isString(id) ? objGet(contextValue[offset * 2 + 1] as IdObj, id) : id ) as UsedThing; }; export const useThings = ( offset: Offsets, ): IdObj => ({...useContext(Context)[offset * 2 + 1]}) as IdObj; export const useThingOrThingById = ( thingOrThingId: T | Id | undefined, offset: Offsets, ): T | undefined => { const thing = useThing(thingOrThingId as Id, offset); return isUndefined(thingOrThingId) || isString(thingOrThingId) ? (thing as T | undefined) : (thingOrThingId as T); }; export const useProvideThing = ( thingId: Id, thing: ThingsByOffset[Offset], offset: Offset, ): void => { const {16: addExtraThingById, 17: delExtraThingById} = useContext(Context); const owner = useRef({}).current; useEffect(() => { addExtraThingById?.(offset, thingId, thing, owner); return () => delExtraThingById?.(offset, thingId, owner); }, [addExtraThingById, thingId, thing, offset, delExtraThingById, owner]); }; export const useThingIds = (offset: Offsets): Ids => objIds((useContext(Context)[offset * 2 + 1] ?? {}) as IdObj);