Comparing version 1.0.0-1532117391884 to 1.0.0-1532121829928
@@ -20,3 +20,3 @@ import ProxyStateTree from 'proxy-state-tree'; | ||
} | ||
export default function computed<Config, NewValue>(cb: (config: Config) => (state: object) => NewValue, options?: ComputedOptions): (config: Config) => (state: object) => NewValue; | ||
export default function computed<Config, NewValue>(cb: (config: Config) => (state: object) => NewValue, options?: ComputedOptions): (config: Config) => NewValue; | ||
export {}; |
@@ -8,2 +8,3 @@ export class Computed { | ||
this.cacheLimit = options.cacheLimit || this.cacheLimit; | ||
return this.evaluate.bind(this); | ||
} | ||
@@ -10,0 +11,0 @@ evaluate(actionChain, proxyStateTree, path) { |
import App, { computed } from './'; | ||
describe('Computed', () => { | ||
test('should instantiate app with computed', () => { | ||
const state = { | ||
foo: 'bar', | ||
test: computed((foo) => (state) => state.foo + foo), | ||
}; | ||
const app = new App({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => state.foo + foo, | ||
}, | ||
state, | ||
}); | ||
expect(app.computed.test(123)).toEqual('bar123'); | ||
expect(app.state.test(123)).toEqual('bar123'); | ||
}); | ||
test('should not recalculate when not dirty', () => { | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new App({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.state.test(123); | ||
expect(runCount).toEqual(1); | ||
@@ -33,29 +31,27 @@ }); | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new App({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(321); | ||
app.state.test(123); | ||
app.state.test(321); | ||
expect(runCount).toEqual(2); | ||
}); | ||
test.only('should flag as dirty when state changes', () => { | ||
test('should flag as dirty when state changes', () => { | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new App({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
actions: (action) => ({ | ||
@@ -65,5 +61,5 @@ changeFoo: action().mutation((_, state) => (state.foo = 'bar2')), | ||
}); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.actions.changeFoo(); | ||
expect(app.computed.test(123)).toEqual('bar2123'); | ||
expect(app.state.test(123)).toEqual('bar2123'); | ||
expect(runCount).toEqual(2); | ||
@@ -73,24 +69,20 @@ }); | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, { | ||
cacheLimit: 1, | ||
}), | ||
}; | ||
const app = new App({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, { | ||
cacheLimit: 1, | ||
}), | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(432); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.state.test(432); | ||
app.state.test(123); | ||
expect(runCount).toEqual(3); | ||
}); | ||
}); | ||
/* | ||
- Should expose to actions as provider | ||
*/ | ||
//# sourceMappingURL=computed.test.js.map |
@@ -6,7 +6,6 @@ import Devtools from './Devtools'; | ||
export { default as computed } from './computed'; | ||
declare type Configuration<State, Providers, Actions, Computed> = { | ||
declare type Configuration<State, Providers, Actions> = { | ||
state?: State; | ||
providers?: Providers; | ||
actions?: Actions; | ||
computed?: Computed; | ||
}; | ||
@@ -27,5 +26,3 @@ declare type Options = { | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} | ActionsCallback<Providers, State>, Computed extends { | ||
[key: string]: (...args: any[]) => (state: State) => any; | ||
}> { | ||
} | ActionsCallback<Providers, State>> { | ||
private proxyStateTree; | ||
@@ -39,4 +36,3 @@ devtools: Devtools; | ||
state: State; | ||
computed: Computed; | ||
constructor(configuration: Configuration<State, Providers, Actions, Computed>, options?: Options); | ||
constructor(configuration: Configuration<State, Providers, Actions>, options?: Options); | ||
private initializeDevtools; | ||
@@ -43,0 +39,0 @@ trackState(): number; |
@@ -5,3 +5,2 @@ import { ActionChain } from 'action-chain'; | ||
import Action from './Action'; | ||
import { Computed } from './computed'; | ||
export { default as compose } from './compose'; | ||
@@ -48,7 +47,2 @@ export { default as derived } from './derived'; | ||
}), {}); | ||
this.computed = Object.keys(configuration.computed || {}).reduce((aggr, key) => Object.assign(aggr, { | ||
[key]: configuration.computed[key] instanceof Computed | ||
? configuration.computed[key].evaluate(actionChain, proxyStateTree, key) | ||
: new Computed(configuration.computed[key]).evaluate(actionChain, proxyStateTree, key), | ||
}), {}); | ||
this.proxyStateTree = proxyStateTree; | ||
@@ -55,0 +49,0 @@ if (options.devtools && typeof window !== 'undefined') { |
@@ -5,7 +5,6 @@ import * as React from 'react'; | ||
export declare type IReactComponent<P = any> = React.StatelessComponent<P> | React.ComponentClass<P> | React.ClassicComponentClass<P>; | ||
export declare type TConnect<State = {}, Actions = {}, Computed = {}> = { | ||
export declare type TConnect<State = {}, Actions = {}> = { | ||
app: { | ||
state: State; | ||
actions: Actions; | ||
computed: Computed; | ||
}; | ||
@@ -15,8 +14,6 @@ }; | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} | ActionsCallback<Providers, State>, Computed extends { | ||
[key: string]: (...args: any[]) => (state: State) => any; | ||
}> extends App<State, Providers, Actions, Computed> { | ||
} | ActionsCallback<Providers, State>> extends App<State, Providers, Actions> { | ||
connect: <Props, ConnectedActions = Actions extends { | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} ? { [Namespace in keyof Actions]: ReturnType<Actions[Namespace]>; } : Actions extends ActionsCallback<Providers, State> ? ReturnType<Actions> : any>(Component: IReactComponent<Props & TConnect<State, ConnectedActions, Computed>>) => IReactComponent<Pick<Props & TConnect<State, ConnectedActions, Computed>, ({ [P in keyof (Props & TConnect<State, ConnectedActions, Computed>)]: P; } & { | ||
} ? { [Namespace in keyof Actions]: ReturnType<Actions[Namespace]>; } : Actions extends ActionsCallback<Providers, State> ? ReturnType<Actions> : any>(Component: IReactComponent<Props & TConnect<State, ConnectedActions>>) => IReactComponent<Pick<Props & TConnect<State, ConnectedActions>, ({ [P in keyof (Props & TConnect<State, ConnectedActions>)]: P; } & { | ||
app: never; | ||
@@ -23,0 +20,0 @@ } & { |
@@ -86,3 +86,2 @@ import * as React from 'react'; | ||
actions: instance.actions, | ||
computed: instance.computed, | ||
__componentInstanceId: this.__componentInstanceId, | ||
@@ -120,3 +119,2 @@ }, | ||
actions: instance.actions, | ||
computed: instance.computed, | ||
__componentInstanceId: this.__componentInstanceId, | ||
@@ -123,0 +121,0 @@ }, |
@@ -20,3 +20,3 @@ import ProxyStateTree from 'proxy-state-tree'; | ||
} | ||
export default function computed<Config, NewValue>(cb: (config: Config) => (state: object) => NewValue, options?: ComputedOptions): (config: Config) => (state: object) => NewValue; | ||
export default function computed<Config, NewValue>(cb: (config: Config) => (state: object) => NewValue, options?: ComputedOptions): (config: Config) => NewValue; | ||
export {}; |
@@ -10,2 +10,3 @@ "use strict"; | ||
this.cacheLimit = options.cacheLimit || this.cacheLimit; | ||
return this.evaluate.bind(this); | ||
} | ||
@@ -12,0 +13,0 @@ evaluate(actionChain, proxyStateTree, path) { |
@@ -6,27 +6,25 @@ "use strict"; | ||
test('should instantiate app with computed', () => { | ||
const state = { | ||
foo: 'bar', | ||
test: _1.computed((foo) => (state) => state.foo + foo), | ||
}; | ||
const app = new _1.default({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => state.foo + foo, | ||
}, | ||
state, | ||
}); | ||
expect(app.computed.test(123)).toEqual('bar123'); | ||
expect(app.state.test(123)).toEqual('bar123'); | ||
}); | ||
test('should not recalculate when not dirty', () => { | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: _1.computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new _1.default({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.state.test(123); | ||
expect(runCount).toEqual(1); | ||
@@ -36,29 +34,27 @@ }); | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: _1.computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new _1.default({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(321); | ||
app.state.test(123); | ||
app.state.test(321); | ||
expect(runCount).toEqual(2); | ||
}); | ||
test.only('should flag as dirty when state changes', () => { | ||
test('should flag as dirty when state changes', () => { | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: _1.computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}), | ||
}; | ||
const app = new _1.default({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: (foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, | ||
}, | ||
state, | ||
actions: (action) => ({ | ||
@@ -68,5 +64,5 @@ changeFoo: action().mutation((_, state) => (state.foo = 'bar2')), | ||
}); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.actions.changeFoo(); | ||
expect(app.computed.test(123)).toEqual('bar2123'); | ||
expect(app.state.test(123)).toEqual('bar2123'); | ||
expect(runCount).toEqual(2); | ||
@@ -76,24 +72,20 @@ }); | ||
let runCount = 0; | ||
const state = { | ||
foo: 'bar', | ||
test: _1.computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, { | ||
cacheLimit: 1, | ||
}), | ||
}; | ||
const app = new _1.default({ | ||
state: { | ||
foo: 'bar', | ||
}, | ||
computed: { | ||
test: _1.computed((foo) => (state) => { | ||
runCount++; | ||
return state.foo + foo; | ||
}, { | ||
cacheLimit: 1, | ||
}), | ||
}, | ||
state, | ||
}); | ||
app.computed.test(123); | ||
app.computed.test(432); | ||
app.computed.test(123); | ||
app.state.test(123); | ||
app.state.test(432); | ||
app.state.test(123); | ||
expect(runCount).toEqual(3); | ||
}); | ||
}); | ||
/* | ||
- Should expose to actions as provider | ||
*/ | ||
//# sourceMappingURL=computed.test.js.map |
@@ -6,7 +6,6 @@ import Devtools from './Devtools'; | ||
export { default as computed } from './computed'; | ||
declare type Configuration<State, Providers, Actions, Computed> = { | ||
declare type Configuration<State, Providers, Actions> = { | ||
state?: State; | ||
providers?: Providers; | ||
actions?: Actions; | ||
computed?: Computed; | ||
}; | ||
@@ -27,5 +26,3 @@ declare type Options = { | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} | ActionsCallback<Providers, State>, Computed extends { | ||
[key: string]: (...args: any[]) => (state: State) => any; | ||
}> { | ||
} | ActionsCallback<Providers, State>> { | ||
private proxyStateTree; | ||
@@ -39,4 +36,3 @@ devtools: Devtools; | ||
state: State; | ||
computed: Computed; | ||
constructor(configuration: Configuration<State, Providers, Actions, Computed>, options?: Options); | ||
constructor(configuration: Configuration<State, Providers, Actions>, options?: Options); | ||
private initializeDevtools; | ||
@@ -43,0 +39,0 @@ trackState(): number; |
@@ -7,3 +7,2 @@ "use strict"; | ||
const Action_1 = require("./Action"); | ||
const computed_1 = require("./computed"); | ||
var compose_1 = require("./compose"); | ||
@@ -13,4 +12,4 @@ exports.compose = compose_1.default; | ||
exports.derived = derived_1.default; | ||
var computed_2 = require("./computed"); | ||
exports.computed = computed_2.default; | ||
var computed_1 = require("./computed"); | ||
exports.computed = computed_1.default; | ||
class App { | ||
@@ -54,7 +53,2 @@ constructor(configuration, options = {}) { | ||
}), {}); | ||
this.computed = Object.keys(configuration.computed || {}).reduce((aggr, key) => Object.assign(aggr, { | ||
[key]: configuration.computed[key] instanceof computed_1.Computed | ||
? configuration.computed[key].evaluate(actionChain, proxyStateTree, key) | ||
: new computed_1.Computed(configuration.computed[key]).evaluate(actionChain, proxyStateTree, key), | ||
}), {}); | ||
this.proxyStateTree = proxyStateTree; | ||
@@ -61,0 +55,0 @@ if (options.devtools && typeof window !== 'undefined') { |
@@ -5,7 +5,6 @@ import * as React from 'react'; | ||
export declare type IReactComponent<P = any> = React.StatelessComponent<P> | React.ComponentClass<P> | React.ClassicComponentClass<P>; | ||
export declare type TConnect<State = {}, Actions = {}, Computed = {}> = { | ||
export declare type TConnect<State = {}, Actions = {}> = { | ||
app: { | ||
state: State; | ||
actions: Actions; | ||
computed: Computed; | ||
}; | ||
@@ -15,8 +14,6 @@ }; | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} | ActionsCallback<Providers, State>, Computed extends { | ||
[key: string]: (...args: any[]) => (state: State) => any; | ||
}> extends App<State, Providers, Actions, Computed> { | ||
} | ActionsCallback<Providers, State>> extends App<State, Providers, Actions> { | ||
connect: <Props, ConnectedActions = Actions extends { | ||
[namespace: string]: ActionsCallback<Providers, State>; | ||
} ? { [Namespace in keyof Actions]: ReturnType<Actions[Namespace]>; } : Actions extends ActionsCallback<Providers, State> ? ReturnType<Actions> : any>(Component: IReactComponent<Props & TConnect<State, ConnectedActions, Computed>>) => IReactComponent<Pick<Props & TConnect<State, ConnectedActions, Computed>, ({ [P in keyof (Props & TConnect<State, ConnectedActions, Computed>)]: P; } & { | ||
} ? { [Namespace in keyof Actions]: ReturnType<Actions[Namespace]>; } : Actions extends ActionsCallback<Providers, State> ? ReturnType<Actions> : any>(Component: IReactComponent<Props & TConnect<State, ConnectedActions>>) => IReactComponent<Pick<Props & TConnect<State, ConnectedActions>, ({ [P in keyof (Props & TConnect<State, ConnectedActions>)]: P; } & { | ||
app: never; | ||
@@ -23,0 +20,0 @@ } & { |
@@ -88,3 +88,2 @@ "use strict"; | ||
actions: instance.actions, | ||
computed: instance.computed, | ||
__componentInstanceId: this.__componentInstanceId, | ||
@@ -122,3 +121,2 @@ }, | ||
actions: instance.actions, | ||
computed: instance.computed, | ||
__componentInstanceId: this.__componentInstanceId, | ||
@@ -125,0 +123,0 @@ }, |
{ | ||
"name": "overmind", | ||
"version": "1.0.0-1532117391884", | ||
"version": "1.0.0-1532121829928", | ||
"description": "Functional actions", | ||
@@ -36,5 +36,5 @@ "author": "Christian Alfoni <christianalfoni@gmail.com>", | ||
"@types/node": "^10.5.1", | ||
"action-chain": "1.0.0-1532117391884", | ||
"action-chain": "1.0.0-1532121829927", | ||
"is-plain-object": "^2.0.4", | ||
"proxy-state-tree": "1.0.0-1532117391884", | ||
"proxy-state-tree": "1.0.0-1532121829928", | ||
"tslib": "^1.9.3" | ||
@@ -41,0 +41,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
230424
2137
+ Addedaction-chain@1.0.0-1532121829927(transitive)
+ Addedproxy-state-tree@1.0.0-1532121829928(transitive)
- Removedaction-chain@1.0.0-1532117391884(transitive)
- Removedproxy-state-tree@1.0.0-1532117391884(transitive)