Skip to content

add res to all nodes #97

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

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
43 changes: 29 additions & 14 deletions src/Substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ export class Substrate {
* @throws {Error} when the client encounters an error making the request.
*/
async run(...nodes: Node[]): Promise<SubstrateResponse> {
const serialized = Substrate.serialize(...nodes);
return this.runSerialized(serialized, nodes);
return this.runSerialized(nodes);
}

/**
Expand All @@ -93,10 +92,10 @@ export class Substrate {
* @throws {Error} when the client encounters an error making the request.
*/
async runSerialized(
serialized: any,
nodes: Node[] | null = null,
nodes: Node[],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really sure why this was optional before but seems like it's not used anywhere else, so it seemed okay to change to required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure why it can also accept null there either, but that makes sense to me to remove that since we're always passing it through for now

endpoint: string = "/compose",
): Promise<SubstrateResponse> {
const serialized = Substrate.serialize(...nodes);
const url = this.baseUrl + endpoint;
const req = { dag: serialized };
// NOTE: we're creating the signal this way instead of AbortController.timeout because it is only very
Expand All @@ -114,10 +113,9 @@ export class Substrate {
const json = await apiResponse.json();
const res = new SubstrateResponse(request, apiResponse, json);
/** TODO stop setting output on node */
if (nodes) {
// @ts-expect-error (accessing protected)
for (let node of nodes) node.response = res;
}

// @ts-expect-error (accessing protected)
for (let node of Substrate.findAllNodes(nodes)) node.response = res;

return res;
} else {
Expand Down Expand Up @@ -181,31 +179,48 @@ export class Substrate {
}

/**
* Transform an array of nodes into JSON for the Substrate API
* Return a set of all nodes and their dependent nodes.
*/
static serialize(...nodes: Node[]): any {
static findAllNodes(fromNodes: Node[]): Set<Node> {
const allNodes = new Set<Node>();
const allFutures = new Set<Future<any>>();

for (let node of nodes) {
for (let node of fromNodes) {
// @ts-ignore: .references() is protected
const refs = node.references();
for (let n of refs.nodes) {
allNodes.add(n);
}
}
return allNodes;
}

/**
* Return a set of all futures and their dependent futures.
*/
static findAllFutures(fromNodes: Node[]): Set<Future<any>> {
const allFutures = new Set<Future<any>>();
for (let node of fromNodes) {
// @ts-ignore: .references() is protected
const refs = node.references();
for (let f of refs.futures) {
allFutures.add(f);
}
}
return allFutures;
}

/**
* Transform an array of nodes into JSON for the Substrate API
*/
static serialize(...nodes: Node[]): any {
const allFutures = this.findAllFutures(nodes);
const allNodes = this.findAllNodes(nodes);
const allEdges: Record<string, Set<string>> = {};
for (let n of allNodes) {
allEdges[n.id] = new Set<string>();
for (let d of n.depends) {
allEdges[n.id]!.add(d.id);
}
}

return {
nodes: Array.from(allNodes).map((node) => node.toJSON()),
futures: Array.from(allFutures).map((future) => future.toJSON()),
Expand Down
Loading