-
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): command trigger interface
- Loading branch information
Showing
1 changed file
with
48 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,48 @@ | ||
import {signalManager} from './signal-manager.js'; | ||
|
||
import type {OmitFirstParam, Stringifyable} from '@alwatr/type'; | ||
|
||
|
||
/** | ||
* Command trigger/request interface. | ||
*/ | ||
export const commandTrigger = { | ||
/** | ||
* Dispatch request command signal with commandArgument as detail and return untilNext of callback signal. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const returnObject = await commandTrigger.request<ArgumentType, ReturnType>('show-dialog', {foo: 'bar'}); | ||
* ``` | ||
*/ | ||
request: signalManager.requestCommand, | ||
|
||
/** | ||
* Bind define command to special command. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const showDialog = commandTrigger.bind<ArgumentType, ReturnType>('show-dialog'); | ||
* ``` | ||
*/ | ||
bind: <TArgument extends Record<string, Stringifyable>, TReturn extends Stringifyable>(commandId: string) =>({ | ||
/** | ||
* Command signal Id. | ||
*/ | ||
id: commandId, | ||
|
||
/** | ||
* Dispatch request command signal with commandArgument as detail and return untilNext of callback signal. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const returnObject = await showDialog.request({foo: 'bar'}); | ||
* ``` | ||
*/ | ||
request: signalManager.requestCommand.bind(null, commandId) as unknown as | ||
OmitFirstParam<typeof signalManager.setContextProvider<TArgument, TReturn>>, | ||
}), | ||
} as const; |