Skip to content

Commit 97b927e

Browse files
refactor: better codemirror loading.
1 parent 098eadd commit 97b927e

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

.oxlintrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
"no-console": "error",
1313
"no-unused-vars": "error",
1414
"no-shadow": "error",
15+
"no-restricted-syntax": [
16+
"error",
17+
{
18+
"selector": "CallExpression[callee.type='MemberExpression'][callee.property.name=/^(then|catch|finally)$/]",
19+
"message": "Prefer async/await over promise chains (.then/.catch/.finally)."
20+
}
21+
],
1522
"import/no-cycle": "error",
1623
"import/no-duplicates": "error",
1724
"import/no-self-import": "error",

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Repository structure:
4444
- Preserve progressive loading behavior (lazy-load optional compilers/runtime pieces where possible).
4545
- Do not introduce bundler-only assumptions into src/ runtime code.
4646
- Prefer async/await over promise chains.
47+
- Do not use IIFE, find another pattern instead.
4748

4849
## CDN and runtime expectations
4950

src/editor-codemirror.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cdnImports, importFromCdnWithFallback } from './cdn.js'
22

33
let codeMirrorRuntime = null
4+
let codeMirrorRuntimePromise = null
45

56
const resolveLanguageExtension = (runtime, language) => {
67
if (language === 'javascript-jsx') {
@@ -18,9 +19,7 @@ const resolveLanguageExtension = (runtime, language) => {
1819
return runtime.css()
1920
}
2021

21-
const ensureCodeMirrorRuntime = async () => {
22-
if (codeMirrorRuntime) return codeMirrorRuntime
23-
22+
const loadCodeMirrorRuntime = async () => {
2423
const [
2524
state,
2625
view,
@@ -92,10 +91,25 @@ const ensureCodeMirrorRuntime = async () => {
9291
throw new Error('CodeMirror runtime did not expose expected APIs.')
9392
}
9493

95-
codeMirrorRuntime = runtime
9694
return runtime
9795
}
9896

97+
const ensureCodeMirrorRuntime = async () => {
98+
if (codeMirrorRuntime) return codeMirrorRuntime
99+
if (!codeMirrorRuntimePromise) {
100+
codeMirrorRuntimePromise = loadCodeMirrorRuntime()
101+
}
102+
103+
try {
104+
const runtime = await codeMirrorRuntimePromise
105+
codeMirrorRuntime = runtime
106+
return runtime
107+
} catch (error) {
108+
codeMirrorRuntimePromise = null
109+
throw error
110+
}
111+
}
112+
99113
export const createCodeMirrorEditor = async ({
100114
parent,
101115
value,

0 commit comments

Comments
 (0)