-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp-framework.ts
More file actions
137 lines (115 loc) · 3.56 KB
/
app-framework.ts
File metadata and controls
137 lines (115 loc) · 3.56 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
export interface KernelContext {
invocationId: string;
}
export interface KernelAction {
name: string;
handler: (context: KernelContext, input: any) => Promise<any>;
}
export interface KernelJson {
apps: KernelAppJson[];
}
export interface KernelAppJson {
name: string;
actions: KernelActionJson[];
}
export interface KernelActionJson {
name: string;
}
export class KernelApp {
name: string;
actions: Map<string, KernelAction> = new Map();
constructor(name: string) {
this.name = name;
// Register this app in the global registry
appRegistry.registerApp(this);
}
/**
* Define an action
*/
action<T, R>(
nameOrHandler: string | ((input: T) => Promise<R>) | ((context: KernelContext, input: T) => Promise<R>),
handler?: ((input: T) => Promise<R>) | ((context: KernelContext, input: T) => Promise<R>),
) {
let actionName: string;
let actionHandler: (context: KernelContext, input: T) => Promise<R>;
if (typeof nameOrHandler === 'string' && handler) {
// Case: app.action("name", handler)
actionName = nameOrHandler;
// Create a handler that accepts context and input, adapting if needed
if (handler.length === 1) {
// Original handler only takes input, so we adapt it to the new signature
const singleArgHandler = handler as (input: T) => Promise<R>;
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
} else {
// Handler takes both context and input
actionHandler = handler as (context: KernelContext, input: T) => Promise<R>;
}
} else if (typeof nameOrHandler === 'function') {
// Case: app.action(handler)
actionName = nameOrHandler.name || 'default';
// Create a handler that accepts context and input, adapting if needed
if (nameOrHandler.length === 1) {
// Original handler only takes input, so we adapt it to the new signature
const singleArgHandler = nameOrHandler as (input: T) => Promise<R>;
actionHandler = async (context: KernelContext, input: T) => singleArgHandler(input);
} else {
// Handler takes both context and input
actionHandler = nameOrHandler as (context: KernelContext, input: T) => Promise<R>;
}
} else {
throw new Error('Invalid action definition');
}
// Register the action
this.actions.set(actionName, {
name: actionName,
handler: actionHandler,
});
return actionHandler;
}
/**
* Get all actions for this app
*/
getActions(): KernelAction[] {
return Array.from(this.actions.values());
}
/**
* Get an action by name
*/
getAction(name: string): KernelAction | undefined {
return this.actions.get(name);
}
/**
* Export app information without handlers
*/
toJSON(): KernelAppJson {
return {
name: this.name,
actions: this.getActions().map((action) => ({
name: action.name,
})),
};
}
}
// Registry for storing all Kernel apps
class KernelAppRegistry {
private apps: Map<string, KernelApp> = new Map();
registerApp(app: KernelApp): void {
this.apps.set(app.name, app);
}
getApps(): KernelApp[] {
return Array.from(this.apps.values());
}
getAppByName(name: string): KernelApp | undefined {
return this.apps.get(name);
}
export(): KernelJson {
return {
apps: this.getApps().map((app) => app.toJSON()),
};
}
exportJSON(): string {
return JSON.stringify(this.export(), null, 2);
}
}
// Create a singleton registry for apps
export const appRegistry = new KernelAppRegistry();