@hyper-fetch/sockets 4.0.2 → 5.0.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/README.md +0 -9
- package/dist/index.cjs.js +852 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +264 -131
- package/dist/index.esm.js +832 -1
- package/dist/index.esm.js.map +4 -4
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +158 -0
- package/src/adapter/adapter.sse.ts +127 -0
- package/src/adapter/adapter.types.ts +83 -11
- package/src/adapter/adapter.utils.ts +12 -7
- package/src/adapter/adapter.websocket.ts +187 -0
- package/src/adapter/index.ts +3 -1
- package/src/emitter/emitter.ts +103 -30
- package/src/emitter/emitter.types.ts +63 -8
- package/src/listener/listener.ts +62 -21
- package/src/listener/listener.types.ts +23 -4
- package/src/socket/socket.events.ts +44 -32
- package/src/socket/socket.ts +29 -28
- package/src/socket/socket.types.ts +8 -13
- package/src/socket/socket.utils.ts +7 -7
- package/src/types/connect.types.ts +6 -0
- package/src/types/extract.types.ts +11 -12
- package/src/types/index.ts +1 -0
- package/src/adapter/adapter.ts +0 -238
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import { SocketInstance } from "socket";
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const getSocketUrl = (socket: SocketInstance) => {
|
|
4
4
|
const queryParams = socket.queryParamsStringify(socket.queryParams).substring(1);
|
|
5
5
|
const authParams = socket.queryParamsStringify(socket.auth).substring(1);
|
|
6
6
|
const connector = queryParams && authParams ? "&" : "";
|
|
7
|
-
const
|
|
7
|
+
const queryPrefix = authParams || queryParams ? "?" : "";
|
|
8
|
+
const fullUrl = `${socket.url}${queryPrefix}${authParams}${connector}${queryParams}`;
|
|
9
|
+
return fullUrl;
|
|
10
|
+
};
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
if (!window?.EventSource) return null;
|
|
11
|
-
return new EventSource(fullUrl, socket.options.adapterOptions?.eventSourceInit);
|
|
12
|
-
}
|
|
12
|
+
export const getWebsocketAdapter = (socket: SocketInstance) => {
|
|
13
13
|
if (!window?.WebSocket) return null;
|
|
14
|
-
return new WebSocket(
|
|
14
|
+
return new WebSocket(getSocketUrl(socket), socket.options.adapterOptions?.protocols);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const getSSEAdapter = (socket: SocketInstance) => {
|
|
18
|
+
if (!window?.EventSource) return null;
|
|
19
|
+
return new EventSource(getSocketUrl(socket), socket.options.adapterOptions?.protocols);
|
|
15
20
|
};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
+
import { DateInterval, parseResponse } from "@hyper-fetch/core";
|
|
3
|
+
|
|
4
|
+
import { EmitterAcknowledgeType, EmitterInstance } from "emitter";
|
|
5
|
+
import { ListenerInstance } from "listener";
|
|
6
|
+
import {
|
|
7
|
+
ListenerCallbackType,
|
|
8
|
+
WSMessageType,
|
|
9
|
+
getSocketAdapterBindings,
|
|
10
|
+
getWebsocketAdapter,
|
|
11
|
+
WebsocketAdapterType,
|
|
12
|
+
SocketData,
|
|
13
|
+
} from "adapter";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* -------------------------------------------
|
|
17
|
+
* Websocket
|
|
18
|
+
* -------------------------------------------
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const websocketAdapter: WebsocketAdapterType = (socket) => {
|
|
22
|
+
const {
|
|
23
|
+
open,
|
|
24
|
+
listeners,
|
|
25
|
+
connecting,
|
|
26
|
+
reconnectionAttempts,
|
|
27
|
+
removeListener,
|
|
28
|
+
onConnect,
|
|
29
|
+
onReconnect,
|
|
30
|
+
onDisconnect,
|
|
31
|
+
onListen,
|
|
32
|
+
onEmit,
|
|
33
|
+
onOpen,
|
|
34
|
+
onClose,
|
|
35
|
+
onError,
|
|
36
|
+
onEvent,
|
|
37
|
+
} = getSocketAdapterBindings(socket);
|
|
38
|
+
|
|
39
|
+
let pingTimer: ReturnType<typeof setTimeout> | undefined;
|
|
40
|
+
let pongTimer: ReturnType<typeof setTimeout> | undefined;
|
|
41
|
+
let adapter = getWebsocketAdapter(socket);
|
|
42
|
+
|
|
43
|
+
const connect = () => {
|
|
44
|
+
const enabled = onConnect();
|
|
45
|
+
if (!enabled) return;
|
|
46
|
+
|
|
47
|
+
// Clean environment
|
|
48
|
+
adapter?.close();
|
|
49
|
+
adapter = getWebsocketAdapter(socket);
|
|
50
|
+
|
|
51
|
+
// Make sure we picked good environment
|
|
52
|
+
if (!adapter) return;
|
|
53
|
+
|
|
54
|
+
// Reconnection timeout
|
|
55
|
+
const timeout = setTimeout(() => {
|
|
56
|
+
reconnect();
|
|
57
|
+
}, socket.reconnectTime);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Mount listeners
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
adapter.onopen = () => {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
onOpen();
|
|
66
|
+
onHeartbeat();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
adapter.onclose = () => {
|
|
70
|
+
onClose();
|
|
71
|
+
clearTimers();
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
adapter.onerror = (event) => {
|
|
75
|
+
onError(new Error(event.type));
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
adapter.onmessage = (event: MessageEvent<SocketData>) => {
|
|
79
|
+
const extra = parseResponse(event);
|
|
80
|
+
extra.data = parseResponse(extra.data);
|
|
81
|
+
|
|
82
|
+
const eventListeners: Map<ListenerCallbackType<any, any>, VoidFunction> = listeners.get(extra.data.endpoint) ||
|
|
83
|
+
new Map();
|
|
84
|
+
|
|
85
|
+
eventListeners.forEach((_, action) => {
|
|
86
|
+
action({ data: extra.data.data, extra });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
onEvent(extra.data.endpoint, extra.data.data, extra);
|
|
90
|
+
onHeartbeat();
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const disconnect = () => {
|
|
95
|
+
onDisconnect();
|
|
96
|
+
adapter.close();
|
|
97
|
+
clearTimers();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const reconnect = () => {
|
|
101
|
+
onReconnect(disconnect, connect);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const clearTimers = () => {
|
|
105
|
+
clearTimeout(pingTimer);
|
|
106
|
+
clearTimeout(pongTimer);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const sendEventMessage = (payload: WSMessageType) => {
|
|
110
|
+
adapter.send(JSON.stringify({ id: payload.id, endpoint: payload.endpoint, data: payload.data }));
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const onHeartbeat = () => {
|
|
114
|
+
const options = socket.options.adapterOptions || {};
|
|
115
|
+
const {
|
|
116
|
+
heartbeat = false,
|
|
117
|
+
pingTimeout = DateInterval.second * 5,
|
|
118
|
+
pongTimeout = DateInterval.second * 5,
|
|
119
|
+
heartbeatMessage = "heartbeat",
|
|
120
|
+
} = options;
|
|
121
|
+
if (connecting || !heartbeat) return;
|
|
122
|
+
clearTimers();
|
|
123
|
+
pingTimer = setTimeout(() => {
|
|
124
|
+
const id = "heartbeat";
|
|
125
|
+
sendEventMessage({ id, data: heartbeatMessage, endpoint: "heartbeat" });
|
|
126
|
+
pongTimer = setTimeout(() => {
|
|
127
|
+
adapter.close();
|
|
128
|
+
}, pongTimeout);
|
|
129
|
+
}, pingTimeout);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const listen = (listener: ListenerInstance, callback: ListenerCallbackType<WebsocketAdapterType, any>) => {
|
|
133
|
+
return onListen(listener, callback);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const emit = async (
|
|
137
|
+
eventMessageId: string,
|
|
138
|
+
emitter: EmitterInstance,
|
|
139
|
+
ack?: EmitterAcknowledgeType<any, WebsocketAdapterType>,
|
|
140
|
+
) => {
|
|
141
|
+
const enabled = onEmit(emitter);
|
|
142
|
+
|
|
143
|
+
if (!enabled) return;
|
|
144
|
+
if (ack || emitter.connections.size) {
|
|
145
|
+
let timeout;
|
|
146
|
+
const unmount = onListen(emitter, (response) => {
|
|
147
|
+
if (response.extra.data.id === eventMessageId) {
|
|
148
|
+
ack({ ...response, error: null });
|
|
149
|
+
clearTimeout(timeout);
|
|
150
|
+
unmount();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
timeout = setTimeout(() => {
|
|
154
|
+
unmount();
|
|
155
|
+
ack({ error: new Error("Server did not acknowledge the event"), data: null, extra: null });
|
|
156
|
+
}, emitter.timeout);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const emitterInstance = await socket.__modifySend(emitter);
|
|
160
|
+
sendEventMessage({ id: eventMessageId, data: emitterInstance.data, endpoint: emitterInstance.endpoint });
|
|
161
|
+
socket.events.emitEmitterEvent(emitterInstance);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// Initialize
|
|
165
|
+
|
|
166
|
+
if (socket.autoConnect) {
|
|
167
|
+
connect();
|
|
168
|
+
}
|
|
169
|
+
socket.appManager.events.onOnline(() => {
|
|
170
|
+
if (socket.autoConnect && !open) {
|
|
171
|
+
connect();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
open,
|
|
177
|
+
listeners,
|
|
178
|
+
reconnectionAttempts,
|
|
179
|
+
connecting,
|
|
180
|
+
listen,
|
|
181
|
+
removeListener,
|
|
182
|
+
emit,
|
|
183
|
+
connect,
|
|
184
|
+
reconnect,
|
|
185
|
+
disconnect,
|
|
186
|
+
};
|
|
187
|
+
};
|
package/src/adapter/index.ts
CHANGED
package/src/emitter/emitter.ts
CHANGED
|
@@ -1,30 +1,42 @@
|
|
|
1
|
-
import { DateInterval, getUniqueRequestId } from "@hyper-fetch/core";
|
|
1
|
+
import { DateInterval, ExtractRouteParams, ParamsType, getUniqueRequestId } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { Socket } from "socket";
|
|
4
|
-
import { EmitterOptionsType,
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { EmitterAcknowledgeType, EmitterOptionsType, EmitType } from "emitter";
|
|
5
|
+
import { SocketAdapterType, ExtractEmitterOptionsType } from "adapter";
|
|
6
|
+
import { ConnectMethodType } from "types";
|
|
7
7
|
|
|
8
|
-
export class Emitter<
|
|
9
|
-
|
|
8
|
+
export class Emitter<
|
|
9
|
+
Payload,
|
|
10
|
+
Response,
|
|
11
|
+
Endpoint extends string,
|
|
12
|
+
AdapterType extends SocketAdapterType,
|
|
13
|
+
MappedData = void,
|
|
14
|
+
HasParams extends boolean = false,
|
|
15
|
+
HasData extends boolean = false,
|
|
16
|
+
> {
|
|
17
|
+
readonly endpoint: Endpoint;
|
|
18
|
+
params?: ParamsType;
|
|
10
19
|
timeout: number;
|
|
11
20
|
data: Payload | null = null;
|
|
12
|
-
options: ExtractEmitterOptionsType<
|
|
21
|
+
options: ExtractEmitterOptionsType<AdapterType>;
|
|
22
|
+
connections: Set<ConnectMethodType<AdapterType, Response>> = new Set();
|
|
13
23
|
|
|
14
24
|
constructor(
|
|
15
|
-
readonly socket: Socket<
|
|
16
|
-
readonly emitterOptions: EmitterOptionsType<
|
|
17
|
-
json?: Partial<Emitter<Payload, Response,
|
|
25
|
+
readonly socket: Socket<AdapterType>,
|
|
26
|
+
readonly emitterOptions: EmitterOptionsType<Endpoint, AdapterType>,
|
|
27
|
+
json?: Partial<Emitter<Payload, Response, Endpoint, AdapterType, MappedData>>,
|
|
18
28
|
readonly dataMapper?: (data: Payload) => MappedData,
|
|
19
29
|
) {
|
|
20
|
-
const {
|
|
21
|
-
|
|
30
|
+
const { endpoint, timeout = DateInterval.second * 2, options } = emitterOptions;
|
|
31
|
+
|
|
32
|
+
this.endpoint = json?.endpoint ?? endpoint;
|
|
22
33
|
this.data = json?.data;
|
|
23
34
|
this.timeout = json?.timeout ?? timeout;
|
|
24
35
|
this.options = json?.options ?? options;
|
|
36
|
+
this.params = json?.params;
|
|
25
37
|
}
|
|
26
38
|
|
|
27
|
-
setOptions(options: ExtractEmitterOptionsType<
|
|
39
|
+
setOptions(options: ExtractEmitterOptionsType<AdapterType>) {
|
|
28
40
|
return this.clone({ options });
|
|
29
41
|
}
|
|
30
42
|
|
|
@@ -34,39 +46,100 @@ export class Emitter<Payload, Response, Adapter extends AdapterType, MappedData
|
|
|
34
46
|
|
|
35
47
|
setData(data: Payload) {
|
|
36
48
|
if (this.dataMapper) {
|
|
37
|
-
return this.clone<MappedData>({ data: this.dataMapper(data) });
|
|
49
|
+
return this.clone<MappedData, MappedData, HasParams, true>({ data: this.dataMapper(data) });
|
|
38
50
|
}
|
|
39
|
-
return this.clone({ data });
|
|
51
|
+
return this.clone<Payload, MappedData, HasParams, true>({ data });
|
|
40
52
|
}
|
|
41
53
|
|
|
42
54
|
setDataMapper = <MapperData>(mapper: (data: Payload) => MapperData) => {
|
|
43
55
|
return this.clone<Payload, MapperData>(undefined, mapper);
|
|
44
56
|
};
|
|
45
57
|
|
|
46
|
-
|
|
47
|
-
|
|
58
|
+
setParams(params: ExtractRouteParams<Endpoint>) {
|
|
59
|
+
return this.clone<Payload, MappedData, true>({ params });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Attach global logic to the received events
|
|
64
|
+
* @param callback
|
|
65
|
+
*/
|
|
66
|
+
onData(callback: ConnectMethodType<AdapterType, Response>) {
|
|
67
|
+
this.connections.add(callback);
|
|
68
|
+
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private paramsMapper = (params: ParamsType | null | undefined): Endpoint => {
|
|
73
|
+
let endpoint = this.emitterOptions.endpoint as string;
|
|
74
|
+
if (params) {
|
|
75
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
76
|
+
endpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return endpoint as Endpoint;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
clone<
|
|
84
|
+
NewPayload = Payload,
|
|
85
|
+
MapperData = MappedData,
|
|
86
|
+
Params extends boolean = HasParams,
|
|
87
|
+
Data extends boolean = HasData,
|
|
88
|
+
>(
|
|
89
|
+
options?: Partial<EmitterOptionsType<Endpoint, AdapterType>> & { params?: ParamsType; data?: NewPayload },
|
|
48
90
|
mapper?: (data: NewPayload) => MapperData,
|
|
49
|
-
)
|
|
50
|
-
const json: Partial<Emitter<NewPayload, Response,
|
|
91
|
+
) {
|
|
92
|
+
const json: Partial<Emitter<NewPayload, Response, Endpoint, AdapterType, MapperData, Params, Data>> = {
|
|
51
93
|
timeout: this.timeout,
|
|
52
94
|
options: this.options,
|
|
53
95
|
data: this.data as unknown as NewPayload,
|
|
54
|
-
|
|
55
|
-
|
|
96
|
+
params: options?.params || this.params,
|
|
97
|
+
...options,
|
|
98
|
+
endpoint: this.paramsMapper(options?.params || this.params),
|
|
56
99
|
};
|
|
57
100
|
const mapperFn = (mapper || this.dataMapper) as typeof mapper;
|
|
58
101
|
|
|
59
|
-
|
|
102
|
+
const newInstance = new Emitter<NewPayload, Response, Endpoint, AdapterType, MapperData, Params, Data>(
|
|
103
|
+
this.socket,
|
|
104
|
+
this.emitterOptions,
|
|
105
|
+
json,
|
|
106
|
+
mapperFn,
|
|
107
|
+
);
|
|
108
|
+
newInstance.connections = this.connections;
|
|
109
|
+
return newInstance;
|
|
60
110
|
}
|
|
61
111
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
112
|
+
getAck = (ack?: EmitterAcknowledgeType<any, AdapterType>) => {
|
|
113
|
+
if (ack) {
|
|
114
|
+
return (response: { data: any; extra: any; error: any }) => {
|
|
115
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
116
|
+
return ack(response as any);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (this.connections.size) {
|
|
120
|
+
return (response: { data: any; extra: any; error: any }) => {
|
|
121
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
};
|
|
68
126
|
|
|
69
|
-
|
|
127
|
+
emit: EmitType<this> = ({
|
|
128
|
+
data,
|
|
129
|
+
params,
|
|
130
|
+
options,
|
|
131
|
+
ack,
|
|
132
|
+
}: {
|
|
133
|
+
data?: Payload;
|
|
134
|
+
params?: ExtractRouteParams<Endpoint>;
|
|
135
|
+
options?: Partial<EmitterOptionsType<Endpoint, AdapterType>>;
|
|
136
|
+
ack?: EmitterAcknowledgeType<any, AdapterType>;
|
|
137
|
+
} = {}) => {
|
|
138
|
+
const instance = this.clone({ ...options, data, params });
|
|
139
|
+
|
|
140
|
+
const eventMessageId = getUniqueRequestId(instance.endpoint);
|
|
141
|
+
|
|
142
|
+
this.socket.adapter.emit(eventMessageId, instance, instance.getAck(ack));
|
|
70
143
|
return eventMessageId;
|
|
71
|
-
}
|
|
144
|
+
};
|
|
72
145
|
}
|
|
@@ -1,15 +1,70 @@
|
|
|
1
|
+
import { ExtractRouteParams, NegativeTypes } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import { SocketAdapterInstance, ExtractEmitterOptionsType, ExtractSocketExtraType } from "adapter";
|
|
1
4
|
import { Emitter } from "emitter";
|
|
5
|
+
import {
|
|
6
|
+
ExtractEmitterAdapterType,
|
|
7
|
+
ExtractEmitterHasDataType,
|
|
8
|
+
ExtractEmitterHasParamsType,
|
|
9
|
+
ExtractEmitterEndpointType,
|
|
10
|
+
ExtractEmitterPayloadType,
|
|
11
|
+
} from "types";
|
|
2
12
|
|
|
3
|
-
export type EmitterInstance = Emitter<any, any, any>;
|
|
13
|
+
export type EmitterInstance = Emitter<any, any, any, SocketAdapterInstance, any, any, any>;
|
|
4
14
|
|
|
5
|
-
export type EmitterOptionsType<
|
|
6
|
-
|
|
15
|
+
export type EmitterOptionsType<Endpoint extends string, AdapterType extends SocketAdapterInstance> = {
|
|
16
|
+
endpoint: Endpoint;
|
|
7
17
|
timeout?: number;
|
|
8
|
-
options?:
|
|
18
|
+
options?: ExtractEmitterOptionsType<AdapterType>;
|
|
9
19
|
};
|
|
10
20
|
|
|
11
|
-
export type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
21
|
+
export type EmitterAcknowledgeType<Response, AdapterType extends SocketAdapterInstance> = (
|
|
22
|
+
response:
|
|
23
|
+
| { error: Error; data: null; extra: null }
|
|
24
|
+
| { error: null; data: Response; extra: ExtractSocketExtraType<AdapterType> },
|
|
25
|
+
) => void;
|
|
26
|
+
|
|
27
|
+
// Emit
|
|
28
|
+
|
|
29
|
+
export type EmitDataType<Payload, HasData extends boolean> = HasData extends false
|
|
30
|
+
? {
|
|
31
|
+
data: Payload;
|
|
32
|
+
}
|
|
33
|
+
: { data?: never };
|
|
34
|
+
|
|
35
|
+
export type EmitParamsType<Params, HasData extends boolean> = HasData extends false
|
|
36
|
+
? Params extends NegativeTypes
|
|
37
|
+
? { params?: never }
|
|
38
|
+
: {
|
|
39
|
+
params: Params;
|
|
40
|
+
}
|
|
41
|
+
: { params?: never };
|
|
42
|
+
|
|
43
|
+
export type EmitRestType<Emitter extends EmitterInstance> = {
|
|
44
|
+
options?: Partial<EmitterOptionsType<ExtractEmitterEndpointType<Emitter>, ExtractEmitterAdapterType<Emitter>>>;
|
|
45
|
+
ack?: EmitterAcknowledgeType<ExtractEmitterPayloadType<Emitter>, ExtractEmitterAdapterType<Emitter>>;
|
|
15
46
|
};
|
|
47
|
+
|
|
48
|
+
export type EmitType<Emitter extends EmitterInstance> = ExtractEmitterHasDataType<Emitter> extends false
|
|
49
|
+
? (
|
|
50
|
+
options: EmitDataType<ExtractEmitterPayloadType<Emitter>, ExtractEmitterHasParamsType<Emitter>> &
|
|
51
|
+
EmitParamsType<ExtractRouteParams<ExtractEmitterEndpointType<Emitter>>, ExtractEmitterHasParamsType<Emitter>> &
|
|
52
|
+
EmitRestType<Emitter>,
|
|
53
|
+
) => string
|
|
54
|
+
: ExtractRouteParams<ExtractEmitterEndpointType<Emitter>> extends NegativeTypes
|
|
55
|
+
? (
|
|
56
|
+
options?: EmitDataType<ExtractEmitterPayloadType<Emitter>, ExtractEmitterHasParamsType<Emitter>> &
|
|
57
|
+
EmitParamsType<ExtractRouteParams<ExtractEmitterEndpointType<Emitter>>, ExtractEmitterHasParamsType<Emitter>> &
|
|
58
|
+
EmitRestType<Emitter>,
|
|
59
|
+
) => string
|
|
60
|
+
: ExtractEmitterHasParamsType<Emitter> extends false
|
|
61
|
+
? (
|
|
62
|
+
options: EmitDataType<ExtractEmitterPayloadType<Emitter>, ExtractEmitterHasParamsType<Emitter>> &
|
|
63
|
+
EmitParamsType<ExtractRouteParams<ExtractEmitterEndpointType<Emitter>>, ExtractEmitterHasParamsType<Emitter>> &
|
|
64
|
+
EmitRestType<Emitter>,
|
|
65
|
+
) => string
|
|
66
|
+
: (
|
|
67
|
+
options?: EmitDataType<ExtractEmitterPayloadType<Emitter>, ExtractEmitterHasParamsType<Emitter>> &
|
|
68
|
+
EmitParamsType<ExtractRouteParams<ExtractEmitterEndpointType<Emitter>>, ExtractEmitterHasParamsType<Emitter>> &
|
|
69
|
+
EmitRestType<Emitter>,
|
|
70
|
+
) => string;
|
package/src/listener/listener.ts
CHANGED
|
@@ -1,42 +1,83 @@
|
|
|
1
|
+
import { ExtractRouteParams, ParamsType } from "@hyper-fetch/core";
|
|
2
|
+
|
|
1
3
|
import { Socket } from "socket";
|
|
2
|
-
import { ListenerOptionsType } from "listener";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { ListenType, ListenerOptionsType } from "listener";
|
|
5
|
+
import { ExtractSocketExtraType, SocketAdapterType, ExtractListenerOptionsType } from "adapter";
|
|
6
|
+
import { ConnectMethodType } from "types";
|
|
5
7
|
|
|
6
|
-
export class Listener<
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
export class Listener<
|
|
9
|
+
Response,
|
|
10
|
+
Endpoint extends string,
|
|
11
|
+
AdapterType extends SocketAdapterType,
|
|
12
|
+
HasParams extends boolean = false,
|
|
13
|
+
> {
|
|
14
|
+
readonly endpoint: Endpoint;
|
|
15
|
+
params?: ParamsType;
|
|
16
|
+
options?: ExtractListenerOptionsType<AdapterType>;
|
|
17
|
+
connections: Set<ConnectMethodType<AdapterType, Response>> = new Set();
|
|
9
18
|
|
|
10
19
|
constructor(
|
|
11
|
-
readonly socket: Socket<
|
|
12
|
-
readonly listenerOptions?: ListenerOptionsType<
|
|
20
|
+
readonly socket: Socket<AdapterType>,
|
|
21
|
+
readonly listenerOptions?: ListenerOptionsType<Endpoint, AdapterType>,
|
|
13
22
|
) {
|
|
14
|
-
const {
|
|
15
|
-
this.
|
|
23
|
+
const { endpoint, options } = listenerOptions;
|
|
24
|
+
this.endpoint = endpoint;
|
|
16
25
|
this.options = options;
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
setOptions(options: ExtractListenerOptionsType<
|
|
28
|
+
setOptions(options: ExtractListenerOptionsType<AdapterType>) {
|
|
20
29
|
return this.clone({ options });
|
|
21
30
|
}
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
return
|
|
32
|
+
setParams(params: ExtractRouteParams<Endpoint>) {
|
|
33
|
+
return this.clone<true>({ params });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private paramsMapper = (params: ParamsType | null | undefined): Endpoint => {
|
|
37
|
+
let endpoint = this.listenerOptions.endpoint as string;
|
|
38
|
+
if (params) {
|
|
39
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
40
|
+
endpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return endpoint as Endpoint;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Attach global logic to the received events
|
|
49
|
+
* @param callback
|
|
50
|
+
*/
|
|
51
|
+
onData(callback: ConnectMethodType<AdapterType, Response>) {
|
|
52
|
+
this.connections.add(callback);
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
clone<Params extends boolean = HasParams>(options?: Partial<ListenerOptionsType<Endpoint, AdapterType>>) {
|
|
57
|
+
const newInstance = new Listener<Response, Endpoint, AdapterType, Params>(this.socket, {
|
|
25
58
|
...this.listenerOptions,
|
|
26
|
-
...
|
|
27
|
-
|
|
59
|
+
...options,
|
|
60
|
+
endpoint: this.paramsMapper(options?.params || this.params),
|
|
28
61
|
});
|
|
62
|
+
|
|
63
|
+
newInstance.connections = this.connections;
|
|
64
|
+
return newInstance;
|
|
29
65
|
}
|
|
30
66
|
|
|
31
|
-
listen
|
|
32
|
-
const instance = this.clone();
|
|
67
|
+
listen: ListenType<this, AdapterType> = ({ callback, ...options }) => {
|
|
68
|
+
const instance = this.clone(options);
|
|
33
69
|
|
|
34
|
-
|
|
70
|
+
const action = (response: { data: Response; extra: ExtractSocketExtraType<AdapterType> }) => {
|
|
71
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
72
|
+
return callback(response as any);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
this.socket.adapter.listen(instance, action);
|
|
35
76
|
|
|
36
77
|
const removeListener = () => {
|
|
37
|
-
this.socket.adapter.removeListener(instance.
|
|
78
|
+
this.socket.adapter.removeListener(instance.endpoint, action);
|
|
38
79
|
};
|
|
39
80
|
|
|
40
|
-
return
|
|
41
|
-
}
|
|
81
|
+
return removeListener;
|
|
82
|
+
};
|
|
42
83
|
}
|
|
@@ -1,8 +1,27 @@
|
|
|
1
|
+
import { ExtractRouteParams, NegativeTypes } from "@hyper-fetch/core";
|
|
2
|
+
|
|
3
|
+
import { SocketAdapterInstance, ExtractListenerOptionsType, ListenerCallbackType, SocketAdapterType } from "adapter";
|
|
1
4
|
import { Listener } from "listener";
|
|
5
|
+
import { ExtractListenerHasParams, ExtractListenerEndpointType, ExtractListenerResponseType } from "types";
|
|
2
6
|
|
|
3
|
-
export type ListenerInstance = Listener<any, any>;
|
|
7
|
+
export type ListenerInstance = Listener<any, any, SocketAdapterInstance, any>;
|
|
4
8
|
|
|
5
|
-
export type ListenerOptionsType<
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
export type ListenerOptionsType<Endpoint extends string, AdapterType extends SocketAdapterInstance> = {
|
|
10
|
+
endpoint: Endpoint;
|
|
11
|
+
params?: ExtractRouteParams<Endpoint>;
|
|
12
|
+
options?: ExtractListenerOptionsType<AdapterType>;
|
|
8
13
|
};
|
|
14
|
+
|
|
15
|
+
export type ListenType<Listener extends ListenerInstance, Adapter extends SocketAdapterType> = (
|
|
16
|
+
options: ExtractRouteParams<ExtractListenerEndpointType<Listener>> extends NegativeTypes
|
|
17
|
+
? { callback: ListenerCallbackType<Adapter, ExtractListenerResponseType<Listener>> }
|
|
18
|
+
: ExtractListenerHasParams<Listener> extends false
|
|
19
|
+
? {
|
|
20
|
+
params: ExtractRouteParams<ExtractListenerEndpointType<Listener>>;
|
|
21
|
+
callback: ListenerCallbackType<Adapter, ExtractListenerResponseType<Listener>>;
|
|
22
|
+
}
|
|
23
|
+
: {
|
|
24
|
+
params?: never;
|
|
25
|
+
callback: ListenerCallbackType<Adapter, ExtractListenerResponseType<Listener>>;
|
|
26
|
+
},
|
|
27
|
+
) => () => void;
|