MCPcopy Create free account
hub / github.com/Power-Playground/app / createProviderMaker

Function createProviderMaker

core/src/utils/createProviderMaker.ts:40–103  ·  view source on GitHub ↗
(
  mount: (
    editor: IStandaloneCodeEditor,
    monaco: typeof monacoEditor
  ) => T,
  clear: (
    editor: IStandaloneCodeEditor,
    mountInitValue: T,
    monaco: typeof monacoEditor
  ) => void,
  opts?: {
    anytime?: (type: Keys | null, ...args: unknown[]) => void
    watchEventKeys?: Keys[]
  }
)

Source from the content-addressed store, hash-verified

38export type ProviderMaker = ReturnType<typeof createProviderMaker>
39
40export function createProviderMaker<T, Keys extends WatchEventKeys>(
41 mount: (
42 editor: IStandaloneCodeEditor,
43 monaco: typeof monacoEditor
44 ) => T,
45 clear: (
46 editor: IStandaloneCodeEditor,
47 mountInitValue: T,
48 monaco: typeof monacoEditor
49 ) => void,
50 opts?: {
51 anytime?: (type: Keys | null, ...args: unknown[]) => void
52 watchEventKeys?: Keys[]
53 }
54) {
55 const {
56 anytime,
57 watchEventKeys = DEFAULT_WATCH_EVENT_KEYS as Keys[]
58 } = opts ?? {}
59 return (
60 monaco: typeof monacoEditor,
61 editor: IStandaloneCodeEditor,
62 selector: { languages: string[] },
63 provider: Provider<T>
64 ) => {
65 const mountInitValue = mount(editor, monaco)
66
67 const debounce = asyncDebounce()
68 let isCancel = { value: false }
69 let prevDispose: (() => void) | undefined = undefined
70
71 async function callback(type: Keys | null, ...args: unknown[]) {
72 try {
73 anytime?.(type, ...args)
74 } catch (e) {
75 if (e instanceof StopThisTimeError) {
76 return
77 }
78 console.error(e)
79 }
80 const model = editor.getModel()
81 if (!model) return
82
83 if (!selector.languages.includes(model.getLanguageId())) {
84 clear(editor, mountInitValue, monaco)
85 return
86 }
87 try { await debounce(300) } catch { return }
88
89 isCancel.value = true
90 isCancel = { value: false }
91
92 prevDispose?.()
93 prevDispose = await provider(model, { mountInitValue, isCancel })
94 }
95 callback(null).catch(console.error)
96 return watchEventKeys
97 .map(key => editor[key](callback.bind(null, key)).dispose)

Callers 2

GlyphProvider.tsFile · 0.90

Calls 2

asyncDebounceFunction · 0.90
callbackFunction · 0.85

Tested by

no test coverage detected