@adimm/x-injection-reactjs 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- <h1 align="center">
2
- xInjection ReactJS <a href="https://www.npmjs.com/package/@adimm/x-injection-reactjs" target="__blank"><img src="https://badgen.net/npm/v/@adimm/x-injection-reactjs"></a>
1
+ <p align="center">
2
+ <img width="260px" height="auto" alt="xInjection Logo" src="https://raw.githubusercontent.com/AdiMarianMutu/x-injection/main/assets/logo.png"><br /><a href="https://www.npmjs.com/package/@adimm/x-injection-reactjs" target="__blank"><img src="https://badgen.net/npm/v/@adimm/x-injection-reactjs"></a>
3
3
  <a href="https://app.codecov.io/gh/AdiMarianMutu/x-injection-reactjs" target="__blank"><img src="https://badgen.net/codecov/c/github/AdiMarianMutu/x-injection-reactjs"></a>
4
4
  <img src="https://badgen.net/npm/license/@adimm/x-injection-reactjs">
5
- </h1>
5
+ </p>
6
6
 
7
7
  <p align="center">
8
8
  <a href="https://github.com/AdiMarianMutu/x-injection-reactjs/actions/workflows/ci.yml?query=branch%3Amain" target="__blank"><img src="https://github.com/AdiMarianMutu/x-injection-reactjs/actions/workflows/ci.yml/badge.svg?branch=main"></a>
package/dist/index.cjs CHANGED
@@ -127,4 +127,4 @@ function _({use: e, inject: r}) {
127
127
  };
128
128
  }
129
129
 
