servido
Version:
Versatile services for React ⚗️
64 lines (63 loc) • 4.5 kB
TypeScript
/** There are a lot of asynchronity related to services, and the ServiceExecution class is intended to make that clearer.
* A service execution exists in the root of every constructed service, which is set done as soon as the service has deconstructed.
*
* A child-execution is passed to the `asyncConstructor` method, which is set done when the method resolves or when the service has been deconstructed.
* A child-execution is also passed to the `getData`, `handleData`, and `handleDataError` config methods, which is set done as soon as the data has been retrieved and handled,
* hydration of the data has been requested, or when the service has been deconstructed.
*
* Now, when developing a service, the execution of the service can be very useful for other purposes. Child-executions can be created for all sorts of asynchronous
* and cancelable purposes. For instance, a form service may use a child-execution for its submission process, allowing the submission to be canceled mid-way, during validation or so,
* or if the service is deconstructed.
*/
export declare class ServiceExecution {
private _done?;
private _doneListeners?;
private key;
/** Returns a child execution context that will be done whenever its parent is done, or when it is done itself. Its state does not affect the state of its parent. */
nest(): ServiceExecution;
setDone(): void;
/** Listen to whenever the execution is done. Returns a function to stop listening. */
onDone(listener: ExecutionDoneListener): ExecutionDoneUnsubscriber;
/** Run actions for the execution. If any of the actions returns `void` or if the execution is done, the next action will not be run.
* Every action inherits the value returned from the previous action (unless that value is `void`). */
run<_1>(_1: () => _1 | Promise<_1>): Promise<void | _1>;
run<_1, _2>(_1: () => _1 | void | Promise<_1 | void>, _2: (_1: _1) => _2 | void | Promise<_2 | void>): Promise<_2 | void>;
run<_1, _2, _3>(_1: () => _1 | void | Promise<_1 | void>, _2: (_1: _1) => _2 | void | Promise<_2 | void>, _3: (_2: _2) => _3 | void | Promise<_3 | void>): Promise<_3 | void>;
run<_1, _2, _3, _4>(_1: () => _1 | void | Promise<_1 | void>, _2: (_1: _1) => _2 | void | Promise<_2 | void>, _3: (_2: _2) => _3 | void | Promise<_3 | void>, _4: (_3: _3) => _4 | void | Promise<_4 | void>): Promise<_4 | void>;
run<_1, _2, _3, _4, _5>(_1: () => _1 | void | Promise<_1 | void>, _2: (_1: _1) => _2 | void | Promise<_2 | void>, _3: (_2: _2) => _3 | void | Promise<_3 | void>, _4: (_3: _3) => _4 | void | Promise<_4 | void>, _5: (_4: _4) => _5 | void | Promise<_5 | void>): Promise<_5 | void>;
/** Resolves to whenever the execution is done. */
get promise(): Promise<void>;
/** The current state of the execution. */
get done(): boolean;
protected toString(): string;
static nest<ET extends ServiceExecution>(parent: ServiceExecution, child?: ET): ET;
static nestMany<ET extends ServiceExecution>(parents: ServiceExecution[], child?: ET): ET;
}
interface ExecutionDoneListener {
(): void;
}
interface ExecutionDoneUnsubscriber {
(): boolean;
}
/** It is oftentime useful to declare a slot for a category of executions. For instance, a service might have
* a method that uses an execution but that should only be processing once. It simply features that if there is a current
* pending execution in the slot when a new execution is set, the previous execution is set done. It also provides the getter `promise`
* which returns a promise that is resolved when the last execution is done. */
export declare class ServiceExecutionSlot {
private _current;
constructor();
set(execution?: ServiceExecution): ServiceExecution;
setDone(): void;
get current(): ServiceExecution;
get done(): boolean;
get promise(): Promise<void>;
/** Returns a service execution slot to be used inside a component. If deps change, a new execution slot will be constructed and the previous one will be set to done.
* Additionally, the current execution is set to done on unmount. */
static use(deps?: readonly any[]): ServiceExecutionSlot;
}
export declare class ServiceDataExecution extends ServiceExecution {
nest(): any;
static from(parent: ServiceExecution): ServiceExecution | ServiceDataExecution;
static fromMany(parents: ServiceExecution[]): ServiceDataExecution;
}
export {};