1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 3x 18x 5x 5x 5x 18x 12x 5x 5x 2x 2x 1x 4x 4x 1x 4x 4x 4x | import { STATEX_ACTION_KEY, STATEX_DATA_BINDINGS_KEY } from './constance' import { Action } from './action' import { DataObserver } from '../@angular' import { Observable } from 'rxjs/Observable' import { State } from './state' import { StateSelector } from './state-selector' import { Subscription } from 'rxjs/Subscription' declare var Reflect: any /** * Bind data for give key and target using a selector function * * @param {any} target * @param {any} key * @param {any} selectorFunc */ export function bindData(target: any, key: string, selector: StateSelector): Subscription { return State.select(selector).subscribe(args => { if (typeof target.setState === 'function') { let state = {} state[key] = args target.setState(state) } if (typeof target[key] === 'function') return target[key].call(target, args) target[key] = args }) } /** * Binds action to a function * * @example * class TodoStore { * * @action * addTodo(state: State, action: AddTodoAction): State { * // return modified state * } * } * * @export * @param {*} target * @param {string} propertyKey * @param {PropertyDescriptor} descriptor * @returns */ export function action(targetAction?: Action) { return (target: any, propertyKey: string, descriptor: PropertyDescriptor): Promise<any> | Observable<any> | any => { if (targetAction == undefined) { let metadata = Reflect.getMetadata('design:paramtypes', target, propertyKey) if (metadata == undefined || metadata.length < 2) throw new Error('@action() must be applied to a function with two arguments. ' + 'eg: reducer(state: State, action: SubclassOfAction): State { }') targetAction = metadata[1] } let statexActions = {} if (Reflect.hasMetadata(STATEX_ACTION_KEY, target)) { statexActions = Reflect.getMetadata(STATEX_ACTION_KEY, target) } statexActions[propertyKey] = targetAction Reflect.defineMetadata(STATEX_ACTION_KEY, statexActions, target) return { value: function (state: any, payload: Action): Observable<any> { return descriptor.value.call(this, state, payload) } } } } /** * Add @data meta * * @export * @param {*} target * @param {any} propertyKey * @param {any} selector * @param {any} bindImmediate */ export function data(selector: StateSelector, bindImmediate?: boolean) { return (target: any, propertyKey: string) => { let metaTarget = target instanceof DataObserver ? target : target.constructor let bindingsMeta = Reflect.getMetadata(STATEX_DATA_BINDINGS_KEY, metaTarget) if (!Reflect.hasMetadata(STATEX_DATA_BINDINGS_KEY, metaTarget)) { bindingsMeta = { selectors: {}, subscriptions: [], destroyed: !bindImmediate } } bindingsMeta.selectors[propertyKey] = selector if (bindImmediate) { bindingsMeta.subscriptions.push({ target: target, subscription: bindData(target, propertyKey, selector) }) } Reflect.defineMetadata(STATEX_DATA_BINDINGS_KEY, bindingsMeta, metaTarget) } } /** * Subscribe to the state events and map it to properties * * @export */ export function subscribe(propsClass) { let dataBindings = Reflect.getMetadata(STATEX_DATA_BINDINGS_KEY, propsClass || this) if (dataBindings != undefined) { const selectors = dataBindings.selectors || {} dataBindings.subscriptions = (dataBindings.subscriptions || []).concat( Object.keys(selectors).map(key => ({ target: this, subscription: bindData(this, key, selectors[key]) })) ) Reflect.defineMetadata(STATEX_DATA_BINDINGS_KEY, dataBindings, this) } } /** * Unsubscribe from the state changes * * @export */ export function unsubscribe() { let dataBindings = Reflect.getMetadata(STATEX_DATA_BINDINGS_KEY, this) if (dataBindings != undefined) { const subscriptions = dataBindings.subscriptions || [] subscriptions.forEach(item => item.target === this && item.subscription != undefined && item.subscription.unsubscribe()) dataBindings.subscriptions = subscriptions.filter(item => item.target !== this) Reflect.defineMetadata(STATEX_DATA_BINDINGS_KEY, dataBindings, this) } } |