UNPKG

@hf-chimera/store

Version:

Cross-end reactivity API

1 lines 5.44 kB
{"version":3,"file":"react.cjs","names":["normalizeParams"],"sources":["../../packages/adapters/react/hooks.ts"],"sourcesContent":["import { useEffect, useMemo, useRef, useState } from \"react\";\nimport type { ChimeraCollectionQuery } from \"../../../src/query/ChimeraCollectionQuery\";\nimport type { ChimeraItemQuery } from \"../../../src/query/ChimeraItemQuery\";\nimport type { ChimeraEntityId } from \"../../../src/shared/types\";\nimport type { ChimeraEntityRepository } from \"../../../src/store/ChimeraEntityRepository\";\nimport type {\n\tAnyChimeraStore,\n\tChimeraStoreEntities,\n\tChimeraStoreEntityType,\n\tChimeraStoreOperatorMap,\n} from \"../../../src/store/ChimeraStore\";\nimport { type AnyChimeraParams, normalizeParams } from \"../shared/params\";\n\nconst CHIMERA_COLLECTION_UPDATE_EVENTS = [\n\t\"ready\",\n\t\"updated\",\n\t\"selfUpdated\",\n\t\"selfItemCreated\",\n\t\"itemAdded\",\n\t\"itemUpdated\",\n\t\"selfItemUpdated\",\n\t\"itemDeleted\",\n\t\"selfItemDeleted\",\n\t\"error\",\n] as const;\n\nconst CHIMERA_ITEM_UPDATE_EVENTS = [\n\t\"initialized\",\n\t\"selfCreated\",\n\t\"ready\",\n\t\"updated\",\n\t\"selfUpdated\",\n\t\"deleted\",\n\t\"selfDeleted\",\n\t\"error\",\n] as const;\n\nexport const createChimeraHooks = <T extends AnyChimeraStore>(\n\tstore: T,\n): {\n\tuseChimeraStore: () => T;\n\tuseChimeraRepository: <EntityName extends ChimeraStoreEntities<T>>(\n\t\tentityName: EntityName,\n\t) => ChimeraEntityRepository<ChimeraStoreEntityType<T, EntityName>, ChimeraStoreOperatorMap<T>>;\n\tuseChimeraCollection: <EntityName extends ChimeraStoreEntities<T>, Meta = any>(\n\t\tentityName: EntityName,\n\t\tparams: AnyChimeraParams<T, EntityName, Meta>,\n\t\tdeps?: unknown[],\n\t) => ChimeraCollectionQuery<ChimeraStoreEntityType<T, EntityName>, ChimeraStoreOperatorMap<T>>;\n\tuseChimeraItem: <EntityName extends ChimeraStoreEntities<T>, Meta = any>(\n\t\tentityName: EntityName,\n\t\tid: ChimeraEntityId,\n\t\tmeta?: Meta,\n\t) => ChimeraItemQuery<ChimeraStoreEntityType<T, EntityName>>;\n} => {\n\tconst useChimeraRepository = <EntityName extends ChimeraStoreEntities<T>>(entityName: EntityName) =>\n\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: this hook is generated for a specific store so it never changes\n\t\tuseMemo(() => store.from(entityName), [entityName]);\n\n\treturn {\n\t\tuseChimeraStore: () => store,\n\t\tuseChimeraRepository,\n\t\tuseChimeraCollection: (entityName, params, deps?) => {\n\t\t\tconst [, trigger] = useState(() => ({}));\n\n\t\t\tconst repository = useChimeraRepository(entityName);\n\n\t\t\tconst oldDeps = useRef(deps);\n\t\t\tif ((oldDeps.current && !deps) || (!oldDeps.current && deps)) {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t\"useChimeraCollection deps is not a reactive param!\\n\" +\n\t\t\t\t\t\t\"Use deps if you want to control dependencies manually.\\n\" +\n\t\t\t\t\t\t\"Omit it if you already have a stable reference to params\",\n\t\t\t\t);\n\t\t\t}\n\t\t\toldDeps.current = deps;\n\n\t\t\tconst memeParams = useMemo(\n\t\t\t\t() => normalizeParams(params),\n\t\t\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: Very unlikely it will be changed over time, anyway warning for this already added.\n\t\t\t\tdeps ? deps : [params],\n\t\t\t);\n\t\t\tconst collection = useMemo(() => repository.getCollection(memeParams), [repository, memeParams]);\n\n\t\t\tuseEffect(() => {\n\t\t\t\tconst handler = () => trigger({});\n\t\t\t\tfor (const event of CHIMERA_COLLECTION_UPDATE_EVENTS) {\n\t\t\t\t\tcollection.on(event, handler);\n\t\t\t\t}\n\t\t\t\treturn () => {\n\t\t\t\t\tfor (const event of CHIMERA_COLLECTION_UPDATE_EVENTS) {\n\t\t\t\t\t\tcollection.off(event, handler);\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}, [collection]);\n\n\t\t\treturn collection;\n\t\t},\n\t\tuseChimeraItem: (entityName, id, meta?) => {\n\t\t\tconst [, trigger] = useState(() => ({}));\n\n\t\t\tconst repository = useChimeraRepository(entityName);\n\t\t\tconst item = repository.getItem(id, meta);\n\n\t\t\tuseEffect(() => {\n\t\t\t\tconst handler = () => trigger({});\n\t\t\t\tfor (const event of CHIMERA_ITEM_UPDATE_EVENTS) {\n\t\t\t\t\titem.on(event, handler);\n\t\t\t\t}\n\t\t\t\treturn () => {\n\t\t\t\t\tfor (const event of CHIMERA_ITEM_UPDATE_EVENTS) {\n\t\t\t\t\t\titem.off(event, handler);\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}, [item]);\n\n\t\t\treturn item;\n\t\t},\n\t};\n};\n"],"mappings":";;;;;;;;AAaA,MAAM,mCAAmC;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AAED,MAAM,6BAA6B;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AAED,MAAa,sBACZ,UAgBI;CACJ,MAAM,wBAAoE,wCAE3D,MAAM,KAAK,WAAW,EAAE,CAAC,WAAW,CAAC;AAEpD,QAAO;EACN,uBAAuB;EACvB;EACA,uBAAuB,YAAY,QAAQ,SAAU;GACpD,MAAM,GAAG,sCAA2B,EAAE,EAAE;GAExC,MAAM,aAAa,qBAAqB,WAAW;GAEnD,MAAM,4BAAiB,KAAK;AAC5B,OAAK,QAAQ,WAAW,CAAC,QAAU,CAAC,QAAQ,WAAW,KACtD,SAAQ,KACP,uKAGA;AAEF,WAAQ,UAAU;GAElB,MAAM,sCACCA,+BAAgB,OAAO,EAE7B,OAAO,OAAO,CAAC,OAAO,CACtB;GACD,MAAM,sCAA2B,WAAW,cAAc,WAAW,EAAE,CAAC,YAAY,WAAW,CAAC;AAEhG,8BAAgB;IACf,MAAM,gBAAgB,QAAQ,EAAE,CAAC;AACjC,SAAK,MAAM,SAA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