Skip to content

Commit

Permalink
rf
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Oct 16, 2024
1 parent 175d0cf commit 8f87cfe
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfama",
"version": "1.3.6",
"version": "1.3.7",
"author": "Abhishiv Saxena<abhishiv@gmail.com>",
"license": "MIT",
"description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM",
Expand Down
4 changes: 2 additions & 2 deletions src/core/state/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const createSignal = <T = any>(val: T): Signal<T> => {
}
}

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;
Expand Down
42 changes: 21 additions & 21 deletions src/core/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
export const createStoreManager = <T>(
id: number,
observedObject: Record<any, any>
): StoreManager<T> {
): StoreManager<T> => {
const manager: StoreManager<T> = {
id: "store|" + id,
value: observedObject,
Expand All @@ -31,14 +31,14 @@ export function createStoreManager<T>(
createStoreSubscription(manager, cursor, token.wire),
};
return manager;
}
};

// Get the value from the store based on a cursor path
function createStoreSubscription<T>(
const createStoreSubscription = <T>(
manager: StoreManager,
cursor: StoreCursor,
wire: Wire
): any {
): any => {
const cursorPath = getCursor(cursor);
const encodedCursor = encodeCursor(cursorPath);
manager.wires.add(wire);
Expand All @@ -56,16 +56,16 @@ function createStoreSubscription<T>(
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",
Expand All @@ -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<Wire> {
): Set<Wire> => {
const matchingWires = new Set<Wire>();

manager.wires.forEach((wire) => {
Expand All @@ -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("/")
Expand All @@ -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("/");
Expand All @@ -150,7 +150,7 @@ function triggerStoreTasks(
});
}
});
}
};

//
// Function to adjust cursor paths for array changes
Expand All @@ -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,
Expand Down Expand Up @@ -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[] = [];

Expand All @@ -226,7 +226,7 @@ function adjustCursorsInSet(
}
});

return { toRemove, toAdd };
return { rm: toRemove, add: toAdd };
}

function isPathMatching(cursor: string[], changePath: string[]): boolean {
Expand Down
31 changes: 0 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,3 @@ export type UsePromiseResp<T = unknown, E = unknown> = {
$loading: Signal<boolean | undefined>;
$status: Signal<"loading" | "loaded" | "failed">;
};

export function usePromise<T = unknown, E = unknown>(
queryName: string,
utils: ComponentUtils,
promise: () => Promise<T>
): UsePromiseResp<T, E> {
const $loading = utils.signal<boolean | undefined>(
queryName + "/loading",
true
);
const $data = utils.signal<any>(queryName + "/data", null);
const $error = utils.signal<any>(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 };
}
14 changes: 1 addition & 13 deletions src/stdlib/Portal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,7 @@ export type PortalProps = {

export const Portal = component<PortalProps>(
"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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = unknown, V = unknown> = ObjPathProxy<T, T>;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/ts-object-path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ObjProxyArg<TRoot, T> =
| ObjPathProxy<TRoot, T>
| ((p: ObjPathProxy<TRoot, TRoot>) => ObjPathProxy<TRoot, T>);

export const PATH_FLAG = Symbol("Object path");
export const PATH_FLAG = Symbol();

export function createProxy<T>(
path: PropertyKey[] = [],
Expand Down

0 comments on commit 8f87cfe

Please sign in to comment.