Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check code length
Browse files Browse the repository at this point in the history
emmerich committed Apr 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent e56d8c0 commit 5a37175
Showing 2 changed files with 21 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/components/DocumentView/CodeBlock/highlight.ts
Original file line number Diff line number Diff line change
@@ -36,7 +36,10 @@ type PositionedToken = ThemedToken & { start: number; end: number };
* This is done per invocation of the Cloudflare worker, so we can store it in-memory.
*/
let lineCount = 0;
const LINE_LIMIT = 750;
let tokenCount = 0;
const LINE_LIMIT = 10000;

const runner = asyncMutexFunction();

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

const lineCountBefore = lineCount;
const highlighter = await loadHighlighter();
await loadHighlighterLanguage(highlighter, langName);

const lines = highlighter.codeToTokensBase(code, {
lang: langName,
tokenizeMaxLineLength: 120,
});

let currentIndex = 0;


console.log(`${block.key}${code.length} ${lineCountBefore} ${tokenCount}`);
return lines.map((tokens, index) => {
tokenCount += tokens.length;
const lineBlock = block.nodes[index];
const result: HighlightToken[] = [];

19 changes: 11 additions & 8 deletions src/lib/async.ts
Original file line number Diff line number Diff line change
@@ -228,27 +228,30 @@ const UndefinedSymbol = Symbol('Undefined');
* where I/O cannot be performed on behalf of a different request.
*/
export function singleton<R>(execute: () => Promise<R>): () => Promise<R> {
let cachedResult: R | typeof UndefinedSymbol = UndefinedSymbol;
let cachedResult: Promise<R> | typeof UndefinedSymbol = UndefinedSymbol;
const states = new WeakMap<object, Promise<R>>();

return async () => {
console.log('cachedResult', cachedResult === UndefinedSymbol);
if (cachedResult !== UndefinedSymbol) {
// Result is actually shared between requests
return cachedResult;
}

// Promises are not shared between requests in Cloudflare Workers
const ctx = await getGlobalContext();
const current = states.get(ctx);
if (current) {
return current;
}
// const ctx = await getGlobalContext();
// const current = states.get(ctx);
// if (current) {
// console.log(`states.get`)
// return current;
// }

const promise = execute();
states.set(ctx, promise);
console.log(`states miss, set cachedResult`);
cachedResult = promise;
// states.set(ctx, promise);

const result = await promise;
cachedResult = result;
return result;
};
}

0 comments on commit 5a37175

Please sign in to comment.