-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added tools for creating controllers and view controllers which…
… are compatible with Ditox.js dependency container.
- Loading branch information
Showing
10 changed files
with
646 additions
and
123 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { declareModule, Token } from 'ditox'; | ||
import { DependencyModule, useDependencyContainer } from 'ditox-react'; | ||
import React, { FC, useEffect, useMemo } from 'react'; | ||
import { Controller, ControllerFactory } from 'rx-effects'; | ||
|
||
type AnyObject = Record<string, any>; | ||
|
||
export function useInjectableController<Result extends Record<string, unknown>>( | ||
factory: ControllerFactory<Result>, | ||
): Controller<Result> { | ||
const container = useDependencyContainer('strict'); | ||
const controller = useMemo(() => factory(container), [container, factory]); | ||
|
||
useEffect(() => () => controller.destroy(), [controller]); | ||
|
||
return controller; | ||
} | ||
|
||
export const useViewController = useInjectableController; | ||
|
||
export function useControllerFactory<Args extends unknown[], Result>( | ||
factoryDeclaration: (...args: Args) => ControllerFactory<Controller<Result>>, | ||
...args: Args | ||
): Controller<Result> { | ||
const controllerFactory = useMemo( | ||
() => factoryDeclaration(...args), | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[factoryDeclaration, ...args], | ||
); | ||
|
||
return useInjectableController<Controller<Result>>(controllerFactory); | ||
} | ||
|
||
export function createViewControllerContainer<T extends AnyObject>( | ||
token: Token<T>, | ||
factory: ControllerFactory<T>, | ||
): FC { | ||
const module = declareModule({ factory, token }); | ||
|
||
return ({ children }) => ( | ||
<DependencyModule module={module}>{children}</DependencyModule> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "react" | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Container, injectable, Token } from 'ditox'; | ||
import { Controller } from './controller'; | ||
import { createScope, Scope } from './scope'; | ||
|
||
export function createController<T>( | ||
factory: (scope: Scope) => T & { destroy?: () => void }, | ||
): Controller<T> { | ||
const scope = createScope(); | ||
|
||
const controller = factory(scope); | ||
|
||
return { | ||
...controller, | ||
|
||
destroy: () => { | ||
controller?.destroy?.(); | ||
scope.destroy(); | ||
}, | ||
}; | ||
} | ||
|
||
export type ControllerFactory<Result> = ( | ||
container: Container, | ||
) => Controller<Result>; | ||
|
||
export type InferredService<Factory> = Factory extends ControllerFactory< | ||
infer Service | ||
> | ||
? Service | ||
: never; | ||
|
||
declare type ValuesProps = { | ||
[key: string]: unknown; | ||
}; | ||
declare type TokenProps<Props extends ValuesProps> = { | ||
[K in keyof Props]: Token<Props[K]>; | ||
}; | ||
|
||
export function createInjectableController<Props extends ValuesProps, Result>( | ||
tokens: TokenProps<Props>, | ||
factory: (scope: Scope, props: Props) => Result, | ||
): ControllerFactory<Result> { | ||
return injectable( | ||
(props) => createController((scope) => factory(scope, props as Props)), | ||
tokens, | ||
); | ||
} | ||
|
||
export const createViewController = createInjectableController; | ||
|
||
export function declareControllerFactory<Result, Args extends unknown[]>( | ||
factory: (...args: Args) => ControllerFactory<Result>, | ||
): (...args: Args) => ControllerFactory<Result> { | ||
return factory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters