mosx
Version:
Multiview observable state management engine
77 lines (76 loc) • 2.67 kB
TypeScript
import { IComputed } from "mobx";
import { MosxContext } from "./context";
/**
* Decorator for observable and computed properties in Mosx
*
* example:
* @mx.Object // Decorate class to make it visible for Tracker
* class Item {
* // simple types: string, number, boolen
* @mx name = "" // any type proprty
* @mx.string id = "123" //string type property
* @mx.private.boolean bool = false // private boolean property
*
* // complex types: object, array, map, set
* @mx.object(Item) obj = new Item()
* @mx.array(Item) arr = new Array<Items>()
* @mx.map(Item) map = new Map<string, Item>()
* @mx.set(Item) set = new Set<Item>()
*
* // public computed property
* @mx.computed.string
* get flag() { return this.bool } // if this.bool change, computed property flag will change too
*
* // observable property, but not visible for tracker
* // @mx.observable === mobx.observable
* @mx.observable public mobx = "mobx"
*
* // private computed property
* @mx.computed.private.string
* get flag2() { return this.mobx } // if this.mobx change, computed property flag2 will change too
* }
*
* // use class decorator for private objectes
* @mx.Object.private
* class PrivateItem extends Item {
* // PrivateItem inherits @mx properties from Item
* }
*
* // alternative way via extending of Mosx
* class Item extends Mosx {
* constructor(owner, tags) {
* super(owner, tags) // owner and tags - construcror params
* }
* }
*/
export declare type PrimitiveType = "any" | "string" | "number" | "boolean" | object;
export declare type DefinitionType = PrimitiveType | PrimitiveType[] | {
map: PrimitiveType;
} | {
set: PrimitiveType;
};
interface MosxTypesDecorator {
string: PropertyDecorator;
number: PropertyDecorator;
boolean: PropertyDecorator;
object: (mType: object) => PropertyDecorator;
array: (mType: PrimitiveType) => PropertyDecorator;
map: (mType: PrimitiveType) => PropertyDecorator;
set: (mType: PrimitiveType) => PropertyDecorator;
}
declare type ClassDecorator = <T extends new (...args: any[]) => {}>(constructor: T) => T;
interface MosxPrivateDecorator {
private: PropertyDecorator & MosxTypesDecorator;
computed: IComputed & MosxTypesDecorator & {
private: PropertyDecorator & MosxTypesDecorator;
};
observable: any;
Object: ClassDecorator & {
private: ClassDecorator;
};
}
declare type MosxDecorator = PropertyDecorator & MosxPrivateDecorator & MosxTypesDecorator & {
$context: MosxContext;
};
export declare const mx: MosxDecorator;
export {};