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
Sync API Refactor #87
Draft
helmturner
wants to merge
19
commits into
main
Choose a base branch
from
experimental-simplifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6b9752a
initial new abstraction for sync
helmturner 98a8f81
types: clean up interfaces in iterableTypes.ts
helmturner f588184
chore: refactor sync api
helmturner 78491c8
types: fix new type signature for TsonType
helmturner 92ffe33
fix: un-break guards and circular reference checks
helmturner d7c8dbc
tests: fix irrelevant type error in test
helmturner 16944e9
chore: cleanup (#88)
helmturner 8737f51
chore: cleanup
helmturner ab9df8e
chore: fix typo
helmturner f97876b
chore: cleanup
helmturner c9b3e9a
chore: cleanup
helmturner 7c74043
chore: format
helmturner 2f14075
tests: catch missing coverage case, cleanup
helmturner 0d467de
fix: guard shouldn't pass if neither throws nor true
helmturner 23bc039
tests: add test for assert
helmturner 14f2fa6
pff
helmturner 88db45d
chore: right serialization result, wrong indexes
helmturner f367de1
fix: correct pointer values
helmturner 9aa3dc1
fix: disambiguate serialization output of builtins
helmturner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export async function* mapIterable<T, TValue>( | ||
iterable: AsyncIterable<T>, | ||
fn: (v: T) => TValue, | ||
): AsyncIterable<TValue> { | ||
for await (const value of iterable) { | ||
yield fn(value); | ||
} | ||
} | ||
|
||
export async function reduceIterable< | ||
T, | ||
TInitialValue extends Promise<any> = Promise<T>, | ||
TKey extends PropertyKey | bigint = bigint, | ||
TKeyFn extends (prev?: TKey) => TKey = (prev?: TKey) => TKey, | ||
>( | ||
iterable: Iterable<T>, | ||
fn: (acc: Awaited<TInitialValue>, v: T, i: TKey) => Awaited<TInitialValue>, | ||
initialValue: TInitialValue = Promise.resolve() as TInitialValue, | ||
incrementKey: TKeyFn = ((prev?: bigint) => | ||
prev === undefined ? 0n : prev + 1n) as TKeyFn, | ||
): Promise<Awaited<TInitialValue>> { | ||
let acc = initialValue; | ||
let i = incrementKey(); | ||
|
||
for await (const value of iterable) { | ||
acc = fn(await acc, value, i); | ||
i = incrementKey(i); | ||
} | ||
|
||
return Promise.resolve(acc); | ||
} | ||
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
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,3 @@ | ||
export function isComplexValue(arg: unknown): arg is object { | ||
return (arg !== null && typeof arg === "object") || typeof arg === "function"; | ||
} |
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,58 @@ | ||
import * as v from "vitest"; | ||
|
||
import { AsyncGenerator, Generator } from "./iterableTypes.js"; | ||
|
||
v.describe("Async Iterable Types", () => { | ||
v.it("should be interchangeable with the original type signatures", () => { | ||
const generator = (async function* () { | ||
await Promise.resolve(); | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(); | ||
|
||
v.expectTypeOf(generator).toMatchTypeOf<AsyncGenerator<number>>(); | ||
|
||
const iterable = { | ||
[Symbol.asyncIterator]: () => generator, | ||
}; | ||
|
||
v.expectTypeOf(iterable).toMatchTypeOf<AsyncIterable<number>>(); | ||
|
||
const iterableIterator = iterable[Symbol.asyncIterator](); | ||
|
||
v.expectTypeOf(iterableIterator).toMatchTypeOf< | ||
AsyncIterableIterator<number> | ||
>(); | ||
|
||
const iterator = iterableIterator[Symbol.asyncIterator](); | ||
|
||
v.expectTypeOf(iterator).toMatchTypeOf<AsyncIterableIterator<number>>(); | ||
}); | ||
}); | ||
|
||
v.describe("Iterable Types", () => { | ||
v.it("should be interchangeable with the original type signatures", () => { | ||
const generator = (function* () { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(); | ||
|
||
v.expectTypeOf(generator).toMatchTypeOf<Generator<number>>(); | ||
|
||
const iterable = { | ||
[Symbol.iterator]: () => generator, | ||
}; | ||
|
||
v.expectTypeOf(iterable).toMatchTypeOf<Iterable<number>>(); | ||
|
||
const iterableIterator = iterable[Symbol.iterator](); | ||
|
||
v.expectTypeOf(iterableIterator).toMatchTypeOf<IterableIterator<number>>(); | ||
|
||
const iterator = iterableIterator[Symbol.iterator](); | ||
|
||
v.expectTypeOf(iterator).toMatchTypeOf<IterableIterator<number>>(); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is equivalent to the other way around. Did you want to use the
satisfies
operator?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this is necessary due to the changes in
src/sync/syncTypes.ts
(sorry for the slow reply!)