-
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): new contextConsumer interface
- Loading branch information
Showing
1 changed file
with
135 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,135 @@ | ||
import {Stringifyable, OmitFirstParam} from '@alwatr/type'; | ||
|
||
import {signalManager} from './signal-manager.js'; | ||
|
||
/** | ||
* Context consumer interface. | ||
*/ | ||
export const contextConsumer = { | ||
/** | ||
* Get context value. | ||
* | ||
* Return undefined if context not set before or expired. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const currentProductList = contextConsumer.getValue<ProductListType>('product-list'); | ||
* if (currentProductList === undefined) { | ||
* // productList not set before or expired. | ||
* } | ||
* ``` | ||
*/ | ||
getValue: signalManager.getDetail, | ||
|
||
/** | ||
* Waits until the context value changes. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const newProductList = await contextConsumer.untilChange<ProductListType>('product-list'); | ||
* ``` | ||
*/ | ||
untilChange: signalManager.untilNext, | ||
|
||
/** | ||
* Subscribe to context changes, work like addEventListener. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const listener = contextConsumer.subscribe<ProductListType>('product-list', (productList) => { | ||
* console.log(productList); | ||
* }); | ||
* // ... | ||
* contextConsumer.unsubscribe(listener); | ||
* ``` | ||
*/ | ||
subscribe: signalManager.subscribe, | ||
|
||
/** | ||
* Unsubscribe from context changes, work like removeEventListener. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const listener = contextConsumer.subscribe<ProductListType>('product-list', (productList) => { | ||
* console.log(productList); | ||
* }); | ||
* // ... | ||
* contextConsumer.unsubscribe(listener); | ||
* ``` | ||
*/ | ||
unsubscribe: signalManager.unsubscribe, | ||
|
||
/** | ||
* Bind this interface to special context. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const productListConsumer = contextConsumer.bind<ProductListType>('product-list'); | ||
* ``` | ||
*/ | ||
bind: <T extends Stringifyable>(contextId: string) =>({ | ||
/** | ||
* Event signal Id. | ||
*/ | ||
id: contextId, | ||
|
||
/** | ||
* Get context value. | ||
* | ||
* Return undefined if context not set before or expired. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const currentProductList = productListConsumer.getValue(); | ||
* if (currentProductList === undefined) { | ||
* // productList not set before or expired. | ||
* } | ||
* ``` | ||
*/ | ||
getValue: signalManager.getDetail.bind(null, contextId) as OmitFirstParam<typeof signalManager.getDetail<T>>, | ||
|
||
/** | ||
* Waits until the context value changes. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const newProductList = await productListConsumer.untilChange(); | ||
* ``` | ||
*/ | ||
untilChange: signalManager.untilNext.bind(null, contextId) as OmitFirstParam<typeof signalManager.untilNext<T>>, | ||
|
||
/** | ||
* Subscribe to context changes, work like addEventListener. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const listener = productListConsumer.subscribe((productList) => console.log(productList)); | ||
* // ... | ||
* productListConsumer.unsubscribe(listener); | ||
* ``` | ||
*/ | ||
subscribe: signalManager.subscribe.bind(null, contextId) as unknown as | ||
OmitFirstParam<typeof signalManager.subscribe<T>>, | ||
|
||
/** | ||
* Unsubscribe from context changes, work like removeEventListener. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* const listener = productListConsumer.subscribe((productList) => console.log(productList)); | ||
* // ... | ||
* productListConsumer.unsubscribe(listener); | ||
* ``` | ||
*/ | ||
unsubscribe: signalManager.unsubscribe, | ||
}), | ||
} as const; |