Skip to content

Commit 578ed40

Browse files
committed
fix: remove old events.
feat: remove events from Client feat: add reason when stopping to server feat: add HandlerTypes to types.ts chore: fix tests
1 parent f0332c9 commit 578ed40

File tree

6 files changed

+104
-126
lines changed

6 files changed

+104
-126
lines changed

src/RPCClient.ts

+2-32
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import type {
1616
MapCallers,
1717
} from './types';
1818
import type { ErrorRPCRemote } from './errors';
19-
import { CreateDestroy, ready } from '@matrixai/async-init/dist/CreateDestroy';
2019
import Logger from '@matrixai/logger';
2120
import { Timer } from '@matrixai/timer';
22-
import { createDestroy } from '@matrixai/async-init';
2321
import * as rpcUtilsMiddleware from './middleware';
2422
import * as rpcErrors from './errors';
2523
import * as rpcUtils from './utils';
@@ -30,24 +28,8 @@ import { toError } from './utils';
3028

3129
const timerCleanupReasonSymbol = Symbol('timerCleanUpReasonSymbol');
3230

33-
/**
34-
* Events:
35-
* - {@link events.Event}
36-
*/
37-
interface RPCClient<M extends ClientManifest>
38-
extends createDestroy.CreateDestroy {}
39-
/**
40-
* You must provide an error handler `addEventListener('error')`.
41-
* Otherwise, errors will just be ignored.
42-
*
43-
* Events:
44-
* - {@link events.EventRPCClientDestroy}
45-
* - {@link events.EventRPCClientDestroyed}
46-
*/
47-
@createDestroy.CreateDestroy({
48-
eventDestroy: events.EventRPCClientDestroy,
49-
eventDestroyed: events.EventRPCClientDestroyed,
50-
})
31+
interface RPCClient<M extends ClientManifest> {}
32+
5133
class RPCClient<M extends ClientManifest> {
5234
/**
5335
* @param obj
@@ -175,16 +157,9 @@ class RPCClient<M extends ClientManifest> {
175157
} = {}): Promise<void> {
176158
this.logger.info(`Destroying ${this.constructor.name}`);
177159

178-
// You can dispatch an event before the actual destruction starts
179-
this.dispatchEvent(new events.EventRPCClientDestroy());
180-
181-
// Dispatch an event after the client has been destroyed
182-
this.dispatchEvent(new events.EventRPCClientDestroyed());
183-
184160
this.logger.info(`Destroyed ${this.constructor.name}`);
185161
}
186162

187-
@ready(new rpcErrors.ErrorRPCCallerFailed())
188163
public get methods(): MapCallers<M> {
189164
return this.methodsProxy as MapCallers<M>;
190165
}
@@ -198,7 +173,6 @@ class RPCClient<M extends ClientManifest> {
198173
* the provided I type.
199174
* @param ctx - ContextTimed used for timeouts and cancellation.
200175
*/
201-
@ready(new rpcErrors.ErrorMissingCaller())
202176
public async unaryCaller<I extends JSONValue, O extends JSONValue>(
203177
method: string,
204178
parameters: I,
@@ -235,7 +209,6 @@ class RPCClient<M extends ClientManifest> {
235209
* the provided I type.
236210
* @param ctx - ContextTimed used for timeouts and cancellation.
237211
*/
238-
@ready(new rpcErrors.ErrorRPCCallerFailed())
239212
public async serverStreamCaller<I extends JSONValue, O extends JSONValue>(
240213
method: string,
241214
parameters: I,
@@ -264,7 +237,6 @@ class RPCClient<M extends ClientManifest> {
264237
* @param method - Method name of the RPC call
265238
* @param ctx - ContextTimed used for timeouts and cancellation.
266239
*/
267-
@ready(new rpcErrors.ErrorRPCCallerFailed())
268240
public async clientStreamCaller<I extends JSONValue, O extends JSONValue>(
269241
method: string,
270242
ctx: Partial<ContextTimedInput> = {},
@@ -299,7 +271,6 @@ class RPCClient<M extends ClientManifest> {
299271
* @param method - Method name of the RPC call
300272
* @param ctx - ContextTimed used for timeouts and cancellation.
301273
*/
302-
@ready(new rpcErrors.ErrorRPCCallerFailed())
303274
public async duplexStreamCaller<I extends JSONValue, O extends JSONValue>(
304275
method: string,
305276
ctx: Partial<ContextTimedInput> = {},
@@ -439,7 +410,6 @@ class RPCClient<M extends ClientManifest> {
439410
* @param ctx - ContextTimed used for timeouts and cancellation.
440411
* @param id - Id is generated only once, and used throughout the stream for the rest of the communication
441412
*/
442-
@ready(new rpcErrors.ErrorRPCCallerFailed())
443413
public async rawStreamCaller(
444414
method: string,
445415
headerParams: JSONValue,

src/RPCServer.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { JSONValue } from './types';
1818
import type { IdGen } from './types';
1919
import type { ErrorRPC, ErrorRPCRemote } from './errors';
2020
import { ReadableStream, TransformStream } from 'stream/web';
21-
import { ready } from '@matrixai/async-init/dist/CreateDestroy';
21+
import { ready } from '@matrixai/async-init/dist/StartStop';
2222
import Logger from '@matrixai/logger';
2323
import { PromiseCancellable } from '@matrixai/async-cancellable';
2424
import { Timer } from '@matrixai/timer';
@@ -60,7 +60,7 @@ class RPCServer extends EventTarget {
6060
* @param obj.manifest - Server manifest used to define the rpc method
6161
* handlers.
6262
* @param obj.middlewareFactory - Middleware used to process the rpc messages.
63-
* The middlewareFactory needs to be a function that starts a pair of
63+
* The middlewareFactory needs to be a function that creates a pair of
6464
* transform streams that convert `Uint8Array` to `JSONRPCRequest` on the forward
6565
* path and `JSONRPCResponse` to `Uint8Array` on the reverse path.
6666
* @param obj.streamKeepAliveTimeoutTime - Time before a connection is cleaned up due to no activity. This is the
@@ -72,7 +72,7 @@ class RPCServer extends EventTarget {
7272
* the handler to handle timeout before it is forced to end. Defaults to 2,000 milliseconds.
7373
* @param obj.logger
7474
*/
75-
public static async startRPCServer({
75+
public static async start({
7676
manifest,
7777
middlewareFactory = rpcUtilsMiddleware.defaultServerMiddlewareWrapper(),
7878
handlerTimeoutTime = Infinity, // 1 minute
@@ -209,10 +209,15 @@ class RPCServer extends EventTarget {
209209
this.filterSensitive = filterSensitive || rpcUtils.filterSensitive;
210210
}
211211

212-
public async stop(force: boolean = true): Promise<void> {
213-
// Log and dispatch an event before starting the destruction
212+
public async stop({
213+
force = true,
214+
reason = '',
215+
}: {
216+
force?: boolean;
217+
reason?: string;
218+
}): Promise<void> {
219+
// Log an event before starting the destruction
214220
this.logger.info(`Stopping ${this.constructor.name}`);
215-
this.dispatchEvent(new events.EventRPCServerStopping());
216221

217222
// Your existing logic for stopping active streams and other cleanup
218223
if (force) {
@@ -225,8 +230,7 @@ class RPCServer extends EventTarget {
225230
await activeStream;
226231
}
227232

228-
// Log and dispatch an event after the destruction has been completed
229-
this.dispatchEvent(new events.EventRPCServerStopped());
233+
// Log an event after the destruction has been completed
230234
this.logger.info(`Stopped ${this.constructor.name}`);
231235
}
232236

@@ -449,7 +453,6 @@ class RPCServer extends EventTarget {
449453
/**
450454
* ID is associated with the stream, not individual messages.
451455
*/
452-
@ready(new rpcErrors.ErrorRPCHandlerFailed())
453456
public handleStream(rpcStream: RPCStream<Uint8Array, Uint8Array>) {
454457
// This will take a buffer stream of json messages and set up service
455458
// handling for it.

src/events.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,10 @@ class EventRPCClientConnect extends EventRPCClient {}
3434

3535
class EventRPCServerConnection extends EventRPCServer<RPCServer> {}
3636

37-
class EventRPCServerCreate extends EventRPCServer {}
38-
39-
class EventRPCServerCreated extends EventRPCServer {}
40-
41-
class EventRPCServerDestroy extends EventRPCServer {}
42-
43-
class EventRPCServerDestroyed extends EventRPCServer {}
4437
class EventRPCServerStart extends EventRPCServer {}
38+
4539
class EventRPCServerStarted extends EventRPCServer {}
40+
4641
class EventRPCServerStopping extends EventRPCServer {}
4742

4843
class EventRPCServerStopped extends EventRPCServer {}
@@ -81,10 +76,6 @@ export {
8176
EventRPCClientError,
8277
EventRPCClientConnect,
8378
EventRPCServerConnection,
84-
EventRPCServerCreate,
85-
EventRPCServerCreated,
86-
EventRPCServerDestroy,
87-
EventRPCServerDestroyed,
8879
EventRPCServerError,
8980
EventRPCConnectionError,
9081
EventRPCServerStopping,

src/types.ts

+14
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,25 @@ type JSONValue =
332332
| undefined;
333333

334334
type POJO = { [key: string]: any };
335+
335336
type PromiseDeconstructed<T> = {
336337
p: Promise<T>;
337338
resolveP: (value: T | PromiseLike<T>) => void;
338339
rejectP: (reason?: any) => void;
339340
};
340341

342+
type HandlerTypes<T> = T extends Handler<
343+
infer Container,
344+
infer Input,
345+
infer Output
346+
>
347+
? {
348+
container: Container;
349+
input: Input;
350+
output: Output;
351+
}
352+
: never;
353+
341354
export type {
342355
IdGen,
343356
JSONRPCRequestMessage,
@@ -365,4 +378,5 @@ export type {
365378
JSONValue,
366379
POJO,
367380
PromiseDeconstructed,
381+
HandlerTypes,
368382
};

0 commit comments

Comments
 (0)