forked from riot/riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriot.d.ts
203 lines (186 loc) · 5.98 KB
/
riot.d.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {
SlotBindingData,
TemplateChunk,
BindingData,
AttributeExpressionData,
ExpressionType,
BindingType,
} from '@riotjs/dom-bindings'
// Internal Types and shortcuts
export type RegisteredComponentsMap = Map<string, () => RiotComponent>
export type ComponentEnhancer = <Props = any, State = any>(
component: RiotComponent<Props, State>,
) => RiotComponent<Props, State>
export type InstalledPluginsSet = Set<ComponentEnhancer>
export type RiotComponentsMap = {
[key: string]: RiotComponentWrapper
}
export type AutobindObjectMethods<Object, This> = {
[K in keyof Object]: Object[K] extends (...args: any) => any
? (this: This, ...args: Parameters<Object[K]>) => ReturnType<Object[K]>
: Object[K]
}
export interface RiotComponent<Props = any, State = any> {
// automatically generated on any component instance
readonly props: Props
readonly root: HTMLElement
readonly name?: string
readonly slots: SlotBindingData[]
// mutable state property
state: State
// optional alias to map the children component names
components?: RiotComponentsMap
mount(
element: HTMLElement,
initialState?: State,
parentScope?: object,
): RiotComponent<Props, State>
update(
newState?: Partial<State>,
parentScope?: object,
): RiotComponent<Props, State>
unmount(keepRootElement?: boolean): RiotComponent<Props, State>
// Helpers
$(selector: string): Element | null
$$(selector: string): Element[]
// state handling methods
shouldUpdate?(newProps: Props, oldProps: Props): boolean
// lifecycle methods
onBeforeMount?(props: Props, state: State): void
onMounted?(props: Props, state: State): void
onBeforeUpdate?(props: Props, state: State): void
onUpdated?(props: Props, state: State): void
onBeforeUnmount?(props: Props, state: State): void
onUnmounted?(props: Props, state: State): void
}
// The Riot component object without the internals
// The internal attributes will be handled by the framework
export type RiotComponentWithoutInternals<Component extends RiotComponent> =
Omit<
Component,
| 'props'
| 'root'
| 'name'
| 'slots'
| 'mount'
| 'update'
| 'unmount'
| '$'
| '$$'
>
export type RiotComponentWithoutInternalsAndInitialState<
Component extends RiotComponent,
> = Omit<RiotComponentWithoutInternals<Component>, 'state'>
// Riot Pure Component interface that should be used together with riot.pure
export interface RiotPureComponent<Context = object> {
mount(element: HTMLElement, context?: Context): RiotPureComponent<Context>
update(context?: Context): RiotPureComponent<Context>
unmount(keepRootElement: boolean): RiotPureComponent<Context>
}
export interface PureComponentFactoryFunction<
InitialProps = any,
Context = any,
> {
({
slots,
attributes,
props,
}: {
slots?: SlotBindingData<Context>[]
attributes?: AttributeExpressionData<Context>[]
props?: InitialProps
}): RiotPureComponent<Context>
}
// This object interface is created anytime a riot file will be compiled into javascript
export interface RiotComponentWrapper<Component = RiotComponent> {
readonly css?: string | null
readonly exports?: RiotComponentFactoryFunction<Component> | Component | null
readonly name?: string | null
template?(
template: (
template: string,
bindings?: BindingData<Component>[],
) => TemplateChunk<Component>,
expressionTypes: Record<keyof typeof ExpressionType, number>,
bindingTypes: Record<keyof typeof BindingType, number>,
getComponent: (componentName: string) => any,
): TemplateChunk<Component> | null
}
// Interface for components factory functions
export interface RiotComponentFactoryFunction<Component> {
(): Component
components?: RiotComponentsMap
}
// Riot public API
export declare function register<Props, State>(
componentName: string,
wrapper: RiotComponentWrapper<RiotComponent<Props, State>>,
): RegisteredComponentsMap
export declare function unregister(
componentName: string,
): RegisteredComponentsMap
export declare function mount<Props, State>(
selector: string | HTMLElement,
initialProps?: Props,
componentName?: string,
): RiotComponent<Props, State>[]
export declare function unmount(
selector: string | HTMLElement,
keepRootElement?: boolean,
): HTMLElement[]
export declare function install(plugin: ComponentEnhancer): InstalledPluginsSet
export declare function uninstall(
plugin: ComponentEnhancer,
): InstalledPluginsSet
export declare function component<
Props,
State,
Component = RiotComponent<Props, State>,
>(
wrapper: RiotComponentWrapper<Component>,
): (
el: HTMLElement,
initialProps?: Props,
meta?: {
slots: SlotBindingData[]
attributes: AttributeExpressionData[]
parentScope: any
},
) => Component
export declare function pure<
InitialProps = any,
Context = any,
FactoryFunction = PureComponentFactoryFunction<InitialProps, Context>,
>(func: FactoryFunction): FactoryFunction
export declare const version: string
// typescript specific methods
export declare function withTypes<
Component extends RiotComponent,
ComponentFactory = RiotComponentFactoryFunction<
AutobindObjectMethods<RiotComponentWithoutInternals<Component>, Component>
>,
>(fn: ComponentFactory): () => Component
export declare function withTypes<
Component extends RiotComponent,
ComponentFactory = RiotComponentFactoryFunction<
AutobindObjectMethods<
RiotComponentWithoutInternalsAndInitialState<Component>,
Component
>
>,
>(fn: ComponentFactory): () => Component
export declare function withTypes<
Component extends RiotComponent,
ComponentObjectWithInitialState = RiotComponentWithoutInternals<Component>,
>(
component: AutobindObjectMethods<ComponentObjectWithInitialState, Component>,
): Component
export declare function withTypes<
Component extends RiotComponent,
ComponentObjectWithoutInitialState = RiotComponentWithoutInternalsAndInitialState<Component>,
>(
component: AutobindObjectMethods<
ComponentObjectWithoutInitialState,
Component
>,
): Component