Skip to content

Commit 95e23e8

Browse files
committed
feat: make node methods async
1 parent b8725e5 commit 95e23e8

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/traversal.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export const selToBlock = (function () {
141141
export interface Node {
142142
kind: Kind;
143143
value: any;
144-
lookupBySegment(seg: PathSegment): Node | null;
145-
entries(): Generator<{pathSegment: PathSegment; value: Node}, any, any>;
144+
lookupBySegment(seg: PathSegment): Promise<Node | null>;
145+
entries(): AsyncGenerator<{pathSegment: PathSegment; value: Node}, any, any>;
146146
}
147147

148148
export class BasicNode implements Node {
@@ -152,14 +152,18 @@ export class BasicNode implements Node {
152152
this.kind = is(value);
153153
this.value = value;
154154
}
155-
lookupBySegment(seg: PathSegment): Node | null {
155+
async lookupBySegment(seg: PathSegment): Promise<Node | null> {
156156
const val = this.value[seg.value];
157157
if (val) {
158158
return val;
159159
}
160160
return null;
161161
}
162-
entries(): Generator<{pathSegment: PathSegment; value: BasicNode}, any, any> {
162+
entries(): AsyncGenerator<
163+
{pathSegment: PathSegment; value: BasicNode},
164+
any,
165+
any
166+
> {
163167
if (this.kind === Kind.List) {
164168
return arrayEntries(this.value);
165169
}
@@ -544,9 +548,9 @@ function parseLimit(node: LimitNode): RecursionLimit {
544548
}
545549
}
546550

547-
function* arrayEntries(
551+
async function* arrayEntries(
548552
node: Array<any>
549-
): Generator<{pathSegment: PathSegment; value: BasicNode}, any, any> {
553+
): AsyncGenerator<{pathSegment: PathSegment; value: BasicNode}, any, any> {
550554
for (let i = 0; i < node.length; i++) {
551555
yield {
552556
pathSegment: new PathSegment(i),
@@ -555,9 +559,9 @@ function* arrayEntries(
555559
}
556560
}
557561

558-
function* mapEntries(node: {
562+
async function* mapEntries(node: {
559563
[key: string]: any;
560-
}): Generator<{pathSegment: PathSegment; value: BasicNode}, any, any> {
564+
}): AsyncGenerator<{pathSegment: PathSegment; value: BasicNode}, any, any> {
561565
for (const [k, v] of Object.entries(node)) {
562566
yield {
563567
pathSegment: new PathSegment(k),
@@ -637,7 +641,7 @@ export async function* walkBlocks(
637641
const attn = sel.interests();
638642
if (attn.length) {
639643
for (const ps of attn) {
640-
const value = nd.lookupBySegment(ps);
644+
const value = await nd.lookupBySegment(ps);
641645
if (value === null) {
642646
break;
643647
}
@@ -648,7 +652,7 @@ export async function* walkBlocks(
648652
}
649653
} else {
650654
// visit everything
651-
for (const {pathSegment, value} of nd.entries()) {
655+
for await (const {pathSegment, value} of nd.entries()) {
652656
const sNext = sel.explore(nd.value, pathSegment);
653657
if (sNext !== null) {
654658
yield* walkBlocks(value, sNext, source);

0 commit comments

Comments
 (0)