Skip to content

Commit 5a37175

Browse files
committed
Check code length
1 parent e56d8c0 commit 5a37175

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/components/DocumentView/CodeBlock/highlight.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ type PositionedToken = ThemedToken & { start: number; end: number };
3636
* This is done per invocation of the Cloudflare worker, so we can store it in-memory.
3737
*/
3838
let lineCount = 0;
39-
const LINE_LIMIT = 750;
39+
let tokenCount = 0;
40+
const LINE_LIMIT = 10000;
41+
42+
const runner = asyncMutexFunction();
4043

4144
/**
4245
* Highlight a code block while preserving inline elements.
@@ -62,17 +65,20 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
6265
return a.start - b.start;
6366
});
6467

68+
const lineCountBefore = lineCount;
6569
const highlighter = await loadHighlighter();
6670
await loadHighlighterLanguage(highlighter, langName);
67-
71+
6872
const lines = highlighter.codeToTokensBase(code, {
6973
lang: langName,
7074
tokenizeMaxLineLength: 120,
7175
});
72-
76+
7377
let currentIndex = 0;
74-
78+
79+
console.log(`${block.key}${code.length} ${lineCountBefore} ${tokenCount}`);
7580
return lines.map((tokens, index) => {
81+
tokenCount += tokens.length;
7682
const lineBlock = block.nodes[index];
7783
const result: HighlightToken[] = [];
7884

src/lib/async.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,30 @@ const UndefinedSymbol = Symbol('Undefined');
228228
* where I/O cannot be performed on behalf of a different request.
229229
*/
230230
export function singleton<R>(execute: () => Promise<R>): () => Promise<R> {
231-
let cachedResult: R | typeof UndefinedSymbol = UndefinedSymbol;
231+
let cachedResult: Promise<R> | typeof UndefinedSymbol = UndefinedSymbol;
232232
const states = new WeakMap<object, Promise<R>>();
233233

234234
return async () => {
235+
console.log('cachedResult', cachedResult === UndefinedSymbol);
235236
if (cachedResult !== UndefinedSymbol) {
236237
// Result is actually shared between requests
237238
return cachedResult;
238239
}
239240

240241
// Promises are not shared between requests in Cloudflare Workers
241-
const ctx = await getGlobalContext();
242-
const current = states.get(ctx);
243-
if (current) {
244-
return current;
245-
}
242+
// const ctx = await getGlobalContext();
243+
// const current = states.get(ctx);
244+
// if (current) {
245+
// console.log(`states.get`)
246+
// return current;
247+
// }
246248

247249
const promise = execute();
248-
states.set(ctx, promise);
250+
console.log(`states miss, set cachedResult`);
251+
cachedResult = promise;
252+
// states.set(ctx, promise);
249253

250254
const result = await promise;
251-
cachedResult = result;
252255
return result;
253256
};
254257
}

0 commit comments

Comments
 (0)