Skip to content

Commit f6b7167

Browse files
committed
Commit
1 parent 94ff07f commit f6b7167

8 files changed

+26
-102
lines changed

index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export * from './json-stringify.ts'
22
export * from './json-parse-stream.ts'
33
export * from './json-stringify-stream.ts'
4-
export * from './json-fetch.ts'
5-
export * from './ndjson-parse-stream.ts'
6-
export * from './ndjson-stringify-stream.ts'
4+
export * from './json-fetch.ts'

json-fetch.ts

-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,3 @@ export class JSONStreamResponse extends StreamResponse {
5858
super(_body, { headers, ...rest });
5959
}
6060
}
61-
62-
export type {
63-
JSONStreamRequest as JSONRequest,
64-
JSONStreamResponse as JSONResponse,
65-
}

json-parse-stream.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// deno-lint-ignore-file no-explicit-any no-cond-assign ban-unused-ignore no-unused-vars
22
import { streamToAsyncIter } from 'https://ghuc.cc/qwtel/whatwg-stream-to-async-iter/index.ts'
33
import { JSONParser } from './json-parser.js';
4-
import { JSONParseLazyPromise } from './json-parse-lazy-promise.ts';
4+
import { TaskPromise } from './task-promise.ts';
55
import { normalize, match } from './json-path.ts'
66

77
// FIXME: avoid string concatenation/joining
@@ -76,9 +76,9 @@ export class JSONParseNexus<T = any> extends TransformStream<string | Uint8Array
7676
this.#reader = this.readable.getReader();
7777
}
7878

79-
promise<T = any>(jsonPath: string): JSONParseLazyPromise<T | undefined> {
79+
promise<T = any>(jsonPath: string): TaskPromise<T | undefined> {
8080
const reader = this.stream(jsonPath).getReader();
81-
return JSONParseLazyPromise.from(async () => {
81+
return TaskPromise.from(async () => {
8282
const x = await reader.read();
8383
return x.done ? undefined : x.value;
8484
})

ndjson-parse-stream.ts

-42
This file was deleted.

ndjson-stringify-stream.ts

-20
This file was deleted.

scripts/build_npm.ts

-8
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,10 @@ await build({
5151
name: '@worker-tools/stream-response',
5252
version: 'latest',
5353
},
54-
// 'https://ghuc.cc/worker-tools/extendable-promise/index.ts': {
55-
// name: '@worker-tools/extendable-promise',
56-
// version: 'latest',
57-
// },
5854
'https://ghuc.cc/qwtel/whatwg-stream-to-async-iter/index.ts': {
5955
name: 'whatwg-stream-to-async-iter',
6056
version: 'latest',
6157
},
62-
'https://ghuc.cc/qwtel/typed-array-utils/index.ts': {
63-
name: 'typed-array-utils',
64-
version: 'latest',
65-
},
6658
},
6759
packageManager: 'pnpm',
6860
compilerOptions: {

json-parse-lazy-promise.ts renamed to task-promise.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,57 @@ const id = <T = any>(_: T) => _;
66

77
type Awaitable<T> = T | PromiseLike<T>;
88

9-
// FIXME: Ugh...
9+
export type TaskState = 'idle' | 'pending' | 'fulfilled' | 'rejected';
10+
1011
class Task<T> {
1112
#task;
1213
#promise;
13-
#state = 'idle'
14+
#state: TaskState = 'idle'
1415

15-
constructor(task: () => Awaitable<T>, promise = new ResolvablePromise<T>()) {
16+
constructor(task: () => Awaitable<T>) {
1617
this.#task = task;
17-
this.#promise = promise;
18+
this.#promise = new ResolvablePromise<T>();
1819
}
1920

2021
execute() {
2122
if (this.#state === 'idle') {
2223
this.#state = 'pending'
2324
this.#promise.resolve(this.#task())
24-
this.#promise.then(() => { this.#state = 'fulfilled' }, () => { this.#state = 'rejected' })
25+
this.#promise.then(
26+
() => { this.#state = 'fulfilled' },
27+
() => { this.#state = 'rejected' },
28+
);
2529
}
2630
}
27-
get state() { return this.#state }
28-
get promise() { return this.#promise }
31+
get state(): TaskState { return this.#state }
32+
get promise(): Promise<T> { return this.#promise }
2933
}
3034

3135
const lock = Symbol('key');
3236

3337
// TODO: Make own module?
3438
// TODO: Add abort signal?
35-
export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
39+
// FIXME: use executor instead of task functions?
40+
// Remove extra type??
41+
export class TaskPromise<T, TT = T> implements Promise<T> {
3642
#task: Task<TT>;
3743
#mapFn;
3844
#mappedPromise;
3945

4046
static from<T>(task: () => Awaitable<T>) {
41-
return new JSONParseLazyPromise<T>(lock, new Task(task))
47+
return new TaskPromise<T>(lock, new Task(task))
4248
}
4349

4450
private constructor(
4551
key: symbol,
4652
task: Task<TT>,
47-
mapFn?: ((value: TT, i?: 0) => Awaitable<T>) | undefined | null,
53+
mapFn?: ((value: TT, i?: 0, p?: TaskPromise<T, TT>) => Awaitable<T>) | undefined | null,
4854
thisArg?: any,
4955
) {
50-
if (key !== lock) throw Error('Illegal constructor invocation');
56+
if (key !== lock) throw Error('Illegal constructor');
5157
this.#task = task;
5258
this.#mapFn = mapFn;
53-
this.#mappedPromise = this.#task.promise.then(mapFn && (x => mapFn.call(thisArg, x, 0)))
59+
this.#mappedPromise = this.#task.promise.then(mapFn && (x => mapFn.call(thisArg, x, 0, this)));
5460
}
5561

5662
get state() {
@@ -74,11 +80,11 @@ export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
7480
* Returns another lazy promise that triggers execution via `.then`
7581
*/
7682
map<U = T>(
77-
mapFn?: ((value: T, i?: 0) => Awaitable<U>) | undefined | null,
83+
mapFn?: ((value: T, i?: 0, p?: TaskPromise<T, TT>) => Awaitable<U>) | undefined | null,
7884
thisArg?: any
79-
): JSONParseLazyPromise<U, TT> {
85+
): TaskPromise<U, TT> {
8086
// @ts-ignore: types of id function (x => x) not correctly inferred...
81-
return new JSONParseLazyPromise(lock, this.#task, pipe(this.#mapFn??id, mapFn??id), thisArg);
87+
return new TaskPromise(lock, this.#task, pipe(this.#mapFn??id, mapFn??id), thisArg);
8288
}
8389

8490
catch<V = never>(onrejected?: ((reason: any) => Awaitable<V>) | null): Promise<T | V> {
@@ -91,5 +97,5 @@ export class JSONParseLazyPromise<T, TT = T> implements Promise<T> {
9197
return this.#mappedPromise.finally(onfinally)
9298
}
9399

94-
[Symbol.toStringTag] = 'JSONParseLazyPromise'
100+
[Symbol.toStringTag] = 'LazyPromise'
95101
}

test/index.test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ test('stringify', () => {
2323
assertExists(json.jsonStringifyGenerator)
2424
assertExists(json.jsonStringifyStream)
2525
})
26-
27-
test('nld-json', () => {
28-
assertExists(json.NDJSONParseStream)
29-
assertExists(json.NDJSONStringifyStream)
30-
})

0 commit comments

Comments
 (0)