badmfck-signal
Version:
An implementation of a signaling mechanism used to connect components and transfer data between them
209 lines (208 loc) • 7.72 kB
TypeScript
/**
* Signal by Igor Bloom
* Copyright (C) 2014-2023
*
* An implementation of a signaling mechanism used to connect components and transfer data between them.
*
* How to use:
*
* Create an instance of Signal with string data type:
* export const S_TEST:Signal<string>=new Signal();
*
* Subscribe to signal, with id: test1
* S_TEST.subscribe(str=>{console.log(str)},"test1")
*
* Call invokation procedure
* S_TEST.invoke("test");
*
* Remove subscribtion, using id:
* S_TEST.remove("test1")
*
* Remove all subscribtions from signal:
* S_TEST.clear();
*
*/
import { Binder } from "./Binder";
import { DataProvider } from "./DataProvider";
export { Binder, DataProvider };
export declare class Signal<T> {
static nextID: number;
private busy;
private tempAdd;
private tempRem;
private tempInvoke;
private callbacks;
private tempClear;
name: string;
type: "signal" | "request" | "binder";
onSubscribe?: (callback?: (data: T) => void) => void;
static onLog?: (signal: Signal<any>, level: number, text: string) => void;
private static reactUseState;
private static reactUseEffect;
/**
* Setup react hook before using it
* @param useState - reference to react useState
* @param useEffect - reference to react useEffect
*/
static setupReact(useState: any, useEffect: any): void;
/**
* Use signal as react hook
* @param group string or Signal
* @param deps array of dependencies for react useEffect hook
* @param onInvoke callback, invokes when signalling data
* @returns
*/
static useSignal<T>(group?: string | Signal<any>, deps?: any[], onInvoke?: (data?: T | null) => void): T | null;
/**
* Create signal instance
* @param name @optional, signal name
* @param onSubscribe @optional, callback, fires when someone subscribe to signal
* @param onLog @optional, onLog callback, with level:number and text:string.
*/
constructor(name?: string, onSubscribe?: (callback?: (data: T) => void) => void, handler?: (data: T) => void);
/**
* Subscribe to signal
* @param callback will be called when the signal is invoked.
* @param id Optional parameter, callback identificator
* @returns id as string
*/
subscribe(callback: (data: T) => void, id?: string): string;
/**
* Use signal as react webhook, be sure to call Signal.setupReact(useState,useEffect) before.
* @optional @param dependensies - array of dependensies to use with useEffect hook,
* @example const test = S_TEST.use(); // when signal invokes, variable test will receive signalling data
* @returns dataobject with type <T>, or null
*/
use(dependensies?: any[], onInvke?: (data?: T | null) => void): T | null;
/**
* Get list of ids of subscribtions
* @returns array of subscribtion's ids
*/
getSubscribtions(): string[];
/**
* Removes all subscriptions to a signal
* @returns void
*/
removeAll(): void;
/**
* Remove subscribtion from signal
* @param id @optional uses to identify and find callback to remove
* @returns true if succsess
*/
unsubscribe(id: string): boolean;
/**
* Invoke signal, all callback will be called with providen data
* @param data Data Object passes to each callback
* @returns void
*/
invoke(data: T): void;
}
/**
* Uses to handle signal subscribtions
* add and remove callbacks from/to signals
* clear all signals in handler
*/
export declare class SignalHandler {
static nextID: number;
private id;
private signals;
constructor();
/**
* Add to Signal or group of signals callback
* @param signal Array of Signals or Signal
* @param cb callback to add to signal or to array of signals
* @returns void
*/
add<T>(signal: (Signal<T>) | Signal<T>[], cb: (data: T) => void): void;
/**
* Clear all subscribtions with all signals in handler
*/
clear(): void;
}
export declare class Req<T, K> {
private static reactUseState;
private static reactUseEffect;
/**
* Setup react hook before using it
* @param useState - reference to react useState
* @param useEffect - reference to react useEffect
*/
static setupReact(useState: any, useEffect: any): void;
private static nextID;
private worker?;
name: string;
type: "signal" | "request" | "binder";
constructor(worker?: (request: T) => Promise<K>, name?: string);
request(data: T): Promise<K>;
/**
* React hook for Req
* @param request Request data <T>
* @param dep dependecies for useEffect
* @param onChange invokes when Req change it value
* @returns data <K>
*/
use(request: T, dep?: any[], onChange?: (data?: K | null) => void): K | null;
/**
* React hook for Req, with initial value and ability to change value directly from component
* @param request Request data <T>
* @param dep dependencies for useEffect
* @param initialValue initial value
* @param onChange invokes when Req change it value
* @returns array, first item stored value, second item - callback to reset in hook req.value
*/
useValue(request: T, onChange?: (data?: K | null) => void, dep?: any[]): [K | null, (value: K) => void];
/**
* Complete Request hook <T,K>
* @param onChange - will fire when data available in request
* @param initialRequest - intial T request param
* @returns array: 0 - available request data, 1 - callback to execute request (T), 2 - callback to reset available request data (K), 3 - Busy indicator, a boolean value, true when Request gathering data, false - when request completed
*/
useRequest(onChange?: ((data?: K | null) => void) | null, initialRequest?: T): [K | null, (value: T) => void, (value: K) => void, boolean];
set listener(_listener: (request: T) => Promise<K>);
}
/**
* Subscribe to signalling group, using type to define group. Be sure to unsubscribe in time.
* @param callback callback function, will be called when signal invoking
* @param group Signal group
* @returns callback id, use it to unsubscribe
*/
export declare function s_subscribe(callback: (data?: any) => void, group?: string): string;
/**
* Subscribe to signalling group for once, after callback fires, unsubscribe automatically
* @param callback
* @param group @optional Signal group, if empty, will use global signalling pipe
* @returns void
*/
export declare function s_subscribeOnce(callback: (data?: any) => void, group?: string): void;
/**
* Unsubscribe from signal group by callback id
* @param id callback id
* @returns true if success
*/
export declare function s_unsubscribe(id: string): boolean;
/**
* Invoke all callback in signal
* @param data Data object passes to each callback
* @param group Signal group, if empty, fill invoke on global signalling pipeline
* @returns void
*/
export declare function s_invoke(data: any, group?: string): void;
/**
* Invoke all callback in signal and remove all of them after invokation.
* @param data Data object passes to each callback
* @param group Signal group, if empty, fill invoke on global signalling pipeline
* @returns void
*/
export declare function s_invokeOnce(data: any, group?: string): void;
/**
* Clear all callbacks in Signalling group
* @param group Signal group
* @returns
*/
export declare function s_removeAll(group?: string): void;
/**
* Destroy all signal types
*/
export declare function s_destroy(): void;
export declare function useSignal<T>(group?: string | Signal<any>): T | null | undefined;
export default Signal;