This repository was archived by the owner on Aug 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
This repository was archived by the owner on Aug 13, 2025. It is now read-only.
Turn AsyncGenerators into Generators #115
Copy link
Copy link
Open
Labels
⭐️ EnhancementImprovement or new feature for usersImprovement or new feature for users
Description
What is the reason behind matchers producing AsyncGenerators. They don't do IO or wait on callbacks as far as I can see. Having an async function which doesn't need to be one has serious disadvantages, like async-infection up the call-chain, race conditions and so on.
A simple modification of packages/selector/src/text/match-text-position.ts
removes the unnecessary async.
export function textPositionSelectorMatcher(
selector: TextPositionSelector,
): <TChunk extends Chunk<any>>(
scope: Chunker<TChunk>,
) => AsyncGenerator<ChunkRange<TChunk>, void, void> {
const { start, end } = selector;
return async function* matchAll<TChunk extends Chunk<string>>(
textChunks: Chunker<TChunk>,
) {
const codeUnitSeeker = new TextSeeker(textChunks);
const codePointSeeker = new CodePointSeeker(codeUnitSeeker);
codePointSeeker.seekTo(start);
const startChunk = codeUnitSeeker.currentChunk;
const startIndex = codeUnitSeeker.offsetInChunk;
codePointSeeker.seekTo(end);
const endChunk = codeUnitSeeker.currentChunk;
const endIndex = codeUnitSeeker.offsetInChunk;
yield { startChunk, startIndex, endChunk, endIndex };
};
}
export function textPositionSelectorMatcher(
selector: TextPositionSelector,
): <TChunk extends Chunk<any>>(
scope: Chunker<TChunk>,
) => Generator<ChunkRange<TChunk>, void, void> {
const { start, end } = selector;
return function* matchAll<TChunk extends Chunk<string>>(
textChunks: Chunker<TChunk>,
) {
const codeUnitSeeker = new TextSeeker(textChunks);
const codePointSeeker = new CodePointSeeker(codeUnitSeeker);
codePointSeeker.seekTo(start);
const startChunk = codeUnitSeeker.currentChunk;
const startIndex = codeUnitSeeker.offsetInChunk;
codePointSeeker.seekTo(end);
const endChunk = codeUnitSeeker.currentChunk;
const endIndex = codeUnitSeeker.offsetInChunk;
yield { startChunk, startIndex, endChunk, endIndex };
};
}
Mario-Eis
Metadata
Metadata
Assignees
Labels
⭐️ EnhancementImprovement or new feature for usersImprovement or new feature for users