UNPKG

badmfck-signal

Version:

An implementation of a signaling mechanism used to connect components and transfer data between them

55 lines (41 loc) 1.24 kB
#An implementation of a signaling mechanism used to connect components and transfer data between them How to use: Create an instance of Signal with prefered data type: const S_TEST:Signal<string>=new Signal() Subscribe to signal, obtain callback id. const callbackID = S_TEST.subscribe(str=>{console.log(str)}) Call invokation procedure S_TEST.invoke("test") Remove subscribtion, using id: S_TEST.remove("test1") Remove all subscribtions from signal: S_TEST.clear(); shortcuts: Subscribe to signalling pipe with name stored in group. group is optional, if not exists, global pipeline will use s_subscribe(group) s_unsubscribe(id) unsubscribe from signal The main principle is using one point in project to store all major interactions between sources without connectig them directly, also called as GlobalDispatcher or Spine. example: file: GlobalDispatcher.ts export class GD{ static S_SAVE_DATA:Signal<string>=new Signal(); } file: DataManager.ts export GD from './GD' export class DataManager{ constructor(){ GD.S_SAVE_DATA.subscribe(data=>{ console.log(`${data}` saved!) }) } } file: ViewController.ts export GD from './GD' button.onClick=()=>{ GD.S_SAVE_DATA.invoke("Test!") }