Skip to content

Commit 2eeaafd

Browse files
committed
Fix
1 parent f992752 commit 2eeaafd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

json-parse-stream.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
7777
}
7878

7979
promise<T = any>(jsonPath: string): JSONParseLazyPromise<T | undefined> {
80-
const stream = this.stream(jsonPath);
80+
const reader = this.stream(jsonPath).getReader();
8181
return JSONParseLazyPromise.from(async () => {
82-
if (!this.writable.locked) throw TypeError('Cannot await result while there\'s no data source.')
83-
const x = await stream.getReader().read();
82+
const x = await reader.read();
8483
return x.done ? undefined : x.value;
8584
})
8685
}
@@ -92,7 +91,6 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
9291
this.#queues.set(path, queue)
9392
},
9493
pull: async () => {
95-
if (!this.writable.locked) throw TypeError('Cannot await result while there\'s no data source.')
9694
while (true) {
9795
const { done, value } = await this.#reader.read();
9896
// FIXME: avoid duplicate match

test/json-parse-stream.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,19 @@ test('iterations throws without data source', () => {
288288
const items = nexus.iterable('$.items.*')
289289
assertRejects(() => items.next(), TypeError)
290290
})
291+
292+
test('stream remains open when only promises left', async () => {
293+
const nexus = new JSONParseNexus();
294+
295+
const stream = nexus.stream('$.items.*')
296+
const type = nexus.promise('$.type')
297+
298+
new JSONStringifyReadable({
299+
items: asyncGen(items),
300+
type: 'foo',
301+
}).pipeThrough(nexus)
302+
303+
await collect(stream)
304+
await timeout(10)
305+
assertEquals(await type, 'foo')
306+
})

0 commit comments

Comments
 (0)