@xynogen/pix-display 0.3.5 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/code-blocks.ts +29 -1
package/package.json
CHANGED
package/src/code-blocks.ts
CHANGED
|
@@ -19,6 +19,7 @@ type RenderMode = "tui" | "rpc" | "json" | "print";
|
|
|
19
19
|
type PatchablePrototype = {
|
|
20
20
|
[PATCHED]?: boolean;
|
|
21
21
|
render(width: number): string[];
|
|
22
|
+
invalidate(): void;
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
let activeTheme: CodeFrameTheme | undefined;
|
|
@@ -136,9 +137,36 @@ function patchAssistantRenderer(): void {
|
|
|
136
137
|
if (prototype[PATCHED]) return;
|
|
137
138
|
|
|
138
139
|
const nativeRender = prototype.render;
|
|
140
|
+
const nativeInvalidate = prototype.invalidate;
|
|
141
|
+
const cache = new WeakMap<
|
|
142
|
+
PatchablePrototype,
|
|
143
|
+
{
|
|
144
|
+
width: number;
|
|
145
|
+
theme: CodeFrameTheme;
|
|
146
|
+
input: string[];
|
|
147
|
+
output: string[];
|
|
148
|
+
}
|
|
149
|
+
>();
|
|
150
|
+
prototype.invalidate = function invalidateCodeFrames(): void {
|
|
151
|
+
cache.delete(this);
|
|
152
|
+
nativeInvalidate.call(this);
|
|
153
|
+
};
|
|
139
154
|
prototype.render = function renderWithCodeFrames(width: number): string[] {
|
|
140
155
|
const lines = nativeRender.call(this, width);
|
|
141
|
-
|
|
156
|
+
if (!activeTheme) return lines;
|
|
157
|
+
const previous = cache.get(this);
|
|
158
|
+
if (
|
|
159
|
+
previous &&
|
|
160
|
+
previous.width === width &&
|
|
161
|
+
previous.theme === activeTheme &&
|
|
162
|
+
previous.input.length === lines.length &&
|
|
163
|
+
lines.every((line, i) => line === previous.input[i])
|
|
164
|
+
) {
|
|
165
|
+
return previous.output;
|
|
166
|
+
}
|
|
167
|
+
const output = renderCodeFences(lines, width, activeTheme);
|
|
168
|
+
cache.set(this, { width, theme: activeTheme, input: [...lines], output });
|
|
169
|
+
return output;
|
|
142
170
|
};
|
|
143
171
|
prototype[PATCHED] = true;
|
|
144
172
|
}
|