diff --git a/core/signal/src/requestable-context-consumer.ts b/core/signal/src/requestable-context-consumer.ts new file mode 100644 index 000000000..60f20cac7 --- /dev/null +++ b/core/signal/src/requestable-context-consumer.ts @@ -0,0 +1,49 @@ +import {Stringifyable, OmitFirstParam} from '@alwatr/type'; + +import {contextConsumer} from './context-consumer.js'; +import {signalManager} from './signal-manager.js'; + +/** + * Requestable context consumer interface. + */ +export const requestableContextConsumer = { + ...contextConsumer, + + /** + * Send new context request to the provider. + * + * Example: + * + * ```ts + * requestableContextConsumer.requestContext('product-list', {foo: 'bar'}); + * const newProductList = await requestableContextConsumer.untilChange('product-list'); + * ``` + */ + request: signalManager.requestContext, + + /** + * Bind this interface to special context. + * + * Example: + * + * ```ts + * const productListConsumer = requestableContextConsumer.bind('product-list'); + * ``` + */ + bind: (contextId: string) =>({ + ...contextConsumer.bind(contextId), + + /** + * Send new context request to the provider. + * + * Example: + * + * ```ts + * productListConsumer.requestContext({foo: 'bar'}); + * const newProductList = await productListConsumer.untilChange(); + * ``` + */ + request: signalManager.requestContext as + OmitFirstParam>, + } as const), +} as const;