import * as _ from 'lodash' import Notifier from './Notifier' import {checkOrder} from './StoreMemberGenerators' export default class StoreWithMetadata { public store: {[field: string]: any} = {} public actionCallDepth: number = 0 public notifier = new Notifier constructor(storeSource: any) { const methodNames = Object.getOwnPropertyNames(Object.getPrototypeOf(storeSource)) const builtInMethodNames = Object.getOwnPropertyNames(Object.getPrototypeOf({})) const fieldNames = Object.keys(storeSource) const storeMemberNames = _.difference(methodNames, builtInMethodNames).concat(fieldNames) for (const storeMemberName of storeMemberNames) { for (const storeMemberGeneratorClass of checkOrder) { const storeMemberGenerator = new storeMemberGeneratorClass(storeSource, storeMemberName) if (storeMemberGenerator.isMatched()) { storeMemberGenerator.run() this.store = storeSource break } } } } get state(): {[field: string]: any} { const state: {[field: string]: any} = {} for (const stateFieldName of Object.keys(this.store)) { if (!(this.store[stateFieldName] instanceof Function)) { state[stateFieldName] = this.store[stateFieldName] } } return state } }