11// deno-lint-ignore-file no-explicit-any no-cond-assign ban-unused-ignore no-unused-vars
22import { streamToAsyncIter } from 'https://ghuc.cc/qwtel/whatwg-stream-to-async-iter/index.ts'
3- import { ResolvablePromise } from 'https://ghuc.cc/worker-tools/resolvable-promise/index.ts' ;
43import { JSONParser } from './json-parser.js' ;
4+ import { JSONParseLazyPromise } from './json-parse-lazy-promise.ts' ;
55import { normalize , match } from './json-path.ts'
66
7- async function * identity < T > ( iter : Iterable < T > | AsyncIterable < T > ) {
8- for await ( const x of iter ) yield x ;
9- }
10-
117// FIXME: avoid string concatenation/joining
128const mkPath = ( parser : any ) => {
139 const path = [ ...parser . stack . map ( ( _ : any ) => _ . key ) , parser . key ] ; // TODO: modify parser to provide key efficiently
@@ -34,7 +30,7 @@ export class JSONParseStream<T = any> extends TransformStream<string | Uint8Arra
3430 }
3531 } ;
3632 } ,
37- transform : ( chunk , controller ) => {
33+ transform : ( chunk ) => {
3834 parser . write ( chunk ) ;
3935 } ,
4036 } ) ;
@@ -46,10 +42,10 @@ export class JSONParseStream<T = any> extends TransformStream<string | Uint8Arra
4642
4743const remove = < K , V > ( m : Map < K , V > , k : K ) => { const v = m . get ( k ) ; m . delete ( k ) ; return v ; }
4844
45+
4946/** @deprecated Rename!!! */
5047export class JSONParseNexus < T = any > extends TransformStream < string | Uint8Array , [ string , T ] > {
5148 #queues = new Map < string , ReadableStreamDefaultController < any > > ( ) ;
52- #lazies = new Map < string , ResolvablePromise < any > > ( ) ;
5349 #reader: ReadableStreamDefaultReader < [ string , T ] >
5450
5551 constructor ( ) {
@@ -68,13 +64,6 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
6864 remove ( this . #queues, expr ) ! . close ( )
6965 }
7066 }
71- for ( const expr of this . #lazies. keys ( ) ) {
72- if ( match ( expr , path ) ) {
73- remove ( this . #lazies, expr ) ! . resolve ( value )
74- } else if ( expr . startsWith ( path ) ) {
75- remove ( this . #lazies, expr ) ! . resolve ( undefined )
76- }
77- }
7867
7968 controller . enqueue ( [ path , value ] ) ;
8069 } ;
@@ -87,34 +76,12 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
8776 this . #reader = this . readable . getReader ( ) ;
8877 }
8978
90- /**
91- * Returns a promise that resolves with the value found at the provided `jsonPath` or `undefined` otherwise.
92- *
93- * __Starts to pull values form the underlying sink immediately!__
94- * If the value is located after a large array in the JSON, the entire array will be parsed and kept in a queue!
95- * Consider using `lazy` instead if pulling form a stream elsewhere.
96- */
97- async eager < U = any > ( jsonPath : string ) : Promise < U | undefined > {
98- const x = await this . stream ( jsonPath ) . getReader ( ) . read ( ) ;
99- return x . done ? undefined : x . value ;
100- }
101-
102- /**
103- * Returns a promise that resolves with the value found at the provided `jsonPath` or `undefined` otherwise.
104- *
105- * __Does not pull from the underlying sink on its own!__
106- * If there isn't another consumer pulling past the point where the value if found, it will never resolve!
107- * Consider using `eager` instead when running into deadlocks.
108- */
109- lazy < U = any > ( jsonPath : string ) : Promise < U | undefined > & { pull : ( ) => Promise < U | undefined > } {
110- const p = new ResolvablePromise < U | undefined > ( ) ;
111- this . #lazies. set ( normalize ( jsonPath ) , p )
112- return Object . assign ( p , { pull : ( ) => this . eager ( jsonPath ) } )
113- }
114-
115- /** @deprecated Use lazy/eager instead to meet your use case */
116- promise < T = any > ( jsonPath : string ) : Promise < T | undefined > {
117- return this . eager ( jsonPath ) ;
79+ promise < T = any > ( jsonPath : string ) : JSONParseLazyPromise < T | undefined > {
80+ const stream = this . stream ( jsonPath ) ;
81+ return new JSONParseLazyPromise ( async ( ) => {
82+ const x = await stream . getReader ( ) . read ( ) ;
83+ return x . done ? undefined : x . value ;
84+ } )
11885 }
11986
12087 stream < U = any > ( jsonPath : string ) : ReadableStream < U > {
@@ -123,17 +90,16 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
12390 start : ( queue ) => {
12491 this . #queues. set ( path , queue )
12592 } ,
126- pull : async ( queue ) => {
127- // console.log('pull', jsonPath, queue.desiredSize)
93+ pull : async ( ) => {
12894 while ( true ) {
12995 const { done, value } = await this . #reader. read ( ) ;
13096 // FIXME: avoid duplicate match
13197 if ( done || match ( value [ 0 ] , path ) ) break ;
13298 }
133- // console.log('pull result', jsonPath, queue.desiredSize)
13499 } ,
135100 cancel : ( err ) => {
136- // If one of the child streams errors, error the whole pipeline. // TODO: or should it?
101+ // If one of the child streams errors, error the whole pipeline.
102+ // TODO: Or should it?
137103 this . #reader. cancel ( err )
138104 } ,
139105 } , { highWaterMark : 0 } ) // does not pull on its own
0 commit comments