Skip to content

Commit

Permalink
Merge pull request #694 from sveltejs/oncreate-before-bindings
Browse files Browse the repository at this point in the history
Call `oncreate` functions before updating bindings
  • Loading branch information
Rich-Harris committed Jul 7, 2017
2 parents ef33466 + d89d70e commit 459ca95
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ export default function dom(
@dispatchObservers( this, this._observers.pre, newState, oldState );
${block.hasUpdateMethod && `this._fragment.update( newState, this._state );`}
@dispatchObservers( this, this._observers.post, newState, oldState );
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
${(generator.hasComponents || generator.hasIntroTransitions) &&
`this._flush();`}
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
`;

if (hasJs) {
Expand Down Expand Up @@ -200,7 +200,7 @@ export default function dom(
options.css !== false &&
`if ( !document.getElementById( '${generator.cssId}-style' ) ) @add_css();`}
${(generator.hasComponents || generator.hasIntroTransitions) &&
`this._renderHooks = [];`}
`this._oncreate = [];`}
${generator.hasComplexBindings && `this._bindings = [];`}
this._fragment = @create_main_fragment( this._state, this );
Expand All @@ -219,15 +219,15 @@ export default function dom(
this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, null );
}
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
${(generator.hasComponents || generator.hasIntroTransitions) &&
`this._flush();`}
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
${templateProperties.oncreate &&
deindent`
if ( options._root ) {
options._root._renderHooks.push( @template.oncreate.bind( this ) );
options._root._oncreate.push( @template.oncreate.bind( this ) );
} else {
@template.oncreate.call( this );
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/dom/visitors/Element/addTransitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function addTransitions(
const fn = `@template.transitions.${intro.name}`;

block.builders.intro.addBlock(deindent`
#component._renderHooks.push( function () {
#component._oncreate.push( function () {
if ( !${name} ) ${name} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, true, null );
${name}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} });
Expand Down Expand Up @@ -58,7 +58,7 @@ export default function addTransitions(
}

block.builders.intro.addBlock(deindent`
#component._renderHooks.push( function () {
#component._oncreate.push( function () {
${introName} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, true, null );
${introName}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} });
Expand Down
6 changes: 3 additions & 3 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export function set(newState) {
}

export function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/computed-collapsed-if/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/each-block-changed-check/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/event-handlers-custom/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/if-block-no-update/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/if-block-simple/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
9 changes: 5 additions & 4 deletions test/js/samples/non-imported-component/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down Expand Up @@ -183,14 +183,15 @@ function SvelteComponent ( options ) {
this._yield = options._yield;

this._torndown = false;
this._renderHooks = [];
this._oncreate = [];

this._fragment = create_main_fragment( this._state, this );

if ( options.target ) {
this._fragment.create();
this._fragment.mount( options.target, null );
}

this._flush();
}

Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/non-imported-component/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ function SvelteComponent ( options ) {
this._yield = options._yield;

this._torndown = false;
this._renderHooks = [];
this._oncreate = [];

this._fragment = create_main_fragment( this._state, this );

if ( options.target ) {
this._fragment.create();
this._fragment.mount( options.target, null );
}

this._flush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ function SvelteComponent ( options ) {
}

if ( options._root ) {
options._root._renderHooks.push( template.oncreate.bind( this ) );
options._root._oncreate.push( template.oncreate.bind( this ) );
} else {
template.oncreate.call( this );
}
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/onrender-onteardown-rewritten/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function SvelteComponent ( options ) {
}

if ( options._root ) {
options._root._renderHooks.push( template.oncreate.bind( this ) );
options._root._oncreate.push( template.oncreate.bind( this ) );
} else {
template.oncreate.call( this );
}
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/use-elements-as-anchors/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ function set(newState) {
}

function _flush() {
if (!this._renderHooks) return;
if (!this._oncreate) return;

while (this._renderHooks.length) {
this._renderHooks.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/runtime/samples/bindings-before-oncreate/One.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Two bind:foo/>

<script>
import Two from './Two.html';

export default {
components: {
Two
},

oncreate() {
this.snapshot = this.get('foo');
}
};
</script>
15 changes: 15 additions & 0 deletions test/runtime/samples/bindings-before-oncreate/Two.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
export default {
data() {
return {
bar: 1
};
},

computed: {
foo(bar) {
return bar * 2;
}
}
};
</script>
5 changes: 5 additions & 0 deletions test/runtime/samples/bindings-before-oncreate/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
test(assert, component) {
assert.equal(component.refs.one.snapshot, 2);
}
};
9 changes: 9 additions & 0 deletions test/runtime/samples/bindings-before-oncreate/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<One ref:one/>

<script>
import One from './One.html';

export default {
components: { One }
};
</script>
30 changes: 30 additions & 0 deletions test/runtime/samples/flush-before-bindings/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{#each things as thing}}
<Visibility bind:isVisible="visibilityMap[thing]">
<p>{{thing}} ({{visibilityMap[thing]}})</p>
</Visibility>
{{/each}}

<script>
import Visibility from './Visibility.html';
import counter from './counter.js';

export default {
data() {
return {
things: ['first thing', 'second thing'],
visibilityMap: {}
};
},

computed: {
visibleThings: (things, visibilityMap) => {
counter.count += 1;
return things.filter(text => visibilityMap[text]);
}
},

components: {
Visibility
}
};
</script>
11 changes: 11 additions & 0 deletions test/runtime/samples/flush-before-bindings/Visibility.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
{{yield}}
</div>

<script>
export default {
oncreate() {
this.set({ isVisible: true });
}
};
</script>
21 changes: 21 additions & 0 deletions test/runtime/samples/flush-before-bindings/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import counter from './counter.js';

export default {
'skip-ssr': true,

html: `
<div><p>first thing (true)</p></div>
<div><p>second thing (true)</p></div>
`,

test(assert, component) {
const visibleThings = component.get('visibleThings');
assert.deepEqual(visibleThings, ['first thing', 'second thing']);

const snapshots = component.snapshots;
assert.deepEqual(snapshots, [visibleThings]);

// TODO minimise the number of recomputations during oncreate
// assert.equal(counter.count, 1);
}
};
3 changes: 3 additions & 0 deletions test/runtime/samples/flush-before-bindings/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
count: 0
};
Loading

0 comments on commit 459ca95

Please sign in to comment.