@moeru/eventa 1.0.0-beta.3 → 1.0.0-beta.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.
- package/README.md +50 -1
- package/dist/adapters/broadcast-channel/index.d.mts +2 -2
- package/dist/adapters/broadcast-channel/index.mjs +6 -9
- package/dist/adapters/broadcast-channel/index.mjs.map +1 -1
- package/dist/adapters/electron/main.d.mts +2 -2
- package/dist/adapters/electron/main.mjs +7 -9
- package/dist/adapters/electron/main.mjs.map +1 -1
- package/dist/adapters/electron/renderer.d.mts +134 -3
- package/dist/adapters/electron/renderer.mjs +4 -6
- package/dist/adapters/electron/renderer.mjs.map +1 -1
- package/dist/adapters/event-emitter/index.d.mts +2 -2
- package/dist/adapters/event-emitter/index.mjs +4 -7
- package/dist/adapters/event-emitter/index.mjs.map +1 -1
- package/dist/adapters/event-target/index.d.mts +2 -2
- package/dist/adapters/event-target/index.mjs +5 -8
- package/dist/adapters/event-target/index.mjs.map +1 -1
- package/dist/adapters/websocket/h3/index.d.mts +442 -3
- package/dist/adapters/websocket/h3/index.mjs +7 -9
- package/dist/adapters/websocket/h3/index.mjs.map +1 -1
- package/dist/adapters/websocket/index.d.mts +1 -1
- package/dist/adapters/websocket/index.mjs +1 -1
- package/dist/adapters/websocket/native/index.d.mts +2 -2
- package/dist/adapters/websocket/native/index.mjs +4 -5
- package/dist/adapters/websocket/native/index.mjs.map +1 -1
- package/dist/adapters/webworkers/index.d.mts +2 -2
- package/dist/adapters/webworkers/index.mjs +5 -6
- package/dist/adapters/webworkers/index.mjs.map +1 -1
- package/dist/adapters/webworkers/worker/index.d.mts +1 -1
- package/dist/adapters/webworkers/worker/index.mjs +6 -7
- package/dist/adapters/webworkers/worker/index.mjs.map +1 -1
- package/dist/adapters/window-message/index.d.mts +50 -0
- package/dist/adapters/window-message/index.mjs +79 -0
- package/dist/adapters/window-message/index.mjs.map +1 -0
- package/dist/adapters/worker-threads/index.d.mts +2 -2
- package/dist/adapters/worker-threads/index.mjs +5 -6
- package/dist/adapters/worker-threads/index.mjs.map +1 -1
- package/dist/adapters/worker-threads/worker/index.d.mts +1 -1
- package/dist/adapters/worker-threads/worker/index.mjs +6 -7
- package/dist/adapters/worker-threads/worker/index.mjs.map +1 -1
- package/dist/{context-BNM9nsGp.d.mts → context-BsqFeoer.d.mts} +2 -2
- package/dist/{context-c10xPp3f.mjs → context-Dht_IZMb.mjs} +22 -24
- package/dist/context-Dht_IZMb.mjs.map +1 -0
- package/dist/{eventa-M1FMVrH1.d.mts → eventa-DSnoBa1O.d.mts} +3 -3
- package/dist/{index-giKJThn3.d.mts → index-g6DulgOI.d.mts} +88 -88
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +3 -4
- package/dist/{internal-C-4X8bpL.mjs → internal-CHUsXYiP.mjs} +3 -4
- package/dist/{internal-C-4X8bpL.mjs.map → internal-CHUsXYiP.mjs.map} +1 -1
- package/dist/{shared-eQ-hnVHv.mjs → shared-CA5MGwv0.mjs} +5 -7
- package/dist/shared-CA5MGwv0.mjs.map +1 -0
- package/dist/{shared-BcRXZZxh.d.mts → shared-CVTLm458.d.mts} +2 -2
- package/dist/{shared-NFMSqOPw.d.mts → shared-CfXZb-7K.d.mts} +2 -2
- package/dist/{shared-DeDRsHPI.mjs → shared-Dl0_bFq0.mjs} +4 -5
- package/dist/{shared-DeDRsHPI.mjs.map → shared-Dl0_bFq0.mjs.map} +1 -1
- package/dist/{src-CTkYOGiO.mjs → src-CTs6h4Ci.mjs} +56 -63
- package/dist/src-CTs6h4Ci.mjs.map +1 -0
- package/package.json +14 -7
- package/dist/context-c10xPp3f.mjs.map +0 -1
- package/dist/shared-eQ-hnVHv.mjs.map +0 -1
- package/dist/src-CTkYOGiO.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ console.log(await someMethod(42)) // => { output: '42' }
|
|
|
80
80
|
|
|
81
81
|
### Adapters
|
|
82
82
|
|
|
83
|
-
Eventa comes with various adapters for common use scenarios across browsers and Node.js,
|
|
83
|
+
Eventa comes with various adapters for common use scenarios across browsers and Node.js, including Electron, `window.postMessage`, Web Workers, Worker Threads, BroadcastChannel, EventTarget, EventEmitter, and WebSockets.
|
|
84
84
|
|
|
85
85
|
<details>
|
|
86
86
|
<summary>Electron</summary>
|
|
@@ -179,6 +179,55 @@ Eventa comes with various adapters for common use scenarios across browsers and
|
|
|
179
179
|
```
|
|
180
180
|
</details>
|
|
181
181
|
|
|
182
|
+
<details>
|
|
183
|
+
<summary>Window Message (iframe / popup)</summary>
|
|
184
|
+
|
|
185
|
+
1. Define shared invoke events once:
|
|
186
|
+
```ts
|
|
187
|
+
import { defineInvokeEventa } from '@moeru/eventa'
|
|
188
|
+
|
|
189
|
+
export const echoEvents = defineInvokeEventa<{ echoed: string }, { message: string }>('window:echo')
|
|
190
|
+
```
|
|
191
|
+
2. In the host page, bridge Eventa to the child frame or popup:
|
|
192
|
+
```ts
|
|
193
|
+
import { defineInvoke } from '@moeru/eventa'
|
|
194
|
+
import { createContext } from '@moeru/eventa/adapters/window-message'
|
|
195
|
+
|
|
196
|
+
import { echoEvents } from './shared-events'
|
|
197
|
+
|
|
198
|
+
const iframe = document.querySelector('iframe')!
|
|
199
|
+
const { context: hostCtx } = createContext({
|
|
200
|
+
channel: 'demo:window-message',
|
|
201
|
+
currentWindow: window,
|
|
202
|
+
targetWindow: () => iframe.contentWindow,
|
|
203
|
+
targetOrigin: '*',
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
const echo = defineInvoke(hostCtx, echoEvents)
|
|
207
|
+
console.log(await echo({ message: 'hello iframe' })) // => { echoed: 'iframe:hello iframe' }
|
|
208
|
+
```
|
|
209
|
+
3. In the iframe or popup window, create the peer context and register handlers:
|
|
210
|
+
```ts
|
|
211
|
+
import { defineInvokeHandler } from '@moeru/eventa'
|
|
212
|
+
import { createContext } from '@moeru/eventa/adapters/window-message'
|
|
213
|
+
|
|
214
|
+
import { echoEvents } from './shared-events'
|
|
215
|
+
|
|
216
|
+
const { context: childCtx } = createContext({
|
|
217
|
+
channel: 'demo:window-message',
|
|
218
|
+
currentWindow: window,
|
|
219
|
+
targetWindow: () => window.parent,
|
|
220
|
+
targetOrigin: '*',
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
defineInvokeHandler(childCtx, echoEvents, ({ message }) => ({
|
|
224
|
+
echoed: `iframe:${message}`,
|
|
225
|
+
}))
|
|
226
|
+
```
|
|
227
|
+
4. This adapter handles normal invoke responses and handler-thrown errors once both sides have created their Eventa contexts. If the iframe or popup script throws before the bridge is established, Eventa may never start in that peer at all, so the caller will not get a transport-level failure automatically. More generally, `window.postMessage` does not expose a worker-style fatal error channel, so if the other window disappears, never boots, or crashes before replying, callers should use `AbortSignal` or their own timeout/liveness policy.
|
|
228
|
+
|
|
229
|
+
</details>
|
|
230
|
+
|
|
182
231
|
<details>
|
|
183
232
|
<summary>EventTarget</summary>
|
|
184
233
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as EventTag, s as EventaType } from "../../eventa-
|
|
2
|
-
import { t as EventContext } from "../../context-
|
|
1
|
+
import { n as EventTag, s as EventaType } from "../../eventa-DSnoBa1O.mjs";
|
|
2
|
+
import { t as EventContext } from "../../context-BsqFeoer.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/adapters/broadcast-channel/shared.d.ts
|
|
5
5
|
interface Payload<T> {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { a as defineEventa, c as matchBy, i as and, l as nanoid,
|
|
2
|
-
import "../../src-
|
|
3
|
-
|
|
1
|
+
import { a as defineEventa, c as matchBy, i as and, l as nanoid, o as defineInboundEventa, s as defineOutboundEventa, t as createContext$1 } from "../../context-Dht_IZMb.mjs";
|
|
2
|
+
import "../../src-CTs6h4Ci.mjs";
|
|
4
3
|
//#region src/adapters/broadcast-channel/internal.ts
|
|
5
4
|
function generatePayload(type, payload) {
|
|
6
5
|
return {
|
|
@@ -12,11 +11,9 @@ function generatePayload(type, payload) {
|
|
|
12
11
|
function parsePayload(data) {
|
|
13
12
|
return data;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
//#endregion
|
|
17
15
|
//#region src/adapters/broadcast-channel/shared.ts
|
|
18
16
|
const errorEvent = { ...defineEventa() };
|
|
19
|
-
|
|
20
17
|
//#endregion
|
|
21
18
|
//#region src/adapters/broadcast-channel/index.ts
|
|
22
19
|
function withRemoval(channel, type, listener) {
|
|
@@ -29,12 +26,12 @@ function createContext(channel, options) {
|
|
|
29
26
|
const ctx = createContext$1();
|
|
30
27
|
const { messageEvents: message = true, messageErrorEvents: messageError = true, closeOnDispose = false } = options || {};
|
|
31
28
|
const cleanupRemoval = [];
|
|
32
|
-
ctx.on(and(matchBy((e) => e._flowDirection ===
|
|
33
|
-
const message
|
|
29
|
+
ctx.on(and(matchBy((e) => e._flowDirection === "outbound" || !e._flowDirection), matchBy("*")), (event) => {
|
|
30
|
+
const message = generatePayload(event.id, {
|
|
34
31
|
...defineOutboundEventa(event.type),
|
|
35
32
|
...event
|
|
36
33
|
});
|
|
37
|
-
channel.postMessage(message
|
|
34
|
+
channel.postMessage(message);
|
|
38
35
|
});
|
|
39
36
|
if (message) cleanupRemoval.push(withRemoval(channel, "message", (event) => {
|
|
40
37
|
try {
|
|
@@ -56,7 +53,7 @@ function createContext(channel, options) {
|
|
|
56
53
|
}
|
|
57
54
|
};
|
|
58
55
|
}
|
|
59
|
-
|
|
60
56
|
//#endregion
|
|
61
57
|
export { createContext };
|
|
58
|
+
|
|
62
59
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["createBaseContext"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["createBaseContext"],"sources":["../../../src/adapters/broadcast-channel/internal.ts","../../../src/adapters/broadcast-channel/shared.ts","../../../src/adapters/broadcast-channel/index.ts"],"sourcesContent":["import type { EventTag } from '../..'\nimport type { Payload } from './shared'\n\nimport { nanoid } from '../..'\n\nexport function generatePayload<T>(type: EventTag<any, any>, payload: T): Payload<T> {\n return {\n id: nanoid(),\n type,\n payload,\n }\n}\n\nexport function parsePayload<T>(data: unknown): Payload<T> {\n return data as Payload<T>\n}\n","import type { EventTag } from '../../eventa'\n\nimport { defineEventa } from '../../eventa'\n\nexport interface Payload<T> {\n id: string\n type: EventTag<any, any>\n payload: T\n}\n\nexport const errorEvent = { ...defineEventa<{ error: unknown }>() }\n","import type { EventContext } from '../../context'\nimport type { DirectionalEventa, Eventa } from '../../eventa'\n\nimport { createContext as createBaseContext } from '../../context'\nimport { and, defineInboundEventa, defineOutboundEventa, EventaFlowDirection, matchBy } from '../../eventa'\nimport { generatePayload, parsePayload } from './internal'\nimport { errorEvent } from './shared'\n\nfunction withRemoval<K extends keyof BroadcastChannelEventMap>(channel: BroadcastChannel, type: K, listener: (event: BroadcastChannelEventMap[K]) => void) {\n channel.addEventListener(type, listener)\n\n return {\n remove: () => {\n channel.removeEventListener(type, listener)\n },\n }\n}\n\nexport interface BroadcastChannelAdapterOptions {\n /**\n * Whether to listen to `message` events.\n * @default true\n */\n messageEvents?: boolean\n /**\n * Whether to listen to `messageerror` events.\n * @default true\n */\n messageErrorEvents?: boolean\n /**\n * Whether to close the BroadcastChannel when disposing the context.\n * @default false\n */\n closeOnDispose?: boolean\n}\n\nexport function createContext(channel: BroadcastChannel, options?: BroadcastChannelAdapterOptions) {\n const ctx = createBaseContext() as EventContext<any, { raw: { message?: MessageEvent, messageError?: MessageEvent, error?: unknown } }>\n\n const {\n messageEvents: message = true,\n messageErrorEvents: messageError = true,\n closeOnDispose = false,\n } = options || {}\n\n const cleanupRemoval: Array<{ remove: () => void }> = []\n\n ctx.on(and(matchBy((e: DirectionalEventa<any>) => e._flowDirection === EventaFlowDirection.Outbound || !e._flowDirection), matchBy('*')), (event) => {\n const message = generatePayload(event.id, { ...defineOutboundEventa(event.type), ...event })\n channel.postMessage(message)\n })\n\n if (message) {\n cleanupRemoval.push(withRemoval(channel, 'message', (event) => {\n try {\n const { type, payload } = parsePayload<Eventa<any>>(event.data)\n ctx.emit(defineInboundEventa(type), payload.body, { raw: { message: event } })\n }\n catch (error) {\n console.error('Failed to parse BroadcastChannel message:', error)\n ctx.emit(errorEvent, { error }, { raw: { error } })\n }\n }))\n }\n\n if (messageError) {\n cleanupRemoval.push(withRemoval(channel, 'messageerror', (event) => {\n ctx.emit(errorEvent, { error: event }, { raw: { messageError: event } })\n }))\n }\n\n return {\n context: ctx,\n dispose: () => {\n cleanupRemoval.forEach(removal => removal.remove())\n if (closeOnDispose) {\n channel.close?.()\n }\n },\n }\n}\n\nexport type * from './shared'\n"],"mappings":";;;AAKA,SAAgB,gBAAmB,MAA0B,SAAwB;AACnF,QAAO;EACL,IAAI,QAAQ;EACZ;EACA;EACD;;AAGH,SAAgB,aAAgB,MAA2B;AACzD,QAAO;;;;ACJT,MAAa,aAAa,EAAE,GAAG,cAAkC,EAAE;;;ACFnE,SAAS,YAAsD,SAA2B,MAAS,UAAwD;AACzJ,SAAQ,iBAAiB,MAAM,SAAS;AAExC,QAAO,EACL,cAAc;AACZ,UAAQ,oBAAoB,MAAM,SAAS;IAE9C;;AAqBH,SAAgB,cAAc,SAA2B,SAA0C;CACjG,MAAM,MAAMA,iBAAmB;CAE/B,MAAM,EACJ,eAAe,UAAU,MACzB,oBAAoB,eAAe,MACnC,iBAAiB,UACf,WAAW,EAAE;CAEjB,MAAM,iBAAgD,EAAE;AAExD,KAAI,GAAG,IAAI,SAAS,MAA8B,EAAE,mBAAA,cAAmD,CAAC,EAAE,eAAe,EAAE,QAAQ,IAAI,CAAC,GAAG,UAAU;EACnJ,MAAM,UAAU,gBAAgB,MAAM,IAAI;GAAE,GAAG,qBAAqB,MAAM,KAAK;GAAE,GAAG;GAAO,CAAC;AAC5F,UAAQ,YAAY,QAAQ;GAC5B;AAEF,KAAI,QACF,gBAAe,KAAK,YAAY,SAAS,YAAY,UAAU;AAC7D,MAAI;GACF,MAAM,EAAE,MAAM,YAAY,aAA0B,MAAM,KAAK;AAC/D,OAAI,KAAK,oBAAoB,KAAK,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,SAAS,OAAO,EAAE,CAAC;WAEzE,OAAO;AACZ,WAAQ,MAAM,6CAA6C,MAAM;AACjE,OAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;;GAErD,CAAC;AAGL,KAAI,aACF,gBAAe,KAAK,YAAY,SAAS,iBAAiB,UAAU;AAClE,MAAI,KAAK,YAAY,EAAE,OAAO,OAAO,EAAE,EAAE,KAAK,EAAE,cAAc,OAAO,EAAE,CAAC;GACxE,CAAC;AAGL,QAAO;EACL,SAAS;EACT,eAAe;AACb,kBAAe,SAAQ,YAAW,QAAQ,QAAQ,CAAC;AACnD,OAAI,eACF,SAAQ,SAAS;;EAGtB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as EventContext } from "../../context-
|
|
2
|
-
import { n as errorEvent, t as Payload } from "../../shared-
|
|
1
|
+
import { t as EventContext } from "../../context-BsqFeoer.mjs";
|
|
2
|
+
import { n as errorEvent, t as Payload } from "../../shared-CfXZb-7K.mjs";
|
|
3
3
|
import { BrowserWindow, IpcMain, IpcMainEvent } from "electron";
|
|
4
4
|
|
|
5
5
|
//#region src/adapters/electron/main.d.ts
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { c as matchBy, i as and,
|
|
2
|
-
import "../../
|
|
3
|
-
import { n as generatePayload, r as parsePayload, t as errorEvent } from "../../shared-DeDRsHPI.mjs";
|
|
4
|
-
|
|
1
|
+
import { c as matchBy, i as and, o as defineInboundEventa, s as defineOutboundEventa, t as createContext$1 } from "../../context-Dht_IZMb.mjs";
|
|
2
|
+
import { n as generatePayload, r as parsePayload, t as errorEvent } from "../../shared-Dl0_bFq0.mjs";
|
|
5
3
|
//#region src/adapters/electron/main.ts
|
|
6
4
|
function withRemoval(ipcMain, type, listener) {
|
|
7
5
|
ipcMain.on(type, listener);
|
|
@@ -13,7 +11,7 @@ function createContext(ipcMain, window, options) {
|
|
|
13
11
|
const ctx = createContext$1();
|
|
14
12
|
const { messageEventName = "eventa-message", errorEventName = "eventa-error", extraListeners = {}, onlySameWindow = false } = options || {};
|
|
15
13
|
const cleanupRemoval = [];
|
|
16
|
-
ctx.on(and(matchBy("*"), matchBy((e) => e._flowDirection ===
|
|
14
|
+
ctx.on(and(matchBy("*"), matchBy((e) => e._flowDirection === "outbound" || !e._flowDirection)), (event, options) => {
|
|
17
15
|
const eventBody = generatePayload(event.id, {
|
|
18
16
|
...defineOutboundEventa(event.type),
|
|
19
17
|
...event
|
|
@@ -22,11 +20,11 @@ function createContext(ipcMain, window, options) {
|
|
|
22
20
|
if (window != null) {
|
|
23
21
|
if (window.isDestroyed()) return;
|
|
24
22
|
if (onlySameWindow) {
|
|
25
|
-
if (window.webContents.id === options
|
|
23
|
+
if (window.webContents.id === options?.raw.ipcMainEvent.sender.id) window?.webContents?.send(messageEventName, eventBody);
|
|
26
24
|
} else window?.webContents?.send(messageEventName, eventBody);
|
|
27
25
|
} else {
|
|
28
|
-
if (options
|
|
29
|
-
options
|
|
26
|
+
if (options?.raw.ipcMainEvent.sender.isDestroyed()) return;
|
|
27
|
+
options?.raw.ipcMainEvent.sender.send(messageEventName, eventBody);
|
|
30
28
|
}
|
|
31
29
|
} catch (error) {
|
|
32
30
|
if (!(error instanceof Error) || error?.message !== "Object has been destroyed") throw error;
|
|
@@ -61,7 +59,7 @@ function createContext(ipcMain, window, options) {
|
|
|
61
59
|
}
|
|
62
60
|
};
|
|
63
61
|
}
|
|
64
|
-
|
|
65
62
|
//#endregion
|
|
66
63
|
export { createContext };
|
|
64
|
+
|
|
67
65
|
//# sourceMappingURL=main.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.mjs","names":["createBaseContext"
|
|
1
|
+
{"version":3,"file":"main.mjs","names":["createBaseContext"],"sources":["../../../src/adapters/electron/main.ts"],"sourcesContent":["import type { BrowserWindow, IpcMain, IpcMainEvent } from 'electron'\n\nimport type { EventContext } from '../../context'\nimport type { DirectionalEventa, Eventa } from '../../eventa'\n\nimport { createContext as createBaseContext } from '../../context'\nimport { and, defineInboundEventa, defineOutboundEventa, EventaFlowDirection, matchBy } from '../../eventa'\nimport { generatePayload, parsePayload } from './internal'\nimport { errorEvent } from './shared'\n\nfunction withRemoval(ipcMain: IpcMain, type: string, listener: Parameters<IpcMain['on']>[1]) {\n ipcMain.on(type, listener)\n\n return {\n remove: () => {\n ipcMain.off(type, listener)\n },\n }\n}\n\nexport function createContext(ipcMain: IpcMain, window?: BrowserWindow, options?: {\n onlySameWindow?: boolean\n messageEventName?: string | false\n errorEventName?: string | false\n extraListeners?: Record<string, (_, event: Event) => void | Promise<void>>\n throwIfFailedToSend?: boolean\n}) {\n const ctx = createBaseContext() as EventContext<\n { invokeRequest?: { raw?: { ipcMainEvent: IpcMainEvent, event: Event | unknown } } },\n { raw: { ipcMainEvent: IpcMainEvent, event: Event | unknown } }\n >\n\n const {\n messageEventName = 'eventa-message',\n errorEventName = 'eventa-error',\n extraListeners = {},\n onlySameWindow = false,\n } = options || {}\n\n const cleanupRemoval: Array<{ remove: () => void }> = []\n\n ctx.on(and(\n matchBy('*'),\n matchBy((e: DirectionalEventa<any>) => e._flowDirection === EventaFlowDirection.Outbound || !e._flowDirection),\n ), (event, options) => {\n const eventBody = generatePayload(event.id, { ...defineOutboundEventa(event.type), ...event })\n if (messageEventName !== false) {\n try {\n if (window != null) {\n if (window.isDestroyed()) {\n return\n }\n\n if (onlySameWindow) {\n if (window.webContents.id === options?.raw.ipcMainEvent.sender.id) {\n window?.webContents?.send(messageEventName, eventBody)\n }\n }\n else {\n window?.webContents?.send(messageEventName, eventBody)\n }\n }\n else {\n if (options?.raw.ipcMainEvent.sender.isDestroyed()) {\n return\n }\n\n options?.raw.ipcMainEvent.sender.send(messageEventName, eventBody)\n }\n }\n catch (error) {\n // NOTICE: Electron may throw if the window is closed before sending\n // ignore the error if it's about destroyed object\n if (!(error instanceof Error) || error?.message !== 'Object has been destroyed') {\n throw error\n }\n }\n }\n })\n\n if (messageEventName) {\n cleanupRemoval.push(withRemoval(ipcMain, messageEventName, (ipcMainEvent, event: Event | unknown) => {\n try {\n const { type, payload } = parsePayload<Eventa<any>>(event)\n ctx.emit(defineInboundEventa(type), payload.body, { raw: { ipcMainEvent, event } })\n }\n catch (error) {\n console.error('Failed to parse IpcMain message:', error)\n ctx.emit(errorEvent, { error }, { raw: { ipcMainEvent, event } })\n }\n }))\n }\n\n if (errorEventName) {\n cleanupRemoval.push(withRemoval(ipcMain, errorEventName, (ipcMainEvent, error: Event | unknown) => {\n ctx.emit(errorEvent, { error }, { raw: { ipcMainEvent, event: error } })\n }))\n }\n\n for (const [eventName, listener] of Object.entries(extraListeners)) {\n cleanupRemoval.push(withRemoval(ipcMain, eventName, listener))\n }\n\n return {\n context: ctx,\n dispose: () => {\n cleanupRemoval.forEach(removal => removal.remove())\n },\n }\n}\n\nexport type * from './shared'\n"],"mappings":";;;AAUA,SAAS,YAAY,SAAkB,MAAc,UAAwC;AAC3F,SAAQ,GAAG,MAAM,SAAS;AAE1B,QAAO,EACL,cAAc;AACZ,UAAQ,IAAI,MAAM,SAAS;IAE9B;;AAGH,SAAgB,cAAc,SAAkB,QAAwB,SAMrE;CACD,MAAM,MAAMA,iBAAmB;CAK/B,MAAM,EACJ,mBAAmB,kBACnB,iBAAiB,gBACjB,iBAAiB,EAAE,EACnB,iBAAiB,UACf,WAAW,EAAE;CAEjB,MAAM,iBAAgD,EAAE;AAExD,KAAI,GAAG,IACL,QAAQ,IAAI,EACZ,SAAS,MAA8B,EAAE,mBAAA,cAAmD,CAAC,EAAE,eAAe,CAC/G,GAAG,OAAO,YAAY;EACrB,MAAM,YAAY,gBAAgB,MAAM,IAAI;GAAE,GAAG,qBAAqB,MAAM,KAAK;GAAE,GAAG;GAAO,CAAC;AAC9F,MAAI,qBAAqB,MACvB,KAAI;AACF,OAAI,UAAU,MAAM;AAClB,QAAI,OAAO,aAAa,CACtB;AAGF,QAAI;SACE,OAAO,YAAY,OAAO,SAAS,IAAI,aAAa,OAAO,GAC7D,SAAQ,aAAa,KAAK,kBAAkB,UAAU;UAIxD,SAAQ,aAAa,KAAK,kBAAkB,UAAU;UAGrD;AACH,QAAI,SAAS,IAAI,aAAa,OAAO,aAAa,CAChD;AAGF,aAAS,IAAI,aAAa,OAAO,KAAK,kBAAkB,UAAU;;WAG/D,OAAO;AAGZ,OAAI,EAAE,iBAAiB,UAAU,OAAO,YAAY,4BAClD,OAAM;;GAIZ;AAEF,KAAI,iBACF,gBAAe,KAAK,YAAY,SAAS,mBAAmB,cAAc,UAA2B;AACnG,MAAI;GACF,MAAM,EAAE,MAAM,YAAY,aAA0B,MAAM;AAC1D,OAAI,KAAK,oBAAoB,KAAK,EAAE,QAAQ,MAAM,EAAE,KAAK;IAAE;IAAc;IAAO,EAAE,CAAC;WAE9E,OAAO;AACZ,WAAQ,MAAM,oCAAoC,MAAM;AACxD,OAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK;IAAE;IAAc;IAAO,EAAE,CAAC;;GAEnE,CAAC;AAGL,KAAI,eACF,gBAAe,KAAK,YAAY,SAAS,iBAAiB,cAAc,UAA2B;AACjG,MAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK;GAAE;GAAc,OAAO;GAAO,EAAE,CAAC;GACxE,CAAC;AAGL,MAAK,MAAM,CAAC,WAAW,aAAa,OAAO,QAAQ,eAAe,CAChE,gBAAe,KAAK,YAAY,SAAS,WAAW,SAAS,CAAC;AAGhE,QAAO;EACL,SAAS;EACT,eAAe;AACb,kBAAe,SAAQ,YAAW,QAAQ,QAAQ,CAAC;;EAEtD"}
|
|
@@ -1,7 +1,138 @@
|
|
|
1
|
-
import { t as EventContext } from "../../context-
|
|
2
|
-
import { n as errorEvent, t as Payload } from "../../shared-
|
|
3
|
-
import {
|
|
1
|
+
import { t as EventContext } from "../../context-BsqFeoer.mjs";
|
|
2
|
+
import { n as errorEvent, t as Payload } from "../../shared-CfXZb-7K.mjs";
|
|
3
|
+
import { IpcRendererEvent } from "electron";
|
|
4
4
|
|
|
5
|
+
//#region node_modules/.pnpm/@electron-toolkit+preload@3.0.2_electron@41.3.0/node_modules/@electron-toolkit/preload/dist/index.d.ts
|
|
6
|
+
type IpcRendererListener = (event: IpcRendererEvent, ...args: any[]) => void;
|
|
7
|
+
interface IpcRenderer {
|
|
8
|
+
/**
|
|
9
|
+
* Listens to `channel`, when a new message arrives `listener` would be called with
|
|
10
|
+
* `listener(event, args...)`.
|
|
11
|
+
*/
|
|
12
|
+
on(channel: string, listener: IpcRendererListener): () => void;
|
|
13
|
+
/**
|
|
14
|
+
* Adds a one time `listener` function for the event. This `listener` is invoked
|
|
15
|
+
* only the next time a message is sent to `channel`, after which it is removed.
|
|
16
|
+
*/
|
|
17
|
+
once(channel: string, listener: IpcRendererListener): () => void;
|
|
18
|
+
/**
|
|
19
|
+
* Removes all listeners, or those of the specified `channel`.
|
|
20
|
+
*/
|
|
21
|
+
removeAllListeners(channel: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Removes the specified `listener` from the listener array for the specified
|
|
24
|
+
* `channel`.
|
|
25
|
+
*
|
|
26
|
+
* @deprecated Use `on` listen return instead
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
removeListener(channel: string, listener: (...args: any[]) => void): this;
|
|
30
|
+
/**
|
|
31
|
+
* Send an asynchronous message to the main process via `channel`, along with
|
|
32
|
+
* arguments. Arguments will be serialized with the Structured Clone Algorithm,
|
|
33
|
+
* just like `window.postMessage`, so prototype chains will not be included.
|
|
34
|
+
* Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an
|
|
35
|
+
* exception.
|
|
36
|
+
*
|
|
37
|
+
* **NOTE:** Sending non-standard JavaScript types such as DOM objects or special
|
|
38
|
+
* Electron objects will throw an exception.
|
|
39
|
+
*
|
|
40
|
+
* Since the main process does not have support for DOM objects such as
|
|
41
|
+
* `ImageBitmap`, `File`, `DOMMatrix` and so on, such objects cannot be sent over
|
|
42
|
+
* Electron's IPC to the main process, as the main process would have no way to
|
|
43
|
+
* decode them. Attempting to send such objects over IPC will result in an error.
|
|
44
|
+
*
|
|
45
|
+
* The main process handles it by listening for `channel` with the `ipcMain`
|
|
46
|
+
* module.
|
|
47
|
+
*
|
|
48
|
+
* If you need to transfer a `MessagePort` to the main process, use
|
|
49
|
+
* `ipcRenderer.postMessage`.
|
|
50
|
+
*
|
|
51
|
+
* If you want to receive a single response from the main process, like the result
|
|
52
|
+
* of a method call, consider using `ipcRenderer.invoke`.
|
|
53
|
+
*/
|
|
54
|
+
send(channel: string, ...args: any[]): void;
|
|
55
|
+
/**
|
|
56
|
+
* Resolves with the response from the main process.
|
|
57
|
+
*
|
|
58
|
+
* Send a message to the main process via `channel` and expect a result
|
|
59
|
+
* asynchronously. Arguments will be serialized with the Structured Clone
|
|
60
|
+
* Algorithm, just like `window.postMessage`, so prototype chains will not be
|
|
61
|
+
* included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw
|
|
62
|
+
* an exception.
|
|
63
|
+
*
|
|
64
|
+
* **NOTE:** Sending non-standard JavaScript types such as DOM objects or special
|
|
65
|
+
* Electron objects will throw an exception.
|
|
66
|
+
*
|
|
67
|
+
* Since the main process does not have support for DOM objects such as
|
|
68
|
+
* `ImageBitmap`, `File`, `DOMMatrix` and so on, such objects cannot be sent over
|
|
69
|
+
* Electron's IPC to the main process, as the main process would have no way to
|
|
70
|
+
* decode them. Attempting to send such objects over IPC will result in an error.
|
|
71
|
+
*
|
|
72
|
+
* The main process should listen for `channel` with `ipcMain.handle()`.
|
|
73
|
+
*
|
|
74
|
+
* For example:
|
|
75
|
+
*
|
|
76
|
+
* If you need to transfer a `MessagePort` to the main process, use
|
|
77
|
+
* `ipcRenderer.postMessage`.
|
|
78
|
+
*
|
|
79
|
+
* If you do not need a response to the message, consider using `ipcRenderer.send`.
|
|
80
|
+
*/
|
|
81
|
+
invoke(channel: string, ...args: any[]): Promise<any>;
|
|
82
|
+
/**
|
|
83
|
+
* Send a message to the main process, optionally transferring ownership of zero or
|
|
84
|
+
* more `MessagePort` objects.
|
|
85
|
+
*
|
|
86
|
+
* The transferred `MessagePort` objects will be available in the main process as
|
|
87
|
+
* `MessagePortMain` objects by accessing the `ports` property of the emitted
|
|
88
|
+
* event.
|
|
89
|
+
*
|
|
90
|
+
* **NOTE:** Cannot transfer these when `contextIsolation: true`.
|
|
91
|
+
*
|
|
92
|
+
* For example:
|
|
93
|
+
*
|
|
94
|
+
* For more information on using `MessagePort` and `MessageChannel`, see the MDN
|
|
95
|
+
* documentation.
|
|
96
|
+
*/
|
|
97
|
+
postMessage(channel: string, message: any, transfer?: MessagePort[]): void;
|
|
98
|
+
/**
|
|
99
|
+
* The value sent back by the `ipcMain` handler.
|
|
100
|
+
*
|
|
101
|
+
* Send a message to the main process via `channel` and expect a result
|
|
102
|
+
* synchronously. Arguments will be serialized with the Structured Clone Algorithm,
|
|
103
|
+
* just like `window.postMessage`, so prototype chains will not be included.
|
|
104
|
+
* Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an
|
|
105
|
+
* exception.
|
|
106
|
+
*
|
|
107
|
+
* **NOTE:** Sending non-standard JavaScript types such as DOM objects or special
|
|
108
|
+
* Electron objects will throw an exception.
|
|
109
|
+
*
|
|
110
|
+
* Since the main process does not have support for DOM objects such as
|
|
111
|
+
* `ImageBitmap`, `File`, `DOMMatrix` and so on, such objects cannot be sent over
|
|
112
|
+
* Electron's IPC to the main process, as the main process would have no way to
|
|
113
|
+
* decode them. Attempting to send such objects over IPC will result in an error.
|
|
114
|
+
*
|
|
115
|
+
* The main process handles it by listening for `channel` with `ipcMain` module,
|
|
116
|
+
* and replies by setting `event.returnValue`.
|
|
117
|
+
*
|
|
118
|
+
* warning: **WARNING**: Sending a synchronous message will block the whole
|
|
119
|
+
* renderer process until the reply is received, so use this method only as a last
|
|
120
|
+
* resort. It's much better to use the asynchronous version, `invoke()`.
|
|
121
|
+
*/
|
|
122
|
+
sendSync(channel: string, ...args: any[]): any;
|
|
123
|
+
/**
|
|
124
|
+
* Sends a message to a window with `webContentsId` via `channel`.
|
|
125
|
+
*
|
|
126
|
+
* @deprecated This methond has been removed since Electron 28.
|
|
127
|
+
*/
|
|
128
|
+
sendTo(webContentsId: number, channel: string, ...args: any[]): void;
|
|
129
|
+
/**
|
|
130
|
+
* Like `ipcRenderer.send` but the event will be sent to the `<webview>` element in
|
|
131
|
+
* the host page instead of the main process.
|
|
132
|
+
*/
|
|
133
|
+
sendToHost(channel: string, ...args: any[]): void;
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
5
136
|
//#region src/adapters/electron/renderer.d.ts
|
|
6
137
|
declare function createContext(ipcRenderer: IpcRenderer, options?: {
|
|
7
138
|
messageEventName?: string | false;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { c as matchBy, i as and,
|
|
2
|
-
import "../../
|
|
3
|
-
import { n as generatePayload, r as parsePayload, t as errorEvent } from "../../shared-DeDRsHPI.mjs";
|
|
4
|
-
|
|
1
|
+
import { c as matchBy, i as and, o as defineInboundEventa, s as defineOutboundEventa, t as createContext$1 } from "../../context-Dht_IZMb.mjs";
|
|
2
|
+
import { n as generatePayload, r as parsePayload, t as errorEvent } from "../../shared-Dl0_bFq0.mjs";
|
|
5
3
|
//#region src/adapters/electron/renderer.ts
|
|
6
4
|
function createContext(ipcRenderer, options) {
|
|
7
5
|
const ctx = createContext$1();
|
|
8
6
|
const { messageEventName = "eventa-message", errorEventName = "eventa-error", extraListeners = {} } = options || {};
|
|
9
7
|
const cleanupRemoval = [];
|
|
10
|
-
ctx.on(and(matchBy((e) => e._flowDirection ===
|
|
8
|
+
ctx.on(and(matchBy((e) => e._flowDirection === "outbound" || !e._flowDirection), matchBy("*")), (event) => {
|
|
11
9
|
const eventBody = generatePayload(event.id, {
|
|
12
10
|
...defineOutboundEventa(event.type),
|
|
13
11
|
...event
|
|
@@ -47,7 +45,7 @@ function createContext(ipcRenderer, options) {
|
|
|
47
45
|
}
|
|
48
46
|
};
|
|
49
47
|
}
|
|
50
|
-
|
|
51
48
|
//#endregion
|
|
52
49
|
export { createContext };
|
|
50
|
+
|
|
53
51
|
//# sourceMappingURL=renderer.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderer.mjs","names":["createBaseContext"
|
|
1
|
+
{"version":3,"file":"renderer.mjs","names":["createBaseContext"],"sources":["../../../src/adapters/electron/renderer.ts"],"sourcesContent":["import type { IpcRenderer, IpcRendererListener } from '@electron-toolkit/preload'\n\nimport type { EventContext } from '../../context'\nimport type { DirectionalEventa, Eventa } from '../../eventa'\n\nimport { createContext as createBaseContext } from '../../context'\nimport { and, defineInboundEventa, defineOutboundEventa, EventaFlowDirection, matchBy } from '../../eventa'\nimport { generatePayload, parsePayload } from './internal'\nimport { errorEvent } from './shared'\n\nexport function createContext(ipcRenderer: IpcRenderer, options?: {\n messageEventName?: string | false\n errorEventName?: string | false\n extraListeners?: Record<string, IpcRendererListener>\n}) {\n const ctx = createBaseContext() as EventContext<any, { raw: { ipcRendererEvent: Electron.IpcRendererEvent, event: Event | unknown } }>\n\n const {\n messageEventName = 'eventa-message',\n errorEventName = 'eventa-error',\n extraListeners = {},\n } = options || {}\n\n const cleanupRemoval: Array<{ remove: () => void }> = []\n\n ctx.on(and(\n matchBy((e: DirectionalEventa<any>) => e._flowDirection === EventaFlowDirection.Outbound || !e._flowDirection),\n matchBy('*'),\n ), (event) => {\n const eventBody = generatePayload(event.id, { ...defineOutboundEventa(event.type), ...event })\n if (messageEventName !== false) {\n try {\n ipcRenderer.send(messageEventName, eventBody)\n }\n catch (error) {\n if (!(error instanceof Error) || error?.message !== 'Object has been destroyed') {\n throw error\n }\n }\n }\n })\n\n if (messageEventName) {\n ipcRenderer.on(messageEventName, (ipcRendererEvent, event) => {\n try {\n const { type, payload } = parsePayload<Eventa<any>>(event)\n ctx.emit(defineInboundEventa(type), payload.body, { raw: { ipcRendererEvent, event } })\n }\n catch (error) {\n console.error('Failed to parse IpcRenderer message:', error)\n ctx.emit(errorEvent, { error }, { raw: { ipcRendererEvent, event } })\n }\n })\n }\n\n if (errorEventName) {\n ipcRenderer.on(errorEventName, (ipcRendererEvent, error) => {\n ctx.emit(errorEvent, { error }, { raw: { ipcRendererEvent, event: error } })\n })\n }\n\n for (const [eventName, listener] of Object.entries(extraListeners)) {\n ipcRenderer.on(eventName, listener)\n }\n\n return {\n context: ctx,\n dispose: () => {\n cleanupRemoval.forEach(removal => removal.remove())\n },\n }\n}\n\nexport type * from './shared'\n"],"mappings":";;;AAUA,SAAgB,cAAc,aAA0B,SAIrD;CACD,MAAM,MAAMA,iBAAmB;CAE/B,MAAM,EACJ,mBAAmB,kBACnB,iBAAiB,gBACjB,iBAAiB,EAAE,KACjB,WAAW,EAAE;CAEjB,MAAM,iBAAgD,EAAE;AAExD,KAAI,GAAG,IACL,SAAS,MAA8B,EAAE,mBAAA,cAAmD,CAAC,EAAE,eAAe,EAC9G,QAAQ,IAAI,CACb,GAAG,UAAU;EACZ,MAAM,YAAY,gBAAgB,MAAM,IAAI;GAAE,GAAG,qBAAqB,MAAM,KAAK;GAAE,GAAG;GAAO,CAAC;AAC9F,MAAI,qBAAqB,MACvB,KAAI;AACF,eAAY,KAAK,kBAAkB,UAAU;WAExC,OAAO;AACZ,OAAI,EAAE,iBAAiB,UAAU,OAAO,YAAY,4BAClD,OAAM;;GAIZ;AAEF,KAAI,iBACF,aAAY,GAAG,mBAAmB,kBAAkB,UAAU;AAC5D,MAAI;GACF,MAAM,EAAE,MAAM,YAAY,aAA0B,MAAM;AAC1D,OAAI,KAAK,oBAAoB,KAAK,EAAE,QAAQ,MAAM,EAAE,KAAK;IAAE;IAAkB;IAAO,EAAE,CAAC;WAElF,OAAO;AACZ,WAAQ,MAAM,wCAAwC,MAAM;AAC5D,OAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK;IAAE;IAAkB;IAAO,EAAE,CAAC;;GAEvE;AAGJ,KAAI,eACF,aAAY,GAAG,iBAAiB,kBAAkB,UAAU;AAC1D,MAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK;GAAE;GAAkB,OAAO;GAAO,EAAE,CAAC;GAC5E;AAGJ,MAAK,MAAM,CAAC,WAAW,aAAa,OAAO,QAAQ,eAAe,CAChE,aAAY,GAAG,WAAW,SAAS;AAGrC,QAAO;EACL,SAAS;EACT,eAAe;AACb,kBAAe,SAAQ,YAAW,QAAQ,QAAQ,CAAC;;EAEtD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as EventTag, s as EventaType } from "../../eventa-
|
|
2
|
-
import { t as EventContext } from "../../context-
|
|
1
|
+
import { n as EventTag, s as EventaType } from "../../eventa-DSnoBa1O.mjs";
|
|
2
|
+
import { t as EventContext } from "../../context-BsqFeoer.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/adapters/event-emitter/shared.d.ts
|
|
5
5
|
interface Payload<T> {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { a as defineEventa, c as matchBy, i as and, l as nanoid,
|
|
2
|
-
import "../../src-
|
|
3
|
-
|
|
1
|
+
import { a as defineEventa, c as matchBy, i as and, l as nanoid, o as defineInboundEventa, s as defineOutboundEventa, t as createContext$1 } from "../../context-Dht_IZMb.mjs";
|
|
2
|
+
import "../../src-CTs6h4Ci.mjs";
|
|
4
3
|
//#region src/adapters/event-emitter/internal.ts
|
|
5
4
|
function generatePayload(type, payload) {
|
|
6
5
|
return {
|
|
@@ -12,11 +11,9 @@ function generatePayload(type, payload) {
|
|
|
12
11
|
function parsePayload(data) {
|
|
13
12
|
return data;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
//#endregion
|
|
17
15
|
//#region src/adapters/event-emitter/shared.ts
|
|
18
16
|
const errorEvent = { ...defineEventa() };
|
|
19
|
-
|
|
20
17
|
//#endregion
|
|
21
18
|
//#region src/adapters/event-emitter/index.ts
|
|
22
19
|
function withRemoval(eventTarget, type, listener) {
|
|
@@ -29,7 +26,7 @@ function createContext(eventTarget, options) {
|
|
|
29
26
|
const ctx = createContext$1();
|
|
30
27
|
const { messageEventName = "message", errorEventName = "error", extraListeners = {} } = options || {};
|
|
31
28
|
const cleanupRemoval = [];
|
|
32
|
-
ctx.on(and(matchBy((e) => e._flowDirection ===
|
|
29
|
+
ctx.on(and(matchBy((e) => e._flowDirection === "outbound" || !e._flowDirection), matchBy("*")), (event) => {
|
|
33
30
|
const detail = generatePayload(event.id, {
|
|
34
31
|
...defineOutboundEventa(event.type),
|
|
35
32
|
...event
|
|
@@ -56,7 +53,7 @@ function createContext(eventTarget, options) {
|
|
|
56
53
|
}
|
|
57
54
|
};
|
|
58
55
|
}
|
|
59
|
-
|
|
60
56
|
//#endregion
|
|
61
57
|
export { createContext };
|
|
58
|
+
|
|
62
59
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["createBaseContext"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["createBaseContext"],"sources":["../../../src/adapters/event-emitter/internal.ts","../../../src/adapters/event-emitter/shared.ts","../../../src/adapters/event-emitter/index.ts"],"sourcesContent":["import type { EventTag } from '../..'\nimport type { Payload as CustomEventDetailDetail } from './shared'\n\nimport { nanoid } from '../..'\n\nexport function generatePayload<T>(type: EventTag<any, any>, payload: T): CustomEventDetailDetail<T> {\n return {\n id: nanoid(),\n type,\n payload,\n }\n}\n\nexport function parsePayload<T>(data: unknown): CustomEventDetailDetail<T> {\n return data as CustomEventDetailDetail<T>\n}\n","import type { EventTag } from '../../eventa'\n\nimport { defineEventa } from '../../eventa'\n\nexport interface Payload<T> {\n id: string\n type: EventTag<any, any>\n payload: T\n}\n\nexport const errorEvent = { ...defineEventa<{ error: unknown }>() }\n","import type { EventContext } from '../../context'\nimport type { DirectionalEventa, Eventa } from '../../eventa'\n\nimport { createContext as createBaseContext } from '../../context'\nimport { and, defineInboundEventa, defineOutboundEventa, EventaFlowDirection, matchBy } from '../../eventa'\nimport { generatePayload, parsePayload } from './internal'\nimport { errorEvent } from './shared'\n\nfunction withRemoval(eventTarget: NodeJS.EventEmitter, type: string, listener: Parameters<NodeJS.EventEmitter['on']>[1]) {\n eventTarget.on(type, listener)\n\n return {\n remove: () => {\n eventTarget.off(type, listener)\n },\n }\n}\n\nexport function createContext(eventTarget: NodeJS.EventEmitter, options?: {\n messageEventName?: string | false\n errorEventName?: string | false\n extraListeners?: Record<string, (event: Event) => void | Promise<void>>\n}) {\n const ctx = createBaseContext() as EventContext<any, { raw: { event: CustomEvent | Event | unknown } }>\n\n const {\n messageEventName = 'message',\n errorEventName = 'error',\n extraListeners = {},\n } = options || {}\n\n const cleanupRemoval: Array<{ remove: () => void }> = []\n\n ctx.on(and(\n matchBy((e: DirectionalEventa<any>) => e._flowDirection === EventaFlowDirection.Outbound || !e._flowDirection),\n matchBy('*'),\n ), (event) => {\n const detail = generatePayload(event.id, { ...defineOutboundEventa(event.type), ...event })\n eventTarget.emit(event.id, detail)\n })\n\n if (messageEventName) {\n cleanupRemoval.push(withRemoval(eventTarget, messageEventName, (event) => {\n try {\n const { type, payload } = parsePayload<Eventa<any>>((event as CustomEvent).detail)\n ctx.emit(defineInboundEventa(type), payload.body, { raw: { event } })\n }\n catch (error) {\n console.error('Failed to parse EventEmitter message:', error)\n ctx.emit(errorEvent, { error }, { raw: { event } })\n }\n }))\n }\n\n if (errorEventName) {\n cleanupRemoval.push(withRemoval(eventTarget, errorEventName, (error) => {\n ctx.emit(errorEvent, { error }, { raw: { event: error } })\n }))\n }\n\n for (const [eventName, listener] of Object.entries(extraListeners)) {\n cleanupRemoval.push(withRemoval(eventTarget, eventName, listener))\n }\n\n return {\n context: ctx,\n dispose: () => {\n cleanupRemoval.forEach(removal => removal.remove())\n },\n }\n}\n\nexport type * from './shared'\n"],"mappings":";;;AAKA,SAAgB,gBAAmB,MAA0B,SAAwC;AACnG,QAAO;EACL,IAAI,QAAQ;EACZ;EACA;EACD;;AAGH,SAAgB,aAAgB,MAA2C;AACzE,QAAO;;;;ACJT,MAAa,aAAa,EAAE,GAAG,cAAkC,EAAE;;;ACFnE,SAAS,YAAY,aAAkC,MAAc,UAAoD;AACvH,aAAY,GAAG,MAAM,SAAS;AAE9B,QAAO,EACL,cAAc;AACZ,cAAY,IAAI,MAAM,SAAS;IAElC;;AAGH,SAAgB,cAAc,aAAkC,SAI7D;CACD,MAAM,MAAMA,iBAAmB;CAE/B,MAAM,EACJ,mBAAmB,WACnB,iBAAiB,SACjB,iBAAiB,EAAE,KACjB,WAAW,EAAE;CAEjB,MAAM,iBAAgD,EAAE;AAExD,KAAI,GAAG,IACL,SAAS,MAA8B,EAAE,mBAAA,cAAmD,CAAC,EAAE,eAAe,EAC9G,QAAQ,IAAI,CACb,GAAG,UAAU;EACZ,MAAM,SAAS,gBAAgB,MAAM,IAAI;GAAE,GAAG,qBAAqB,MAAM,KAAK;GAAE,GAAG;GAAO,CAAC;AAC3F,cAAY,KAAK,MAAM,IAAI,OAAO;GAClC;AAEF,KAAI,iBACF,gBAAe,KAAK,YAAY,aAAa,mBAAmB,UAAU;AACxE,MAAI;GACF,MAAM,EAAE,MAAM,YAAY,aAA2B,MAAsB,OAAO;AAClF,OAAI,KAAK,oBAAoB,KAAK,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;WAEhE,OAAO;AACZ,WAAQ,MAAM,yCAAyC,MAAM;AAC7D,OAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;;GAErD,CAAC;AAGL,KAAI,eACF,gBAAe,KAAK,YAAY,aAAa,iBAAiB,UAAU;AACtE,MAAI,KAAK,YAAY,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,OAAO,EAAE,CAAC;GAC1D,CAAC;AAGL,MAAK,MAAM,CAAC,WAAW,aAAa,OAAO,QAAQ,eAAe,CAChE,gBAAe,KAAK,YAAY,aAAa,WAAW,SAAS,CAAC;AAGpE,QAAO;EACL,SAAS;EACT,eAAe;AACb,kBAAe,SAAQ,YAAW,QAAQ,QAAQ,CAAC;;EAEtD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as EventTag, s as EventaType } from "../../eventa-
|
|
2
|
-
import { t as EventContext } from "../../context-
|
|
1
|
+
import { n as EventTag, s as EventaType } from "../../eventa-DSnoBa1O.mjs";
|
|
2
|
+
import { t as EventContext } from "../../context-BsqFeoer.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/adapters/event-target/shared.d.ts
|
|
5
5
|
interface CustomEventDetail<T> {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { a as defineEventa, c as matchBy, i as and, l as nanoid,
|
|
2
|
-
import "../../src-
|
|
3
|
-
|
|
1
|
+
import { a as defineEventa, c as matchBy, i as and, l as nanoid, o as defineInboundEventa, s as defineOutboundEventa, t as createContext$1 } from "../../context-Dht_IZMb.mjs";
|
|
2
|
+
import "../../src-CTs6h4Ci.mjs";
|
|
4
3
|
//#region src/adapters/event-target/internal.ts
|
|
5
4
|
function generateCustomEventDetail(type, payload) {
|
|
6
5
|
return {
|
|
@@ -12,11 +11,9 @@ function generateCustomEventDetail(type, payload) {
|
|
|
12
11
|
function parseCustomEventDetail(data) {
|
|
13
12
|
return data;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
//#endregion
|
|
17
15
|
//#region src/adapters/event-target/shared.ts
|
|
18
16
|
const workerErrorEvent = { ...defineEventa() };
|
|
19
|
-
|
|
20
17
|
//#endregion
|
|
21
18
|
//#region src/adapters/event-target/index.ts
|
|
22
19
|
function withRemoval(eventTarget, type, listener) {
|
|
@@ -29,12 +26,12 @@ function createContext(eventTarget, options) {
|
|
|
29
26
|
const ctx = createContext$1();
|
|
30
27
|
const { messageEventName = "message", errorEventName = "error", extraListeners = {} } = options || {};
|
|
31
28
|
const cleanupRemoval = [];
|
|
32
|
-
ctx.on(and(matchBy((e) => e._flowDirection ===
|
|
29
|
+
ctx.on(and(matchBy((e) => e._flowDirection === "outbound" || !e._flowDirection), matchBy("*")), (event) => {
|
|
33
30
|
const detail = generateCustomEventDetail(event.id, {
|
|
34
31
|
...defineOutboundEventa(event.type),
|
|
35
32
|
...event
|
|
36
33
|
});
|
|
37
|
-
const customEvent = new CustomEvent(messageEventName ||
|
|
34
|
+
const customEvent = new CustomEvent(messageEventName || "event", {
|
|
38
35
|
detail,
|
|
39
36
|
bubbles: true,
|
|
40
37
|
cancelable: true
|
|
@@ -61,7 +58,7 @@ function createContext(eventTarget, options) {
|
|
|
61
58
|
}
|
|
62
59
|
};
|
|
63
60
|
}
|
|
64
|
-
|
|
65
61
|
//#endregion
|
|
66
62
|
export { createContext };
|
|
63
|
+
|
|
67
64
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["createBaseContext"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["createBaseContext"],"sources":["../../../src/adapters/event-target/internal.ts","../../../src/adapters/event-target/shared.ts","../../../src/adapters/event-target/index.ts"],"sourcesContent":["import type { EventTag } from '../..'\nimport type { CustomEventDetail as CustomEventDetailDetail } from './shared'\n\nimport { nanoid } from '../..'\n\nexport function generateCustomEventDetail<T>(type: EventTag<any, any>, payload: T): CustomEventDetailDetail<T> {\n return {\n id: nanoid(),\n type,\n payload,\n }\n}\n\nexport function parseCustomEventDetail<T>(data: unknown): CustomEventDetailDetail<T> {\n return data as CustomEventDetailDetail<T>\n}\n","import type { EventTag } from '../../eventa'\n\nimport { defineEventa } from '../../eventa'\n\nexport interface CustomEventDetail<T> {\n id: string\n type: EventTag<any, any>\n payload: T\n}\n\nexport const workerErrorEvent = { ...defineEventa<{ error: unknown }>() }\n","import type { EventContext } from '../../context'\nimport type { DirectionalEventa, Eventa } from '../../eventa'\n\nimport { createContext as createBaseContext } from '../../context'\nimport { and, defineInboundEventa, defineOutboundEventa, EventaFlowDirection, EventaType, matchBy } from '../../eventa'\nimport { generateCustomEventDetail, parseCustomEventDetail } from './internal'\nimport { workerErrorEvent } from './shared'\n\nfunction withRemoval(eventTarget: EventTarget, type: string, listener: EventListenerOrEventListenerObject | null) {\n eventTarget.addEventListener(type, listener)\n\n return {\n remove: () => {\n eventTarget.removeEventListener(type, listener)\n },\n }\n}\n\nexport function createContext(eventTarget: EventTarget, options?: {\n messageEventName?: string | false\n errorEventName?: string | false\n extraListeners?: Record<string, (event: Event) => void | Promise<void>>\n}) {\n const ctx = createBaseContext() as EventContext<any, { raw: { event: CustomEvent | Event | unknown } }>\n\n const {\n messageEventName = 'message',\n errorEventName = 'error',\n extraListeners = {},\n } = options || {}\n\n const cleanupRemoval: Array<{ remove: () => void }> = []\n\n ctx.on(and(\n matchBy((e: DirectionalEventa<any>) => e._flowDirection === EventaFlowDirection.Outbound || !e._flowDirection),\n matchBy('*'),\n ), (event) => {\n const detail = generateCustomEventDetail(event.id, { ...defineOutboundEventa(event.type), ...event })\n\n const customEvent = new CustomEvent(messageEventName || EventaType.Event, {\n detail,\n bubbles: true,\n cancelable: true,\n })\n\n eventTarget.dispatchEvent(customEvent)\n })\n\n if (messageEventName) {\n cleanupRemoval.push(withRemoval(eventTarget, messageEventName, (event) => {\n try {\n const { type, payload } = parseCustomEventDetail<Eventa<any>>((event as CustomEvent).detail)\n ctx.emit(defineInboundEventa(type), payload.body, { raw: { event } })\n }\n catch (error) {\n console.error('Failed to parse WebSocket message:', error)\n ctx.emit(workerErrorEvent, { error }, { raw: { event } })\n }\n }))\n }\n\n if (errorEventName) {\n cleanupRemoval.push(withRemoval(eventTarget, errorEventName, (error) => {\n ctx.emit(workerErrorEvent, { error }, { raw: { event: error } })\n }))\n }\n\n for (const [eventName, listener] of Object.entries(extraListeners)) {\n cleanupRemoval.push(withRemoval(eventTarget, eventName, listener))\n }\n\n return {\n context: ctx,\n dispose: () => {\n cleanupRemoval.forEach(removal => removal.remove())\n },\n }\n}\n\nexport type * from './shared'\n"],"mappings":";;;AAKA,SAAgB,0BAA6B,MAA0B,SAAwC;AAC7G,QAAO;EACL,IAAI,QAAQ;EACZ;EACA;EACD;;AAGH,SAAgB,uBAA0B,MAA2C;AACnF,QAAO;;;;ACJT,MAAa,mBAAmB,EAAE,GAAG,cAAkC,EAAE;;;ACFzE,SAAS,YAAY,aAA0B,MAAc,UAAqD;AAChH,aAAY,iBAAiB,MAAM,SAAS;AAE5C,QAAO,EACL,cAAc;AACZ,cAAY,oBAAoB,MAAM,SAAS;IAElD;;AAGH,SAAgB,cAAc,aAA0B,SAIrD;CACD,MAAM,MAAMA,iBAAmB;CAE/B,MAAM,EACJ,mBAAmB,WACnB,iBAAiB,SACjB,iBAAiB,EAAE,KACjB,WAAW,EAAE;CAEjB,MAAM,iBAAgD,EAAE;AAExD,KAAI,GAAG,IACL,SAAS,MAA8B,EAAE,mBAAA,cAAmD,CAAC,EAAE,eAAe,EAC9G,QAAQ,IAAI,CACb,GAAG,UAAU;EACZ,MAAM,SAAS,0BAA0B,MAAM,IAAI;GAAE,GAAG,qBAAqB,MAAM,KAAK;GAAE,GAAG;GAAO,CAAC;EAErG,MAAM,cAAc,IAAI,YAAY,oBAAA,SAAsC;GACxE;GACA,SAAS;GACT,YAAY;GACb,CAAC;AAEF,cAAY,cAAc,YAAY;GACtC;AAEF,KAAI,iBACF,gBAAe,KAAK,YAAY,aAAa,mBAAmB,UAAU;AACxE,MAAI;GACF,MAAM,EAAE,MAAM,YAAY,uBAAqC,MAAsB,OAAO;AAC5F,OAAI,KAAK,oBAAoB,KAAK,EAAE,QAAQ,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;WAEhE,OAAO;AACZ,WAAQ,MAAM,sCAAsC,MAAM;AAC1D,OAAI,KAAK,kBAAkB,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;;GAE3D,CAAC;AAGL,KAAI,eACF,gBAAe,KAAK,YAAY,aAAa,iBAAiB,UAAU;AACtE,MAAI,KAAK,kBAAkB,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,OAAO,EAAE,CAAC;GAChE,CAAC;AAGL,MAAK,MAAM,CAAC,WAAW,aAAa,OAAO,QAAQ,eAAe,CAChE,gBAAe,KAAK,YAAY,aAAa,WAAW,SAAS,CAAC;AAGpE,QAAO;EACL,SAAS;EACT,eAAe;AACb,kBAAe,SAAQ,YAAW,QAAQ,QAAQ,CAAC;;EAEtD"}
|