|
1 | | -import { EventEmitter } from "events"; |
2 | | -import { |
3 | | - ChainData, |
4 | | - ProgressLog, |
5 | | - UserActionLog, |
6 | | - PackageNotification, |
7 | | - DirectoryItem, |
8 | | - InstalledPackageData |
9 | | -} from "../src/common/types"; |
10 | | - |
11 | | -/** HOW TO: |
12 | | - * - ON: |
13 | | - * eventBus.on(eventBusTag.logUi, (data) => { |
14 | | - * doStuff(data); |
15 | | - * }); |
16 | | - * |
17 | | - * - EMIT: |
18 | | - * eventBus.emit(eventBusTag.logUi, data); |
19 | | - */ |
20 | | -class MyEmitter extends EventEmitter {} |
21 | | - |
22 | | -const eventBus = new MyEmitter(); |
23 | | - |
24 | | -/** |
25 | | - * Offer a default mechanism to run listeners within a try/catch block |
26 | | - */ |
27 | | - |
28 | | -function eventBusOnSafe<T>( |
29 | | - eventName: string, |
30 | | - listener: (arg: T) => void |
31 | | -): void { |
32 | | - eventBus.on(eventName, (arg: T) => { |
33 | | - try { |
34 | | - listener(arg); |
35 | | - } catch (e) { |
36 | | - console.error("Error on event", eventName, e); |
37 | | - } |
38 | | - }); |
39 | | -} |
40 | | - |
41 | | -function eventBusOnSafeAsync<T>( |
42 | | - eventName: string, |
43 | | - listener: (arg: T) => void |
44 | | -): void { |
45 | | - eventBus.on(eventName, async (arg: T) => { |
46 | | - try { |
47 | | - await listener(arg); |
48 | | - } catch (e) { |
49 | | - console.error("Error on event", eventName, e); |
50 | | - } |
51 | | - }); |
52 | | -} |
53 | | - |
54 | | -/* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */ |
55 | | -const busFactoryNoArgAsync = (event: string) => ({ |
56 | | - on: (listener: () => Promise<void>): void => |
57 | | - eventBusOnSafeAsync(event, listener), |
58 | | - emit: (): void => { |
59 | | - eventBus.emit(event); |
60 | | - } |
61 | | -}); |
62 | | -/* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */ |
63 | | -const busFactoryNoArg = (event: string) => ({ |
64 | | - on: (listener: () => void): void => eventBusOnSafe(event, listener), |
65 | | - emit: (): void => { |
66 | | - eventBus.emit(event); |
67 | | - } |
68 | | -}); |
69 | | -// const busFactoryAsync = <T>(event: string) => ({ |
70 | | -// on: (listener: (arg: T) => Promise<void>) => |
71 | | -// eventBusOnSafeAsync<T>(event, listener), |
72 | | -// emit: (arg: T): void => { |
73 | | -// eventBus.emit(event, arg); |
74 | | -// } |
75 | | -// }); |
76 | | -/* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */ |
77 | | -const busFactory = <T>(event: string) => ({ |
78 | | - on: (listener: (arg: T) => void): void => eventBusOnSafe<T>(event, listener), |
79 | | - emit: (arg: T): void => { |
80 | | - eventBus.emit(event, arg); |
81 | | - } |
82 | | -}); |
83 | | - |
84 | | -// call: "INTERNAL_CALL", |
85 | | - |
86 | | -export const chainData = busFactory<ChainData[]>("CHAIN_DATAS"); |
87 | | -type PackageModifiedType = { dnpNames: string[]; removed?: boolean }; |
88 | | -export const packagesModified = busFactory<PackageModifiedType>( |
89 | | - "PACKAGE_MODIFIED" |
90 | | -); |
91 | | -export const directory = busFactory<DirectoryItem[]>("DIRECTORY"); |
92 | | -export const packages = busFactory<InstalledPackageData[]>("PACKAGES"); |
93 | | -export const logUi = busFactory<ProgressLog>("LOGUI"); |
94 | | -export const logUserAction = busFactory<UserActionLog>("LOG_USER_ACTION"); |
95 | | -export const notification = busFactory<PackageNotification>("NOTIFICATION"); |
96 | | - |
97 | | -// Requests (without argument) |
98 | | -export const requestChainData = busFactoryNoArg("REQUEST_CHAIN_DATA"); |
99 | | -export const requestAutoUpdateData = busFactoryNoArgAsync( |
100 | | - "REQUEST_AUTO_UPDATE_DATA" |
101 | | -); |
102 | | -export const requestDevices = busFactoryNoArgAsync("REQUEST_DEVICES"); |
103 | | -export const requestPackages = busFactoryNoArgAsync("REQUEST_PACKAGES"); |
104 | | -export const requestSystemInfo = busFactoryNoArgAsync("REQUEST_SYSTEM_INFO"); |
105 | | -export const runNatRenewal = busFactoryNoArg("RUN_NAT_RENEWAL"); |
106 | | -export const initializedDb = busFactoryNoArg("INITIALIZED_DB"); |
107 | | -export const runEthClientInstaller = busFactoryNoArg( |
108 | | - "RUN_ETH_MULTI_CLIENT_WATCHER" |
109 | | -); |
| 1 | +import { EventBus } from "@dappnode/dappmanager/src/eventBus"; |
| 2 | + |
| 3 | +// Mock placeholder empty subscription object to allow compilation |
| 4 | +const emptySubscription = { on: () => {}, emit: () => {} }; |
| 5 | + |
| 6 | +export const eventBus: EventBus = { |
| 7 | + chainData: emptySubscription, |
| 8 | + directory: emptySubscription, |
| 9 | + logUi: emptySubscription, |
| 10 | + logUserAction: emptySubscription, |
| 11 | + notification: emptySubscription, |
| 12 | + packages: emptySubscription, |
| 13 | + packagesModified: emptySubscription, |
| 14 | + telegramStatusChanged: emptySubscription, |
| 15 | + |
| 16 | + // Events without arguments |
| 17 | + initializedDb: emptySubscription, |
| 18 | + requestAutoUpdateData: emptySubscription, |
| 19 | + requestChainData: emptySubscription, |
| 20 | + requestDevices: emptySubscription, |
| 21 | + requestPackages: emptySubscription, |
| 22 | + requestSystemInfo: emptySubscription, |
| 23 | + runEthClientInstaller: emptySubscription, |
| 24 | + runNatRenewal: emptySubscription |
| 25 | +}; |
0 commit comments