react-mobx-observed
Version:
`@observed` decorator for MobX and React/React-Native projects.
105 lines (104 loc) • 3.86 kB
TypeScript
import { Observable } from 'rxjs';
declare type Dictionary<T> = {
[key: string]: T;
};
interface IParams<T> {
/**
* The source observable of the property. The property is updated according to this observable.
*
* @memberof IParams
*/
source: Observable<T> | ((this: any) => Observable<T>);
/**
* Indicates whether the source may return an immediate observable as it is computed and has dependent
* properties inside the function. In that case, it will be wrapped by `computed` function and
* automatically update the property.
*
* @type {boolean}
* @memberof IParams
*/
isSourceComputed?: boolean;
/**
* Initializes a computed function and whenever the function changes, the source will be subscribed
* again.
*
* @returns {*} Anything.
* @memberof IParams
*/
computedBy?(): any;
/**
* Indicates whether the value should be loaded immediately.
*
* `true` by default.
*
* @type {boolean}
* @memberof IParams
*/
loadImmediately?: boolean;
/**
* The name(s) of the `props` of the `React.Component` class.
* When any of these properties is changed, then the source will be called again and value will be updated.
*
* @type {(string | (string[]))}
* @memberof IParams
*/
onPropsChange?: string | (string[]);
/**
* A value selector function. If this function is given, then values emitted from the `source` will be passed
* to this function to determine whether the value should be assigned to the property or not.
*
* @param {T} value Value emitted from the observable.
* @param {number} index The index of the value.
* @returns {(boolean | { select: boolean, value: any })} True if the value should be assigned to the property.
* @memberof IParams
*/
select?(value: T, index: number): boolean | {
select: boolean;
value: any;
};
/**
* Manual function to control whether the source should be called again to update the property when this
* function return true.
*
* @param {*} prevProps Previous properties of the current `React.Component`.
* @param {*} prevState Previous state of the current `React.Component`.
* @param {*} snapshot Snapshot of the current `React.Component`.
* @returns {boolean} True if the property should be updated by executing the observable again.
* @memberof IParams
*/
shouldUpdate?(prevProps: any, prevState: any, snapshot: any): boolean;
/**
* Prepares the observed property with side effects.
* The function is called for each time the observable emits a value.
*
* Anything returned from the function will be checked whether the key is in
* the target or not. If the key is in the target, then the value of the
* side effect will be assigned to the property.
*
* @param {T} value Value to be emitted.
* @returns {(Dictionary<any> | undefined)} Side effect list as
* ```js
* { [key: propertyKey of target]: newValue }
* ```
* @memberof IParams
*/
makeSideEffects?(value: T): Dictionary<any> | undefined;
}
/**
* Makes the property `observable` and then connects the property to the `source` observable.
* Whenever the `source` emits a value, the property will be updated accordingly.
*
* **NOTE**: This decorator also adds `loadData` function to the class to reset all dependent properties.
*
* **RECOMMENDATION**: Mark the property as `readonly`.
*
* @export
* @template T Property type.
* @param {IParams<T>} params Parameters.
* @returns {PropertyDecorator} Property decorator.
*/
export declare function observed<T>(params: IParams<T>): PropertyDecorator;
export interface HasObservedValues {
loadData(): void;
}
export {};