-
Notifications
You must be signed in to change notification settings - Fork 458
/
base_channel.ts
97 lines (80 loc) 路 2.96 KB
/
base_channel.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
/*
* Copyright 漏 2019 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { EventCallback } from '../event';
import { eventWithModuleNameReg } from '../../constants';
import { EndpointHandlers } from '../../types';
import { Logger } from '../../logger';
export interface InvokeRequest {
methodName: string;
context: {
header?: { height: number; timestamp: number; aggregateCommit: { height: number } };
};
params?: Record<string, unknown>;
}
export abstract class BaseChannel {
public readonly eventsList: ReadonlyArray<string>;
public readonly endpointsList: ReadonlyArray<string>;
public readonly namespace: string;
protected readonly endpointHandlers: EndpointHandlers;
protected readonly _logger: Logger;
private _requestId: number;
public constructor(
logger: Logger,
namespace: string,
events: ReadonlyArray<string>,
endpoints: EndpointHandlers,
) {
this._logger = logger;
this.namespace = namespace;
this.eventsList = events;
this.endpointHandlers = new Map();
this._requestId = 0;
for (const [methodName, handler] of endpoints) {
this.endpointHandlers.set(methodName, handler);
}
this.endpointsList = [...this.endpointHandlers.keys()];
}
public isValidEventName(name: string, throwError = true): boolean | never {
const result = eventWithModuleNameReg.test(name);
if (throwError && !result) {
throw new Error(`[${this.namespace}] Invalid event name ${name}.`);
}
return result;
}
public isValidActionName(name: string, throwError = true): boolean | never {
const result = eventWithModuleNameReg.test(name);
if (throwError && !result) {
throw new Error(`[${this.namespace}] Invalid action name ${name}.`);
}
return result;
}
protected _getNextRequestId(): string {
this._requestId += 1;
return this._requestId.toString();
}
// Listen to any event happening in the application
// Specified as moduleName_eventName
// If its related to your own moduleName specify as :eventName
public abstract subscribe(eventName: string, cb: EventCallback): void;
public abstract unsubscribe(eventName: string, cb: EventCallback): void;
// Publish the event on the channel
// Specified as moduleName_eventName
// If its related to your own moduleName specify as :eventName
public abstract publish(eventName: string, data?: Record<string, unknown>): void;
// Call action of any moduleName through controller
// Specified as moduleName_actionName
public abstract invoke<T>(req: InvokeRequest): Promise<T>;
public abstract registerToBus(arg: unknown): Promise<void>;
public abstract once(eventName: string, cb: EventCallback): void;
}