@cerebral/fluent
Advanced tools
Comparing version 1.0.0-1518472516428 to 1.0.0-1518516413042
@@ -13,4 +13,15 @@ import { ObservableMap as MobxObservableMap } from 'mobx'; | ||
export { Module, Provider, CerebralError } from 'cerebral'; | ||
export declare type LegacyState = { | ||
export declare type TLegacyState = { | ||
set(path: string, value: any): void; | ||
get(path: string): any; | ||
toggle(path: string): void; | ||
push(path: string, value: any): void; | ||
merge(path: string, value: {}): void; | ||
pop(path: string): void; | ||
shift(path: string): void; | ||
unshift(path: string, value: any): void; | ||
splice(path: string, index: number, length: number): void; | ||
concat(path: string, value: any[]): void; | ||
increment(path: string, delta: number): void; | ||
unset(path: string): void; | ||
}; | ||
@@ -22,3 +33,2 @@ export declare class FluentController<State = {}, Signals = {}> extends BaseControllerClass { | ||
contextProviders: any; | ||
useLegacyStateApi: boolean; | ||
constructor(rootModule: ModuleClass, options: ControllerOptions & { | ||
@@ -33,2 +43,3 @@ useStrict?: boolean; | ||
useStrict?: boolean; | ||
useLegacyStateApi?: boolean; | ||
}): FluentController<State, Signals>; |
@@ -92,3 +92,4 @@ import { observable, untracked, transaction, extras, ObservableMap as MobxObservableMap, useStrict } from 'mobx'; | ||
options.Model = Model; | ||
options.Model.useLegacyStateApi = options.useLegacyStateApi; | ||
return new FluentController(rootModule, options); | ||
} |
@@ -45,3 +45,5 @@ /* eslint-env mocha */ | ||
}); | ||
const controller = Controller(rootModule); | ||
const controller = Controller(rootModule, { | ||
useLegacyStateApi: true | ||
}); | ||
controller.signals.test(); | ||
@@ -48,0 +50,0 @@ assert.equal(controller.state.foo, 'bar2'); |
@@ -187,4 +187,4 @@ import { extractModuleProp, isObject, BaseModel } from 'cerebral/internal'; | ||
]; | ||
function CreateStateProvider(state, devtools, model) { | ||
if (!devtools) { | ||
function CreateStateProvider(state, devtools, model, useLegacyStateApi) { | ||
if (!devtools && useLegacyStateApi) { | ||
legacyApi.forEach((method) => { | ||
@@ -201,17 +201,19 @@ Object.defineProperty(state, method, { | ||
? (context) => { | ||
legacyApi.forEach((method) => { | ||
Object.defineProperty(state, method, { | ||
writable: true, | ||
value(path, ...args) { | ||
if (method !== 'get') { | ||
devtools.sendExecutionData({ | ||
type: 'mutation', | ||
method: method, | ||
args: [path].concat(args.map((value) => (isObservable(value) ? value.value : value))) | ||
}, context.execution, context.functionDetails, context.props); | ||
if (useLegacyStateApi) { | ||
legacyApi.forEach((method) => { | ||
Object.defineProperty(state, method, { | ||
writable: true, | ||
value(path, ...args) { | ||
if (method !== 'get') { | ||
devtools.sendExecutionData({ | ||
type: 'mutation', | ||
method: method, | ||
args: [path.split('.')].concat(args.map((value) => (isObservable(value) ? value.value : value))) | ||
}, context.execution, context.functionDetails, context.props); | ||
} | ||
return model[method](path.split('.'), ...args); | ||
} | ||
return model[method](path.split('.'), ...args); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
return new Proxy(state, createValidator(state, context.execution, context.functionDetails, context.props, devtools)); | ||
@@ -229,3 +231,3 @@ } | ||
}); | ||
this.StateProvider = (devtools) => CreateStateProvider(this.state, devtools, this); | ||
this.StateProvider = (devtools) => CreateStateProvider(this.state, devtools, this, Model.useLegacyStateApi); | ||
} | ||
@@ -328,2 +330,3 @@ // Used by initial state changed | ||
} | ||
Model.useLegacyStateApi = false; | ||
export default Model; |
import { BaseModel } from 'cerebral/internal'; | ||
import { BaseControllerClass } from 'cerebral'; | ||
declare class Model extends BaseModel { | ||
static useLegacyStateApi: boolean; | ||
constructor(controller: BaseControllerClass); | ||
@@ -5,0 +6,0 @@ set(path: string[], value: any): void; |
{ | ||
"name": "@cerebral/fluent", | ||
"version": "1.0.0-1518472516428", | ||
"version": "1.0.0-1518516413042", | ||
"description": "Makes Cerebral typescript friendly", | ||
@@ -27,3 +27,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"cerebral": "^4.2.0-1518472516428", | ||
"cerebral": "^4.2.0-1518516413042", | ||
"mobx": "^3.4.1" | ||
@@ -30,0 +30,0 @@ }, |
60079
1464