Skip to content

Commit 731f840

Browse files
authored
Merge pull request #416 from sveltejs/gh-413
recompute computed values with functions as dependencies
2 parents ec5995b + 44287f8 commit 731f840

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
@@ -245,22 +245,23 @@ export default function dom ( parsed, source, options ) {
245245

246246
if ( computations.length ) {
247247
const builder = new CodeBuilder();
248+
const differs = generator.helper( 'differs' );
248249

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

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

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

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

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

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)