130
- c(_, "hookFactory");//# sourceMappingURL=index.cjs.map
130
+ c(_, "hookFactory");
package/dist/index.js CHANGED
@@ -117,4 +117,4 @@ function I({use: e, inject: r}) {
117
117
 
118
118
  r(I, "hookFactory");
119
119
 
120
- export { i as REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT, I as hookFactory, w as provideModuleToComponent, u as useComponentModule, m as useInject, a as useInjectMany };//# sourceMappingURL=index.js.map
120
+ export { i as REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT, I as hookFactory, w as provideModuleToComponent, u as useComponentModule, m as useInject, a as useInjectMany };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adimm/x-injection-reactjs",
3
3
  "description": "ReactJS integration of the `xInjection` library.",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "author": "Adi-Marian Mutu",
6
6
  "homepage": "https://github.com/AdiMarianMutu/x-injection-reactjs#readme",
7
7
  "bugs": "https://github.com/AdiMarianMutu/x-injection-reactjs/issues",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/react-context.ts","../src/core/hooks/use-component-module.ts","../src/core/hooks/use-inject.ts","../src/core/hooks/use-inject-many.ts","../src/core/provide-module-to-component.hoc.tsx","../src/helpers/hooks/use-make-or-get-component-module.ts","../src/helpers/hooks/use-effect-once.ts","../src/helpers/component-provider-module.ts","../src/core/hook-factory.ts","../src/errors/hook-factory.ts"],"sourcesContent":["export * from './core';\nexport type * from './types';\n","import { AppModule, IProviderModule } from '@adimm/x-injection';\nimport { createContext } from 'react';\n\nexport const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT = createContext<IProviderModule>(AppModule);\n","import type { IProviderModule } from '@adimm/x-injection';\nimport { useContext } from 'react';\n\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\n/** Can be used to retrieve the {@link IProviderModule | Module} from the current context. */\nexport function useComponentModule(): IProviderModule {\n return useContext(REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT);\n}\n","import type { ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve a single dependency.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param provider The {@link ProviderToken}.\n * @param options See {@link UseInjectOptions}.\n * @returns The resolved {@link T | dependency}.\n */\nexport function useInject<\n T,\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n>(provider: ProviderToken<T>, options?: UseInjectOptions<IsOptional, AsList>) {\n const componentModule = useComponentModule();\n\n return componentModule.get<T, IsOptional, AsList>(provider, options?.isOptional, options?.asList);\n}\n\nexport type UseInjectOptions<\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n> = {\n /**\n * When set to `false` an exception will be thrown when the supplied `ProviderToken` isn't bound.\n *\n * Defaults to `false`.\n */\n isOptional?: IsOptional;\n\n /**\n * Set to `true` if you need to retrieve _all_ the bound identifiers of the supplied `ProviderToken`.\n *\n * Defaults to `false`.\n */\n asList?: AsList;\n};\n","import type { ProviderIdentifier, ProviderModuleGetManyParam, ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve multiple dependencies at once.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param deps Either one or more {@link ProviderToken}.\n * @returns Tuple containing the {@link D | dependencies}.\n */\nexport function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken | ProviderIdentifier)[]>(\n ...deps: D\n) {\n const componentModule = useComponentModule();\n\n return componentModule.getMany<D>(...deps);\n}\n","import type { IProviderModule, ModuleOrBlueprint } from '@adimm/x-injection';\nimport React from 'react';\n\nimport { ComponentProviderModuleHelpers, useMakeOrGetComponentModule } from '../helpers';\nimport type { PropsWithModule } from '../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from './react-context';\n\nconst ComponentRenderer = React.memo(_ComponentRenderer);\n\n/**\n * Can be used to easily provide a {@link module} to a component.\n *\n * **Note:** _An error will be thrown if a `global` module is provided._\n *\n * @example\n * ```tsx\n * interface MyComponentProps {\n * firstName: string;\n * lastName: string;\n * }\n *\n * export const MyComponent = provideModuleToComponent<MyComponentProps>(\n * MyComponentModule,\n * ({ firstName, lastName }) => {\n * const service = useInject(MyComponentService);\n *\n * return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>\n * }\n * );\n *\n * function App() {\n * return <MyComponent firstName={'John'} lastName={'Doe'} />;\n * }\n * ```\n *\n * @param module The {@link ModuleOrBlueprint} which should be consumed by the {@link component}.\n * @returns The provided {@link toComponent | Component}.\n */\nexport function provideModuleToComponent<\n P extends Record<string, any>,\n C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>,\n>(module: ModuleOrBlueprint, component: ReactElementWithProviderModule<P>): C {\n return ((componentProps: PropsWithModule<P>) => {\n const moduleCtx = useMakeOrGetComponentModule(module, componentProps);\n\n return (\n <REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider value={moduleCtx}>\n <ComponentRenderer module={moduleCtx} componentProps={componentProps} component={component as any} />\n </REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider>\n );\n }) as any;\n}\n\nfunction _ComponentRenderer<P extends Record<string, any>>({\n module,\n component,\n componentProps,\n}: {\n module: IProviderModule;\n component: ReactElementWithProviderModule<P>;\n componentProps: P;\n}) {\n return <>{component(ComponentProviderModuleHelpers.forwardPropsWithModule(component, componentProps, module))}</>;\n}\n\nexport type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React.ReactNode;\n","import {\n InjectionProviderModuleError,\n ProviderModuleHelpers,\n ProviderTokenHelpers,\n type IProviderModule,\n type ModuleOrBlueprint,\n type ProviderModule,\n} from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport type { PropsWithModule } from '../../types';\nimport { useEffectOnce } from './use-effect-once';\n\nexport function useMakeOrGetComponentModule(\n originalModule: ModuleOrBlueprint,\n componentProps?: PropsWithModule<object>\n): IProviderModule {\n const componentModule = useMemo(() => {\n /* istanbul ignore next */\n const { module: forwardedModule, inject } = componentProps ?? {};\n const module = ProviderModuleHelpers.tryBlueprintToModule(forwardedModule ?? originalModule) as ProviderModule;\n\n if (module.options.isGlobal) {\n throw new InjectionProviderModuleError(module, `A 'global' module can't be supplied to a component!`);\n }\n\n inject?.forEach((provider) => {\n if (!module.hasProvider(provider)) {\n throw new InjectionProviderModuleError(\n module,\n `The [${ProviderTokenHelpers.providerTokenToString(provider)}] provider can't be replaced because it is not part of the component module!`\n );\n }\n\n module.update.removeProvider(ProviderTokenHelpers.toProviderIdentifier(provider));\n module.update.addProvider(provider);\n });\n\n return module;\n }, [originalModule, componentProps?.inject]);\n\n useEffectOnce(() => {\n return () => {\n componentModule.dispose();\n };\n });\n\n return componentModule;\n}\n","import { useEffect, useRef, useState } from 'react';\n\n// Credits: https://stackoverflow.com/a/74000921\n\n/** Custom {@link useEffect} hook which will be run once. _(In `StrictMode` as well)_ */\nexport function useEffectOnce(effect: () => React.EffectCallback) {\n const destroyFunc = useRef<React.EffectCallback>(undefined);\n const effectCalled = useRef(false);\n const renderAfterCalled = useRef(false);\n const [, forceRerender] = useState(0);\n\n if (effectCalled.current) renderAfterCalled.current = true;\n\n useEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect();\n effectCalled.current = true;\n }\n\n // this forces one render after the effect is run\n forceRerender((x) => x + 1);\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) return;\n\n destroyFunc.current?.();\n };\n }, []);\n}\n","import { isFunction, type ModuleOrBlueprint } from '@adimm/x-injection';\n\nimport type { ReactElementWithProviderModule } from '../core';\nimport type { PropsWithModule } from '../types';\n\nexport namespace ComponentProviderModuleHelpers {\n export function forwardPropsWithModule<P extends Record<string, any>>(\n component: ReactElementWithProviderModule<P> | React.ReactElement,\n props: Record<string, any>,\n module: ModuleOrBlueprint\n ): PropsWithModule<P> {\n const isReactElement = typeof component === 'object' && 'type' in component;\n\n const result = {\n ...props,\n } as any;\n\n if ((isReactElement && isFunction(component.type)) || isFunction(component)) {\n result['module'] = module;\n }\n\n return result;\n }\n}\n","import type { ProviderToken } from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport { InjectionHookFactoryError } from '../errors';\nimport { useComponentModule } from './hooks';\n\nexport function hookFactory<P extends HookParams, D extends any[], T>({\n use: hook,\n inject,\n}: HookFactoryParams<P, D, T>): (p: P) => T {\n return (p: P) => {\n const componentModule = useComponentModule();\n\n const deps = useMemo(() => {\n if (inject.length === 0) {\n throw new InjectionHookFactoryError(componentModule, `The 'deps' property array is missing!`);\n }\n\n return componentModule.getMany(...inject);\n }, [inject]);\n\n return hook({ ...p, deps: [...deps] } as any);\n };\n}\n\nexport interface HookFactoryParams<P extends HookParams, D extends any[], T> {\n use: (p: HookWithDeps<P, D>) => T;\n inject: ProviderToken[];\n}\n\nexport type HookWithDeps<P extends HookParams, D extends any[]> = P & {\n /** Array containing the resolved dependencies. */\n deps: D;\n};\n\ntype HookParams = Record<string, any> | void;\n","import { InjectionProviderModuleError } from '@adimm/x-injection';\n\nexport class InjectionHookFactoryError extends InjectionProviderModuleError {\n override name = InjectionHookFactoryError.name;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;ACAA,yBAA2C;AAC3C,mBAA8B;AAEvB,IAAMA,gDAA4CC,4BAA+BC,4BAAAA;;;ACFxF,IAAAC,gBAA2B;AAKpB,SAASC,qBAAAA;AACd,aAAOC,0BAAWC,yCAAAA;AACpB;AAFgBF;;;ACQT,SAASG,UAIdC,UAA4BC,SAA8C;AAC1E,QAAMC,kBAAkBC,mBAAAA;AAExB,SAAOD,gBAAgBE,IAA2BJ,UAAUC,SAASI,YAAYJ,SAASK,MAAAA;AAC5F;AARgBP;;;ACDT,SAASQ,iBACXC,MAAO;AAEV,QAAMC,kBAAkBC,mBAAAA;AAExB,SAAOD,gBAAgBE,QAAO,GAAOH,IAAAA;AACvC;AANgBD;;;ACZhB,IAAAK,gBAAkB;;;ACDlB,IAAAC,sBAOO;AACP,IAAAC,gBAAwB;;;ACRxB,IAAAC,gBAA4C;AAKrC,SAASC,cAAcC,QAAkC;AAC9D,QAAMC,kBAAcC,sBAA6BC,MAAAA;AACjD,QAAMC,mBAA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IACpC,GAAG;MAACA;KAAO;AAEX,WAAOD,KAAK;MAAE,GAAGE;MAAGG,MAAM;WAAIA;;IAAM,CAAA;EACtC;AACF;AAjBgBP;","names":["REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","createContext","AppModule","import_react","useComponentModule","useContext","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","useInject","provider","options","componentModule","useComponentModule","get","isOptional","asList","useInjectMany","deps","componentModule","useComponentModule","getMany","import_react","import_x_injection","import_react","import_react","useEffectOnce","effect","destroyFunc","useRef","undefined","effectCalled","renderAfterCalled","forceRerender","useState","current","useEffect","x","useMakeOrGetComponentModule","originalModule","componentProps","componentModule","useMemo","module","forwardedModule","inject","ProviderModuleHelpers","tryBlueprintToModule","options","isGlobal","InjectionProviderModuleError","forEach","provider","hasProvider","ProviderTokenHelpers","providerTokenToString","update","removeProvider","toProviderIdentifier","addProvider","useEffectOnce","dispose","import_x_injection","ComponentProviderModuleHelpers","forwardPropsWithModule","component","props","module","isReactElement","result","isFunction","type","ComponentRenderer","React","memo","_ComponentRenderer","provideModuleToComponent","module","component","componentProps","moduleCtx","useMakeOrGetComponentModule","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","Provider","value","ComponentProviderModuleHelpers","forwardPropsWithModule","import_react","import_x_injection","InjectionHookFactoryError","InjectionProviderModuleError","name","hookFactory","use","hook","inject","p","componentModule","useComponentModule","deps","useMemo","length","InjectionHookFactoryError","getMany"]}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/react-context.ts","../src/core/hooks/use-component-module.ts","../src/core/hooks/use-inject.ts","../src/core/hooks/use-inject-many.ts","../src/core/provide-module-to-component.hoc.tsx","../src/helpers/hooks/use-make-or-get-component-module.ts","../src/helpers/hooks/use-effect-once.ts","../src/helpers/component-provider-module.ts","../src/core/hook-factory.ts","../src/errors/hook-factory.ts"],"sourcesContent":["import { AppModule, IProviderModule } from '@adimm/x-injection';\nimport { createContext } from 'react';\n\nexport const REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT = createContext<IProviderModule>(AppModule);\n","import type { IProviderModule } from '@adimm/x-injection';\nimport { useContext } from 'react';\n\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from '../react-context';\n\n/** Can be used to retrieve the {@link IProviderModule | Module} from the current context. */\nexport function useComponentModule(): IProviderModule {\n return useContext(REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT);\n}\n","import type { ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve a single dependency.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param provider The {@link ProviderToken}.\n * @param options See {@link UseInjectOptions}.\n * @returns The resolved {@link T | dependency}.\n */\nexport function useInject<\n T,\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n>(provider: ProviderToken<T>, options?: UseInjectOptions<IsOptional, AsList>) {\n const componentModule = useComponentModule();\n\n return componentModule.get<T, IsOptional, AsList>(provider, options?.isOptional, options?.asList);\n}\n\nexport type UseInjectOptions<\n IsOptional extends boolean | undefined = undefined,\n AsList extends boolean | undefined = undefined,\n> = {\n /**\n * When set to `false` an exception will be thrown when the supplied `ProviderToken` isn't bound.\n *\n * Defaults to `false`.\n */\n isOptional?: IsOptional;\n\n /**\n * Set to `true` if you need to retrieve _all_ the bound identifiers of the supplied `ProviderToken`.\n *\n * Defaults to `false`.\n */\n asList?: AsList;\n};\n","import type { ProviderIdentifier, ProviderModuleGetManyParam, ProviderToken } from '@adimm/x-injection';\n\nimport { useComponentModule } from './use-component-module';\n\n/**\n * Low-level hook which can be used to resolve multiple dependencies at once.\n *\n * **Note:** _In order to better modularize your code-base, you should strive to create custom hooks by using the_\n * _`hookFactory` method to compose a custom hook._\n *\n * @param deps Either one or more {@link ProviderToken}.\n * @returns Tuple containing the {@link D | dependencies}.\n */\nexport function useInjectMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken | ProviderIdentifier)[]>(\n ...deps: D\n) {\n const componentModule = useComponentModule();\n\n return componentModule.getMany<D>(...deps);\n}\n","import type { IProviderModule, ModuleOrBlueprint } from '@adimm/x-injection';\nimport React from 'react';\n\nimport { ComponentProviderModuleHelpers, useMakeOrGetComponentModule } from '../helpers';\nimport type { PropsWithModule } from '../types';\nimport { REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT } from './react-context';\n\nconst ComponentRenderer = React.memo(_ComponentRenderer);\n\n/**\n * Can be used to easily provide a {@link module} to a component.\n *\n * **Note:** _An error will be thrown if a `global` module is provided._\n *\n * @example\n * ```tsx\n * interface MyComponentProps {\n * firstName: string;\n * lastName: string;\n * }\n *\n * export const MyComponent = provideModuleToComponent<MyComponentProps>(\n * MyComponentModule,\n * ({ firstName, lastName }) => {\n * const service = useInject(MyComponentService);\n *\n * return <h1>Hello {service.computeUserName(firstName, lastName)}!</h1>\n * }\n * );\n *\n * function App() {\n * return <MyComponent firstName={'John'} lastName={'Doe'} />;\n * }\n * ```\n *\n * @param module The {@link ModuleOrBlueprint} which should be consumed by the {@link component}.\n * @returns The provided {@link toComponent | Component}.\n */\nexport function provideModuleToComponent<\n P extends Record<string, any>,\n C extends ReactElementWithProviderModule<P> = ReactElementWithProviderModule<P>,\n>(module: ModuleOrBlueprint, component: ReactElementWithProviderModule<P>): C {\n return ((componentProps: PropsWithModule<P>) => {\n const moduleCtx = useMakeOrGetComponentModule(module, componentProps);\n\n return (\n <REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider value={moduleCtx}>\n <ComponentRenderer module={moduleCtx} componentProps={componentProps} component={component as any} />\n </REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT.Provider>\n );\n }) as any;\n}\n\nfunction _ComponentRenderer<P extends Record<string, any>>({\n module,\n component,\n componentProps,\n}: {\n module: IProviderModule;\n component: ReactElementWithProviderModule<P>;\n componentProps: P;\n}) {\n return <>{component(ComponentProviderModuleHelpers.forwardPropsWithModule(component, componentProps, module))}</>;\n}\n\nexport type ReactElementWithProviderModule<P extends Record<string, any>> = (p: PropsWithModule<P>) => React.ReactNode;\n","import {\n InjectionProviderModuleError,\n ProviderModuleHelpers,\n ProviderTokenHelpers,\n type IProviderModule,\n type ModuleOrBlueprint,\n type ProviderModule,\n} from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport type { PropsWithModule } from '../../types';\nimport { useEffectOnce } from './use-effect-once';\n\nexport function useMakeOrGetComponentModule(\n originalModule: ModuleOrBlueprint,\n componentProps?: PropsWithModule<object>\n): IProviderModule {\n const componentModule = useMemo(() => {\n /* istanbul ignore next */\n const { module: forwardedModule, inject } = componentProps ?? {};\n const module = ProviderModuleHelpers.tryBlueprintToModule(forwardedModule ?? originalModule) as ProviderModule;\n\n if (module.options.isGlobal) {\n throw new InjectionProviderModuleError(module, `A 'global' module can't be supplied to a component!`);\n }\n\n inject?.forEach((provider) => {\n if (!module.hasProvider(provider)) {\n throw new InjectionProviderModuleError(\n module,\n `The [${ProviderTokenHelpers.providerTokenToString(provider)}] provider can't be replaced because it is not part of the component module!`\n );\n }\n\n module.update.removeProvider(ProviderTokenHelpers.toProviderIdentifier(provider));\n module.update.addProvider(provider);\n });\n\n return module;\n }, [originalModule, componentProps?.inject]);\n\n useEffectOnce(() => {\n return () => {\n componentModule.dispose();\n };\n });\n\n return componentModule;\n}\n","import { useEffect, useRef, useState } from 'react';\n\n// Credits: https://stackoverflow.com/a/74000921\n\n/** Custom {@link useEffect} hook which will be run once. _(In `StrictMode` as well)_ */\nexport function useEffectOnce(effect: () => React.EffectCallback) {\n const destroyFunc = useRef<React.EffectCallback>(undefined);\n const effectCalled = useRef(false);\n const renderAfterCalled = useRef(false);\n const [, forceRerender] = useState(0);\n\n if (effectCalled.current) renderAfterCalled.current = true;\n\n useEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect();\n effectCalled.current = true;\n }\n\n // this forces one render after the effect is run\n forceRerender((x) => x + 1);\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) return;\n\n destroyFunc.current?.();\n };\n }, []);\n}\n","import { isFunction, type ModuleOrBlueprint } from '@adimm/x-injection';\n\nimport type { ReactElementWithProviderModule } from '../core';\nimport type { PropsWithModule } from '../types';\n\nexport namespace ComponentProviderModuleHelpers {\n export function forwardPropsWithModule<P extends Record<string, any>>(\n component: ReactElementWithProviderModule<P> | React.ReactElement,\n props: Record<string, any>,\n module: ModuleOrBlueprint\n ): PropsWithModule<P> {\n const isReactElement = typeof component === 'object' && 'type' in component;\n\n const result = {\n ...props,\n } as any;\n\n if ((isReactElement && isFunction(component.type)) || isFunction(component)) {\n result['module'] = module;\n }\n\n return result;\n }\n}\n","import type { ProviderToken } from '@adimm/x-injection';\nimport { useMemo } from 'react';\n\nimport { InjectionHookFactoryError } from '../errors';\nimport { useComponentModule } from './hooks';\n\nexport function hookFactory<P extends HookParams, D extends any[], T>({\n use: hook,\n inject,\n}: HookFactoryParams<P, D, T>): (p: P) => T {\n return (p: P) => {\n const componentModule = useComponentModule();\n\n const deps = useMemo(() => {\n if (inject.length === 0) {\n throw new InjectionHookFactoryError(componentModule, `The 'deps' property array is missing!`);\n }\n\n return componentModule.getMany(...inject);\n }, [inject]);\n\n return hook({ ...p, deps: [...deps] } as any);\n };\n}\n\nexport interface HookFactoryParams<P extends HookParams, D extends any[], T> {\n use: (p: HookWithDeps<P, D>) => T;\n inject: ProviderToken[];\n}\n\nexport type HookWithDeps<P extends HookParams, D extends any[]> = P & {\n /** Array containing the resolved dependencies. */\n deps: D;\n};\n\ntype HookParams = Record<string, any> | void;\n","import { InjectionProviderModuleError } from '@adimm/x-injection';\n\nexport class InjectionHookFactoryError extends InjectionProviderModuleError {\n override name = InjectionHookFactoryError.name;\n}\n"],"mappings":";;;;AAAA,SAASA,iBAAkC;AAC3C,SAASC,qBAAqB;AAEvB,IAAMC,4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AA,MAAGD,UAAUO,+BAA+BC,uBAAuBR,WAAWC,gBAAgBF,MAAAA,CAAAA,CAAAA;AACvG;AAVSF;;;AIpDT,SAASY,WAAAA,gBAAe;;;ACDxB,SAASC,gCAAAA,qCAAoC;AAEtC,IAAMC,4BAAN,MAAMA,mCAAkCC,8BAAAA;EAF/C,OAE+CA;;;EACpCC,OAAOF,2BAA0BE;AAC5C;;;ADEO,SAASC,YAAsD,EACpEC,KAAKC,MACLC,OAAM,GACqB;AAC3B,SAAO,CAACC,MAAAA;AACN,UAAMC,kBAAkBC,mBAAAA;AAExB,UAAMC,OAAOC,SAAQ,MAAA;AACnB,UAAIL,OAAOM,WAAW,GAAG;AACvB,cAAM,IAAIC,0BAA0BL,iBAAiB,uCAAuC;MAC9F;AAEA,aAAOA,gBAAgBM,QAAO,GAAIR,MAAAA;IACpC,GAAG;MAACA;KAAO;AAEX,WAAOD,KAAK;MAAE,GAAGE;MAAGG,MAAM;WAAIA;;IAAM,CAAA;EACtC;AACF;AAjBgBP;","names":["AppModule","createContext","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","useContext","useComponentModule","useContext","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","useInject","provider","options","componentModule","useComponentModule","get","isOptional","asList","useInjectMany","deps","componentModule","useComponentModule","getMany","React","InjectionProviderModuleError","ProviderModuleHelpers","ProviderTokenHelpers","useMemo","useEffect","useRef","useState","useEffectOnce","effect","destroyFunc","useRef","undefined","effectCalled","renderAfterCalled","forceRerender","useState","current","useEffect","x","useMakeOrGetComponentModule","originalModule","componentProps","componentModule","useMemo","module","forwardedModule","inject","ProviderModuleHelpers","tryBlueprintToModule","options","isGlobal","InjectionProviderModuleError","forEach","provider","hasProvider","ProviderTokenHelpers","providerTokenToString","update","removeProvider","toProviderIdentifier","addProvider","useEffectOnce","dispose","isFunction","ComponentProviderModuleHelpers","forwardPropsWithModule","component","props","module","isReactElement","result","isFunction","type","ComponentRenderer","React","memo","_ComponentRenderer","provideModuleToComponent","module","component","componentProps","moduleCtx","useMakeOrGetComponentModule","REACT_X_INJECTION_PROVIDER_MODULE_CONTEXT","Provider","value","ComponentProviderModuleHelpers","forwardPropsWithModule","useMemo","InjectionProviderModuleError","InjectionHookFactoryError","InjectionProviderModuleError","name","hookFactory","use","hook","inject","p","componentModule","useComponentModule","deps","useMemo","length","InjectionHookFactoryError","getMany"]}