Skip to content

Commit

Permalink
Updated test case and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuntala-motukuri committed Jul 9, 2024
1 parent 8cb9080 commit bed39af
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
87 changes: 81 additions & 6 deletions plugins/async-node/core/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const basicFRFWithActions = {
},
};

let count = 0;

const asyncNodeTest = async (resolvedValue: any) => {
const plugin = new AsyncNodePlugin({
plugins: [new AsyncNodePluginPlugin()],
Expand All @@ -48,10 +46,7 @@ const asyncNodeTest = async (resolvedValue: any) => {

plugin.hooks.onAsyncNode.tap("test", async (node: Node.Node) => {
return new Promise((resolve) => {
setTimeout(() => {
deferredResolve = resolve;
}, 100)
console.log('count--',count++);
deferredResolve = resolve;
});
});

Expand Down Expand Up @@ -119,6 +114,86 @@ test("should return current node view when the resolved node is undefined", asyn
await asyncNodeTest(undefined);
});

test("should handle the promise using .then", async () => {
const plugin = new AsyncNodePlugin({
plugins: [new AsyncNodePluginPlugin()],
});

let deferredResolve: ((value: any) => void) | undefined;

plugin.hooks.onAsyncNode.tap("test", async (node: Node.Node) => {
return new Promise((resolve) => {
deferredResolve = resolve;
});
});
let updateNumber = 0;

const player = new Player({ plugins: [plugin] });

player.hooks.viewController.tap("async-node-test", (vc) => {
vc.hooks.view.tap("async-node-test", (view) => {
view.hooks.onUpdate.tap("async-node-test", (update) => {
updateNumber++;
});
});
});

player.start(basicFRFWithActions as any);

let view = (player.getState() as InProgressState).controllers.view.currentView
?.lastUpdate;

expect(view).toBeDefined();
expect(view?.actions[1]).toBeUndefined();

await waitFor(() => {
expect(updateNumber).toBe(1);
expect(deferredResolve).toBeDefined();
});

if (deferredResolve) {
deferredResolve([
{
asset: {
id: "value-1",
type: "text",
value: "1st value in the multinode",
},
},
{
id: "another-async",
async: true,
},
]);
}

await waitFor(() => {
expect(updateNumber).toBe(2);
});

view = (player.getState() as InProgressState).controllers.view.currentView
?.lastUpdate;

expect(view?.actions[0].asset.type).toBe("action");
expect(view?.actions[1].asset.type).toBe("text");
expect(view?.actions[2]).toBeUndefined();
expect(updateNumber).toBe(2);

if (deferredResolve) {
deferredResolve(null);
}

await waitFor(() => {
expect(updateNumber).toBe(3);
});

view = (player.getState() as InProgressState).controllers.view.currentView
?.lastUpdate;

expect(view?.actions[0].asset.type).toBe("action");
expect(view?.actions.length).toBe(2);
});

test("replaces async nodes with provided node", async () => {
const plugin = new AsyncNodePlugin({
plugins: [new AsyncNodePluginPlugin()],
Expand Down
12 changes: 0 additions & 12 deletions plugins/async-node/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export interface AsyncNodeViewPlugin extends ViewPlugin {
asyncNode: AsyncParallelBailHook<[Node.Node], Node.Node>;
}

let counter =0;
let count = 0;

/**
* Async node plugin used to resolve async nodes in the content
* If an async node is present, allow users to provide a replacement node to be rendered when ready
Expand Down Expand Up @@ -154,20 +151,13 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
let resolvedNode;
if (this.isAsync(node) && this.resolvedMapping.has(node.id)) {
const mappedValue = this.resolvedMapping.get(node.id);
count++;
console.log("mappedValue", mappedValue + '-----', count);
if (mappedValue) {
resolvedNode = mappedValue;
}

} else {
resolvedNode = null;
}


const newNode = resolvedNode || node;

console.log("newNode", newNode);
if (!resolvedNode && node?.type === NodeType.Async) {
queueMicrotask(async () => {
this.basePlugin?.hooks.onAsyncNode.call(node).then((result) => {
Expand All @@ -176,8 +166,6 @@ export class AsyncNodePluginPlugin implements AsyncNodeViewPlugin {
? options.parseNode(result)
: undefined;

counter++;
console.log("parsedNode---", parsedNode + '-----'+'counter---', counter);
this.resolvedMapping.set(node.id, parsedNode ? parsedNode : {});
view.updateAsync();
});
Expand Down

0 comments on commit bed39af

Please sign in to comment.