ouider 0.0.2 → 0.0.3

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.
@@ -0,0 +1,399 @@
1
+ declare class Emits<O extends Record<string, any>> {
2
+ private events;
3
+ constructor(events: O);
4
+ emit(event: keyof O, ...args: any): void;
5
+ }
6
+
7
+ type StateWatcher<T> = <K extends keyof T>(key: K, oldValue: T[K], newValue: T[K]) => void;
8
+ declare class Stated<T> {
9
+ value: T;
10
+ constructor(value: T);
11
+ }
12
+ declare function isStated<T>(ob: any): ob is Stated<T>;
13
+ declare function stated<S extends Record<string, string>, T>(target: T, state: State<S>): Stated<T>;
14
+ declare class State<T extends Record<string, any>> {
15
+ private THRESHOLD_TIME;
16
+ private debounceTime;
17
+ private state;
18
+ private listeners;
19
+ private timer;
20
+ constructor(data: T);
21
+ wrap<T>(obj: T): Stated<T>;
22
+ has(key: keyof T): boolean;
23
+ setValue(key: keyof T, value: any): void;
24
+ getValue(key: keyof T): any;
25
+ get value(): T;
26
+ private dispatchChanges;
27
+ didChange(path: keyof T, oldValue: any, newValue: any): Promise<void>;
28
+ watch(listener: StateWatcher<T>): number;
29
+ unwatch(listener: StateWatcher<T> | number): void;
30
+ }
31
+
32
+ type ComponentProps<K extends Record<string, any>> = K;
33
+ /**
34
+ * Component decorator, it allow an auto registration of components
35
+ * @param options
36
+ * @returns
37
+ */
38
+ declare function Component(options: {
39
+ template: string;
40
+ tag: string;
41
+ use?: OComponentType[];
42
+ css?: string;
43
+ }): <T extends {
44
+ new (...args: any[]): {};
45
+ }>(constructor: T) => {
46
+ new (...args: any[]): {
47
+ template: string;
48
+ css: string | undefined;
49
+ hash: string;
50
+ };
51
+ } & T;
52
+ interface Component<P extends Record<string, any>, O extends Record<string, any>> {
53
+ state: State<any>;
54
+ readonly emits: Emits<O>;
55
+ readonly props: ComponentProps<P>;
56
+ onMounted(): void;
57
+ willMount(): void;
58
+ willUnmount(): void;
59
+ provide<T>(key: string, value: T): void;
60
+ inject<T>(key: string): T | undefined;
61
+ }
62
+ interface ComponentConstructor<P extends Record<string, any>, O extends Record<string, any>> {
63
+ new (props?: P, emits?: O): Component<P, O>;
64
+ }
65
+ declare function createComponent<P extends Record<string, any>, O extends Record<string, any>>(ctr: ComponentConstructor<P, O>, props?: P, emits?: O): Component<P, O>;
66
+ declare class OComponent<P extends Record<string, any> = {}, O extends Record<string, any> = {}> implements Component<P, O> {
67
+ state: State<any>;
68
+ private parent?;
69
+ readonly emits: Emits<O>;
70
+ readonly props: ComponentProps<P>;
71
+ private provides;
72
+ constructor(props?: P, emits?: O);
73
+ onMounted(): void;
74
+ willMount(): void;
75
+ willUnmount(): void;
76
+ /** Provide a value for descendants */
77
+ provide<T>(key: string, value: T): void;
78
+ /** Inject a value from nearest ancestor */
79
+ inject<T>(key: string): T | undefined;
80
+ }
81
+ type LazyLoader<P extends Record<string, any>, O extends Record<string, any>> = () => Promise<{
82
+ default: ComponentConstructor<P, O>;
83
+ }>;
84
+ type OComponentType<P extends Record<string, any> = {}, O extends Record<string, any> = {}> = ComponentConstructor<P, O> | LazyLoader<P, O>;
85
+
86
+ type ProvideFunction<T> = () => T;
87
+ type Provider<T = any> = {
88
+ value?: T;
89
+ provide?: ProvideFunction<T>;
90
+ };
91
+ interface Plugin {
92
+ install(app: App): void;
93
+ }
94
+ /** Injection token key */
95
+ type InjectionKey = string;
96
+ /** Providers type */
97
+ type Providers = Map<InjectionKey, Provider>;
98
+ /**
99
+ * OUIDesigner App
100
+ */
101
+ declare class App {
102
+ private root;
103
+ private options?;
104
+ static currentApp: App | null;
105
+ private providers;
106
+ /**
107
+ * Provide a value through a key to all the app globally
108
+ * @param token the registration key
109
+ * @param value the provider value
110
+ */
111
+ provide<T>(token: InjectionKey, value: T | (() => T)): void;
112
+ /**
113
+ * Get globally a provider through a given key
114
+ * @param token the key to look up globally
115
+ * @returns a provider value
116
+ */
117
+ inject<T>(token: InjectionKey): T | undefined;
118
+ /**
119
+ * Register a plugin to be used by this app
120
+ * @param plugin the plugin to register
121
+ * @returns `this` App instance
122
+ */
123
+ use(plugin: Plugin): this;
124
+ constructor(root: ComponentConstructor<any, any>, options?: {
125
+ css?: string;
126
+ } | undefined);
127
+ /**
128
+ * Mount the App in a host element
129
+ * @param selector the host element where the app's root component should be mounted
130
+ */
131
+ mount(selector: string): Promise<void>;
132
+ }
133
+ /**
134
+ * Provide a value through a key to all the app globally
135
+ * @param token the registration key
136
+ * @param value the provider value
137
+ */
138
+ declare function provide<T>(token: InjectionKey, value: T | (() => T)): void;
139
+ /**
140
+ * Get globally a provider through a given key
141
+ * @param token the key to look up globally
142
+ * @returns a provider value
143
+ */
144
+ declare function inject<T>(token: InjectionKey): T | undefined;
145
+
146
+ type QueryFilter = "children" | "classes" | "style" | "attributes";
147
+ declare namespace ODOM {
148
+ export type NodeType = 'Element' | 'Text' | 'Comment' | 'Attribute' | 'Unknown';
149
+ type Attributes = {
150
+ name: string;
151
+ value: string;
152
+ }[];
153
+ export class OObject {
154
+ uid: string;
155
+ tag: string;
156
+ constructor(uid: string, tag: string);
157
+ addEventListener(event: string, callback: (...args: any[]) => void): Promise<string>;
158
+ dettachEventListener(event: string, cbId: string): Promise<void>;
159
+ invoke(fn: string, ...args: any[]): Promise<void>;
160
+ getProperty<T>(name: string): Promise<T | null>;
161
+ setProperty<T>(name: string, value: T): Promise<void>;
162
+ dispatchEvent(eventName: string, eventClass?: string, initDict?: any): Promise<void>;
163
+ }
164
+ export abstract class ONode extends OObject {
165
+ textContent?: string | null;
166
+ type: NodeType;
167
+ attributes: Attributes;
168
+ children?: ONode[];
169
+ classes?: string[];
170
+ style?: string;
171
+ constructor(node: ONode);
172
+ }
173
+ export class OElement extends ONode {
174
+ constructor(node: ONode);
175
+ addClass(name: string): Promise<void>;
176
+ setAttribute(name: string, value: string): Promise<void>;
177
+ removeAttribute(name: string): Promise<void>;
178
+ appendChild(child: ONode): Promise<void>;
179
+ set innerHTML(value: string);
180
+ cloneNode(state: boolean): Promise<OElement | null>;
181
+ remove(): Promise<void>;
182
+ removeChild(child: ONode): Promise<void>;
183
+ replaceChildNode(node: ONode, child: ONode): Promise<void>;
184
+ replaceWith(node: ONode): Promise<void>;
185
+ after(node: ONode): Promise<void>;
186
+ setHTML(html: string): Promise<void>;
187
+ HTML(): Promise<string>;
188
+ setInnerText(text: string): Promise<void>;
189
+ setContentText(text: string): Promise<void>;
190
+ getContentText(): Promise<string>;
191
+ content(): Promise<string>;
192
+ childNodes(): Promise<OElement[]>;
193
+ hasAttribute(name: string): boolean;
194
+ getAttribute(name: string): Promise<string>;
195
+ attribute(name: string): string | null;
196
+ getAttributeNames(): Promise<string[]>;
197
+ parentNode(): Promise<OElement | null>;
198
+ insertBefore(element: ONode, node: ONode): Promise<unknown>;
199
+ setInputValue(value: string): Promise<void>;
200
+ inputValue(value: string): Promise<string>;
201
+ query(selector: string, filter?: QueryFilter[]): Promise<OElement | null>;
202
+ queryAll(selector: string, filter?: QueryFilter[]): Promise<OElement[] | null>;
203
+ release(): Promise<void>;
204
+ }
205
+ export { };
206
+ }
207
+ declare function Native(): (clazz: any, fnName: string, descriptor: {
208
+ value: Function;
209
+ } | Record<string, any>) => void;
210
+ interface OUIDBridgeInterface {
211
+ invoke(name: string): Promise<any>;
212
+ emit(event: string, data: any): void;
213
+ subscribe(event: string, callback: (id: string, data: string) => void): void;
214
+ }
215
+ type OUIDConfig = {
216
+ appId: string;
217
+ appName: string;
218
+ appSecret: string;
219
+ };
220
+ declare var WebOUID: {
221
+ invoke: (id: string, name: string, argsJson: string) => void;
222
+ } | undefined;
223
+ declare class OUIDBridge implements OUIDBridgeInterface {
224
+ private callbacks;
225
+ private DOM_EVENT_LISTENERS;
226
+ private timers;
227
+ private _config?;
228
+ private listeners;
229
+ constructor();
230
+ config(conf?: OUIDConfig): OUIDBridge;
231
+ invoke(name: string, ...args: any[]): Promise<any>;
232
+ /**
233
+ * Make a synchronous class
234
+ * @param name the name without '_oui' prefix
235
+ * @param args the arguments list
236
+ * @returns a promise of T
237
+ */
238
+ call<T>(name: string, ...args: any[]): Promise<T | null>;
239
+ emit(event: string, data: any): void;
240
+ /**
241
+ *
242
+ * @param event Create a subscription for a given event name, should be possible to register only once ??
243
+ * @param callback
244
+ */
245
+ subscribe(event: string, callback: (id: string, ...args: any[]) => void): void;
246
+ query(selector: string, filter?: QueryFilter[], nodeId?: string): Promise<ODOM.OElement | null>;
247
+ queryAll(selector: string, filter?: QueryFilter[], nodeId?: string): Promise<ODOM.OElement[] | null>;
248
+ createdElement(tag: string, props?: Record<string, string>): Promise<ODOM.OElement | null>;
249
+ createComment(data: string): Promise<ODOM.OElement | null>;
250
+ addEventListener(node: ODOM.OObject | 'window' | 'document', event: string, callback: (...args: any[]) => void): Promise<string>;
251
+ dettachEventListener(node: ODOM.OObject | 'window' | 'document', event: string, cbId: string): Promise<void>;
252
+ injectComponentStyles(css: string): Promise<ODOM.OElement | null>;
253
+ rejectComponentStyles(cssNode: ODOM.ONode): Promise<void>;
254
+ getOObject(id: string): Promise<ODOM.OObject | null>;
255
+ acquireObject(name: string): Promise<ODOM.OObject | null>;
256
+ setTimeout(callback: () => void, delay: number): Promise<number>;
257
+ clearTimeout(id: number): Promise<void>;
258
+ setInterval(callback: () => void, delay: number): Promise<number>;
259
+ clearInterval(id: number): Promise<void>;
260
+ }
261
+ declare const OUID: OUIDBridge;
262
+ declare global {
263
+ var OUID: OUIDBridge;
264
+ }
265
+
266
+ declare class RenderContext {
267
+ private app;
268
+ component: OComponent<Record<string, any>, Record<string, any>>;
269
+ private parentContext;
270
+ static PROVIDE_TOKEN: string;
271
+ private bindings;
272
+ private directives;
273
+ private mountedComponents;
274
+ stack: Record<string, any>[];
275
+ constructor(app: App, component: OComponent<Record<string, any>, Record<string, any>>, parentContext: RenderContext | null, ...frames: Record<string, any>[]);
276
+ get hostElement(): ODOM.OElement;
277
+ bind(binding: Binding): void;
278
+ directive(directive: Directive): void;
279
+ evaluateExpression(expr: string | null | undefined): boolean;
280
+ resolve(key: string | null | undefined, strict?: boolean, ...additionFrames: Record<string, any>[]): any;
281
+ /**
282
+ * Handing (o-model) like (ngModel) update, we should support mutliple syntaxe like o-mode="value" where value is defined directly on the component
283
+ * o-model="data['key']" where data is either defined on the component, of in the enclosing scope in case of for-loop for instance
284
+ * */
285
+ updateValue(key: string, value: any): void;
286
+ push(frame: Record<string, any>): void;
287
+ pop(): void;
288
+ updateBindings(): Promise<void>;
289
+ updateBinding(binding: Binding): Promise<void>;
290
+ updateDirectives(): Promise<void>;
291
+ private render;
292
+ private expandClass;
293
+ private expandStyle;
294
+ private expandStandardAttributes;
295
+ handleElementNode(node: ODOM.OElement): Promise<void>;
296
+ handleTextNode(node: ODOM.OElement): void;
297
+ private componentAttributes;
298
+ mountComponent<T extends Record<string, any>, O extends Record<string, string>>(hostNode: ODOM.OElement, component: OComponentType<T, O>, parentContext: RenderContext | null, props?: Record<string, {
299
+ name: string;
300
+ value: any;
301
+ expr?: string;
302
+ }>, emits?: O): Promise<void>;
303
+ unmountComponent(node: ODOM.OElement): Promise<void>;
304
+ }
305
+ type Binding = {
306
+ type: 'model' | 'interpolation' | 'attribute' | 'prop';
307
+ node: ODOM.OElement;
308
+ key: string;
309
+ template?: string | null;
310
+ context: RenderContext;
311
+ };
312
+ type Directive = {
313
+ type: 'if' | 'for';
314
+ node: ODOM.OElement;
315
+ expr?: string;
316
+ placeholder: ODOM.OElement;
317
+ context: RenderContext;
318
+ active?: boolean;
319
+ list?: string;
320
+ item?: string;
321
+ children?: Map<any, {
322
+ node: ODOM.ONode;
323
+ ctx: RenderContext;
324
+ }>;
325
+ key?: string;
326
+ };
327
+ declare function o<P extends Record<string, any>, O extends Record<string, any>>(component: ComponentConstructor<P, O>, props: P, emits?: O): OComponent<P>;
328
+ declare function o<P extends Record<string, any>, O extends Record<string, any>>(component: OComponentType<P, O>, props: P, emits?: O): Promise<OComponent<P>>;
329
+
330
+ /**
331
+ * Component responsible for display routes
332
+ * Usage: <o-router></o-router>
333
+ */
334
+ declare class RouterComponent extends OComponent {
335
+ private routeStateHander;
336
+ private router;
337
+ willMount(): void;
338
+ onMounted(): void;
339
+ willUnmount(): void;
340
+ }
341
+ declare const ROUTER_INJECTION_TOKEN = "OROUTER_TOKEN";
342
+ declare const ACTIVE_ROUTE_TOKEN = "ACTIVE_ROUTE";
343
+ interface Route {
344
+ path: string;
345
+ name: string;
346
+ component?: OComponentType<any, any>;
347
+ redirectTo?: string;
348
+ }
349
+ declare function useRouter(): Router | undefined;
350
+ declare function createRouter(routes: Routes): Router;
351
+ type Routes = Array<Route>;
352
+ type MatchedRoute = {
353
+ route: Route;
354
+ params: Record<string, any>;
355
+ query: Record<string, string>;
356
+ };
357
+ type Promised<T> = T | Promise<T>;
358
+ type RouteLocationNamed = {
359
+ name: string;
360
+ params?: Record<string, any>;
361
+ };
362
+ type RouteGuardReturn = void | boolean | RouteLocationNamed;
363
+ type RouteGaurdFunction = (to: {
364
+ url: string;
365
+ path: string;
366
+ name: string;
367
+ }, from?: {
368
+ query: Record<string, string>;
369
+ params: Record<string, string>;
370
+ }) => Promised<RouteGuardReturn>;
371
+ interface RouteGuard {
372
+ type: 'before' | 'after';
373
+ fn: RouteGaurdFunction;
374
+ }
375
+ declare class Router implements Plugin {
376
+ routes: Routes;
377
+ private windowObject;
378
+ private guards;
379
+ private eventRegistration;
380
+ constructor(routes: Routes);
381
+ install(app: App): void;
382
+ resolve(path: string): MatchedRoute | null;
383
+ push(options: {
384
+ name?: string;
385
+ path?: string;
386
+ params?: Record<string, string>;
387
+ absolute?: boolean;
388
+ }): void;
389
+ private beforeRouteGoing;
390
+ private afterRouteGoing;
391
+ bind(component: RouterComponent): Promise<(() => void)>;
392
+ unbind(handler: () => void): void;
393
+ beforeEach(fn: RouteGaurdFunction): (() => void);
394
+ afterEach(fn: RouteGaurdFunction): (() => void);
395
+ }
396
+
397
+ declare function components(): Record<string, OComponent<any, any>>;
398
+
399
+ export { ACTIVE_ROUTE_TOKEN, App, type Binding, Component, type ComponentConstructor, type ComponentProps, type Directive, Emits, type InjectionKey, type LazyLoader, Native, OComponent, type OComponentType, ODOM, OUID, OUIDBridge, type OUIDBridgeInterface, type OUIDConfig, type Plugin, type Promised, type Provider, type Providers, ROUTER_INJECTION_TOKEN, RenderContext, type Route, type RouteGaurdFunction, type RouteGuard, type RouteGuardReturn, type RouteLocationNamed, Router, RouterComponent, type Routes, State, type StateWatcher, Stated, WebOUID, components, createComponent, createRouter, inject, isStated, o, provide, stated, useRouter };