From d7bc505e49b44b15b487cad1f30a37384f704b72 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Fri, 18 Apr 2025 18:53:54 +0300 Subject: Improve documentation --- src/shapex.ts | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/shapex.ts b/src/shapex.ts index 30c8ef8..fc829a4 100644 --- a/src/shapex.ts +++ b/src/shapex.ts @@ -1,8 +1,17 @@ +/** + * Dispatches an event with a given name and passes on + * given arguments to it. + */ export type SubscriptionResponseDispatch = { eventName: string; args?: unknown[]; }; +/** + * A response of the subscription callback. Should return new state + * if you want to update state, and/or optionally also any events you + * might want to dispatch. + */ export type SubscriptionResponse = { state?: T; dispatch?: SubscriptionResponseDispatch | SubscriptionResponseDispatch[]; @@ -12,7 +21,14 @@ const isSubscriptionResponseList = ( dispatch: SubscriptionResponseDispatch | SubscriptionResponseDispatch[], ): dispatch is SubscriptionResponseDispatch[] => Array.isArray(dispatch); -export type EventCallback = (state: T, ...args: unknown[]) => SubscriptionResponse; +/** + * A callback passed to subcriptions, called when the event + * that the subscription is listening to is called. + */ +export type EventCallback = ( + state: T, + ...args: unknown[] +) => SubscriptionResponse; type Subscription = { listener: string; @@ -20,6 +36,9 @@ type Subscription = { once: boolean; }; +/** + * An instance of the ShapeX object. + */ export type ShapeXInstance = { subscribe: (listener: string, callback: EventCallback) => number; subscribeOnce: (listener: string, callback: EventCallback) => number; @@ -35,7 +54,9 @@ 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 default function ShapeX( + initialState: T, +): ShapeXInstance { let _state = initialState; const _subscriptions: Map[]> = new Map(); let subscriptionId = 0; @@ -68,7 +89,10 @@ export default function ShapeX(initialState: T): ShapeXInstanc * @param {EventCallback} callback * @returns */ - const subscribeOnce = (listener: string, callback: EventCallback): number => { + const subscribeOnce = ( + listener: string, + callback: EventCallback, + ): number => { if (!_subscriptions.has(listener)) { _subscriptions.set(listener, []); } @@ -95,7 +119,10 @@ export default function ShapeX(initialState: T): ShapeXInstanc * @param {T extends object} newState * @returns {string[]} The list of changes as array of paths. */ - const changedState = (oldState: T, newState: T): string[] => { + const changedState = ( + oldState: T, + newState: T, + ): string[] => { const paths = ( state: R, path: string, @@ -183,7 +210,10 @@ export default function ShapeX(initialState: T): ShapeXInstanc dispatch(dispatchee.eventName, ...(dispatchee.args ?? [])); } } else { - dispatch(response.dispatch.eventName, ...(response.dispatch.args ?? [])); + dispatch( + response.dispatch.eventName, + ...(response.dispatch.args ?? []), + ); } } -- cgit v1.2.3