Skip to content

Commit

Permalink
mod
Browse files Browse the repository at this point in the history
  • Loading branch information
irishcarbomb777 committed May 3, 2024
1 parent 13a91d6 commit 80975d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format": "npx prettier --write .",
"test": "npm run build && jest",
"verdaccio": "pm2 start $(which verdaccio) --name verdaccio",
"localpublish": "npm run build && git add . && git commit -m \"mod\" && npm unpublish --registry http://localhost:4873 && npm publish --registry http://localhost:4873"
"localpublish": "npm run build && git add . && git commit --amend --no-edit && npm unpublish --registry http://localhost:4873 && npm publish --registry http://localhost:4873"
},
"exports": {
".": {
Expand Down Expand Up @@ -62,4 +62,4 @@
"tsx": "^4.7.2",
"typescript": "^5.4.5"
}
}
}
10 changes: 3 additions & 7 deletions src/base/defineNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { TypeOf, ZodDefault, ZodObject, ZodTypeAny, z } from "zod";
import { TypeOf, ZodDefault, ZodObject, ZodOptional, ZodTypeAny, z } from "zod";


// // Note this will extract the actual zod type
type InferZodSchema<T> = {
[P in keyof T]: T[P] extends ZodDefault<infer U> ? U : never;
};
type UnwrapZodOptional<T extends ZodTypeAny> = T extends ZodOptional<infer U> ? U : T;

// type Concrete<Type extends Record<string, any>> = {
// [Property in keyof Type]: Type[Property] extends NonNullable<infer U> ? U : never;
Expand All @@ -13,7 +10,6 @@ type Concrete<T extends Record<string, any>> = {
[P in keyof T]: NonNullable<T[P]>;
};

type DefaultType<T> = T extends ZodTypeAny ? InferZodSchema<T> : never;
export class NodeDefinition<T extends string, StateDefinition extends ZodObject<any>, StateDefaults extends ZodObject<any> = ZodObject<{}>> {
nodeType: T;
stateDefinition: StateDefinition;
Expand All @@ -28,7 +24,7 @@ export class NodeDefinition<T extends string, StateDefinition extends ZodObject<
defaults<Defaults extends { [K in keyof TypeOf<StateDefinition>]?: TypeOf<StateDefinition>[K] }>(
defaults: Defaults
): NodeDefinition<T, StateDefinition, ZodObject<{
[K in keyof Defaults]: StateDefinition['shape'][K]
[K in keyof Defaults]: UnwrapZodOptional<StateDefinition['shape'][K]>
}>> {
const defaultsDefinition = Object.entries(this.stateDefinition.shape).reduce((acc, [key, value]) => ({
...acc,
Expand Down
55 changes: 28 additions & 27 deletions src/layers/ReactCache/defineReactCacheLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,43 @@ export const defineReactCacheLayer = <
nodeType: T,
node?: Node
) => ReturnType<typeof useImmer<Node extends UixNode<T, TypeOf<(N[number] & { nodeType: T })['stateDefinition']>>
? TypeOf<(N[number] & { nodeType: T })['stateDefinition']>
? (Omit<TypeOf<(N[number] & { nodeType: T })['stateDefinition']>, keyof TypeOf<(N[number] & { nodeType: T })['stateDefaults']>>
& TypeOf<(N[number] & { nodeType: T })['stateDefaults']>)
: TypeOf<(N[number] & { nodeType: T })['stateDefaults']>
>>
} => {

} => {
type ReactCache = {
nodeMap: Map<string, UixNode<any, any>>
} & Pick<
GraphLayer<N, R, E, UIdx, PreviousLayers | 'ReactCache'>,
'createNode' | 'updateNode'
>
const nodeStore = createStore<ReactCache>()(
immer(
(set) => ({
nodeMap: new Map(),
createNode: async (nodeType, initialState) => {
const createNodeResult = await graph.createNode(nodeType, initialState)
if (!createNodeResult.ok) return createNodeResult
const node = createNodeResult.val
set((state) => {
state.nodeMap.set(node.nodeId, node)
})
return Ok(node)
},
updateNode: async (nodeKey, state) => {
set((state) => {
state.nodeMap.set(nodeKey.nodeId, {
...state.nodeMap.get(nodeKey.nodeId)!,
...state
})
})
return await graph.updateNode(nodeKey, state)
},
})
)
)
// const nodeStore = createStore<ReactCache>()(
// immer(
// (set) => ({
// nodeMap: new Map(),
// createNode: async (nodeType, initialState) => {
// const createNodeResult = await graph.createNode(nodeType, initialState)
// if (!createNodeResult.ok) return createNodeResult
// const node = createNodeResult.val
// set((state) => {
// state.nodeMap.set(node.nodeId, node)
// })
// return Ok(node)
// },
// updateNode: async (nodeKey, state) => {
// set((state) => {
// state.nodeMap.set(nodeKey.nodeId, {
// ...state.nodeMap.get(nodeKey.nodeId)!,
// ...state
// })
// })
// return await graph.updateNode(nodeKey, state)
// },
// })
// )
// )
return {
...graph,
useNodeState: (nodeType, node) => {
Expand Down

0 comments on commit 80975d5

Please sign in to comment.