ts-browser-helpers
Version:
A collection of utility classes, functions and decorators for javascript/typescript projects, for use in the browser.
74 lines • 3.43 kB
TypeScript
import { AnyFunction } from './types';
/**
* Decorator that redefines a property with getter and setter, and calls a function when the property is changed.
* Parameters passed to the function are the property key and the new value.
* @note - Does not work with "target": "esnext" in tsconfig.json
* @note - Requires "experimentalDecorators": true in tsconfig.json
* @todo add example.
* @param fnKey - use: `<MyClass>.prototype.<myFunction>` or define an arrow function: `(key, value, oldValue, target) => {}`.
* @param paramType -
* if param, the function is called with 4 parameters: `key, value, oldValue, target`, default for {@link onChange}
* if object, the function is called with an object parameter: `{key, value, oldValue, target}`, default for {@link onChange3}
* if void then no params are passed. {@link onChange2}
* Default: false.
*
* @category Decorators
*/
export declare function onChange<TTarget = any>(fnKey: string | ((key: string, value: any, oldValue: any, target: TTarget) => void), paramType?: 'param' | 'object' | 'void'): PropertyDecorator;
/**
* Similar to {@link onChange}, but accepts any function and paramType defaults to 'void'. The function is called with no parameters. if 'void'
* @param fnKey
* @param paramType
*
* @category Decorators
*/
export declare function onChange2(fnKey: string | AnyFunction, paramType?: 'param' | 'object' | 'void'): PropertyDecorator;
/**
* Similar to {@link onChange}, but accepts any function and paramType defaults to 'object'. The function is called with an object parameter: `{key, value, oldValue, target}`.
* @param fnKey
* @param paramType
*
* @category Decorators
*/
export declare function onChange3<TTarget = any>(fnKey: string | ((obj: {
key: string;
value: any;
oldValue: any;
target: TTarget;
}) => void), paramType?: 'object' | 'void'): PropertyDecorator;
/**
* Type of the event dispatched by {@link onChangeDispatchEvent}.
*
* @category Decorators
*/
export interface OnChangeDispatchEventType<T = any> {
detail: {
key: keyof T;
value: T;
oldValue: T;
};
}
/**
* Similar to onChange but dispatches an event instead of calling a function.
* Requires `dispatchEvent` to be defined on the target.
* @param eventName - The name of the event to dispatch. Default: '`key`-changed'
*
* @category Decorators
*/
export declare function onChangeDispatchEvent(eventName?: string): PropertyDecorator;
/**
* Decorator to mark a class property as serializable using the {@link Serialization} class.
* @note - Requires "experimentalDecorators": true in tsconfig.json
* @param key - The key to use when serializing the property. If not provided, the property name is used (for this make sure the property name is not mangled during minification).
* @category Decorators
*/
export declare function serialize(key?: string): PropertyDecorator;
/**
* Decorator to mark a class as serializable using the {@link Serialization} class.
* @note - Requires "experimentalDecorators": true in tsconfig.json
* @param id - The id to use when serializing the class. This is used to identify the class when deserializing. Class names can be mangled during minification, so it is required to provide an id.
*
* @category Decorators
*/
export declare function serializable(id: string): <T extends new (...args: any[]) => any>(constructor: T) => T;
//# sourceMappingURL=decorators.d.ts.map