/
bot.ts
201 lines (178 loc) · 4.57 KB
/
bot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type {
MachinatNode,
MachinatBot,
MachinatUser,
InitScopeFn,
DispatchWrapper,
} from '@machinat/core';
import Engine from '@machinat/core/engine';
import Renderer from '@machinat/core/renderer';
import Queue from '@machinat/core/queue';
import ModuleUtilitiesI from '@machinat/core/base/ModuleUtilities';
import { makeClassProvider } from '@machinat/core/service';
import { WEBSOCKET } from './constant';
import { PlatformUtilitiesI } from './interface';
import { ServerP } from './server';
import {
WebSocketConnection,
WebSocketTopicChannel,
WebSocketUserChannel,
} from './channel';
import createJobs from './utils/createJobs';
import WebSocketWorker from './worker';
import type {
EventInput,
WebSocketJob,
WebSocketResult,
WebSocketComponent,
WebSocketDispatchFrame,
ConnIdentifier,
WebSocketDispatchChannel,
WebSocketDispatchResponse,
} from './types';
type SendResult = {
connections: WebSocketConnection[];
};
const toConnection = ({ serverId, id }: ConnIdentifier) =>
new WebSocketConnection(serverId, id);
/**
* @category Provider
*/
export class WebSocketBot
implements
MachinatBot<WebSocketDispatchChannel, WebSocketJob, WebSocketResult>
{
private _server: ServerP<any, unknown>;
engine: Engine<
WebSocketDispatchChannel,
EventInput,
WebSocketComponent,
WebSocketJob,
WebSocketResult
>;
platform = WEBSOCKET;
constructor(
server: ServerP<any, unknown>,
initScope?: InitScopeFn,
dispatchWrapper?: DispatchWrapper<
WebSocketJob,
WebSocketDispatchFrame,
WebSocketResult
>
) {
this._server = server;
const queue = new Queue<WebSocketJob, WebSocketResult>();
const worker = new WebSocketWorker(server);
const renderer = new Renderer<EventInput, WebSocketComponent>(
WEBSOCKET,
() => {
throw new TypeError(
'general component not supported at websocket platform'
);
}
);
this.engine = new Engine(
WEBSOCKET,
renderer,
queue,
worker,
initScope,
dispatchWrapper
);
}
async start(): Promise<void> {
await this._server.start();
this.engine.start();
}
async stop(): Promise<void> {
await this._server.stop();
this.engine.stop();
}
render(
channel: WebSocketDispatchChannel,
message: MachinatNode
): Promise<null | WebSocketDispatchResponse> {
return this.engine.render<WebSocketDispatchChannel>(
channel,
message,
createJobs
);
}
async send(
channel: WebSocketConnection,
content: EventInput | EventInput[]
): Promise<SendResult> {
const {
results: [{ connections }],
} = await this.engine.dispatchJobs(channel, [
{
target: channel,
values: Array.isArray(content) ? content : [content],
},
]);
return { connections: connections.map(toConnection) };
}
async sendUser(
user: MachinatUser,
content: EventInput | EventInput[]
): Promise<SendResult> {
const channel = new WebSocketUserChannel(user.uid);
const {
results: [{ connections }],
} = await this.engine.dispatchJobs(channel, [
{
target: channel,
values: Array.isArray(content) ? content : [content],
},
]);
return { connections: connections.map(toConnection) };
}
async sendTopic(
topic: string,
content: EventInput | EventInput[]
): Promise<SendResult> {
const channel = new WebSocketTopicChannel(topic);
const {
results: [{ connections }],
} = await this.engine.dispatchJobs(channel, [
{
target: channel,
values: Array.isArray(content) ? content : [content],
},
]);
return { connections: connections.map(toConnection) };
}
disconnect(
connection: WebSocketConnection,
reason?: string
): Promise<boolean> {
return this._server.disconnect(connection, reason);
}
subscribeTopic(
connection: WebSocketConnection,
topic: string
): Promise<boolean> {
return this._server.subscribeTopic(connection, topic);
}
unsubscribeTopic(
connection: WebSocketConnection,
topic: string
): Promise<boolean> {
return this._server.unsubscribeTopic(connection, topic);
}
}
export const BotP = makeClassProvider({
lifetime: 'singleton',
deps: [
ServerP,
{ require: ModuleUtilitiesI, optional: true },
{ require: PlatformUtilitiesI, optional: true },
],
factory: (server, moduleUtils, platformUtils) =>
new WebSocketBot(
server,
moduleUtils?.initScope,
platformUtils?.dispatchWrapper
),
})(WebSocketBot);
export type BotP = WebSocketBot;