xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / clew / src / stream.ts

3.8 KB
1import type {
2  ClewChunk,
3  ClewCorrection,
4  ClewOutput,
5  ClewStream,
6  ClewStreamOptions,
7  ClewUnsubscribe,
8} from "./model.js";
9import type { ClewLanguageSupport } from "./registry.js";
10import {
11  cloneOutput,
12  commonPrefixLength,
13  outputAtBoundary,
14  outputBoundary,
15  stableLineBoundary,
16  tokensFrom,
17  tokensStartingAtOrAfter,
18} from "./shared.js";
19
20function emitPatch<T>(listeners: Set<(value: T) => void>, value: T): void {
21  for (const listener of listeners) {
22    listener(value);
23  }
24}
25
26export function createStream(
27  content: string,
28  options: ClewStreamOptions,
29  support: ClewLanguageSupport,
30): ClewStream {
31  const chunkListeners = new Set<(chunk: ClewChunk) => void>();
32  const correctionListeners = new Set<(correction: ClewCorrection) => void>();
33  const stability = options.stability ?? "eager";
34  const boundaryOf = support.stableBoundary ?? stableLineBoundary;
35
36  let ended = false;
37  let currentContent = content;
38  let currentOutput = support.tokenize(content);
39  let emittedOutput =
40    stability === "stable"
41      ? outputAtBoundary(currentOutput, boundaryOf(currentContent, false))
42      : cloneOutput(currentOutput);
43  let emittedBoundary =
44    stability === "stable"
45      ? outputBoundary(emittedOutput)
46      : currentContent.length;
47
48  function publish(nextBoundary: number, nextOutput: ClewOutput): void {
49    const prefix = commonPrefixLength(emittedOutput.tokens, nextOutput.tokens);
50    const previousBoundary = emittedBoundary;
51
52    if (
53      prefix === emittedOutput.tokens.length &&
54      prefix === nextOutput.tokens.length &&
55      previousBoundary === nextBoundary
56    ) {
57      emittedBoundary = nextBoundary;
58      emittedOutput = cloneOutput(nextOutput);
59      return;
60    }
61
62    if (prefix === emittedOutput.tokens.length) {
63      if (nextBoundary > previousBoundary) {
64        emitPatch(chunkListeners, {
65          from: previousBoundary,
66          to: previousBoundary,
67          tokens: tokensStartingAtOrAfter(nextOutput.tokens, previousBoundary),
68        });
69      }
70    } else {
71      const previousChanged = emittedOutput.tokens[prefix];
72      const nextChanged = nextOutput.tokens[prefix];
73      const from = Math.min(
74        previousChanged?.start ?? previousBoundary,
75        nextChanged?.start ?? nextBoundary,
76      );
77
78      emitPatch(correctionListeners, {
79        from,
80        to: previousBoundary,
81        tokens: tokensFrom(nextOutput.tokens, from),
82      });
83    }
84
85    emittedBoundary = nextBoundary;
86    emittedOutput = cloneOutput(nextOutput);
87  }
88
89  function register<T>(
90    listeners: Set<(value: T) => void>,
91    listener: (value: T) => void,
92  ): ClewUnsubscribe {
93    listeners.add(listener);
94    return () => {
95      listeners.delete(listener);
96    };
97  }
98
99  return {
100    onChunk(listener) {
101      return register(chunkListeners, listener);
102    },
103    onCorrection(listener) {
104      return register(correctionListeners, listener);
105    },
106    write(chunk) {
107      if (ended) {
108        throw new Error("Cannot write to a clew stream after end().");
109      }
110      if (chunk.length === 0) {
111        return;
112      }
113
114      currentContent += chunk;
115      currentOutput = support.tokenize(currentContent);
116
117      const requestedBoundary =
118        stability === "stable"
119          ? boundaryOf(currentContent, false)
120          : currentContent.length;
121      const nextOutput =
122        stability === "stable"
123          ? outputAtBoundary(currentOutput, requestedBoundary)
124          : currentOutput;
125      const nextBoundary =
126        stability === "stable" ? outputBoundary(nextOutput) : requestedBoundary;
127
128      publish(nextBoundary, nextOutput);
129    },
130    snapshot() {
131      return cloneOutput(currentOutput);
132    },
133    end() {
134      if (!ended) {
135        ended = true;
136        publish(currentContent.length, currentOutput);
137      }
138      return cloneOutput(currentOutput);
139    },
140  };
141}