@hyper-fetch/sockets 9.0.3 → 9.2.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.
- package/__tests__/e2e/e2e.delivery.spec.ts +81 -0
- package/__tests__/features/adapter/adapter.sse.codec.spec.ts +101 -0
- package/__tests__/features/adapter/adapter.websocket.codec.spec.ts +208 -0
- package/__tests__/features/delivery/delivery.batch.spec.ts +203 -0
- package/__tests__/features/delivery/delivery.latest.spec.ts +254 -0
- package/__tests__/features/delivery/delivery.typescript.spec.ts +143 -0
- package/__tests__/features/delivery/delivery.utils.spec.ts +122 -0
- package/__tests__/features/listener/listener.delivery.spec.ts +385 -0
- package/__tests__/features/listener/listener.delivery.sse.spec.ts +55 -0
- package/__tests__/features/sdk/sdk.configuration.spec.ts +60 -0
- package/__tests__/features/sdk/sdk.types.spec.ts +41 -17
- package/__tests__/features/socket/socket.methods.spec.ts +40 -0
- package/dist/adapter-sse/sse-adapter.d.ts.map +1 -1
- package/dist/adapter-sse/sse-adapter.types.d.ts +11 -0
- package/dist/adapter-sse/sse-adapter.types.d.ts.map +1 -1
- package/dist/adapter-websockets/websocket-adapter.d.ts.map +1 -1
- package/dist/adapter-websockets/websocket-adapter.types.d.ts +23 -0
- package/dist/adapter-websockets/websocket-adapter.types.d.ts.map +1 -1
- package/dist/delivery/delivery.batch.d.ts +19 -0
- package/dist/delivery/delivery.batch.d.ts.map +1 -0
- package/dist/delivery/delivery.latest.d.ts +18 -0
- package/dist/delivery/delivery.latest.d.ts.map +1 -0
- package/dist/delivery/delivery.types.d.ts +113 -0
- package/dist/delivery/delivery.types.d.ts.map +1 -0
- package/dist/delivery/delivery.utils.d.ts +50 -0
- package/dist/delivery/delivery.utils.d.ts.map +1 -0
- package/dist/delivery/index.d.ts +5 -0
- package/dist/delivery/index.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +261 -20
- package/dist/index.mjs.map +1 -1
- package/dist/listener/listener.d.ts +22 -7
- package/dist/listener/listener.d.ts.map +1 -1
- package/dist/listener/listener.types.d.ts +29 -9
- package/dist/listener/listener.types.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +6 -2
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/dist/socket/socket.d.ts +8 -1
- package/dist/socket/socket.d.ts.map +1 -1
- package/dist/types/extract.types.d.ts +10 -4
- package/dist/types/extract.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter-sse/sse-adapter.ts +23 -4
- package/src/adapter-sse/sse-adapter.types.ts +9 -0
- package/src/adapter-websockets/websocket-adapter.ts +31 -6
- package/src/adapter-websockets/websocket-adapter.types.ts +20 -0
- package/src/delivery/delivery.batch.ts +71 -0
- package/src/delivery/delivery.latest.ts +64 -0
- package/src/delivery/delivery.types.ts +129 -0
- package/src/delivery/delivery.utils.ts +96 -0
- package/src/delivery/index.ts +4 -0
- package/src/index.ts +1 -0
- package/src/listener/listener.ts +48 -9
- package/src/listener/listener.types.ts +45 -10
- package/src/sdk/sdk.ts +27 -7
- package/src/socket/socket.ts +18 -2
- package/src/types/extract.types.ts +10 -4
- package/tsconfig.json +2 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LoggerManager } from "@hyper-fetch/core";
|
|
2
|
+
import { createWebsocketMockingServer, waitForConnection } from "@hyper-fetch/testing";
|
|
3
|
+
import { waitFor } from "@testing-library/dom";
|
|
2
4
|
import { WebsocketAdapter } from "adapter-websockets/websocket-adapter";
|
|
3
5
|
import { Socket } from "socket";
|
|
4
6
|
|
|
@@ -22,6 +24,44 @@ describe("Socket [ Methods ]", () => {
|
|
|
22
24
|
expect(queryParams).toBe("testing");
|
|
23
25
|
});
|
|
24
26
|
|
|
27
|
+
it("should allow to set url without touching the live connection", async () => {
|
|
28
|
+
const socket = new Socket({ url: "ws://localhost:1234", adapterOptions: { autoConnect: false } });
|
|
29
|
+
const reconnectSpy = vi.spyOn(socket.adapter, "reconnect");
|
|
30
|
+
|
|
31
|
+
expect(socket.setUrl("ws://localhost:4321")).toBe(socket);
|
|
32
|
+
expect(socket.url).toBe("ws://localhost:4321");
|
|
33
|
+
expect(reconnectSpy).not.toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should connect to the new url on reconnect and keep listeners", async () => {
|
|
37
|
+
// startServer() cleans every mocked server, so the second one is created only after the first started
|
|
38
|
+
const first = createWebsocketMockingServer("ws://localhost:1234");
|
|
39
|
+
first.startServer();
|
|
40
|
+
const second = createWebsocketMockingServer("ws://localhost:4321");
|
|
41
|
+
|
|
42
|
+
const socket = new Socket({ url: "ws://localhost:1234" });
|
|
43
|
+
const listener = socket.createListener<{ value: number }>()({ topic: "topic" });
|
|
44
|
+
const spy = vi.fn();
|
|
45
|
+
listener.listen(({ data }) => spy(data));
|
|
46
|
+
await waitForConnection(socket);
|
|
47
|
+
await first.getServer().connected;
|
|
48
|
+
|
|
49
|
+
socket.setUrl("ws://localhost:4321");
|
|
50
|
+
await socket.reconnect();
|
|
51
|
+
await second.getServer().connected;
|
|
52
|
+
await waitForConnection(socket);
|
|
53
|
+
|
|
54
|
+
second.emitListenerEvent(listener, { value: 2 });
|
|
55
|
+
|
|
56
|
+
await waitFor(() => {
|
|
57
|
+
expect(spy).toHaveBeenCalledWith({ value: 2 });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await socket.disconnect();
|
|
61
|
+
first.stopServer();
|
|
62
|
+
second.stopServer();
|
|
63
|
+
});
|
|
64
|
+
|
|
25
65
|
it("should allow to activate debug", async () => {
|
|
26
66
|
const socket = new Socket({ url: "ws://localhost:1234" });
|
|
27
67
|
socket.setDebug(true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKhD,OAAO,KAAK,EAAE,qBAAqB,
|
|
1
|
+
{"version":3,"file":"sse-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKhD,OAAO,KAAK,EAAE,qBAAqB,EAAyB,MAAM,qBAAqB,CAAC;AAGxF;;;;GAIG;AAEH,MAAM,MAAM,2BAA2B,GAAG,aAAa,CACrD,YAAY,CAAC,GAAG,CAAC,EACjB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,eAAe,GAAG,MAAM,EACxB,OAAO,oBAAoB,CAC5B,CAAC;AAEF,8FAA8F;AAC9F,eAAO,MAAM,uBAAuB,QAAO,2BAwLtC,CAAC"}
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
+
/** Listener event decoded from an incoming message */
|
|
2
|
+
export type SSEDecodedMessageType = {
|
|
3
|
+
topic: string;
|
|
4
|
+
data: unknown;
|
|
5
|
+
};
|
|
1
6
|
export type SSEAdapterOptionsType = {
|
|
2
7
|
eventSourceInit?: EventSourceInit;
|
|
3
8
|
autoConnect?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Turns an incoming message into a listener event - use it when the server does not send `{ topic, data }`
|
|
11
|
+
* (e.g. `{ type, payload }`). Return `null` to ignore the message.
|
|
12
|
+
* Defaults to parsing the message as JSON and reading its `topic` and `data` fields.
|
|
13
|
+
*/
|
|
14
|
+
decode?: (event: MessageEvent) => SSEDecodedMessageType | null;
|
|
4
15
|
};
|
|
5
16
|
//# sourceMappingURL=sse-adapter.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,qBAAqB,GAAG;IAClC,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,WAAW,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"sse-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.types.ts"],"names":[],"mappings":"AAEA,sDAAsD;AACtD,MAAM,MAAM,qBAAqB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAErE,MAAM,MAAM,qBAAqB,GAAG;IAClC,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,qBAAqB,GAAG,IAAI,CAAC;CAChE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAQ,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD,OAAO,KAAK,EAAE,2BAA2B,
|
|
1
|
+
{"version":3,"file":"websocket-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAQ,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD,OAAO,KAAK,EAAE,2BAA2B,EAA+B,MAAM,2BAA2B,CAAC;AAG1G;;;;GAIG;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,YAAY,CAAC,GAAG,CAAC,EACjB,2BAA2B,EAC3B,SAAS,EACT,SAAS,EACT,eAAe,GAAG,MAAM,EACxB,OAAO,oBAAoB,CAC5B,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,gBAAgB,QAAO,oBAiR/B,CAAC"}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/** Listener event decoded from an incoming frame */
|
|
2
|
+
export type WebsocketDecodedMessageType = {
|
|
3
|
+
topic: string;
|
|
4
|
+
data: unknown;
|
|
5
|
+
};
|
|
6
|
+
/** Emitted message to be encoded into an outgoing frame */
|
|
7
|
+
export type WebsocketEncodeMessageType = {
|
|
8
|
+
topic: string;
|
|
9
|
+
payload: unknown;
|
|
10
|
+
};
|
|
11
|
+
/** Frame accepted by `WebSocket.send` */
|
|
12
|
+
export type WebsocketFrameType = Parameters<WebSocket["send"]>[0];
|
|
1
13
|
export type WebsocketAdapterOptionsType = {
|
|
2
14
|
/**
|
|
3
15
|
* Subprotocols passed to the `WebSocket` constructor (`Sec-WebSocket-Protocol` header).
|
|
@@ -9,5 +21,16 @@ export type WebsocketAdapterOptionsType = {
|
|
|
9
21
|
heartbeatMessage?: string;
|
|
10
22
|
heartbeat?: boolean;
|
|
11
23
|
autoConnect?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Turns an incoming frame into a listener event - use it when the server does not send `{ topic, data }`
|
|
26
|
+
* (e.g. `{ type, payload }`). Return `null` to ignore the frame (pings, unknown messages).
|
|
27
|
+
* Defaults to parsing the frame as JSON and reading its `topic` and `data` fields.
|
|
28
|
+
*/
|
|
29
|
+
decode?: (event: MessageEvent) => WebsocketDecodedMessageType | null;
|
|
30
|
+
/**
|
|
31
|
+
* Turns an emitted message (and the heartbeat ping) into the frame passed to `websocket.send`.
|
|
32
|
+
* Defaults to `JSON.stringify({ topic, data: payload })`.
|
|
33
|
+
*/
|
|
34
|
+
encode?: (message: WebsocketEncodeMessageType) => WebsocketFrameType;
|
|
12
35
|
};
|
|
13
36
|
//# sourceMappingURL=websocket-adapter.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"websocket-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.types.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,MAAM,2BAA2B,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAE3E,2DAA2D;AAC3D,MAAM,MAAM,0BAA0B,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAE7E,yCAAyC;AACzC,MAAM,MAAM,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAElE,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,2BAA2B,GAAG,IAAI,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,0BAA0B,KAAK,kBAAkB,CAAC;CACtE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BatchDeliveryType, DeliveryStrategyType } from '.';
|
|
2
|
+
/**
|
|
3
|
+
* Creates the built-in `batch` strategy.
|
|
4
|
+
*
|
|
5
|
+
* Timeline for `interval: 100` with messages A, B, C at 0/20/40ms and D at 300ms:
|
|
6
|
+
* ```
|
|
7
|
+
* t=0 push(A) buffer=[A], open 100ms window
|
|
8
|
+
* t=20 push(B) buffer=[A, B]
|
|
9
|
+
* t=40 push(C) buffer=[A, B, C]
|
|
10
|
+
* t=100 window closes → deliver([A, B, C])
|
|
11
|
+
* t=300 push(D) buffer=[D], open window
|
|
12
|
+
* t=400 window closes → deliver([D])
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* The delivered `extra` is the one of the last message in the batch.
|
|
16
|
+
* Exported so custom strategies can compose it.
|
|
17
|
+
*/
|
|
18
|
+
export declare const createBatchDeliveryStrategy: <Data, Extra = any>({ interval, maxSize, }: Omit<BatchDeliveryType, "strategy">) => DeliveryStrategyType<Data, Data[], Extra>;
|
|
19
|
+
//# sourceMappingURL=delivery.batch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivery.batch.d.ts","sourceRoot":"","sources":["../../src/delivery/delivery.batch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAExE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,2BAA2B,GAAI,IAAI,EAAE,KAAK,GAAG,GAAG,EAAE,wBAG5D,IAAI,CAAC,iBAAiB,EAAE,UAAU,CAAC,KAAG,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,KAAK,CAiDhF,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DeliveryStrategyType, LatestDeliveryType } from '.';
|
|
2
|
+
/**
|
|
3
|
+
* Creates the built-in `latest` (sampling) strategy.
|
|
4
|
+
*
|
|
5
|
+
* Timeline for `interval: 100` with messages A, B, C at 0/20/40ms and D at 300ms:
|
|
6
|
+
* ```
|
|
7
|
+
* t=0 push(A) idle → deliver(A) immediately, open 100ms window
|
|
8
|
+
* t=20 push(B) pending = B
|
|
9
|
+
* t=40 push(C) pending = C (B is dropped)
|
|
10
|
+
* t=100 window closes → deliver(C), open next window
|
|
11
|
+
* t=200 window closes → nothing pending → idle
|
|
12
|
+
* t=300 push(D) idle → deliver(D) immediately
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Exported so custom strategies can compose it.
|
|
16
|
+
*/
|
|
17
|
+
export declare const createLatestDeliveryStrategy: <Data, Extra = any>({ interval, leading, }: Omit<LatestDeliveryType, "strategy">) => DeliveryStrategyType<Data, Data, Extra>;
|
|
18
|
+
//# sourceMappingURL=delivery.latest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivery.latest.d.ts","sourceRoot":"","sources":["../../src/delivery/delivery.latest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAuB,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9F;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,4BAA4B,GAAI,IAAI,EAAE,KAAK,GAAG,GAAG,EAAE,wBAG7D,IAAI,CAAC,kBAAkB,EAAE,UAAU,CAAC,KAAG,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CA2C/E,CAAC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { SocketAdapterInstance } from '../adapter';
|
|
2
|
+
import { ExtractAdapterExtraType } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* A single message flowing through a delivery strategy.
|
|
5
|
+
*
|
|
6
|
+
* `data` is the payload, `extra` is adapter-specific metadata (for the WebSocket adapter it is the raw `MessageEvent`).
|
|
7
|
+
*/
|
|
8
|
+
export type DeliveryMessageType<Data, Extra = any> = {
|
|
9
|
+
data: Data;
|
|
10
|
+
extra: Extra;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The object a delivery strategy returns for a single `listen()` subscription.
|
|
14
|
+
*
|
|
15
|
+
* Think of it as a tiny pipe: `push` is the input (every raw message lands here), `deliver` from the
|
|
16
|
+
* {@link DeliveryStrategyContextType} is the output, and `dispose` is the cleanup.
|
|
17
|
+
*/
|
|
18
|
+
export type DeliveryControllerType<Input, Extra = any> = {
|
|
19
|
+
/**
|
|
20
|
+
* Receives every raw message of the subscription. The strategy decides if and when to call `deliver`.
|
|
21
|
+
*/
|
|
22
|
+
push: (message: DeliveryMessageType<Input, Extra>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Called when the subscription ends (`unlisten()` / component unmount). Clear timers and drop pending
|
|
25
|
+
* state here. Nothing may be delivered after `dispose` was called. Must be safe to call more than once.
|
|
26
|
+
*/
|
|
27
|
+
dispose: () => void;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Context handed to a delivery strategy when a subscription starts.
|
|
31
|
+
*/
|
|
32
|
+
export type DeliveryStrategyContextType<Output, Extra = any> = {
|
|
33
|
+
/**
|
|
34
|
+
* Hands a (possibly aggregated) message to the user's `listen` callback.
|
|
35
|
+
*/
|
|
36
|
+
deliver: (message: DeliveryMessageType<Output, Extra>) => void;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* A custom delivery strategy. It is a factory invoked once per `listen()` call, so every subscription
|
|
40
|
+
* owns its own timers and buffers.
|
|
41
|
+
*
|
|
42
|
+
* `Input` is the raw message type (the listener's response type), `Output` is what `listen` callbacks
|
|
43
|
+
* receive. Use {@link createDeliveryStrategy} to author one with full type inference.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* // Deliver after 50ms of silence (debounce)
|
|
48
|
+
* const settle = createDeliveryStrategy<Tick>(({ deliver }) => {
|
|
49
|
+
* let timer: ReturnType<typeof setTimeout> | undefined;
|
|
50
|
+
* let last: DeliveryMessageType<Tick> | undefined;
|
|
51
|
+
* return {
|
|
52
|
+
* push: (message) => {
|
|
53
|
+
* last = message;
|
|
54
|
+
* clearTimeout(timer);
|
|
55
|
+
* timer = setTimeout(() => {
|
|
56
|
+
* if (last) deliver(last);
|
|
57
|
+
* last = undefined;
|
|
58
|
+
* }, 50);
|
|
59
|
+
* },
|
|
60
|
+
* dispose: () => {
|
|
61
|
+
* clearTimeout(timer);
|
|
62
|
+
* last = undefined;
|
|
63
|
+
* },
|
|
64
|
+
* };
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export type DeliveryStrategyType<Input, Output = Input, Extra = any> = (context: DeliveryStrategyContextType<Output, Extra>) => DeliveryControllerType<Input, Extra>;
|
|
69
|
+
/**
|
|
70
|
+
* Sampling delivery. The first message of an idle period is delivered immediately (when `leading` is `true`),
|
|
71
|
+
* then at most one message per `interval` is delivered - always the most recent one. Intermediate messages
|
|
72
|
+
* are dropped. Best for state-like data (prices, positions, cursors) where only the newest value matters.
|
|
73
|
+
*/
|
|
74
|
+
export type LatestDeliveryType = {
|
|
75
|
+
strategy: "latest";
|
|
76
|
+
/** Minimum time in milliseconds between two deliveries. */
|
|
77
|
+
interval: number;
|
|
78
|
+
/**
|
|
79
|
+
* Deliver the first message of an idle period immediately instead of waiting for the interval to pass.
|
|
80
|
+
* @default true
|
|
81
|
+
*/
|
|
82
|
+
leading?: boolean;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Batching delivery. The first message opens a window of `interval` milliseconds; when it closes, every
|
|
86
|
+
* message collected in it is delivered at once as an array, in arrival order. Nothing is dropped.
|
|
87
|
+
* Best for event-like data (trades, chat messages, logs) where every message must be processed.
|
|
88
|
+
*/
|
|
89
|
+
export type BatchDeliveryType = {
|
|
90
|
+
strategy: "batch";
|
|
91
|
+
/** Window length in milliseconds, measured from the first message of a batch. */
|
|
92
|
+
interval: number;
|
|
93
|
+
/** Deliver early as soon as this many messages were collected. Defaults to no limit. */
|
|
94
|
+
maxSize?: number;
|
|
95
|
+
};
|
|
96
|
+
/** Built-in, serializable delivery presets. */
|
|
97
|
+
export type DeliveryPresetType = LatestDeliveryType | BatchDeliveryType;
|
|
98
|
+
/**
|
|
99
|
+
* Everything accepted by the listener `delivery` option: a built-in preset or a custom strategy.
|
|
100
|
+
*/
|
|
101
|
+
export type ListenerDeliveryType<Response = any, Adapter extends SocketAdapterInstance = SocketAdapterInstance> = DeliveryPresetType | DeliveryStrategyType<Response, any, ExtractAdapterExtraType<Adapter>>;
|
|
102
|
+
/**
|
|
103
|
+
* Resolves what `listen` callbacks receive for a given `delivery` configuration.
|
|
104
|
+
*
|
|
105
|
+
* - no delivery → `Response`
|
|
106
|
+
* - `{ strategy: "latest" }` → `Response`
|
|
107
|
+
* - `{ strategy: "batch" }` → `Response[]`
|
|
108
|
+
* - custom strategy → its `Output` type
|
|
109
|
+
*/
|
|
110
|
+
export type ResolveDeliveredType<Response, Delivery> = [Delivery] extends [undefined] ? Response : Delivery extends DeliveryStrategyType<any, infer Output, any> ? Output : Delivery extends {
|
|
111
|
+
strategy: "batch";
|
|
112
|
+
} ? Response[] : Response;
|
|
113
|
+
//# sourceMappingURL=delivery.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivery.types.d.ts","sourceRoot":"","sources":["../../src/delivery/delivery.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,OAAO,CAAC;AAErD;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,CAAC,IAAI,EAAE,KAAK,GAAG,GAAG,IAAI;IACnD,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,sBAAsB,CAAC,KAAK,EAAE,KAAK,GAAG,GAAG,IAAI;IACvD;;OAEG;IACH,IAAI,EAAE,CAAC,OAAO,EAAE,mBAAmB,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,CAAC;IAC3D;;;OAGG;IACH,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,2BAA2B,CAAC,MAAM,EAAE,KAAK,GAAG,GAAG,IAAI;IAC7D;;OAEG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,mBAAmB,CAAC,MAAM,EAAE,KAAK,CAAC,KAAK,IAAI,CAAC;CAChE,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,MAAM,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,KAAK,GAAG,GAAG,IAAI,CACrE,OAAO,EAAE,2BAA2B,CAAC,MAAM,EAAE,KAAK,CAAC,KAChD,sBAAsB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAE1C;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,QAAQ,CAAC;IACnB,2DAA2D;IAC3D,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,OAAO,CAAC;IAClB,iFAAiF;IACjF,QAAQ,EAAE,MAAM,CAAC;IACjB,wFAAwF;IACxF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,+CAA+C;AAC/C,MAAM,MAAM,kBAAkB,GAAG,kBAAkB,GAAG,iBAAiB,CAAC;AAExE;;GAEG;AACH,MAAM,MAAM,oBAAoB,CAAC,QAAQ,GAAG,GAAG,EAAE,OAAO,SAAS,qBAAqB,GAAG,qBAAqB,IAC1G,kBAAkB,GAClB,oBAAoB,CAAC,QAAQ,EAAE,GAAG,EAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE1E;;;;;;;GAOG;AACH,MAAM,MAAM,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,GACjF,QAAQ,GACR,QAAQ,SAAS,oBAAoB,CAAC,GAAG,EAAE,MAAM,MAAM,EAAE,GAAG,CAAC,GAC3D,MAAM,GACN,QAAQ,SAAS;IAAE,QAAQ,EAAE,OAAO,CAAA;CAAE,GACpC,QAAQ,EAAE,GACV,QAAQ,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DeliveryControllerType, DeliveryPresetType, DeliveryStrategyContextType, DeliveryStrategyType, ListenerDeliveryType } from '.';
|
|
2
|
+
/**
|
|
3
|
+
* Typed identity helper for authoring custom delivery strategies.
|
|
4
|
+
*
|
|
5
|
+
* `Input` is the listener's response type, `Output` is what `listen` callbacks will receive. TypeScript cannot
|
|
6
|
+
* infer `Output` from how you call `deliver`, so declare it here - the same way `createListener<Response>()`
|
|
7
|
+
* declares the wire type.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const summarize = createDeliveryStrategy<Tick, { count: number; last: Tick }>(({ deliver }) => {
|
|
12
|
+
* let count = 0;
|
|
13
|
+
* let last: Tick | undefined;
|
|
14
|
+
* let timer: ReturnType<typeof setTimeout> | undefined;
|
|
15
|
+
* return {
|
|
16
|
+
* push: ({ data, extra }) => {
|
|
17
|
+
* count += 1;
|
|
18
|
+
* last = data;
|
|
19
|
+
* timer ??= setTimeout(() => {
|
|
20
|
+
* timer = undefined;
|
|
21
|
+
* deliver({ data: { count, last: last! }, extra });
|
|
22
|
+
* count = 0;
|
|
23
|
+
* }, 1000);
|
|
24
|
+
* },
|
|
25
|
+
* dispose: () => clearTimeout(timer),
|
|
26
|
+
* };
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* const onTick = socket.createListener<Tick>()({ topic: "ticks", delivery: summarize });
|
|
30
|
+
* onTick.listen(({ data }) => data.count); // data: { count: number; last: Tick }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const createDeliveryStrategy: <Input, Output = Input, Extra = any>(strategy: DeliveryStrategyType<Input, Output, Extra>) => DeliveryStrategyType<Input, Output, Extra>;
|
|
34
|
+
/** Narrows a `delivery` value to a custom strategy function. */
|
|
35
|
+
export declare const isDeliveryStrategy: (delivery: ListenerDeliveryType) => delivery is DeliveryStrategyType<any, any, any>;
|
|
36
|
+
/**
|
|
37
|
+
* Validates a `delivery` value. Throws for unknown presets so misconfiguration fails at listener creation,
|
|
38
|
+
* not on the first message.
|
|
39
|
+
*/
|
|
40
|
+
export declare const assertDelivery: (delivery: ListenerDeliveryType) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Instantiates a delivery controller for one `listen()` subscription.
|
|
43
|
+
*/
|
|
44
|
+
export declare const createDeliveryController: <Input, Output, Extra>(delivery: DeliveryPresetType | DeliveryStrategyType<Input, any, Extra>, context: DeliveryStrategyContextType<Output, Extra>) => DeliveryControllerType<Input, Extra>;
|
|
45
|
+
/**
|
|
46
|
+
* Stable key describing a `delivery` value - presets by value, custom strategies by identity.
|
|
47
|
+
* Useful for memoization (e.g. React dependency arrays).
|
|
48
|
+
*/
|
|
49
|
+
export declare const getDeliveryKey: (delivery?: ListenerDeliveryType) => string | DeliveryStrategyType<any>;
|
|
50
|
+
//# sourceMappingURL=delivery.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivery.utils.d.ts","sourceRoot":"","sources":["../../src/delivery/delivery.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,sBAAsB,EACtB,kBAAkB,EAClB,2BAA2B,EAC3B,oBAAoB,EAEpB,oBAAoB,EACrB,MAAM,UAAU,CAAC;AAGlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,sBAAsB,GAAI,KAAK,EAAE,MAAM,GAAG,KAAK,EAAE,KAAK,GAAG,GAAG,EACvE,UAAU,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,KACnD,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAa,CAAC;AAE1D,gEAAgE;AAChE,eAAO,MAAM,kBAAkB,GAAI,UAAU,oBAAoB,KAAG,QAAQ,IAAI,oBAAoB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAClF,CAAC;AAEjC;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,oBAAoB,KAAG,IAS/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAI,KAAK,EAAE,MAAM,EAAE,KAAK,EAC3D,UAAU,kBAAkB,GAAG,oBAAoB,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,EACtE,SAAS,2BAA2B,CAAC,MAAM,EAAE,KAAK,CAAC,KAClD,sBAAsB,CAAC,KAAK,EAAE,KAAK,CAUrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,WAAW,oBAAoB,KAAG,MAAM,GAAG,oBAAoB,CAAC,GAAG,CAQjG,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/delivery/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,OAAO,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,OAAO,CAAC"}
|