@casual-simulation/aux-runtime 3.5.3-alpha.16326443512 → 3.5.4

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.
@@ -12294,6 +12294,22 @@ export interface GeoJSONMapLayer extends MapLayerBase {
12294
12294
  data?: object;
12295
12295
  }
12296
12296
 
12297
+ /**
12298
+ * The function signature of a dynamic bot listener.
12299
+ *
12300
+ * That is, a listener that is registered at runtime by a user script instead of parsed from a tag.
12301
+ *
12302
+ * @dochash types/core
12303
+ * @docgroup 01-core
12304
+ * @docname Listener
12305
+ * @docid DynamicListener
12306
+ */
12307
+ export type DynamicListener = (
12308
+ that: any,
12309
+ bot: Bot,
12310
+ tagName: string
12311
+ ) => any;
12312
+
12297
12313
  interface Ai {
12298
12314
  /**
12299
12315
  * Sends a chat message to the AI.
@@ -12801,6 +12817,34 @@ interface Ai {
12801
12817
  }
12802
12818
 
12803
12819
  interface Os {
12820
+
12821
+ /**
12822
+ * Adds the given listener to the given bot for the given tag.
12823
+ *
12824
+ * @param bot The bot that the listener should be added to.
12825
+ * @param tagName The name of the tag that the listener should be added to.
12826
+ * @param listener The listener that should be added to the bot.
12827
+ *
12828
+ * @dochash actions/os/event
12829
+ * @docgroup 02-event-actions
12830
+ * @docname os.addBotListener
12831
+ * @docid os.addBotListener
12832
+ */
12833
+ addBotListener(bot: Bot, tagName: string, listener: DynamicListener): void;
12834
+
12835
+ /**
12836
+ * Removes the given listener from a bot for a specific tag.
12837
+ * @param bot The bot that the listener should be removed from.
12838
+ * @param tagName The name of the tag that the listener should be removed from.
12839
+ * @param listener The listener that should be removed from the bot.
12840
+ *
12841
+ * @dochash actions/os/event
12842
+ * @docgroup 02-event-actions
12843
+ * @docname os.removeBotListener
12844
+ * @docid os.removeBotListener
12845
+ */
12846
+ removeBotListener(bot: Bot, tagName: string, listener: DynamicListener): void;
12847
+
12804
12848
  /**
12805
12849
  * Sleeps for time in ms.
12806
12850
  * @param time Time in ms. 1 second is 1000ms.
@@ -1,4 +1,4 @@
1
- import type { StateUpdatedEvent, Bot, BotSpace, RuntimeBot, CompiledBotListener, BotModuleResult, ResolvedBotModule, ImportMetadata } from '@casual-simulation/aux-common/bots';
1
+ import type { StateUpdatedEvent, Bot, BotSpace, RuntimeBot, BotModuleResult, ResolvedBotModule, ImportMetadata, DynamicListener } from '@casual-simulation/aux-common/bots';
2
2
  import type { Observable, SubscriptionLike } from 'rxjs';
3
3
  import type { AuxGlobalContext } from './AuxGlobalContext';
4
4
  import type { AuxLibrary } from './AuxLibrary';
@@ -90,6 +90,7 @@ export declare class AuxRuntime implements RuntimeBotInterface, RuntimeBotFactor
90
90
  * The number of times that the runtime can call onError for an error from the same script.
91
91
  */
92
92
  repeatedErrorLimit: number;
93
+ get library(): AuxLibrary;
93
94
  get context(): AuxGlobalContext;
94
95
  get currentVersion(): RuntimeStateVersion;
95
96
  get globalObject(): any;
@@ -231,11 +232,16 @@ export declare class AuxRuntime implements RuntimeBotInterface, RuntimeBotFactor
231
232
  getRawValue(bot: CompiledBot, tag: string): any;
232
233
  updateTagMask(bot: CompiledBot, tag: string, spaces: string[], value: any): RealtimeEditConfig;
233
234
  getTagMask(bot: CompiledBot, tag: string): any;
234
- getListener(bot: CompiledBot, tag: string): CompiledBotListener;
235
+ getListener(bot: CompiledBot, tag: string): DynamicListener | null;
236
+ setListener(bot: CompiledBot, tag: string, listener: DynamicListener | null): void;
237
+ getDynamicListeners(bot: CompiledBot, tag: string): DynamicListener[] | null;
238
+ addDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
239
+ removeDynamicListener(bot: CompiledBot, tag: string, listener: DynamicListener): void;
235
240
  getTagLink(bot: CompiledBot, tag: string): RuntimeBot | RuntimeBot[];
236
241
  getSignature(bot: CompiledBot, signature: string): string;
237
242
  private _compileTagOrMask;
238
243
  private _compileTag;
244
+ private _updateListenerPresense;
239
245
  private _compileTagValue;
240
246
  private _compileValue;
241
247
  private _compileTagMaskValue;
@@ -72,6 +72,9 @@ async function _importInterpreterCore() {
72
72
  * This means taking state updates events, shouts and whispers, and emitting additional events to affect the future state.
73
73
  */
74
74
  export class AuxRuntime {
75
+ get library() {
76
+ return this._library;
77
+ }
75
78
  get context() {
76
79
  return this._globalContext;
77
80
  }
@@ -1702,6 +1705,8 @@ export class AuxRuntime {
1702
1705
  newBot.masks[space] = newMasks;
1703
1706
  }
1704
1707
  }
1708
+ newBot.dynamicListeners = existing.dynamicListeners;
1709
+ newBot.listenerOverrides = existing.listenerOverrides;
1705
1710
  existing.script[REPLACE_BOT_SYMBOL](newBot.script);
1706
1711
  }
1707
1712
  let precalculated = {
@@ -2152,6 +2157,8 @@ export class AuxRuntime {
2152
2157
  precalculated: true,
2153
2158
  tags: fromFactory ? bot.tags : { ...bot.tags },
2154
2159
  listeners: {},
2160
+ listenerOverrides: {},
2161
+ dynamicListeners: {},
2155
2162
  modules: {},
2156
2163
  exports: {},
2157
2164
  values: {},
@@ -2345,12 +2352,50 @@ export class AuxRuntime {
2345
2352
  return undefined;
2346
2353
  }
2347
2354
  getListener(bot, tag) {
2348
- const listener = bot.listeners[tag];
2349
- if (listener) {
2350
- return listener;
2355
+ return bot.listenerOverrides[tag] || bot.listeners[tag] || null;
2356
+ }
2357
+ setListener(bot, tag, listener) {
2358
+ if (hasValue(listener)) {
2359
+ bot.listenerOverrides[tag] = listener;
2360
+ }
2361
+ else {
2362
+ delete bot.listenerOverrides[tag];
2363
+ }
2364
+ this._updateListenerPresense(bot, tag);
2365
+ }
2366
+ getDynamicListeners(bot, tag) {
2367
+ if (bot.dynamicListeners && bot.dynamicListeners[tag]) {
2368
+ return bot.dynamicListeners[tag];
2369
+ }
2370
+ return null;
2371
+ }
2372
+ addDynamicListener(bot, tag, listener) {
2373
+ if (!bot.dynamicListeners) {
2374
+ bot.dynamicListeners = {};
2375
+ }
2376
+ if (!bot.dynamicListeners[tag]) {
2377
+ bot.dynamicListeners[tag] = [];
2378
+ }
2379
+ const listeners = bot.dynamicListeners[tag];
2380
+ if (listeners.includes(listener)) {
2381
+ // If the listener already exists, do not add it again.
2382
+ return;
2383
+ }
2384
+ listeners.push(listener);
2385
+ this._updateListenerPresense(bot, tag);
2386
+ }
2387
+ removeDynamicListener(bot, tag, listener) {
2388
+ if (bot.dynamicListeners && bot.dynamicListeners[tag]) {
2389
+ const listeners = bot.dynamicListeners[tag];
2390
+ const index = listeners.indexOf(listener);
2391
+ if (index >= 0) {
2392
+ listeners.splice(index, 1);
2393
+ if (listeners.length <= 0) {
2394
+ delete bot.dynamicListeners[tag];
2395
+ }
2396
+ this._updateListenerPresense(bot, tag);
2397
+ }
2351
2398
  }
2352
- this.getValue(bot, tag);
2353
- return bot.listeners[tag] || null;
2354
2399
  }
2355
2400
  getTagLink(bot, tag) {
2356
2401
  const tagValue = bot.values[tag];
@@ -2443,15 +2488,20 @@ export class AuxRuntime {
2443
2488
  }
2444
2489
  this._compileTagValue(bot, tag, tagValue);
2445
2490
  }
2491
+ _updateListenerPresense(bot, tag) {
2492
+ this._globalContext.recordListenerPresense(bot.id, tag, !!bot.listenerOverrides[tag] ||
2493
+ !!bot.listeners[tag] ||
2494
+ !!bot.dynamicListeners[tag]);
2495
+ }
2446
2496
  _compileTagValue(bot, tag, tagValue) {
2447
2497
  let { value, listener, module } = this._compileValue(bot, tag, tagValue);
2448
2498
  if (listener) {
2449
2499
  bot.listeners[tag] = listener;
2450
- this._globalContext.recordListenerPresense(bot.id, tag, true);
2500
+ this._updateListenerPresense(bot, tag);
2451
2501
  }
2452
2502
  else if (!!bot.listeners[tag]) {
2453
2503
  delete bot.listeners[tag];
2454
- this._globalContext.recordListenerPresense(bot.id, tag, false);
2504
+ this._updateListenerPresense(bot, tag);
2455
2505
  }
2456
2506
  if (module) {
2457
2507
  bot.modules[tag] = module;
@@ -2713,7 +2763,13 @@ export class AuxRuntime {
2713
2763
  // Default import function
2714
2764
  [`_${IMPORT_FACTORY}`]: () => (module, meta) => this._importModule(module, meta),
2715
2765
  },
2716
- arguments: [['that', 'data'], IMPORT_FACTORY, EXPORT_FACTORY],
2766
+ arguments: [
2767
+ ['that', 'data'],
2768
+ '$__bot',
2769
+ '$__tag',
2770
+ IMPORT_FACTORY,
2771
+ EXPORT_FACTORY,
2772
+ ],
2717
2773
  });
2718
2774
  if (hasValue(bot)) {
2719
2775
  this._functionMap.set(functionName, func);
@@ -2727,7 +2783,10 @@ export class AuxRuntime {
2727
2783
  };
2728
2784
  const exportFunc = (valueOrSource, exp) => exports(valueOrSource, exp, meta);
2729
2785
  return this._wrapWithCurrentPromise(() => {
2730
- let result = func(null, importFunc, exportFunc);
2786
+ // Pass null for the argument, bot, and tag
2787
+ // because module functions do not accept arguments
2788
+ // and have the bot and tag injected automatically
2789
+ let result = func(null, null, null, importFunc, exportFunc);
2731
2790
  this._scheduleJobQueueCheck();
2732
2791
  return result;
2733
2792
  });