yeow-api 0.1.4 → 0.1.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/event.ts +12 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeow-api",
3
- "version": "0.1.4",
3
+ "version": "0.1.8",
4
4
  "description": "Yeow API �?TypeScript OOP wrappers over the task protocol",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/event.ts CHANGED
@@ -1,11 +1,6 @@
1
1
  import { call } from './task.js';
2
2
 
3
- /**
4
- * Event subscription with cancellation support.
5
- * Sync handlers: e.cancelled = true takes effect immediately (sync task intercepted by EventBridge).
6
- * Async handlers: e.cancelled is @deprecated (no effect after await).
7
- */
8
- const _handlers: Record<string, Array<{ h: Function; async: boolean }>> = {};
3
+ const _handlers: Record<string, Array<{ h: Function }>> = {};
9
4
 
10
5
  function _process(internalType: string, snapshot: Record<string, unknown>): Record<string, unknown> {
11
6
  const entries = _handlers[internalType];
@@ -20,31 +15,29 @@ function _process(internalType: string, snapshot: Record<string, unknown>): Reco
20
15
  for (const key of Object.keys(snapshot)) {
21
16
  if (!key.startsWith('_')) proxy[key] = snapshot[key];
22
17
  }
18
+ const localMods: Record<string, any> = {};
23
19
  if (cancellable) {
24
20
  Object.defineProperty(proxy, 'cancelled', {
25
- get: () => call('event.isCancelled', {}),
26
- set: (v: boolean) => { call('event.setCancelled', { cancelled: v }); },
21
+ get: () => localMods.cancelled || false,
22
+ set: (v: boolean) => { localMods.cancelled = v; },
27
23
  enumerable: true,
28
24
  });
29
25
  }
30
26
  entry.h(proxy);
31
- } catch (e: any) { console.error(`event handler error: ${e?.message || e}`); }
27
+ if (cancellable && localMods.cancelled) {
28
+ mods.cancelled = true;
29
+ }
30
+ } catch (e: any) { console.error(`event: ${e?.message || e}`); }
32
31
  }
33
32
  return mods;
34
33
  }
35
34
 
36
- // Install the global event handler that init.js calls
37
- (globalThis as any)._onEvent = (msg: any) => {
38
- const eventType = msg.ev;
39
- _process(eventType, msg.sn);
40
- // Notify Java that event processing is done
41
- if (typeof (globalThis as any)._eventComplete === 'function') {
42
- (globalThis as any)._eventComplete(eventType);
43
- }
35
+ // SYNC_CALLBACK handler receives snapshot, returns modifications
36
+ (globalThis as any)._onEvent = (eventType: string, snapshot: any): any => {
37
+ return _process(eventType, snapshot);
44
38
  };
45
39
 
46
40
  export function eventOn(eventType: string, handler: (e: any) => void): () => void {
47
- const async = handler.constructor.name === 'AsyncFunction';
48
41
  if (!_handlers[eventType]) {
49
42
  _handlers[eventType] = [];
50
43
  call('event.subscribe', {
@@ -52,8 +45,7 @@ export function eventOn(eventType: string, handler: (e: any) => void): () => voi
52
45
  eventType,
53
46
  });
54
47
  }
55
- const entry = { h: handler, async };
56
- _handlers[eventType].push(entry);
48
+ _handlers[eventType].push({ h: handler });
57
49
  return () => eventOff(eventType, handler);
58
50
  }
59
51