-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp-runtime.ts
More file actions
59 lines (47 loc) 路 1.51 KB
/
Copy pathapp-runtime.ts
File metadata and controls
59 lines (47 loc) 路 1.51 KB
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
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as runModes from "@eserstack/standards/run-modes";
import * as events from "@eserstack/events";
import * as services from "@eserstack/di/services";
import { type Channel } from "./channel.ts";
import { type Module } from "./module.ts";
export type AppRuntimeState = {
runMode: runModes.RunMode;
events: events.Factory;
di: typeof services.di;
channels: Map<string, Channel>;
modules: Map<string, Module>;
// deno-lint-ignore no-explicit-any
awaits: Array<Promise<any>>;
};
export const createAppRuntimeState = (): AppRuntimeState => {
return {
runMode: runModes.RunModes.NotSet,
events: events.events,
di: services.di,
channels: new Map<string, Channel>(),
modules: new Map<string, Module>(),
awaits: [],
};
};
export class AppRuntime<S extends AppRuntimeState = AppRuntimeState> {
static readonly default = Symbol("default");
readonly state: S;
constructor(state?: S) {
this.state = state ?? createAppRuntimeState() as S;
}
addModule(module: Module) {
const name = module.name ?? module.constructor.name;
this.state.modules.set(name, module);
}
addChannel(channel: Channel) {
const name = channel.name ?? channel.constructor.name;
this.state.channels.set(name, channel);
}
setAsDefault() {
this.state.di.set(AppRuntime.default, this);
}
async awaitAll() {
await Promise.all(this.state.awaits);
this.state.awaits.splice(0);
}
}