@kubohiroya/turbowarp-markdown 0.6.0 → 0.6.1

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/README.md CHANGED
@@ -44,7 +44,7 @@ pnpm install --frozen-lockfile
44
44
  The package is version-pinned when used from npm:
45
45
 
46
46
  ```bash
47
- pnpm add --save-exact @kubohiroya/turbowarp-markdown@0.6.0
47
+ pnpm add --save-exact @kubohiroya/turbowarp-markdown@0.6.1
48
48
  ```
49
49
 
50
50
  ## Quick Start
@@ -183,44 +183,71 @@ var fixedInputEditors = [...new Set(fields.map((f) => f.opcode))].map((opcode) =
183
183
  settings: null
184
184
  })
185
185
  }));
186
- /** Registration follows the advertised opcode membership and the optional host capability. */
186
+ /**
187
+ * Registration follows the advertised opcode membership and the optional host capability.
188
+ * Loading a project disposes the runtime, which releases the registrations, but the extension
189
+ * stays loaded for the next project: they are registered again once that project has loaded.
190
+ * Only the owner's dispose is final.
191
+ */
187
192
  function fixedInputRegistration(scratch) {
188
193
  const host = scratch ?? {};
189
194
  let active;
190
195
  let membership = "";
191
196
  const removers = [];
192
197
  let stopped = false;
198
+ let released = false;
199
+ let requested = [];
193
200
  const clear = () => {
194
- host.vm?.runtime?.off?.("RUNTIME_DISPOSED", shutdown);
201
+ host.vm?.runtime?.off?.("RUNTIME_DISPOSED", release);
202
+ host.vm?.runtime?.off?.("PROJECT_LOADED", restore);
195
203
  for (const remove of removers.splice(0).reverse()) remove();
196
204
  active = void 0;
197
205
  membership = "";
198
206
  };
207
+ const register = (visibleOpcodes) => {
208
+ const api = host.gui?.blockEditors;
209
+ const definitions = fixedInputEditors.filter((definition) => definition.opcodes.every((opcode) => visibleOpcodes.includes(opcode)));
210
+ const key = definitions.map((definition) => definition.editorId).join(",");
211
+ if (api === active && membership === key) return;
212
+ clear();
213
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function" || !definitions.length) return;
214
+ try {
215
+ for (const definition of definitions) {
216
+ const remove = api.registerFixedInputEditor.call(api, definition);
217
+ if (typeof remove !== "function") throw new TypeError("Invalid fixed input cleanup.");
218
+ removers.push(remove);
219
+ }
220
+ active = api;
221
+ membership = key;
222
+ host.vm?.runtime?.on?.("RUNTIME_DISPOSED", release);
223
+ } catch (error) {
224
+ clear();
225
+ throw error;
226
+ }
227
+ };
228
+ /** The runtime of this project is gone; wait for the next project before registering again. */
229
+ const release = () => {
230
+ clear();
231
+ released = true;
232
+ host.vm?.runtime?.on?.("PROJECT_LOADED", restore);
233
+ };
234
+ const restore = () => {
235
+ if (stopped || !released) return;
236
+ released = false;
237
+ try {
238
+ register(requested);
239
+ } catch {}
240
+ };
199
241
  const shutdown = () => {
200
242
  stopped = true;
243
+ released = false;
201
244
  clear();
202
245
  };
203
246
  return {
204
247
  ensure(visibleOpcodes) {
205
- const api = host.gui?.blockEditors;
206
- const definitions = fixedInputEditors.filter((definition) => definition.opcodes.every((opcode) => visibleOpcodes.includes(opcode)));
207
- const key = definitions.map((definition) => definition.editorId).join(",");
208
- if (stopped || api === active && membership === key) return;
209
- clear();
210
- if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function" || !definitions.length) return;
211
- try {
212
- for (const definition of definitions) {
213
- const remove = api.registerFixedInputEditor.call(api, definition);
214
- if (typeof remove !== "function") throw new TypeError("Invalid fixed input cleanup.");
215
- removers.push(remove);
216
- }
217
- active = api;
218
- membership = key;
219
- host.vm?.runtime?.on?.("RUNTIME_DISPOSED", shutdown);
220
- } catch (error) {
221
- clear();
222
- throw error;
223
- }
248
+ requested = visibleOpcodes;
249
+ if (stopped || released) return;
250
+ register(visibleOpcodes);
224
251
  },
225
252
  dispose: shutdown
226
253
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-markdown",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "An immutable Markdown builder extension for TurboWarp.",
5
5
  "author": {
6
6
  "name": "Hiroya Kubo",
@@ -75,38 +75,63 @@ interface OptionalFixedHost {
75
75
  gui?: {blockEditors?: {apiVersion?: number; registerFixedInputEditor?: (definition: FixedInputEditorOptions) => () => void}};
76
76
  vm?: {runtime?: {on?: (event: string, listener: () => void) => void; off?: (event: string, listener: () => void) => void}};
77
77
  }
78
- /** Registration follows the advertised opcode membership and the optional host capability. */
78
+ /**
79
+ * Registration follows the advertised opcode membership and the optional host capability.
80
+ * Loading a project disposes the runtime, which releases the registrations, but the extension
81
+ * stays loaded for the next project: they are registered again once that project has loaded.
82
+ * Only the owner's dispose is final.
83
+ */
79
84
  export function fixedInputRegistration(scratch: unknown) {
80
85
  const host = (scratch ?? {}) as OptionalFixedHost;
81
86
  let active: unknown;
82
87
  let membership = '';
83
88
  const removers: (() => void)[] = [];
84
89
  let stopped = false;
90
+ let released = false;
91
+ let requested: readonly string[] = [];
85
92
  const clear = () => {
86
- host.vm?.runtime?.off?.('RUNTIME_DISPOSED', shutdown);
93
+ host.vm?.runtime?.off?.('RUNTIME_DISPOSED', release);
94
+ host.vm?.runtime?.off?.('PROJECT_LOADED', restore);
87
95
  for (const remove of removers.splice(0).reverse()) remove();
88
96
  active = undefined;
89
97
  membership = '';
90
98
  };
91
- const shutdown = () => {stopped = true; clear();};
99
+ const register = (visibleOpcodes: readonly string[]) => {
100
+ const api = host.gui?.blockEditors;
101
+ const definitions = fixedInputEditors.filter(definition => definition.opcodes.every(opcode => visibleOpcodes.includes(opcode)));
102
+ const key = definitions.map(definition => definition.editorId).join(',');
103
+ if (api === active && membership === key) return;
104
+ clear();
105
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function' || !definitions.length) return;
106
+ try {
107
+ for (const definition of definitions) {
108
+ const remove = api.registerFixedInputEditor.call(api, definition);
109
+ if (typeof remove !== 'function') throw new TypeError('Invalid fixed input cleanup.');
110
+ removers.push(remove);
111
+ }
112
+ active = api;
113
+ membership = key;
114
+ host.vm?.runtime?.on?.('RUNTIME_DISPOSED', release);
115
+ } catch (error) {clear(); throw error;}
116
+ };
117
+ /** The runtime of this project is gone; wait for the next project before registering again. */
118
+ const release = () => {
119
+ clear();
120
+ released = true;
121
+ host.vm?.runtime?.on?.('PROJECT_LOADED', restore);
122
+ };
123
+ const restore = () => {
124
+ if (stopped || !released) return;
125
+ released = false;
126
+ // A refusal here must not break the VM's event dispatch; the next getInfo() retries.
127
+ try {register(requested);} catch {/* retried by the next ensure() */}
128
+ };
129
+ const shutdown = () => {stopped = true; released = false; clear();};
92
130
  return {
93
131
  ensure(visibleOpcodes: readonly string[]) {
94
- const api = host.gui?.blockEditors;
95
- const definitions = fixedInputEditors.filter(definition => definition.opcodes.every(opcode => visibleOpcodes.includes(opcode)));
96
- const key = definitions.map(definition => definition.editorId).join(',');
97
- if (stopped || (api === active && membership === key)) return;
98
- clear();
99
- if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function' || !definitions.length) return;
100
- try {
101
- for (const definition of definitions) {
102
- const remove = api.registerFixedInputEditor.call(api, definition);
103
- if (typeof remove !== 'function') throw new TypeError('Invalid fixed input cleanup.');
104
- removers.push(remove);
105
- }
106
- active = api;
107
- membership = key;
108
- host.vm?.runtime?.on?.('RUNTIME_DISPOSED', shutdown);
109
- } catch (error) {clear(); throw error;}
132
+ requested = visibleOpcodes;
133
+ if (stopped || released) return;
134
+ register(visibleOpcodes);
110
135
  },
111
136
  dispose: shutdown
112
137
  };