Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNSET event in forest #682

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/forest/__tests__/forest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,38 @@ test("hard patch with selector replaces selected state", () => {
compTree!.nodes["comp1"]!.state["breakpoints"]["991"]["styles"]["height"]
).toBe("20px");
});

test("unset event", () => {
const forest = createForest(forestDef);
const compTree = forest.tree(componentTreeDef.modulePath);
forest.handleEvents({
name: "TEST_EVENTS",
events: [
{
type: `CREATE$$${componentTreeDef.modulePath}`,
id: "comp1",
meta: {},
state: {
parent: { id: "body", index: 0 },
},
},
{
type: `PATCH$$${componentTreeDef.modulePath}`,
id: "comp1",
slice: {
alias: "comp1Alias",
},
},
{
type: `UNSET$$${componentTreeDef.modulePath}`,
id: "comp1",
selector: ["parent", "id"],
},
],
meta: { agent: "server-sent" },
});
expect(compTree?.nodes).toBeDefined();
expect(compTree!.nodes["comp1"]!.state).toHaveProperty("parent");
expect(compTree!.nodes["comp1"]!.state.parent).toHaveProperty("index");
expect(compTree!.nodes["comp1"]!.state.parent).not.toHaveProperty("id");
});
26 changes: 26 additions & 0 deletions packages/forest/src/forest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TreeNode,
HardPatchEvent,
TreeDef,
UnsetEvent,
} from "./types";

function mergeStateCustomizer(obj: any, src: any) {
Expand Down Expand Up @@ -318,6 +319,31 @@ export function createForest(def: { trees: TreeDef[] }): Forest {
});
}
}
if (event.type.startsWith("UNSET")) {
const unsetEvent = event as UnsetEvent;
const treeId = unsetEvent.type.slice("UNSET$$".length);
const tree = treeMap[treeId]!;
const selector = unsetEvent.selector;
// store old state
const oldState = JSON.parse(
JSON.stringify(tree.nodes[unsetEvent.id]!.state)
);
let curr = tree.nodes[unsetEvent.id]!.state;
for (let i = 0; i < selector.length; i++) {
if (i === selector.length - 1) {
delete curr[selector[i]!];
break;
}
curr = curr[selector[i]!];
}
// emit change event
forestUpdateSubscribers.forEach((cb) => {
cb(
{ type: "change", id: unsetEvent.id, treeId, oldState },
{ name, meta }
);
});
}
}

function handleEvents(data: {
Expand Down
8 changes: 7 additions & 1 deletion packages/forest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ export type HardPatchEvent = {
selector?: string[];
} & EventDto;

export type UnsetEvent = {
id: string;
selector: string[];
} & EventDto;

export type AnyEvent =
| CreateEvent
| PatchEvent
| DeleteEvent
| LinkEvent
| UnlinkEvent
| HardPatchEvent;
| HardPatchEvent
| UnsetEvent;

export type TreeDefReturnType = {
validateCreate: (event: CreateEvent) => boolean;
Expand Down