@elysiumjs/core 0.4.2 → 0.5.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 +26 -17
- package/dist/app.d.ts +180 -0
- package/dist/cache.d.ts +114 -0
- package/dist/command.d.ts +273 -0
- package/dist/console.d.ts +167 -0
- package/dist/database.d.ts +113 -0
- package/dist/env.d.ts +30 -0
- package/dist/event.d.ts +94 -0
- package/dist/http.d.ts +242 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +24 -0
- package/dist/job.d.ts +106 -0
- package/dist/macros.d.ts +2 -0
- package/dist/middleware.d.ts +70 -0
- package/dist/model.d.ts +4803 -0
- package/dist/module.d.ts +45 -0
- package/dist/redis.d.ts +134 -0
- package/dist/repository.d.ts +164 -0
- package/dist/service.d.ts +132 -0
- package/dist/utils.d.ts +26 -0
- package/dist/wamp.d.ts +273 -0
- package/dist/websocket.d.ts +92 -0
- package/dist/worker.d.ts +220 -0
- package/package.json +15 -8
- package/tsconfig.json +34 -33
- package/index.ts +0 -54
- package/macros.ts +0 -16
- package/src/app.ts +0 -647
- package/src/cache.ts +0 -218
- package/src/command.ts +0 -908
- package/src/console.ts +0 -493
- package/src/database.ts +0 -257
- package/src/env.ts +0 -62
- package/src/event.ts +0 -227
- package/src/http.ts +0 -736
- package/src/job.ts +0 -196
- package/src/middleware.ts +0 -164
- package/src/model.ts +0 -386
- package/src/module.ts +0 -104
- package/src/redis.ts +0 -408
- package/src/repository.ts +0 -295
- package/src/service.ts +0 -351
- package/src/utils.ts +0 -95
- package/src/wamp.ts +0 -569
- package/src/websocket.ts +0 -191
- package/src/worker.ts +0 -849
- package/types.d.ts +0 -35
package/src/database.ts
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { DrizzleConfig } from 'drizzle-orm';
|
|
16
|
-
import type { BunSQLDatabase } from 'drizzle-orm/bun-sql';
|
|
17
|
-
import type { CacheConfig } from 'drizzle-orm/cache/core/types';
|
|
18
|
-
import type { CacheInterface } from './cache';
|
|
19
|
-
|
|
20
|
-
import { getTableName, is, Table } from 'drizzle-orm';
|
|
21
|
-
import { drizzle } from 'drizzle-orm/bun-sql';
|
|
22
|
-
import { Cache as DrizzleCache } from 'drizzle-orm/cache/core';
|
|
23
|
-
|
|
24
|
-
import { Cache } from './cache';
|
|
25
|
-
import { Service } from './service';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Properties used to create a new database connection.
|
|
29
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
30
|
-
*/
|
|
31
|
-
export type DatabaseConnectionProps<
|
|
32
|
-
TSchema extends Record<string, unknown> = Record<string, never>
|
|
33
|
-
> = Omit<DrizzleConfig<TSchema>, 'cache'> &
|
|
34
|
-
(
|
|
35
|
-
| {
|
|
36
|
-
connection:
|
|
37
|
-
| string
|
|
38
|
-
| ({
|
|
39
|
-
url?: string;
|
|
40
|
-
} & Bun.SQLOptions);
|
|
41
|
-
}
|
|
42
|
-
| {
|
|
43
|
-
client: Bun.SQL;
|
|
44
|
-
}
|
|
45
|
-
) & {
|
|
46
|
-
/**
|
|
47
|
-
* Configures caching behavior for the database connection.
|
|
48
|
-
* Set it to `true` to enable caching with default configurations.
|
|
49
|
-
*/
|
|
50
|
-
cache?:
|
|
51
|
-
| true
|
|
52
|
-
| {
|
|
53
|
-
/**
|
|
54
|
-
* Whether to enable caching.
|
|
55
|
-
*/
|
|
56
|
-
enabled?: boolean;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The default TTL for cached data, in seconds.
|
|
60
|
-
* @default 1
|
|
61
|
-
*/
|
|
62
|
-
ttl?: number;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The caching strategy.
|
|
66
|
-
* Set it to `all` to cache all queries automatically.
|
|
67
|
-
* Set it to `explicit` to cache queries manually.
|
|
68
|
-
* @default 'explicit'
|
|
69
|
-
*/
|
|
70
|
-
strategy?: 'explicit' | 'all';
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The storage engine to use.
|
|
74
|
-
* @default 'redis'
|
|
75
|
-
*/
|
|
76
|
-
storage?: 'redis' | 'memory';
|
|
77
|
-
};
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* A database connection.
|
|
82
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
83
|
-
*/
|
|
84
|
-
export type DatabaseConnection = BunSQLDatabase<Record<string, never>> & {
|
|
85
|
-
$client: Bun.SQL;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Database cache implementation for Drizzle.
|
|
90
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
91
|
-
*/
|
|
92
|
-
export class DatabaseCache extends DrizzleCache {
|
|
93
|
-
#cache: Omit<CacheInterface, 'tags'>;
|
|
94
|
-
|
|
95
|
-
private usedTablesPerKey: Record<string, string[]> = {};
|
|
96
|
-
|
|
97
|
-
constructor(
|
|
98
|
-
private readonly cacheStrategy: 'explicit' | 'all' = 'explicit',
|
|
99
|
-
private readonly globalTtl: number = 1,
|
|
100
|
-
storageEngine: 'redis' | 'memory' = 'redis'
|
|
101
|
-
) {
|
|
102
|
-
super();
|
|
103
|
-
this.#cache = Cache[storageEngine].tags('database');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public override strategy(): 'explicit' | 'all' {
|
|
107
|
-
return this.cacheStrategy;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public override async get(key: string): Promise<any[] | undefined> {
|
|
111
|
-
const res = (await this.#cache.get<any[]>(key)) ?? undefined;
|
|
112
|
-
return res;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public override async put(
|
|
116
|
-
key: string,
|
|
117
|
-
response: any,
|
|
118
|
-
tables: string[],
|
|
119
|
-
isTag: boolean,
|
|
120
|
-
config?: CacheConfig
|
|
121
|
-
): Promise<void> {
|
|
122
|
-
await this.#cache.set(key, response, (config?.ex ?? this.globalTtl) * 1000);
|
|
123
|
-
for (const table of tables) {
|
|
124
|
-
const keys = this.usedTablesPerKey[table];
|
|
125
|
-
if (keys === undefined) {
|
|
126
|
-
this.usedTablesPerKey[table] = [key];
|
|
127
|
-
} else {
|
|
128
|
-
keys.push(key);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
public override async onMutate(params: {
|
|
134
|
-
tags: string | string[];
|
|
135
|
-
tables: string | string[] | Table<any> | Table<any>[];
|
|
136
|
-
}): Promise<void> {
|
|
137
|
-
const tagsArray = params.tags ? (Array.isArray(params.tags) ? params.tags : [params.tags]) : [];
|
|
138
|
-
const tablesArray = params.tables
|
|
139
|
-
? Array.isArray(params.tables)
|
|
140
|
-
? params.tables
|
|
141
|
-
: [params.tables]
|
|
142
|
-
: [];
|
|
143
|
-
|
|
144
|
-
const keysToDelete = new Set<string>();
|
|
145
|
-
|
|
146
|
-
for (const table of tablesArray) {
|
|
147
|
-
const tableName = is(table, Table) ? getTableName(table) : (table as string);
|
|
148
|
-
const keys = this.usedTablesPerKey[tableName] ?? [];
|
|
149
|
-
for (const key of keys) keysToDelete.add(key);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (keysToDelete.size > 0 || tagsArray.length > 0) {
|
|
153
|
-
await this.#cache.mdel(tagsArray);
|
|
154
|
-
await this.#cache.mdel([...keysToDelete]);
|
|
155
|
-
|
|
156
|
-
for (const table of tablesArray) {
|
|
157
|
-
const tableName = is(table, Table) ? getTableName(table) : (table as string);
|
|
158
|
-
this.usedTablesPerKey[tableName] = [];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export namespace Database {
|
|
165
|
-
/**
|
|
166
|
-
* Creates a service name for a database connection.
|
|
167
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
168
|
-
* @param name The name of the connection.
|
|
169
|
-
* @returns A service name for the connection.
|
|
170
|
-
*/
|
|
171
|
-
const getConnectionName = (name: string) => {
|
|
172
|
-
return `db.connection.${name}`;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Retrieves the connection with the given name.
|
|
177
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
178
|
-
* @param name The connection name.
|
|
179
|
-
* @returns The connection with the given name.
|
|
180
|
-
*/
|
|
181
|
-
export const getConnection = (name: string) => {
|
|
182
|
-
if (!Service.exists(getConnectionName(name))) {
|
|
183
|
-
// TODO: Use logger service here
|
|
184
|
-
console.error(
|
|
185
|
-
`No connection with name ${name} found. Please make sure to register the connection before using it.`
|
|
186
|
-
);
|
|
187
|
-
process.exit(1);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return Service.get<DatabaseConnection>(getConnectionName(name))!;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Creates and registers a new connection.
|
|
195
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
196
|
-
*
|
|
197
|
-
* This will make the registered connection available for dependency injection with
|
|
198
|
-
* the key `db.connection.{name}`, where `{name}` is replaced with the given name.
|
|
199
|
-
*
|
|
200
|
-
* @param name The connection name.
|
|
201
|
-
* @param config The connection properties.
|
|
202
|
-
* @returns The newly created and registered connection.
|
|
203
|
-
*/
|
|
204
|
-
export const registerConnection = (name: string, config: DatabaseConnectionProps) => {
|
|
205
|
-
if (Service.exists(getConnectionName(name))) {
|
|
206
|
-
// TODO: Use logger service here
|
|
207
|
-
console.error(`A connection with the name ${name} has already been registered.`);
|
|
208
|
-
process.exit(1);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return Service.instance(
|
|
212
|
-
getConnectionName(name),
|
|
213
|
-
drizzle({
|
|
214
|
-
...config,
|
|
215
|
-
cache:
|
|
216
|
-
config.cache === undefined
|
|
217
|
-
? undefined
|
|
218
|
-
: config.cache === true
|
|
219
|
-
? new DatabaseCache()
|
|
220
|
-
: config.cache.enabled
|
|
221
|
-
? new DatabaseCache(config.cache.strategy, config.cache.ttl, config.cache.storage)
|
|
222
|
-
: undefined
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Checks if a connection with the given name exists.
|
|
229
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
230
|
-
* @param name The name of the connection to check.
|
|
231
|
-
* @returns `true` if the connection exists, `false` otherwise.
|
|
232
|
-
*/
|
|
233
|
-
export const connectionExists = (name: string) => {
|
|
234
|
-
return Service.exists(getConnectionName(name));
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Retrieves the default connection.
|
|
239
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
240
|
-
* @returns The default connection.
|
|
241
|
-
*/
|
|
242
|
-
export const getDefaultConnection = () => {
|
|
243
|
-
return getConnection('default');
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Sets the default connection.
|
|
248
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
249
|
-
* @param name The name of the connection to set as default.
|
|
250
|
-
* @returns The default connection.
|
|
251
|
-
*/
|
|
252
|
-
export const setDefaultConnection = (name: string) => {
|
|
253
|
-
const serviceName = getConnectionName('default');
|
|
254
|
-
Service.remove(serviceName);
|
|
255
|
-
return Service.instance(serviceName, getConnection(name));
|
|
256
|
-
};
|
|
257
|
-
}
|
package/src/env.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { AppEnv } from '@elysiumjs/core';
|
|
16
|
-
|
|
17
|
-
const env: Map<string, unknown> = new Map();
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Initializes the environment variables.
|
|
21
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
22
|
-
* @param vars The environment variables to initialize.
|
|
23
|
-
*/
|
|
24
|
-
export const initEnv = (vars: Record<string, unknown>) => {
|
|
25
|
-
for (const [key, value] of Object.entries(vars)) {
|
|
26
|
-
env.set(key, value);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export namespace Env {
|
|
31
|
-
/**
|
|
32
|
-
* Gets the value of an environment variable.
|
|
33
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
34
|
-
* @param name The name of the environment variable.
|
|
35
|
-
* @param defaultValue The default value to return if the environment variable is not set.
|
|
36
|
-
* @returns The value of the environment variable.
|
|
37
|
-
*/
|
|
38
|
-
export const get = <TKey extends keyof AppEnv>(
|
|
39
|
-
name: TKey,
|
|
40
|
-
defaultValue?: AppEnv[TKey]
|
|
41
|
-
): AppEnv[TKey] => {
|
|
42
|
-
return (env.get(name) ?? defaultValue) as AppEnv[TKey];
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Checks if an environment variable exists.
|
|
47
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
48
|
-
* @param name The name of the environment variable.
|
|
49
|
-
* @returns `true` if the environment variable exists, `false` otherwise.
|
|
50
|
-
*/
|
|
51
|
-
export const exists = <TKey extends keyof AppEnv>(name: TKey): boolean => {
|
|
52
|
-
return env.has(name);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Clears the environment variables.
|
|
57
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
58
|
-
*/
|
|
59
|
-
export const clear = () => {
|
|
60
|
-
env.clear();
|
|
61
|
-
};
|
|
62
|
-
}
|
package/src/event.ts
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { EventEmitter } from 'node:events';
|
|
16
|
-
|
|
17
|
-
import { isString } from 'radash';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Internal event bus used by the framework.
|
|
21
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
22
|
-
*/
|
|
23
|
-
class EventBus extends EventEmitter {
|
|
24
|
-
static readonly instance = new EventBus();
|
|
25
|
-
|
|
26
|
-
private constructor() {
|
|
27
|
-
super();
|
|
28
|
-
|
|
29
|
-
this.on('error', (error) => this.emitError(error));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public emitError(error: unknown) {
|
|
33
|
-
this.emit('elysium:error', { data: error, source: null });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public emitEvent(event: string, data: unknown, source: unknown = null) {
|
|
37
|
-
this.emit(event, { data, source });
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Stores the event data with the source.
|
|
43
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
44
|
-
*/
|
|
45
|
-
export type EventData<TData, TSource = null> = {
|
|
46
|
-
/**
|
|
47
|
-
* The emitted data.
|
|
48
|
-
*/
|
|
49
|
-
data: TData;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The source of the event.
|
|
53
|
-
*/
|
|
54
|
-
source: TSource | null;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Event handler function.
|
|
59
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
60
|
-
*/
|
|
61
|
-
export type EventHandler<TData, TSource = any> = (
|
|
62
|
-
event: EventData<TData, TSource>
|
|
63
|
-
) => void | Promise<void>;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Properties required when declaring a method as an event listener.
|
|
67
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
68
|
-
*/
|
|
69
|
-
export type ListenProps = {
|
|
70
|
-
/**
|
|
71
|
-
* The listener mode.
|
|
72
|
-
*
|
|
73
|
-
* Set it to `on` to listen for the event every time it is emitted.
|
|
74
|
-
* Set it to `once` to listen for the event only once.
|
|
75
|
-
*/
|
|
76
|
-
mode: 'on' | 'once';
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* The event name.
|
|
80
|
-
*/
|
|
81
|
-
event: string;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Whether to prepend the listener to the event. Defaults to `false`.
|
|
85
|
-
* @default false
|
|
86
|
-
*/
|
|
87
|
-
prepend?: boolean;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export namespace Event {
|
|
91
|
-
/**
|
|
92
|
-
* Emits an event with the provided data and source.
|
|
93
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
94
|
-
* @param event The event name.
|
|
95
|
-
* @param data The emitted data.
|
|
96
|
-
* @param source The source of the event. Defaults to `null`.
|
|
97
|
-
*/
|
|
98
|
-
export const emit = <TData, TSource = any>(
|
|
99
|
-
event: string,
|
|
100
|
-
data: TData,
|
|
101
|
-
source: TSource | null = null
|
|
102
|
-
): void => {
|
|
103
|
-
EventBus.instance.emitEvent(event, data, source);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Marks a method as an event listener.
|
|
108
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
109
|
-
* @param options The decorator options.
|
|
110
|
-
*/
|
|
111
|
-
export function on(options: Omit<ListenProps, 'mode'>): MethodDecorator;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Registers an event listener for the specified event.
|
|
115
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
116
|
-
* @param event The event name.
|
|
117
|
-
* @param handler The event handler function.
|
|
118
|
-
*/
|
|
119
|
-
export function on<TData, TSource = any>(
|
|
120
|
-
event: string,
|
|
121
|
-
handler: EventHandler<TData, TSource>
|
|
122
|
-
): void;
|
|
123
|
-
|
|
124
|
-
export function on<TData, TSource = any>(
|
|
125
|
-
event: string | Omit<ListenProps, 'mode'>,
|
|
126
|
-
handler?: EventHandler<TData, TSource> | never
|
|
127
|
-
): MethodDecorator | void {
|
|
128
|
-
if (isString(event)) {
|
|
129
|
-
EventBus.instance.on(event, handler!);
|
|
130
|
-
} else {
|
|
131
|
-
return function (_target, _propertyKey, descriptor) {
|
|
132
|
-
const listener = async function (eventData: EventData<any>) {
|
|
133
|
-
try {
|
|
134
|
-
await (descriptor.value as EventHandler<any>)(eventData);
|
|
135
|
-
} catch (e: unknown) {
|
|
136
|
-
EventBus.instance.emitError(e);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const prepend = event.prepend ?? false;
|
|
141
|
-
if (prepend) {
|
|
142
|
-
EventBus.instance.prependListener(event.event, listener);
|
|
143
|
-
} else {
|
|
144
|
-
EventBus.instance.on(event.event, listener);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Marks a method as an event listener that listens for the event only once.
|
|
152
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
153
|
-
* @param options The decorator options.
|
|
154
|
-
*/
|
|
155
|
-
export function once(options: Omit<ListenProps, 'mode'>): MethodDecorator;
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Registers an event listener for the specified event that listens for the event only once.
|
|
159
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
160
|
-
* @param event The event name.
|
|
161
|
-
* @param handler The event handler function.
|
|
162
|
-
*/
|
|
163
|
-
export function once<TData, TSource = any>(
|
|
164
|
-
event: string,
|
|
165
|
-
handler: EventHandler<TData, TSource>
|
|
166
|
-
): void;
|
|
167
|
-
|
|
168
|
-
export function once<TData, TSource = any>(
|
|
169
|
-
event: string | Omit<ListenProps, 'mode'>,
|
|
170
|
-
handler?: EventHandler<TData, TSource> | never
|
|
171
|
-
): MethodDecorator | void {
|
|
172
|
-
if (isString(event)) {
|
|
173
|
-
EventBus.instance.once(event, handler!);
|
|
174
|
-
} else {
|
|
175
|
-
return function (_target, _propertyKey, descriptor) {
|
|
176
|
-
const listener = async function (eventData: EventData<any>) {
|
|
177
|
-
try {
|
|
178
|
-
await (descriptor.value as EventHandler<any>)(eventData);
|
|
179
|
-
} catch (e: unknown) {
|
|
180
|
-
EventBus.instance.emitError(e);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const prepend = event.prepend ?? false;
|
|
185
|
-
if (prepend) {
|
|
186
|
-
EventBus.instance.prependOnceListener(event.event, listener);
|
|
187
|
-
} else {
|
|
188
|
-
EventBus.instance.once(event.event, listener);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Removes all event listeners for the specified event.
|
|
196
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
197
|
-
* @param event The event name.
|
|
198
|
-
*/
|
|
199
|
-
export const off = (event: string): void => {
|
|
200
|
-
EventBus.instance.removeAllListeners(event);
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Removes all event listeners.
|
|
205
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
206
|
-
*/
|
|
207
|
-
export const clear = () => {
|
|
208
|
-
EventBus.instance.removeAllListeners();
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Marks a method as an event listener.
|
|
213
|
-
* @param options The decorator options.
|
|
214
|
-
*/
|
|
215
|
-
export const listen = (options: ListenProps): MethodDecorator => {
|
|
216
|
-
if (options.mode === 'on') {
|
|
217
|
-
return on(options);
|
|
218
|
-
} else if (options.mode === 'once') {
|
|
219
|
-
return once(options);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return function (_target, _propertyKey, _descriptor) {
|
|
223
|
-
// TODO: Use logger service
|
|
224
|
-
console.error("Unknown mode provided to @listen. Use either 'on' or 'once'.");
|
|
225
|
-
};
|
|
226
|
-
};
|
|
227
|
-
}
|