@songfreedom/sse-client 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +301 -2
- package/batch.d.ts +39 -0
- package/batch.d.ts.map +1 -0
- package/batch.js +44 -0
- package/batch.js.map +1 -0
- package/client.d.ts +197 -0
- package/client.d.ts.map +1 -0
- package/client.js +625 -0
- package/client.js.map +1 -0
- package/errors.d.ts +33 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +47 -0
- package/errors.js.map +1 -0
- package/index.d.ts +13 -0
- package/index.d.ts.map +1 -0
- package/index.js +7 -0
- package/index.js.map +1 -0
- package/package.json +47 -3
- package/parser.d.ts +35 -0
- package/parser.d.ts.map +1 -0
- package/parser.js +118 -0
- package/parser.js.map +1 -0
- package/share.d.ts +29 -0
- package/share.d.ts.map +1 -0
- package/share.js +67 -0
- package/share.js.map +1 -0
- package/stream.d.ts +24 -0
- package/stream.d.ts.map +1 -0
- package/stream.js +65 -0
- package/stream.js.map +1 -0
- package/types.d.ts +22 -0
- package/types.d.ts.map +1 -0
- package/types.js +2 -0
- package/types.js.map +1 -0
- package/vue.d.ts +45 -0
- package/vue.d.ts.map +1 -0
- package/vue.js +72 -0
- package/vue.js.map +1 -0
package/vue.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { getCurrentScope, onScopeDispose, ref, shallowRef, } from "vue";
|
|
2
|
+
import { SSEClient } from "./client.js";
|
|
3
|
+
/**
|
|
4
|
+
* Vue composable: auto-reconnect, message ring buffer, cleanup on unmount.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* interface AlarmEvent { level: "high" | "low"; deviceId: string }
|
|
8
|
+
* const { lastEvent, on, status } = useSSE<{ ALARM: AlarmEvent }>({
|
|
9
|
+
* url: "/api/push/subscribe",
|
|
10
|
+
* body: { topics: ["alarm"] },
|
|
11
|
+
* parse: (raw) => JSON.parse(raw),
|
|
12
|
+
* });
|
|
13
|
+
* on("ALARM", (alarm) => console.log(alarm.level));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function useSSE(options) {
|
|
17
|
+
const { immediate = true, maxEvents = 200, ...clientOptions } = options;
|
|
18
|
+
const status = ref("idle");
|
|
19
|
+
const lastEvent = shallowRef(null);
|
|
20
|
+
const events = shallowRef([]);
|
|
21
|
+
const error = shallowRef(null);
|
|
22
|
+
const client = new SSEClient({
|
|
23
|
+
...clientOptions,
|
|
24
|
+
autoStart: false,
|
|
25
|
+
onStatus: (next) => {
|
|
26
|
+
status.value = next;
|
|
27
|
+
clientOptions.onStatus?.(next);
|
|
28
|
+
},
|
|
29
|
+
onError: (err, attempt) => {
|
|
30
|
+
error.value = err;
|
|
31
|
+
clientOptions.onError?.(err, attempt);
|
|
32
|
+
},
|
|
33
|
+
onMessage: (message) => {
|
|
34
|
+
lastEvent.value = message;
|
|
35
|
+
if (maxEvents > 0) {
|
|
36
|
+
const keep = maxEvents - 1;
|
|
37
|
+
const next = keep > 0 ? events.value.slice(-keep) : [];
|
|
38
|
+
next.push(message);
|
|
39
|
+
// Replace the array so a shallowRef triggers, without deep-proxying payloads.
|
|
40
|
+
events.value = next;
|
|
41
|
+
}
|
|
42
|
+
clientOptions.onMessage?.(message);
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const start = () => {
|
|
46
|
+
error.value = null;
|
|
47
|
+
client.start();
|
|
48
|
+
};
|
|
49
|
+
const stop = () => {
|
|
50
|
+
client.close();
|
|
51
|
+
};
|
|
52
|
+
const restart = () => {
|
|
53
|
+
client.restart();
|
|
54
|
+
};
|
|
55
|
+
// Subscriptions follow the scope: unmounting a component (or a qiankun
|
|
56
|
+
// sub-app) unsubscribes automatically.
|
|
57
|
+
const on = ((event, handler) => {
|
|
58
|
+
const unsubscribe = client.on(event, handler);
|
|
59
|
+
if (getCurrentScope()) {
|
|
60
|
+
onScopeDispose(unsubscribe);
|
|
61
|
+
}
|
|
62
|
+
return unsubscribe;
|
|
63
|
+
});
|
|
64
|
+
if (immediate) {
|
|
65
|
+
start();
|
|
66
|
+
}
|
|
67
|
+
if (getCurrentScope()) {
|
|
68
|
+
onScopeDispose(stop);
|
|
69
|
+
}
|
|
70
|
+
return { client, status, lastEvent, events, error, start, stop, restart, on };
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=vue.js.map
|
package/vue.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vue.js","sourceRoot":"","sources":["../src/vue.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,cAAc,EACd,GAAG,EACH,UAAU,GAGX,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,SAAS,EAAyB,MAAM,aAAa,CAAC;AAgC/D;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CACpB,OAAsB;IAEtB,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,SAAS,GAAG,GAAG,EAAE,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IAExE,MAAM,MAAM,GAAG,GAAG,CAAY,MAAM,CAAC,CAAC;IACtC,MAAM,SAAS,GAAG,UAAU,CAAoB,IAAI,CAAC,CAAC;IACtD,MAAM,MAAM,GAAG,UAAU,CAAe,EAAE,CAAC,CAAC;IAC5C,MAAM,KAAK,GAAG,UAAU,CAAkB,IAAI,CAAC,CAAC;IAEhD,MAAM,MAAM,GAAG,IAAI,SAAS,CAAI;QAC9B,GAAG,aAAa;QAChB,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE;YACjB,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;YACpB,aAAa,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC;QACD,OAAO,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE;YACxB,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC;YAClB,aAAa,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