Skip to content

Commit

Permalink
feat(signal): new requestable context provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 28, 2023
1 parent 7b6c820 commit 2c8d576
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
58 changes: 58 additions & 0 deletions core/signal/src/requestable-context-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {Stringifyable, OmitFirstParam} from '@alwatr/type';

import {contextProvider} from './context-provider.js';
import {signalManager} from './signal-manager.js';

/**
* Requestable context provider interface.
*/
export const requestableContextProvider = {
...contextProvider,

/**
* Defines the provider of the context signal that will be called when the context requested.
*
* subscribe to request context signal.
*
* Example:
*
* ```ts
* requestableContextProvider.setProvider<ProductListType, RequestParamType>(
* 'product-list',
* async (requestParam) => {
* return await fetchNewProductList(requestParam)
* },
* );
* ```
*/
setProvider: signalManager.setContextProvider,

/**
* Bind this interface to special context.
*
* Example:
*
* ```ts
* const productListProvider = requestableContextProvider.bind<ProductListType>('product-list');
* ```
*/
bind: <TContext extends Stringifyable, TRquest extends Stringifyable>(contextId: string) =>({
...contextProvider.bind<TContext>(contextId),

/**
* Defines the provider of the context signal that will be called when the context requested.
*
* subscribe to request context signal.
*
* Example:
*
* ```ts
* productListProvider.setProvider(async (requestParam) => {
* return await fetchNewProductList(requestParam)
* });
* ```
*/
setProvider: signalManager.setContextProvider.bind(null, contextId) as
OmitFirstParam<typeof signalManager.setContextProvider<TContext, TRquest>>,
} as const),
} as const;
8 changes: 4 additions & 4 deletions core/signal/src/signal-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,22 @@ export const signalManager = {
* signalManager.setContextProvider('content-change', async (requestParam) => await fetchNewContent(requestParam));
* ```
*/
setContextProvider: <TArgument extends Stringifyable, TReturn extends Stringifyable>(
setContextProvider: <TContext extends Stringifyable, TRquest extends Stringifyable>(
signalId: string,
signalProvider: ProviderFunction<TArgument, TReturn | void>,
signalProvider: ProviderFunction<TRquest, TContext | void>,
options: Partial<ProviderOptions> = {},
): void => {
options.debounce ??= 'AnimationFrame';
options.receivePrevious ??= 'AnimationFrame';
signalManager._logger.logMethodArgs('setContextProvider', {signalId, options});
const requestSignalId = 'request-' + signalId;
signalManager.removeAllListeners(requestSignalId);
signalManager.subscribe<TArgument>(
signalManager.subscribe<TRquest>(
requestSignalId,
async (argumentObject): Promise<void> => {
const signalDetail = await signalProvider(argumentObject);
if (signalDetail !== undefined) {
signalManager.dispatch<TReturn>(signalId, signalDetail, {debounce: options.debounce});
signalManager.dispatch<TContext>(signalId, signalDetail, {debounce: options.debounce});
}
},
{
Expand Down

0 comments on commit 2c8d576

Please sign in to comment.