From b08c553749f6619815c171175dd6bcf8ee6ab035 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Mon, 28 Apr 2025 22:25:28 +0300 Subject: Refactor for only the Node runtime. --- src/shapex.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/shapex.ts') diff --git a/src/shapex.ts b/src/shapex.ts index f15916d..1bd4b17 100644 --- a/src/shapex.ts +++ b/src/shapex.ts @@ -12,7 +12,11 @@ export type SubscriptionResponseDispatch = { * if you want to update state, and/or optionally also any events you * might want to dispatch. */ -export type SubscriptionResponse = { +export type SubscriptionResponse< + T, + W extends unknown = undefined, + D extends unknown = W +> = { state?: T; dispatch?: | SubscriptionResponseDispatch @@ -27,10 +31,11 @@ const isSubscriptionResponseList = ( * A callback passed to subcriptions, called when the event * that the subscription is listening to is called. */ -export type EventCallback = ( - state: T, - data?: W -) => SubscriptionResponse; +export type EventCallback< + T, + W extends unknown = undefined, + D extends unknown = W +> = (state: T, data?: W) => SubscriptionResponse; type Subscription = { listener: string; @@ -89,9 +94,7 @@ export type ShapeXInstance = { * @param {T extends object} initialState The initial application state. * @returns {ShapeXInstance} The ShapeX object. */ -export default function ShapeX( - initialState: T -): ShapeXInstance { +export function ShapeX(initialState: T): ShapeXInstance { let _state = initialState; const _subscriptions: Map< string, @@ -236,11 +239,17 @@ export default function ShapeX( } const scopedSubsriptions = _subscriptions.get(to) ?? []; - const remainingSubscriptions = [] as Array>; + const remainingSubscriptions = [] as Array< + Subscription + >; let callbackCount = 0; for (const subscription of scopedSubsriptions) { - const callback = subscription.callback as unknown as EventCallback; + const callback = subscription.callback as unknown as EventCallback< + T, + W, + unknown + >; const response = withData ? callback(_state, withData) : callback(_state); // Updates state, and checks for state changes, and if any changes present, -- cgit v1.2.3