This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { TsonAsyncType } from "../async/asyncTypes.js"; | ||
import { TsonPromiseRejectionError } from "../errors.js"; | ||
|
||
const ITERATOR_VALUE = 0; | ||
const ITERATOR_ERROR = 1; | ||
const ITERATOR_DONE = 2; | ||
type SerializedIteratorResult = | ||
| [typeof ITERATOR_DONE] | ||
| [typeof ITERATOR_ERROR, unknown] | ||
| [typeof ITERATOR_VALUE, unknown]; | ||
function isAsyncIterator(value: unknown): value is AsyncIterable<unknown> { | ||
return ( | ||
!!value && | ||
typeof value === "object" && | ||
typeof (value as any)[Symbol.asyncIterator] === "function" | ||
); | ||
} | ||
|
||
export const tsonAsyncIterator: TsonAsyncType< | ||
AsyncIterable<unknown>, | ||
SerializedIteratorResult | ||
> = { | ||
async: true, | ||
deserialize: (opts) => { | ||
return (async function* generator() { | ||
try { | ||
for await (const value of opts.stream) { | ||
switch (value[0]) { | ||
case ITERATOR_DONE: { | ||
return; | ||
} | ||
|
||
case ITERATOR_ERROR: { | ||
throw TsonPromiseRejectionError.from(value[1]); | ||
} | ||
|
||
case ITERATOR_VALUE: { | ||
yield value[1]; | ||
break; | ||
} | ||
} | ||
} | ||
} finally { | ||
// `onDone` is a hack and shouldn't be needed | ||
opts.onDone(); | ||
} | ||
})(); | ||
}, | ||
key: "AsyncIterable", | ||
serializeIterator: async function* serialize(opts) { | ||
try { | ||
for await (const value of opts.value) { | ||
yield [ITERATOR_VALUE, value]; | ||
} | ||
|
||
yield [ITERATOR_DONE]; | ||
} catch (err) { | ||
yield [ITERATOR_ERROR, err]; | ||
} | ||
}, | ||
test: isAsyncIterator, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters