@ricsam/isolate-fetch 0.1.14 → 0.1.15

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.
@@ -2146,6 +2146,41 @@ async function setupFetch(context, options) {
2146
2146
  setupServerWebSocket(context, wsCommandCallbacks);
2147
2147
  setupServe(context);
2148
2148
  setupClientWebSocket(context, clientWsCommandCallbacks);
2149
+ const eventCallbacks = new Set;
2150
+ context.global.setSync("__emit", new import_isolated_vm.default.Callback((eventName, payloadJson) => {
2151
+ const payload = JSON.parse(payloadJson);
2152
+ for (const cb of eventCallbacks)
2153
+ cb(eventName, payload);
2154
+ }));
2155
+ context.evalSync(`
2156
+ (function() {
2157
+ const __eventListeners = new Map();
2158
+
2159
+ globalThis.__on = function(event, callback) {
2160
+ let listeners = __eventListeners.get(event);
2161
+ if (!listeners) {
2162
+ listeners = new Set();
2163
+ __eventListeners.set(event, listeners);
2164
+ }
2165
+ listeners.add(callback);
2166
+ return function() {
2167
+ listeners.delete(callback);
2168
+ if (listeners.size === 0) {
2169
+ __eventListeners.delete(event);
2170
+ }
2171
+ };
2172
+ };
2173
+
2174
+ globalThis.__dispatchExternalEvent = function(event, payloadJson) {
2175
+ const listeners = __eventListeners.get(event);
2176
+ if (!listeners) return;
2177
+ const payload = JSON.parse(payloadJson);
2178
+ for (const cb of listeners) {
2179
+ try { cb(payload); } catch (e) { console.error('Event listener error:', e); }
2180
+ }
2181
+ };
2182
+ })();
2183
+ `);
2149
2184
  return {
2150
2185
  dispose() {
2151
2186
  stateMap.clear();
@@ -2450,8 +2485,18 @@ async function setupFetch(context, options) {
2450
2485
  onClientWebSocketCommand(callback) {
2451
2486
  clientWsCommandCallbacks.add(callback);
2452
2487
  return () => clientWsCommandCallbacks.delete(callback);
2488
+ },
2489
+ onEvent(callback) {
2490
+ eventCallbacks.add(callback);
2491
+ return () => eventCallbacks.delete(callback);
2492
+ },
2493
+ dispatchEvent(event, payload) {
2494
+ const json = JSON.stringify(payload);
2495
+ const safeEvent = JSON.stringify(event);
2496
+ const safeJson = JSON.stringify(json);
2497
+ context.evalSync(`__dispatchExternalEvent(${safeEvent}, ${safeJson});`);
2453
2498
  }
2454
2499
  };
2455
2500
  }
2456
2501
 
2457
- //# debugId=7837D23CACB6E1A364756E2164756E21
2502
+ //# debugId=A475F6C880EAF54E64756E2164756E21