@entifix/react-integration 0.10.2 → 0.11.0
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.
|
@@ -18,5 +18,13 @@ import type { ReactiveChannel } from './reactive-channel.js';
|
|
|
18
18
|
* last, so a page that mounts no workspace holds none. One connection per URL,
|
|
19
19
|
* not one per listener: SSE shares the origin's HTTP/1.1 connection budget.
|
|
20
20
|
*/
|
|
21
|
-
export
|
|
21
|
+
export interface EventSourceReactiveChannel extends ReactiveChannel {
|
|
22
|
+
/**
|
|
23
|
+
* How many frames this channel dropped because they did not decode as an
|
|
24
|
+
* entity change. A frame is dropped rather than thrown — see `deliver` — so
|
|
25
|
+
* the count is the only trace a drifted server leaves in the browser.
|
|
26
|
+
*/
|
|
27
|
+
dropped(): number;
|
|
28
|
+
}
|
|
29
|
+
export declare function makeEventSourceReactiveChannel(url: string): EventSourceReactiveChannel;
|
|
22
30
|
//# sourceMappingURL=event-source-channel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-source-channel.d.ts","sourceRoot":"","sources":["../../src/reactive/event-source-channel.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"event-source-channel.d.ts","sourceRoot":"","sources":["../../src/reactive/event-source-channel.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAEV,eAAe,EAChB,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,0BAA2B,SAAQ,eAAe;IACjE;;;;OAIG;IACH,OAAO,IAAI,MAAM,CAAC;CACnB;AAED,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,MAAM,GACV,0BAA0B,CAuF5B"}
|
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* **Same-origin is the whole reason this is SSE and not a WebSocket.** `entifix_at`
|
|
7
|
-
* is `httpOnly` and `sameSite: 'lax'`, and the `WebSocket` constructor accepts
|
|
8
|
-
* no headers — so a socket would need a second class of bearer token handed to
|
|
9
|
-
* client JavaScript, which is the one thing `httpOnly` protects against. An
|
|
10
|
-
* `EventSource` against the app's own proxy path sends the cookie the way every
|
|
11
|
-
* other call in the repo does.
|
|
12
|
-
*
|
|
13
|
-
* What it also gets for free is the half a hand-written client would need:
|
|
14
|
-
* `EventSource` reconnects on its own with backoff and re-sends `Last-Event-ID`.
|
|
15
|
-
* The server accepts that header and ignores it — the stream is a hint that
|
|
16
|
-
* something changed, and the record behind it is the truth.
|
|
17
|
-
*
|
|
18
|
-
* The connection is opened lazily on the first subscriber and closed with the
|
|
19
|
-
* last, so a page that mounts no workspace holds none. One connection per URL,
|
|
20
|
-
* not one per listener: SSE shares the origin's HTTP/1.1 connection budget.
|
|
21
|
-
*/ export function makeEventSourceReactiveChannel(url) {
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
import { EntityChangeEnvelope } from '@entifix/core';
|
|
3
|
+
import { Effect, Result, Schema } from 'effect';
|
|
4
|
+
export function makeEventSourceReactiveChannel(url) {
|
|
22
5
|
const listeners = new Set();
|
|
23
6
|
const connectListeners = new Set();
|
|
24
7
|
let source;
|
|
@@ -27,16 +10,22 @@ import { Effect, Result } from 'effect';
|
|
|
27
10
|
// consumer needs to know is "has this channel been connected", not "is this
|
|
28
11
|
// particular `EventSource` object open right now".
|
|
29
12
|
let connected = false;
|
|
13
|
+
let dropped = 0;
|
|
30
14
|
const deliver = (raw)=>{
|
|
31
15
|
// A frame this build cannot read is dropped rather than thrown: `onmessage`
|
|
32
16
|
// runs inside the browser's event loop, where a throw reaches no caller and
|
|
33
17
|
// would take the rest of this delivery's listeners with it.
|
|
34
|
-
const parsed = Effect.runSync(Effect.result(Effect.flatMap(Effect.try(()=>JSON.parse(raw)),
|
|
18
|
+
const parsed = Effect.runSync(Effect.result(Effect.flatMap(Effect.try(()=>JSON.parse(raw)), Schema.decodeUnknownEffect(EntityChangeEnvelope))));
|
|
35
19
|
if (Result.isFailure(parsed)) {
|
|
20
|
+
dropped += 1;
|
|
36
21
|
return;
|
|
37
22
|
}
|
|
23
|
+
const { meta, data } = parsed.success;
|
|
24
|
+
const event = _extends({}, meta.event, {
|
|
25
|
+
data
|
|
26
|
+
});
|
|
38
27
|
for (const listener of listeners){
|
|
39
|
-
listener(
|
|
28
|
+
listener(event);
|
|
40
29
|
}
|
|
41
30
|
};
|
|
42
31
|
const open = ()=>{
|
|
@@ -65,6 +54,7 @@ import { Effect, Result } from 'effect';
|
|
|
65
54
|
connected = false;
|
|
66
55
|
};
|
|
67
56
|
return {
|
|
57
|
+
dropped: ()=>dropped,
|
|
68
58
|
subscribe (listener) {
|
|
69
59
|
listeners.add(listener);
|
|
70
60
|
open();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/reactive/event-source-channel.ts"],"sourcesContent":["import { type EntityChangeEvent
|
|
1
|
+
{"version":3,"sources":["../../src/reactive/event-source-channel.ts"],"sourcesContent":["import {\n type DomainEvent,\n EntityChangeEnvelope,\n type EntityChangeEvent,\n} from '@entifix/core';\nimport { Effect, Result, Schema } from 'effect';\n\nimport type {\n EntityChangeListener,\n ReactiveChannel,\n} from './reactive-channel.js';\n\n/**\n * The reactive stream's browser end: one `EventSource` over a same-origin URL.\n *\n * **Same-origin is the whole reason this is SSE and not a WebSocket.** `entifix_at`\n * is `httpOnly` and `sameSite: 'lax'`, and the `WebSocket` constructor accepts\n * no headers — so a socket would need a second class of bearer token handed to\n * client JavaScript, which is the one thing `httpOnly` protects against. An\n * `EventSource` against the app's own proxy path sends the cookie the way every\n * other call in the repo does.\n *\n * What it also gets for free is the half a hand-written client would need:\n * `EventSource` reconnects on its own with backoff and re-sends `Last-Event-ID`.\n * The server accepts that header and ignores it — the stream is a hint that\n * something changed, and the record behind it is the truth.\n *\n * The connection is opened lazily on the first subscriber and closed with the\n * last, so a page that mounts no workspace holds none. One connection per URL,\n * not one per listener: SSE shares the origin's HTTP/1.1 connection budget.\n */\nexport interface EventSourceReactiveChannel extends ReactiveChannel {\n /**\n * How many frames this channel dropped because they did not decode as an\n * entity change. A frame is dropped rather than thrown — see `deliver` — so\n * the count is the only trace a drifted server leaves in the browser.\n */\n dropped(): number;\n}\n\nexport function makeEventSourceReactiveChannel(\n url: string,\n): EventSourceReactiveChannel {\n const listeners = new Set<EntityChangeListener>();\n const connectListeners = new Set<() => void>();\n let source: EventSource | undefined;\n // Tracked here rather than read off `source.readyState`, because it must\n // survive the close/reopen cycle the refcount drives: what a reconciling\n // consumer needs to know is \"has this channel been connected\", not \"is this\n // particular `EventSource` object open right now\".\n let connected = false;\n let dropped = 0;\n\n const deliver = (raw: string): void => {\n // A frame this build cannot read is dropped rather than thrown: `onmessage`\n // runs inside the browser's event loop, where a throw reaches no caller and\n // would take the rest of this delivery's listeners with it.\n const parsed = Effect.runSync(\n Effect.result(\n Effect.flatMap(\n Effect.try(() => JSON.parse(raw) as unknown),\n Schema.decodeUnknownEffect(EntityChangeEnvelope),\n ),\n ),\n );\n if (Result.isFailure(parsed)) {\n dropped += 1;\n return;\n }\n const { meta, data } = parsed.success;\n const event: DomainEvent<EntityChangeEvent> = { ...meta.event, data };\n\n for (const listener of listeners) {\n listener(event);\n }\n };\n\n const open = (): void => {\n // Guarded because a server-rendered pass has no `EventSource` at all, and a\n // channel built at module scope must not throw there.\n if (source !== undefined || typeof EventSource === 'undefined') {\n return;\n }\n source = new EventSource(url);\n source.onmessage = message => {\n deliver(message.data as string);\n };\n // Fires on the first open *and* on every browser-driven reconnect, because\n // `EventSource` reconnects the same object rather than handing back a new\n // one — so nothing has to re-arm this.\n source.onopen = () => {\n connected = true;\n for (const listener of connectListeners) {\n listener();\n }\n };\n };\n\n const close = (): void => {\n source?.close();\n source = undefined;\n connected = false;\n };\n\n return {\n dropped: () => dropped,\n subscribe(listener) {\n listeners.add(listener);\n open();\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n close();\n }\n };\n },\n onConnect(listener) {\n connectListeners.add(listener);\n // ⚠️ The replay, and the reason this signal works at all. Registering does\n // *not* open the connection — subscribing is what does that — so a\n // consumer arriving after another one opened the stream would otherwise\n // wait for a network drop that may never come, and a restored pending set\n // would never be reconciled.\n if (connected) listener();\n return () => {\n connectListeners.delete(listener);\n };\n },\n };\n}\n"],"names":["EntityChangeEnvelope","Effect","Result","Schema","makeEventSourceReactiveChannel","url","listeners","Set","connectListeners","source","connected","dropped","deliver","raw","parsed","runSync","result","flatMap","try","JSON","parse","decodeUnknownEffect","isFailure","meta","data","success","event","listener","open","undefined","EventSource","onmessage","message","onopen","close","subscribe","add","delete","size","onConnect"],"mappings":";AAAA,SAEEA,oBAAoB,QAEf,gBAAgB;AACvB,SAASC,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAmChD,OAAO,SAASC,+BACdC,GAAW;IAEX,MAAMC,YAAY,IAAIC;IACtB,MAAMC,mBAAmB,IAAID;IAC7B,IAAIE;IACJ,yEAAyE;IACzE,yEAAyE;IACzE,4EAA4E;IAC5E,mDAAmD;IACnD,IAAIC,YAAY;IAChB,IAAIC,UAAU;IAEd,MAAMC,UAAU,CAACC;QACf,4EAA4E;QAC5E,4EAA4E;QAC5E,4DAA4D;QAC5D,MAAMC,SAASb,OAAOc,OAAO,CAC3Bd,OAAOe,MAAM,CACXf,OAAOgB,OAAO,CACZhB,OAAOiB,GAAG,CAAC,IAAMC,KAAKC,KAAK,CAACP,OAC5BV,OAAOkB,mBAAmB,CAACrB;QAIjC,IAAIE,OAAOoB,SAAS,CAACR,SAAS;YAC5BH,WAAW;YACX;QACF;QACA,MAAM,EAAEY,IAAI,EAAEC,IAAI,EAAE,GAAGV,OAAOW,OAAO;QACrC,MAAMC,QAAwC,aAAKH,KAAKG,KAAK;YAAEF;;QAE/D,KAAK,MAAMG,YAAYrB,UAAW;YAChCqB,SAASD;QACX;IACF;IAEA,MAAME,OAAO;QACX,4EAA4E;QAC5E,sDAAsD;QACtD,IAAInB,WAAWoB,aAAa,OAAOC,gBAAgB,aAAa;YAC9D;QACF;QACArB,SAAS,IAAIqB,YAAYzB;QACzBI,OAAOsB,SAAS,GAAGC,CAAAA;YACjBpB,QAAQoB,QAAQR,IAAI;QACtB;QACA,2EAA2E;QAC3E,0EAA0E;QAC1E,uCAAuC;QACvCf,OAAOwB,MAAM,GAAG;YACdvB,YAAY;YACZ,KAAK,MAAMiB,YAAYnB,iBAAkB;gBACvCmB;YACF;QACF;IACF;IAEA,MAAMO,QAAQ;QACZzB,0BAAAA,OAAQyB,KAAK;QACbzB,SAASoB;QACTnB,YAAY;IACd;IAEA,OAAO;QACLC,SAAS,IAAMA;QACfwB,WAAUR,QAAQ;YAChBrB,UAAU8B,GAAG,CAACT;YACdC;YACA,OAAO;gBACLtB,UAAU+B,MAAM,CAACV;gBACjB,IAAIrB,UAAUgC,IAAI,KAAK,GAAG;oBACxBJ;gBACF;YACF;QACF;QACAK,WAAUZ,QAAQ;YAChBnB,iBAAiB4B,GAAG,CAACT;YACrB,2EAA2E;YAC3E,mEAAmE;YACnE,wEAAwE;YACxE,0EAA0E;YAC1E,6BAA6B;YAC7B,IAAIjB,WAAWiB;YACf,OAAO;gBACLnB,iBAAiB6B,MAAM,CAACV;YAC1B;QACF;IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"version": "0.
|
|
24
|
+
"version": "0.11.0",
|
|
25
25
|
"description": "React hooks that bind entities to data: TanStack Query and Form over the entity adapters, drafts, mutations and live invalidation.",
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"repository": {
|
|
@@ -65,16 +65,16 @@
|
|
|
65
65
|
"LICENSE"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@entifix/
|
|
69
|
-
"@entifix/
|
|
70
|
-
"@entifix/
|
|
68
|
+
"@entifix/core": "0.11.0",
|
|
69
|
+
"@entifix/business": "0.11.0",
|
|
70
|
+
"@entifix/transactions": "0.11.0"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
73
|
"@tanstack/react-form": "1.33.5",
|
|
74
74
|
"@tanstack/react-query": "^5.104.5",
|
|
75
75
|
"effect": "4.0.6",
|
|
76
76
|
"react": "19.3.1",
|
|
77
|
-
"@entifix/testing-unit": "0.
|
|
77
|
+
"@entifix/testing-unit": "0.11.0"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
80
|
"@tanstack/react-form": ">=1.33 <2",
|