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 };
};
}
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.tsremoves the unnecessary async.