setup
setup<
T
>(target
:T
): (…setups
:SetupFunc
<T
>[]) =>DisposeFunc
Defined in: ext/setup.ts:98
Returns a function that applies multiple setups to a specific target and returns a disposer for all.
Type Parameters
Section titled “Type Parameters”Type Parameter |
---|
T |
Parameters
Section titled “Parameters”Parameter | Type | Description |
---|---|---|
target | T | The setup target |
Returns
Section titled “Returns”Function accepting setups, returning a disposer
(…
setups
:SetupFunc
<T
>[]):DisposeFunc
Parameters
Section titled “Parameters”Parameter | Type |
---|---|
…setups | SetupFunc <T >[] |
Returns
Section titled “Returns”Examples
Section titled “Examples”const machine1Setup = setup(machine1); machine1Setup(...extensions); // applies extensions to machine1 machine1Setup(anotherExtension); // can be called again for more setups
Use cases:- Managing setup/teardown for a single target with multiple extensions- Incrementally applying setups to a target
const machine = {};const setupA = (target: any) => () => console.log('A cleanup');const setupB = (target: any) => () => console.log('B cleanup');const machineSetup = setup(machine);const dispose = machineSetup(setupA, setupB);dispose(); // Logs 'B cleanup' then 'A cleanup'
Source
Section titled “Source”This function is useful for creating a setup routine that can be applied to a target, allowing for modular and reusable setup logic. It allows multiple setups to be combined into one, making it easier to manage complex initialization logic in applications. It is particularly useful in scenarios where multiple setups need to be applied to a single target, such as in state machines, event handlers, or other components that require setup and teardown logic
export const setup = <T>(target: T): ((...setups: SetupFunc<T>[]) => DisposeFunc) => (...setups: SetupFunc<T>[]) => createDisposer(setups.map((fn) => fn(target)));