UNPKG

@deepkit/desktop-ui

Version:

Library for desktop UI widgets in Angular 10+

90 lines (89 loc) 2.71 kB
import { Excluded, Type } from '@deepkit/type'; import { ClassType, TypeAnnotation } from '@deepkit/core'; import { Injector } from '@angular/core'; import { Router } from '@angular/router'; /** * If this type decorator is used then the property is not persisted in localStorage, * but in the URL instead and recovered from the URL when reloaded. * * @example * ```typescript * class State extends EfficientState { * shop: number & PartOfUrl = 0; * } * ``` */ export type PartOfUrl = TypeAnnotation<'partOfUrl'>; export type FilterActions<T> = { [name in keyof T]: T[name] extends (a: infer A extends [...a: any[]], ...args: any[]) => infer R ? (...args: A) => R : never; }; /** * EfficientState is a base class for all states that are used in the frontend. * * @example * ```typescript * //state that won't be persisted is used as `VolatileState & Excluded` * class VolatileState { * shops: Shop[] = []; * * user?: User; * } * * export class State extends EfficientState { * shop: number & PartOfUrl = 0; * sidebarVisible: boolean = true; * searchQuery: string = ''; * * volatile: VolatileState & Excluded = new VolatileState(); * * //normal methods are supported * getShop(): Shop | undefined { * return this.volatile.getShop(this.shop); * } * * //actions have access to Angular's DI container * async authenticated([user]: [FrontendUser], client: ControllerClient) { * this.volatile.shops = await client.shop.getShops(); * this.volatile.user = user; * } * } * ``` */ export declare class EfficientState { call: FilterActions<this> & Excluded; constructor(injector?: Injector); } export declare function findPartsOfUrl(stateClass: ClassType): string[]; export declare function getQueryObject(state: EfficientState): any; export declare function findPartsOfUrlForType(type: Type, paths?: string[], prefix?: string, state?: any[]): void; /** * Angular provider factory for the state class. * * @example * ```typescript * * @Injectable() * export class State extends EfficientState { * //events * fileAdded = new EventToken('file.added'); * * //persisted state * shop: number = 0; * sidebarVisible: boolean = true; * } * * @NgModule({ * providers: [ * provideState(State), * ] * }) * class AppModule {} * ``` */ export declare function provideState(stateClass: ClassType, localStorageKey?: string): { provide: ClassType<any>; deps: (typeof Router | typeof Injector)[]; useFactory: (router: Router, injector: Injector) => any; }; export declare type __ΩPartOfUrl = any[]; export declare type __ΩFilterActions = any[];