@kubohiroya/turbowarp-named-functions 0.4.1 → 0.4.2

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.ja.md CHANGED
@@ -47,7 +47,7 @@ JSONの引数を受け取りJSONの結果を返す「名前を持つ関数」を
47
47
  npm packageとして使う場合は、検証済みのversionをexact pinします。
48
48
 
49
49
  ```bash
50
- pnpm add --save-exact @kubohiroya/turbowarp-named-functions@0.4.1
50
+ pnpm add --save-exact @kubohiroya/turbowarp-named-functions@0.4.2
51
51
  ```
52
52
 
53
53
  ## クイックスタート
package/README.md CHANGED
@@ -51,7 +51,7 @@ Run `pnpm run samples:build` after source/bundle changes and `pnpm run samples:c
51
51
  ### npm package
52
52
 
53
53
  ```bash
54
- pnpm add --save-exact @kubohiroya/turbowarp-named-functions@0.4.1
54
+ pnpm add --save-exact @kubohiroya/turbowarp-named-functions@0.4.2
55
55
  ```
56
56
 
57
57
  Standalone bundle:
@@ -1674,16 +1674,17 @@ var NamedFunctionsExtension = class {
1674
1674
  this.upgradeScopes = () => {
1675
1675
  upgradeLegacyScopes(this.runtime.targets, DEFINE_FUNCTION_OPCODE, this.scopeChecked);
1676
1676
  };
1677
+ this.fixedInputEditorsReleased = false;
1678
+ this.releaseFixedInputEditors = () => {
1679
+ this.fixedInputEditorsReleased = true;
1680
+ };
1681
+ this.restoreFixedInputEditors = () => {
1682
+ if (!this.fixedInputEditorsReleased) return;
1683
+ this.fixedInputEditorsReleased = false;
1684
+ this.unregisterFixedInputEditors = this.registerEditors();
1685
+ };
1677
1686
  this.runtime = deps.runtime;
1678
- this.unregisterFixedInputEditors = registerFixedInputEditors(typeof Scratch === "undefined" ? {} : Scratch, createFixedInputEditorDefinitions((where, name, scope) => {
1679
- const target = this.runtime.getTargetById?.(where.targetId);
1680
- return target !== void 0 && hasNameConflict(this.runtime.targets, DEFINE_FUNCTION_OPCODE, {
1681
- target,
1682
- blockId: where.blockId,
1683
- name,
1684
- scope
1685
- });
1686
- }), deps.runtime);
1687
+ this.unregisterFixedInputEditors = this.registerEditors();
1687
1688
  try {
1688
1689
  this.functions = createNamedFunctions({
1689
1690
  ...deps,
@@ -1697,10 +1698,15 @@ var NamedFunctionsExtension = class {
1697
1698
  this.upgradeScopes();
1698
1699
  this.runtime.on("PROJECT_LOADED", this.upgradeScopes);
1699
1700
  this.runtime.on("PROJECT_CHANGED", this.upgradeScopes);
1701
+ this.runtime.on("RUNTIME_DISPOSED", this.releaseFixedInputEditors);
1702
+ this.runtime.on("PROJECT_LOADED", this.restoreFixedInputEditors);
1700
1703
  }
1701
1704
  dispose() {
1702
1705
  this.runtime.off?.("PROJECT_LOADED", this.upgradeScopes);
1703
1706
  this.runtime.off?.("PROJECT_CHANGED", this.upgradeScopes);
1707
+ this.runtime.off?.("RUNTIME_DISPOSED", this.releaseFixedInputEditors);
1708
+ this.runtime.off?.("PROJECT_LOADED", this.restoreFixedInputEditors);
1709
+ this.fixedInputEditorsReleased = false;
1704
1710
  this.unregisterFixedInputEditors();
1705
1711
  }
1706
1712
  getInfo() {
@@ -1827,6 +1833,17 @@ var NamedFunctionsExtension = class {
1827
1833
  lastError() {
1828
1834
  return this.lastErrorMessage;
1829
1835
  }
1836
+ registerEditors() {
1837
+ return registerFixedInputEditors(typeof Scratch === "undefined" ? {} : Scratch, createFixedInputEditorDefinitions((where, name, scope) => {
1838
+ const target = this.runtime.getTargetById?.(where.targetId);
1839
+ return target !== void 0 && hasNameConflict(this.runtime.targets, DEFINE_FUNCTION_OPCODE, {
1840
+ target,
1841
+ blockId: where.blockId,
1842
+ name,
1843
+ scope
1844
+ });
1845
+ }), this.runtime);
1846
+ }
1830
1847
  callByName(nameArg, argsArg, util) {
1831
1848
  const succeed = (result) => {
1832
1849
  this.lastErrorMessage = "";
@@ -17,7 +17,14 @@ export declare class NamedFunctionsExtension implements TurboWarpExtension {
17
17
  private readonly scopeChecked;
18
18
  /** Old hats can arrive with a project, an uploaded sprite, or this extension being added. */
19
19
  private readonly upgradeScopes;
20
- private readonly unregisterFixedInputEditors;
20
+ private unregisterFixedInputEditors;
21
+ /**
22
+ * Loading a project disposes the runtime, which releases the editor registration, but the
23
+ * extension stays loaded for the next project. Register again once that project has loaded.
24
+ */
25
+ private fixedInputEditorsReleased;
26
+ private readonly releaseFixedInputEditors;
27
+ private readonly restoreFixedInputEditors;
21
28
  constructor(deps: NamedFunctionsExtensionDependencies);
22
29
  dispose(): void;
23
30
  getInfo(): Record<string, unknown>;
@@ -55,6 +62,7 @@ export declare class NamedFunctionsExtension implements TurboWarpExtension {
55
62
  isSettled(args: Args): boolean;
56
63
  isDefined(args: Args, util?: Util): boolean;
57
64
  lastError(): string;
65
+ private registerEditors;
58
66
  private callByName;
59
67
  private guard;
60
68
  private toScratchBlock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-named-functions",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Named, JSON-in/JSON-out functions for TurboWarp that can be called by name, synchronously or asynchronously, and exported as tools.",
5
5
  "author": {
6
6
  "name": "Hiroya Kubo",
package/src/extension.ts CHANGED
@@ -69,19 +69,24 @@ export class NamedFunctionsExtension implements TurboWarpExtension {
69
69
  private readonly upgradeScopes = () => {
70
70
  upgradeLegacyScopes(this.runtime.targets, DEFINE_FUNCTION_OPCODE, this.scopeChecked);
71
71
  };
72
- private readonly unregisterFixedInputEditors: () => void;
72
+ private unregisterFixedInputEditors: () => void;
73
+ /**
74
+ * Loading a project disposes the runtime, which releases the editor registration, but the
75
+ * extension stays loaded for the next project. Register again once that project has loaded.
76
+ */
77
+ private fixedInputEditorsReleased = false;
78
+ private readonly releaseFixedInputEditors = () => {
79
+ this.fixedInputEditorsReleased = true;
80
+ };
81
+ private readonly restoreFixedInputEditors = () => {
82
+ if (!this.fixedInputEditorsReleased) return;
83
+ this.fixedInputEditorsReleased = false;
84
+ this.unregisterFixedInputEditors = this.registerEditors();
85
+ };
73
86
 
74
87
  public constructor(deps: NamedFunctionsExtensionDependencies) {
75
88
  this.runtime = deps.runtime;
76
- this.unregisterFixedInputEditors = registerFixedInputEditors(
77
- typeof Scratch === 'undefined' ? {} : Scratch,
78
- createFixedInputEditorDefinitions((where, name, scope) => {
79
- const target = this.runtime.getTargetById?.(where.targetId);
80
- return target !== undefined &&
81
- hasNameConflict(this.runtime.targets, DEFINE_FUNCTION_OPCODE, {target, blockId: where.blockId, name, scope});
82
- }),
83
- deps.runtime
84
- );
89
+ this.unregisterFixedInputEditors = this.registerEditors();
85
90
  try {
86
91
  this.functions = createNamedFunctions({
87
92
  ...deps,
@@ -95,11 +100,16 @@ export class NamedFunctionsExtension implements TurboWarpExtension {
95
100
  this.upgradeScopes();
96
101
  this.runtime.on('PROJECT_LOADED', this.upgradeScopes);
97
102
  this.runtime.on('PROJECT_CHANGED', this.upgradeScopes);
103
+ this.runtime.on('RUNTIME_DISPOSED', this.releaseFixedInputEditors);
104
+ this.runtime.on('PROJECT_LOADED', this.restoreFixedInputEditors);
98
105
  }
99
106
 
100
107
  public dispose(): void {
101
108
  this.runtime.off?.('PROJECT_LOADED', this.upgradeScopes);
102
109
  this.runtime.off?.('PROJECT_CHANGED', this.upgradeScopes);
110
+ this.runtime.off?.('RUNTIME_DISPOSED', this.releaseFixedInputEditors);
111
+ this.runtime.off?.('PROJECT_LOADED', this.restoreFixedInputEditors);
112
+ this.fixedInputEditorsReleased = false;
103
113
  this.unregisterFixedInputEditors();
104
114
  }
105
115
 
@@ -271,6 +281,18 @@ export class NamedFunctionsExtension implements TurboWarpExtension {
271
281
 
272
282
  // ---- internals ----------------------------------------------------------------------------
273
283
 
284
+ private registerEditors(): () => void {
285
+ return registerFixedInputEditors(
286
+ typeof Scratch === 'undefined' ? {} : Scratch,
287
+ createFixedInputEditorDefinitions((where, name, scope) => {
288
+ const target = this.runtime.getTargetById?.(where.targetId);
289
+ return target !== undefined &&
290
+ hasNameConflict(this.runtime.targets, DEFINE_FUNCTION_OPCODE, {target, blockId: where.blockId, name, scope});
291
+ }),
292
+ this.runtime
293
+ );
294
+ }
295
+
274
296
  private callByName(
275
297
  nameArg: unknown,
276
298
  argsArg: unknown,