Skip to content

Commit 44287f8

Browse files
committed
recompute computed values with functions as dependencies (#413)
1 parent b9d3c23 commit 44287f8

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/generators/dom/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,23 @@ export default function dom ( parsed, source, options ) {
246246

247247
if ( computations.length ) {
248248
const builder = new CodeBuilder();
249+
const differs = generator.helper( 'differs' );
249250

250251
computations.forEach( ({ key, deps }) => {
251252
builder.addBlock( deindent`
252-
if ( isInitial || ${deps.map( dep => `( '${dep}' in newState && typeof state.${dep} === 'object' || state.${dep} !== oldState.${dep} )` ).join( ' || ' )} ) {
253+
if ( isInitial || ${deps.map( dep => `( '${dep}' in newState && ${differs}( state.${dep}, oldState.${dep} ) )` ).join( ' || ' )} ) {
253254
state.${key} = newState.${key} = ${generator.alias( 'template' )}.computed.${key}( ${deps.map( dep => `state.${dep}` ).join( ', ' )} );
254255
}
255256
` );
256257
});
257258

258259
builders.main.addBlock( deindent`
259-
function ${generator.alias( 'applyComputations' )} ( state, newState, oldState, isInitial ) {
260+
function ${generator.alias( 'recompute' )} ( state, newState, oldState, isInitial ) {
260261
${builder}
261262
}
262263
` );
263264

264-
builders._set.addLine( `${generator.alias( 'applyComputations' )}( this._state, newState, oldState, false )` );
265+
builders._set.addLine( `${generator.alias( 'recompute' )}( this._state, newState, oldState, false )` );
265266
}
266267

267268
// TODO is the `if` necessary?
@@ -349,7 +350,7 @@ export default function dom ( parsed, source, options ) {
349350

350351
if ( templateProperties.computed ) {
351352
constructorBlock.addLine(
352-
`${generator.alias( 'applyComputations' )}( this._state, this._state, {}, true );`
353+
`${generator.alias( 'recompute' )}( this._state, this._state, {}, true );`
353354
);
354355
}
355356

src/shared/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ export * from './methods.js';
33

44
export function noop () {}
55

6+
export function differs ( a, b ) {
7+
return ( a !== b ) || ( a && ( typeof a === 'object' ) || ( typeof a === 'function' ) );
8+
}
9+
610
export function dispatchObservers ( component, group, newState, oldState ) {
711
for ( var key in group ) {
812
if ( !( key in newState ) ) continue;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
html: '<p>2</p>',
3+
4+
test ( assert, component, target ) {
5+
component.set({ y: 2 });
6+
assert.equal( component.get( 'x' ), 4 );
7+
assert.equal( target.innerHTML, '<p>4</p>' );
8+
component.destroy();
9+
}
10+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p>{{x}}</p>
2+
3+
<script>
4+
let _x;
5+
6+
function getX () {
7+
return _x;
8+
}
9+
10+
export default {
11+
data () {
12+
return {
13+
y: 1
14+
};
15+
},
16+
17+
computed: {
18+
xGetter ( y ) {
19+
_x = y * 2;
20+
return getX;
21+
},
22+
23+
x ( xGetter ) {
24+
return xGetter();
25+
}
26+
}
27+
};
28+
</script>

0 commit comments

Comments
 (0)