Skip to content

Commit 5a0f2cb

Browse files
authored
add res to all nodes (#97)
1 parent eb745a2 commit 5a0f2cb

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/Substrate.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ export class Substrate {
7373
* @throws {Error} when the client encounters an error making the request.
7474
*/
7575
async run(...nodes: Node[]): Promise<SubstrateResponse> {
76-
const serialized = Substrate.serialize(...nodes);
77-
return this.runSerialized(serialized, nodes);
76+
return this.runSerialized(nodes);
7877
}
7978

8079
/**
@@ -93,10 +92,10 @@ export class Substrate {
9392
* @throws {Error} when the client encounters an error making the request.
9493
*/
9594
async runSerialized(
96-
serialized: any,
97-
nodes: Node[] | null = null,
95+
nodes: Node[],
9896
endpoint: string = "/compose",
9997
): Promise<SubstrateResponse> {
98+
const serialized = Substrate.serialize(...nodes);
10099
const url = this.baseUrl + endpoint;
101100
const req = { dag: serialized };
102101
// NOTE: we're creating the signal this way instead of AbortController.timeout because it is only very
@@ -114,10 +113,9 @@ export class Substrate {
114113
const json = await apiResponse.json();
115114
const res = new SubstrateResponse(request, apiResponse, json);
116115
/** TODO stop setting output on node */
117-
if (nodes) {
118-
// @ts-expect-error (accessing protected)
119-
for (let node of nodes) node.response = res;
120-
}
116+
117+
// @ts-expect-error (accessing protected)
118+
for (let node of Substrate.findAllNodes(nodes)) node.response = res;
121119

122120
return res;
123121
} else {
@@ -181,31 +179,48 @@ export class Substrate {
181179
}
182180

183181
/**
184-
* Transform an array of nodes into JSON for the Substrate API
182+
* Return a set of all nodes and their dependent nodes.
185183
*/
186-
static serialize(...nodes: Node[]): any {
184+
static findAllNodes(fromNodes: Node[]): Set<Node> {
187185
const allNodes = new Set<Node>();
188-
const allFutures = new Set<Future<any>>();
189-
190-
for (let node of nodes) {
186+
for (let node of fromNodes) {
191187
// @ts-ignore: .references() is protected
192188
const refs = node.references();
193189
for (let n of refs.nodes) {
194190
allNodes.add(n);
195191
}
192+
}
193+
return allNodes;
194+
}
195+
196+
/**
197+
* Return a set of all futures and their dependent futures.
198+
*/
199+
static findAllFutures(fromNodes: Node[]): Set<Future<any>> {
200+
const allFutures = new Set<Future<any>>();
201+
for (let node of fromNodes) {
202+
// @ts-ignore: .references() is protected
203+
const refs = node.references();
196204
for (let f of refs.futures) {
197205
allFutures.add(f);
198206
}
199207
}
208+
return allFutures;
209+
}
200210

211+
/**
212+
* Transform an array of nodes into JSON for the Substrate API
213+
*/
214+
static serialize(...nodes: Node[]): any {
215+
const allFutures = this.findAllFutures(nodes);
216+
const allNodes = this.findAllNodes(nodes);
201217
const allEdges: Record<string, Set<string>> = {};
202218
for (let n of allNodes) {
203219
allEdges[n.id] = new Set<string>();
204220
for (let d of n.depends) {
205221
allEdges[n.id]!.add(d.id);
206222
}
207223
}
208-
209224
return {
210225
nodes: Array.from(allNodes).map((node) => node.toJSON()),
211226
futures: Array.from(allFutures).map((future) => future.toJSON()),

0 commit comments

Comments
 (0)