-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(signal): event trigger interface
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
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,82 @@ | ||
import {Stringifyable, OmitFirstParam} from '@alwatr/type'; | ||
|
||
import {signalManager} from './signal-manager.js'; | ||
|
||
/** | ||
* Event signal trigger/dispatcher interface. | ||
*/ | ||
export const eventTrigger = { | ||
/** | ||
* Get last event dispatched detail. | ||
* | ||
* Return undefined if signal not dispatched before or expired. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const currentSize = eventTrigger.getLastDetail<ResizeType>('window-resize'); | ||
* if (currentSize === undefined) { | ||
* // event-signal not dispatched yet | ||
* } | ||
* ``` | ||
*/ | ||
getLastDetail: signalManager.getDetail, | ||
|
||
/** | ||
* Dispatch (send) signal to all listeners. | ||
* | ||
* Signal detail changed immediately without any debounce. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* eventTrigger.dispatch<ResizeType>('window-resize', newSize); | ||
* ``` | ||
*/ | ||
dispatch: signalManager.dispatch, | ||
|
||
/** | ||
* Bind this interface to special event. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const resizeEvent = eventTrigger.bind<ResizeType>('window-resize'); | ||
* ``` | ||
*/ | ||
bind: <T extends Stringifyable>(eventId: string) =>({ | ||
/** | ||
* Event signal Id. | ||
*/ | ||
id: eventId, | ||
|
||
/** | ||
* Get last event dispatched detail. | ||
* | ||
* Return undefined if signal not dispatched before or expired. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const currentSize = resizeEvent.getLastDetail(); | ||
* if (currentSize === undefined) { | ||
* // signal not dispatched yet | ||
* } | ||
* ``` | ||
*/ | ||
getLastDetail: signalManager.getDetail.bind(null, eventId) as OmitFirstParam<typeof signalManager.getDetail<T>>, | ||
|
||
/** | ||
* Dispatch (send) signal to all listeners. | ||
* | ||
* Signal detail changed immediately without any debounce. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* eventTrigger.dispatch<ResizeType>(newSize); | ||
* ``` | ||
*/ | ||
dispatch: signalManager.dispatch.bind(null, eventId) as OmitFirstParam<typeof signalManager.dispatch<T>>, | ||
}), | ||
} as const; |