Skip to content

Commit

Permalink
make naming better
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Jun 24, 2024
1 parent 3ae32f4 commit 78612e7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 68 deletions.
46 changes: 26 additions & 20 deletions src/core/state/experimental.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
//import { Observable, Change } from "@gullerya/object-observer";
import onChange, { ApplyData } from "on-change";
import {
wrapWithProxy,
StoreProxy,
isProxy,
getProxyPath,
getProxyMeta,
wrapWithCursorProxy,
CursorProxy,
isCursorProxy,
getCursor,
getCursorProxyMeta,
ObjPathProxy,
} from "../../utils/observer";
getValueUsingPath,
} from "../../utils/index";
import * as Constants from "../constants";
import { getValueUsingPath } from "../../utils/index";

export type { ObjPathProxy } from "../../utils/observer";
export { getProxyMeta, getProxyPath } from "../../utils/observer";
export type { ObjPathProxy } from "../../utils/index";
export {
getCursorProxyMeta as getProxyMeta,
getCursor as getProxyPath,
} from "../../utils/index";

export type Signal<T = unknown> = {
id: string;
Expand All @@ -30,7 +33,7 @@ export type Signal<T = unknown> = {
value: T;
};

export type StoreCursor<T = unknown, V = StoreManager<T>> = StoreProxy<T, V>;
export type StoreCursor<T = unknown, V = StoreManager<T>> = CursorProxy<T, V>;
type extractGeneric<Type> = Type extends ObjPathProxy<unknown, infer X>
? X
: never;
Expand Down Expand Up @@ -138,7 +141,7 @@ const runWire = (
token: SubToken,
subWireFactory: WireFactory
) => {
if (isProxy(arg)) {
if (isCursorProxy(arg)) {
const cursor = arg as StoreCursor;
const v = token(cursor);
token.wire.value = v;
Expand Down Expand Up @@ -265,7 +268,7 @@ export const createStore = <T = unknown>(
(p, value, previousValue, change) => {
const changePath = p.split(".");
const toRun = new Set<Wire>();
console.log("chamge", change, changePath);
//console.log("chamge", change, changePath);
// todo: improve this logic
const manager = storeManager as StoreManager;
for (const wire of manager.wires) {
Expand All @@ -291,7 +294,7 @@ export const createStore = <T = unknown>(
_runWires(toRun);

[...manager.tasks].forEach(({ path, observor }) => {
console.log({ path, changePath, change });
//console.log({ path, changePath, change });
if (path.join("/") === changePath.join("/")) {
observor({ data: change, path: changePath, value });
}
Expand All @@ -301,8 +304,11 @@ export const createStore = <T = unknown>(
},
{}
);
console.log(observedObject);
const s = wrapWithProxy<T, StoreManager<T>>(observedObject, storeManager);
//console.log(observedObject);
const s = wrapWithCursorProxy<T, StoreManager<T>>(
observedObject,
storeManager
);

storeManager.value = observedObject;
STORE_COUNTER++;
Expand All @@ -312,8 +318,8 @@ export const createStore = <T = unknown>(

export const reify = <T = unknown>(cursor: T): extractGeneric<T> => {
const s = cursor as unknown as StoreCursor;
const manager: StoreManager = getProxyMeta<StoreManager>(s);
const cursorPath = getProxyPath(s);
const manager: StoreManager = getCursorProxyMeta<StoreManager>(s);
const cursorPath = getCursor(s);
// console.log({ cursorPath, manager });
//console.log(JSON.stringify(manager.value));
const v = getValueUsingPath(manager.value as any, cursorPath);
Expand All @@ -337,11 +343,11 @@ const decodeCursor = (str: string) => str.split("/").map(decodeURIComponent);
const getSubtoken = (wire: Wire): SubToken => {
const token: Partial<SubToken> = (arg: Signal | StoreCursor) => {
//console.log("arg", arg);
if (isProxy(arg)) {
if (isCursorProxy(arg)) {
const cursor = arg as StoreCursor;
const cursorPath = getProxyPath(cursor);
const cursorPath = getCursor(cursor);
// todo: improve ts here and remove typecast
const manager = getProxyMeta<StoreManager>(cursor);
const manager = getCursorProxyMeta<StoreManager>(cursor);

const encodedCursor = encodeCursor(cursorPath);

Expand Down
12 changes: 4 additions & 8 deletions src/stdlib/Each/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {
import { h, component, Fragment } from "../../dom/index";
import { ComponentUtils, VElement } from "../../dom/types";
import { ParentWireContext } from "../../dom/index";
import { META_FLAG, ObjPathProxy } from "../../utils/observer";
import { META_FLAG, getCursor } from "../../utils/index";
import { TreeStep } from "../../dom/types";
import { getUtils, addNode, removeNode } from "../../dom/api";
import { reifyTree, getTreeStep } from "../../dom/traverser";
import { getProxyMeta, getProxyPath } from "../../utils/observer";
import { getValueUsingPath } from "../../utils/index";

type ArrayOrObject = Array<unknown> | { [key: string]: unknown };
Expand Down Expand Up @@ -55,19 +54,16 @@ export const Each: <T extends ArrayOrObject>(

const cursor = props.cursor;
const store: StoreManager = (cursor as any)[META_FLAG];
const path: string[] = getProxyPath(cursor);
console.log("path,", path);
const path: string[] = getCursor(cursor);

const value: any[] = getValueUsingPath(store.value as any, path) as any[];

console.log("sss", store);

const observor = function ({ data, path }: StoreChange) {
//console.debug("change", changes, path);

console.log("list change", data, path, value);
// console.log("list change", data, path, value);
if (data.name == "push") {
const index = data.result as number; // push returns index
const index = (data.result as number) - 1; // push returns index
const { treeStep, el } = renderArray(
parentStep,
props.renderItem,
Expand Down
42 changes: 42 additions & 0 deletions src/utils/cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
createProxy,
getPath,
ObjPathProxy,
PATH_FLAG,
} from "./ts-object-path";

export type { ObjPathProxy } from "./ts-object-path";

export { PATH_FLAG } from "./ts-object-path";
// used to save additional data in proxy
export const META_FLAG = Symbol("META_FLAG");

export type CursorProxy<T = unknown, V = unknown> = ObjPathProxy<T, T>;

export type CursorProxyInternal<T = unknown, V = unknown> = CursorProxy<
T,
V
> & {
[PATH_FLAG]: string[];
[META_FLAG]: V;
};

export const wrapWithCursorProxy = <T = unknown, V = unknown>(
obj: T,
meta: any
): CursorProxy<T, V> => {
const p = createProxy<T>([], {
[META_FLAG]: meta,
});
return p as CursorProxy<T, V>;
};

export const isCursorProxy = (proxy: unknown) =>
proxy && !!(proxy as CursorProxyInternal)[PATH_FLAG];

export const getCursor = (cursor: CursorProxy) => [
...(cursor as CursorProxyInternal)[PATH_FLAG],
];

export const getCursorProxyMeta = <T = unknown>(cursor: CursorProxy) =>
(cursor as CursorProxyInternal)[META_FLAG] as T;
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export const getValueUsingPath = (
record: Record<string, any>,
path: string[]
) => path.reduce((record, item) => record[item], record);

export * from "./crawl";
export * from "./cursor";
40 changes: 0 additions & 40 deletions src/utils/observer.ts

This file was deleted.

0 comments on commit 78612e7

Please sign in to comment.