-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.ts
More file actions
32 lines (26 loc) · 1.21 KB
/
factory.ts
File metadata and controls
32 lines (26 loc) · 1.21 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
import { Component, type IComponentEnvironment, type IComponentState } from './component.ts'
import { InvalidComponentError } from './errors.ts'
import { repr } from '../../support/strings.ts'
import { type uint32 } from '../../types/numbers.ts'
export type IFactoryComponentState = NonNullable<IComponentState>
export interface FactoryComponentType<
StateT extends IFactoryComponentState = IFactoryComponentState,
EnvT extends IComponentEnvironment = IComponentEnvironment,
> {
(state: StateT, env: EnvT, children: Readonly<Component[]>): Component | Promise<Component>
}
export class FactoryComponent<
StateT extends IFactoryComponentState = IFactoryComponentState,
EnvT extends IComponentEnvironment = IComponentEnvironment,
> extends Component<FactoryComponentType<StateT, EnvT>> {
async evaluate(state: StateT, env: EnvT): Promise<Component> {
const component = await this.type(state, env, this.getChildren())
if (!(component instanceof Component)) {
throw new InvalidComponentError('Expected factory to return a component', { factory: this, result: repr(component) })
}
return component
}
override toString(indentation?: uint32) {
return super.toString(indentation, 'c.factory')
}
}