diff --git a/package.json b/package.json index 0789381..9b5b6a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alfama", - "version": "1.3.6", + "version": "1.3.7", "author": "Abhishiv Saxena", "license": "MIT", "description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM", diff --git a/src/core/state/signal.ts b/src/core/state/signal.ts index 02f31b8..cdb64af 100644 --- a/src/core/state/signal.ts +++ b/src/core/state/signal.ts @@ -22,12 +22,12 @@ export const createSignal = (val: T): Signal => { } } - function set(value: T) { + const set = (value: T) => { if (sig.value === value) return value; sig.value = value; runWires(sig.wires); return val; - } + }; const sig: any = [get, set]; sig.id = "signal|" + SIGNAL_COUNTER; diff --git a/src/core/state/store.ts b/src/core/state/store.ts index 46efbf8..5db64d8 100644 --- a/src/core/state/store.ts +++ b/src/core/state/store.ts @@ -14,10 +14,10 @@ const encodeCursor = (cursor: string[]) => const decodeCursor = (str: string) => str.split("/").map(decodeURIComponent); // Create the store manager with essential functionalities -export function createStoreManager( +export const createStoreManager = ( id: number, observedObject: Record -): StoreManager { +): StoreManager => { const manager: StoreManager = { id: "store|" + id, value: observedObject, @@ -31,14 +31,14 @@ export function createStoreManager( createStoreSubscription(manager, cursor, token.wire), }; return manager; -} +}; // Get the value from the store based on a cursor path -function createStoreSubscription( +const createStoreSubscription = ( manager: StoreManager, cursor: StoreCursor, wire: Wire -): any { +): any => { const cursorPath = getCursor(cursor); const encodedCursor = encodeCursor(cursorPath); manager.wires.add(wire); @@ -56,16 +56,16 @@ function createStoreSubscription( console.log(wire, wire.storesRS, encodedCursor, manager.value); throw e; } -} +}; // Handle changes in the store and trigger associated tasks and wires -export function handleStoreChange( +export const handleStoreChange = ( manager: StoreManager, path: (string | symbol)[], newValue: any, oldValue: any, changeData: ApplyData -) { +) => { // if (path[0] !== "state") // console.log( // "handleStoreChange", @@ -80,14 +80,14 @@ export function handleStoreChange( const wiresToRun = findMatchingWires(manager, changePath, changeData); runWires(wiresToRun); triggerStoreTasks(manager, changePath, newValue, changeData); -} +}; // Find wires that match the change path -function findMatchingWires( +const findMatchingWires = ( manager: StoreManager, changePath: string[], changeData: ApplyData -): Set { +): Set => { const matchingWires = new Set(); manager.wires.forEach((wire) => { @@ -105,14 +105,14 @@ function findMatchingWires( }); return matchingWires; -} +}; // Determine if a cursor matches the change path -function matchCursorToChange( +const matchCursorToChange = ( cursor: string[], changePath: string[], changeData: ApplyData -): boolean { +): boolean => { if (changeData === undefined) { return cursor.length <= changePath.length ? encodeCursor(changePath.slice(0, cursor.length)) == cursor.join("/") @@ -124,15 +124,15 @@ function matchCursorToChange( } return false; -} +}; // Trigger tasks based on the change path -function triggerStoreTasks( +const triggerStoreTasks = ( manager: StoreManager, changePath: string[], newValue: any, changeData: ApplyData -) { +) => { manager.tasks.forEach(({ path, observor }) => { const isPathMatching = changePath.slice(0, path.length).join("/") === path.join("/"); @@ -150,7 +150,7 @@ function triggerStoreTasks( }); } }); -} +}; // // Function to adjust cursor paths for array changes @@ -171,7 +171,7 @@ function adjustCursorForArrayChange( manager.wires.forEach((wire) => { wire.storesRS.forEach((cursorSet) => { - const { toRemove, toAdd } = adjustCursorsInSet( + const { rm: toRemove, add: toAdd } = adjustCursorsInSet( cursorSet, changePath, start, @@ -205,7 +205,7 @@ function adjustCursorsInSet( start: number, deleteCount: number, items: any[] -): { toRemove: string[]; toAdd: string[] } { +): { rm: string[]; add: string[] } { const toRemove: string[] = []; const toAdd: string[] = []; @@ -226,7 +226,7 @@ function adjustCursorsInSet( } }); - return { toRemove, toAdd }; + return { rm: toRemove, add: toAdd }; } function isPathMatching(cursor: string[], changePath: string[]): boolean { diff --git a/src/index.ts b/src/index.ts index 4626e3a..76f1269 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,34 +15,3 @@ export type UsePromiseResp = { $loading: Signal; $status: Signal<"loading" | "loaded" | "failed">; }; - -export function usePromise( - queryName: string, - utils: ComponentUtils, - promise: () => Promise -): UsePromiseResp { - const $loading = utils.signal( - queryName + "/loading", - true - ); - const $data = utils.signal(queryName + "/data", null); - const $error = utils.signal(queryName + "/error", true); - const $status = utils.signal<"loading" | "loaded" | "failed">( - queryName + "/status", - "loading" - ); - promise() - .then((data) => { - $data.set(data); - $status.set("loaded"); - $loading.set(false); - }) - .catch((err) => { - console.log(err, err.message); - $data.set(undefined); - $error.set(err.message); - $status.set("failed"); - $loading.set(undefined); - }); - return { $data, $loading, $error, $status }; -} diff --git a/src/stdlib/Portal/index.tsx b/src/stdlib/Portal/index.tsx index 467a552..99be106 100644 --- a/src/stdlib/Portal/index.tsx +++ b/src/stdlib/Portal/index.tsx @@ -11,19 +11,7 @@ export type PortalProps = { export const Portal = component( "Portal", - ( - props, - { - wire, - setContext, - signal, - utils, - onUnmount, - onMount, - step: parentStep, - renderContext, - } - ) => { + (props, { onUnmount, step: parentStep }) => { onUnmount((step: any) => { if (step && step.dom) rmNodes(step.dom); }); diff --git a/src/utils/cursor.ts b/src/utils/cursor.ts index d26e785..1f00c6f 100644 --- a/src/utils/cursor.ts +++ b/src/utils/cursor.ts @@ -8,7 +8,7 @@ import { export type { ObjPathProxy } from "./ts-object-path"; export { PATH_FLAG } from "./ts-object-path"; -export const META_FLAG = Symbol("META_FLAG"); +export const META_FLAG = Symbol(); export type CursorProxy = ObjPathProxy; diff --git a/src/utils/ts-object-path/index.ts b/src/utils/ts-object-path/index.ts index acae4ec..9574ef4 100644 --- a/src/utils/ts-object-path/index.ts +++ b/src/utils/ts-object-path/index.ts @@ -6,7 +6,7 @@ export type ObjProxyArg = | ObjPathProxy | ((p: ObjPathProxy) => ObjPathProxy); -export const PATH_FLAG = Symbol("Object path"); +export const PATH_FLAG = Symbol(); export function createProxy( path: PropertyKey[] = [],