Merge branch 'master' into gh-691

pull/698/head
Rich Harris 8 years ago committed by GitHub
commit 02ea3e7a44

@ -1,2 +1 @@
--bail
test/test.js test/test.js

@ -125,10 +125,9 @@ export default function dom(
@dispatchObservers( this, this._observers.pre, newState, oldState ); @dispatchObservers( this, this._observers.pre, newState, oldState );
${block.hasUpdateMethod && `this._fragment.update( newState, this._state );`} ${block.hasUpdateMethod && `this._fragment.update( newState, this._state );`}
@dispatchObservers( this, this._observers.post, newState, oldState ); @dispatchObservers( this, this._observers.post, newState, oldState );
${(generator.hasComponents || generator.hasIntroTransitions) && ${generator.hasComponents && `@callAll(this._oncreate);`}
`this._flush();`} ${generator.hasComplexBindings && `@callAll(this._bindings);`}
${generator.hasComplexBindings && ${generator.hasIntroTransitions && `@callAll(this._postcreate);`}
`while ( this._bindings.length ) this._bindings.pop()();`}
`; `;
if (hasJs) { if (hasJs) {
@ -167,7 +166,7 @@ export default function dom(
? `@proto ` ? `@proto `
: deindent` : deindent`
{ {
${['get', 'fire', 'observe', 'on', 'set', '_flush'] ${['get', 'fire', 'observe', 'on', 'set']
.map(n => `${n}: @${n}`) .map(n => `${n}: @${n}`)
.join(',\n')} .join(',\n')}
}`; }`;
@ -208,9 +207,9 @@ export default function dom(
${generator.stylesheet.hasStyles && ${generator.stylesheet.hasStyles &&
options.css !== false && options.css !== false &&
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`} `if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`}
${(generator.hasComponents || generator.hasIntroTransitions) && ${generator.hasComponents && `this._oncreate = [];`}
`this._oncreate = [];`}
${generator.hasComplexBindings && `this._bindings = [];`} ${generator.hasComplexBindings && `this._bindings = [];`}
${generator.hasIntroTransitions && `this._postcreate = [];`}
this._fragment = @create_main_fragment( this._state, this ); this._fragment = @create_main_fragment( this._state, this );
@ -228,19 +227,17 @@ export default function dom(
this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, null ); this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, null );
} }
${(generator.hasComponents || generator.hasIntroTransitions) && ${generator.hasComponents && `@callAll(this._oncreate);`}
`this._flush();`} ${generator.hasComplexBindings && `@callAll(this._bindings);`}
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
${templateProperties.oncreate && ${templateProperties.oncreate && deindent`
deindent`
if ( options._root ) { if ( options._root ) {
options._root._oncreate.push( @template.oncreate.bind( this ) ); options._root._oncreate.push( @template.oncreate.bind( this ) );
} else { } else {
@template.oncreate.call( this ); @template.oncreate.call( this );
} }`}
`}
${generator.hasIntroTransitions && `@callAll(this._postcreate);`}
} }
@assign( ${prototypeBase}, ${proto}); @assign( ${prototypeBase}, ${proto});

@ -163,7 +163,7 @@ function keyed(
block.addVariable(head); block.addVariable(head);
block.addVariable(last); block.addVariable(last);
if (node.children[0] && node.children[0].type === 'Element') { if (node.children[0] && node.children[0].type === 'Element' && !generator.components.has(node.children[0].name)) {
// TODO or text/tag/raw // TODO or text/tag/raw
node._block.first = node.children[0]._state.parentNode; // TODO this is highly confusing node._block.first = node.children[0]._state.parentNode; // TODO this is highly confusing
} else { } else {

@ -45,7 +45,7 @@ export default function visitEventHandler(
const declarations = usedContexts.map(name => { const declarations = usedContexts.map(name => {
if (name === 'state') { if (name === 'state') {
if (shouldHoist) state.usesComponent = true; if (shouldHoist) state.usesComponent = true;
return `var state = #component.get();`; return `var state = ${block.alias('component')}.get();`;
} }
const listName = block.listNames.get(name); const listName = block.listNames.get(name);

@ -23,8 +23,8 @@ export default function addTransitions(
const fn = `@template.transitions.${intro.name}`; const fn = `@template.transitions.${intro.name}`;
block.builders.intro.addBlock(deindent` block.builders.intro.addBlock(deindent`
#component._oncreate.push( function () { #component._postcreate.push( function () {
if ( !${name} ) ${name} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, true, null ); if ( !${name} ) ${name} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, true, null );
${name}.run( true, function () { ${name}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} }); #component.fire( 'intro.end', { node: ${state.name} });
}); });
@ -58,8 +58,8 @@ export default function addTransitions(
} }
block.builders.intro.addBlock(deindent` block.builders.intro.addBlock(deindent`
#component._oncreate.push( function () { #component._postcreate.push( function () {
${introName} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, true, null ); ${introName} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, true, null );
${introName}.run( true, function () { ${introName}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} }); #component.fire( 'intro.end', { node: ${state.name} });
}); });
@ -78,7 +78,7 @@ export default function addTransitions(
// TODO hide elements that have outro'd (unless they belong to a still-outroing // TODO hide elements that have outro'd (unless they belong to a still-outroing
// group) prior to their removal from the DOM // group) prior to their removal from the DOM
block.builders.outro.addBlock(deindent` block.builders.outro.addBlock(deindent`
${outroName} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, false, null ); ${outroName} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, false, null );
${outroName}.run( false, function () { ${outroName}.run( false, function () {
#component.fire( 'outro.end', { node: ${state.name} }); #component.fire( 'outro.end', { node: ${state.name} });
if ( --#outros === 0 ) #outrocallback(); if ( --#outros === 0 ) #outrocallback();

@ -106,15 +106,11 @@ export function onDev(eventName, handler) {
export function set(newState) { export function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
export function _flush() { export function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
export var proto = { export var proto = {
@ -122,8 +118,7 @@ export var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
export var protoDev = { export var protoDev = {
@ -131,6 +126,5 @@ export var protoDev = {
fire: fire, fire: fire,
observe: observeDev, observe: observeDev,
on: onDev, on: onDev,
set: set, set: set
_flush: _flush
}; };

@ -32,7 +32,7 @@ export function hash(str) {
return hash >>> 0; return hash >>> 0;
} }
export function wrapTransition(node, fn, params, intro, outgroup) { export function wrapTransition(component, node, fn, params, intro, outgroup) {
var obj = fn(node, params); var obj = fn(node, params);
var duration = obj.duration || 300; var duration = obj.duration || 300;
var ease = obj.easing || linear; var ease = obj.easing || linear;
@ -78,6 +78,8 @@ export function wrapTransition(node, fn, params, intro, outgroup) {
} }
}, },
start: function(program) { start: function(program) {
component.fire(program.intro ? 'intro.start' : 'outro.start', { node: node });
program.a = this.t; program.a = this.t;
program.b = program.intro ? 1 : 0; program.b = program.intro ? 1 : 0;
program.delta = program.b - program.a; program.delta = program.b - program.a;
@ -149,7 +151,7 @@ export var transitionManager = {
if (!this.running) { if (!this.running) {
this.running = true; this.running = true;
this.next(); requestAnimationFrame(this.bound || (this.bound = this.next.bind(this)));
} }
}, },
@ -186,7 +188,7 @@ export var transitionManager = {
} }
if (this.running) { if (this.running) {
requestAnimationFrame(this.bound || (this.bound = this.next.bind(this))); requestAnimationFrame(this.bound);
} else if (this.stylesheet) { } else if (this.stylesheet) {
var i = this.stylesheet.cssRules.length; var i = this.stylesheet.cssRules.length;
while (i--) this.stylesheet.deleteRule(i); while (i--) this.stylesheet.deleteRule(i);

@ -115,15 +115,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -131,8 +127,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
var template = (function () { var template = (function () {

@ -91,15 +91,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -107,8 +103,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
function recompute ( state, newState, oldState, isInitial ) { function recompute ( state, newState, oldState, isInitial ) {

@ -124,15 +124,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -140,8 +136,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
function create_main_fragment ( state, component ) { function create_main_fragment ( state, component ) {

@ -109,15 +109,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -125,8 +121,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
var template = (function () { var template = (function () {

@ -115,15 +115,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -131,8 +127,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
function create_main_fragment ( state, component ) { function create_main_fragment ( state, component ) {

@ -115,15 +115,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -131,8 +127,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
function create_main_fragment ( state, component ) { function create_main_fragment ( state, component ) {

@ -103,15 +103,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -119,8 +115,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
var template = (function () { var template = (function () {
@ -192,7 +187,7 @@ function SvelteComponent ( options ) {
this._fragment.mount( options.target, null ); this._fragment.mount( options.target, null );
} }
this._flush(); callAll(this._oncreate);
} }
assign( SvelteComponent.prototype, proto ); assign( SvelteComponent.prototype, proto );
@ -202,7 +197,7 @@ SvelteComponent.prototype._set = function _set ( newState ) {
this._state = assign( {}, oldState, newState ); this._state = assign( {}, oldState, newState );
dispatchObservers( this, this._observers.pre, newState, oldState ); dispatchObservers( this, this._observers.pre, newState, oldState );
dispatchObservers( this, this._observers.post, newState, oldState ); dispatchObservers( this, this._observers.post, newState, oldState );
this._flush(); callAll(this._oncreate);
}; };
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {

@ -1,6 +1,6 @@
import Imported from 'Imported.html'; import Imported from 'Imported.html';
import { assign, createText, detachNode, dispatchObservers, insertNode, proto } from "svelte/shared.js"; import { assign, callAll, createText, detachNode, dispatchObservers, insertNode, proto } from "svelte/shared.js";
var template = (function () { var template = (function () {
return { return {
@ -71,7 +71,7 @@ function SvelteComponent ( options ) {
this._fragment.mount( options.target, null ); this._fragment.mount( options.target, null );
} }
this._flush(); callAll(this._oncreate);
} }
assign( SvelteComponent.prototype, proto ); assign( SvelteComponent.prototype, proto );
@ -81,7 +81,7 @@ SvelteComponent.prototype._set = function _set ( newState ) {
this._state = assign( {}, oldState, newState ); this._state = assign( {}, oldState, newState );
dispatchObservers( this, this._observers.pre, newState, oldState ); dispatchObservers( this, this._observers.pre, newState, oldState );
dispatchObservers( this, this._observers.post, newState, oldState ); dispatchObservers( this, this._observers.post, newState, oldState );
this._flush(); callAll(this._oncreate);
}; };
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {

@ -91,15 +91,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -107,8 +103,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
var template = (function () { var template = (function () {

@ -115,15 +115,11 @@ function on(eventName, handler) {
function set(newState) { function set(newState) {
this._set(assign({}, newState)); this._set(assign({}, newState));
this._root._flush(); callAll(this._root._oncreate);
} }
function _flush() { function callAll(fns) {
if (!this._oncreate) return; while (fns && fns.length) fns.pop()();
while (this._oncreate.length) {
this._oncreate.pop()();
}
} }
var proto = { var proto = {
@ -131,8 +127,7 @@ var proto = {
fire: fire, fire: fire,
observe: observe, observe: observe,
on: on, on: on,
set: set, set: set
_flush: _flush
}; };
function create_main_fragment ( state, component ) { function create_main_fragment ( state, component ) {

@ -0,0 +1,24 @@
export default {
data: {
titles: [{ name: 'b' }, { name: 'c' }]
},
html: `
<p>b</p>
<p>c</p>
`,
test (assert, component, target) {
component.set({
titles: [{ name: 'a' }, { name: 'b' }, { name: 'c' }]
});
assert.htmlEqual(target.innerHTML, `
<p>a</p>
<p>b</p>
<p>c</p>
`);
component.destroy();
}
};

@ -0,0 +1,13 @@
{{#each titles as title @name}}
<Nested title="{{title.name}}"/>
{{/each}}
<script>
import Nested from './Nested.html';
export default {
components: {
Nested
}
};
</script>

@ -0,0 +1,18 @@
export default {
data: {
foo: [1],
a: 42
},
html: `
<button>click me</button>
`,
test (assert, component, target, window) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');
button.dispatchEvent(event);
assert.equal(component.snapshot, 42);
}
};

@ -0,0 +1,13 @@
{{#each foo as bar}}
<button on:click='baz(a)'>click me</button>
{{/each}}
<script>
export default {
methods: {
baz(a) {
this.snapshot = a;
}
}
};
</script>

@ -0,0 +1,35 @@
export default {
data: {
visible: true,
things: ['a', 'b', 'c', 'd']
},
test (assert, component, target, window, raf) {
raf.tick(50);
assert.deepEqual(component.intros.sort(), ['a', 'b', 'c', 'd']);
assert.equal(component.introCount, 4);
raf.tick(100);
assert.equal(component.introCount, 0);
component.set({ visible: false });
raf.tick(150);
assert.deepEqual(component.outros.sort(), ['a', 'b', 'c', 'd']);
assert.equal(component.outroCount, 4);
raf.tick(200);
assert.equal(component.outroCount, 0);
component.set({ visible: true });
component.on('intro.start', () => {
throw new Error(`intro.start should fire during set(), not after`);
});
raf.tick(250);
assert.deepEqual(component.intros.sort(), ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'd']);
assert.equal(component.introCount, 4);
component.destroy();
}
};

@ -0,0 +1,45 @@
{{#each things as thing}}
{{#if visible}}
<p transition:foo>{{thing}}</p>
{{/if}}
{{/each}}
<script>
export default {
transitions: {
foo: function ( node, params ) {
return {
duration: 100,
tick: t => {
node.foo = t;
}
};
}
},
oncreate() {
this.intros = [];
this.outros = [];
this.introCount = 0;
this.outroCount = 0;
this.on('intro.start', ({ node }) => {
this.intros.push(node.textContent);
this.introCount += 1;
});
this.on('intro.end', () => {
this.introCount -= 1;
});
this.on('outro.start', ({ node }) => {
this.outros.push(node.textContent);
this.outroCount += 1;
});
this.on('outro.end', () => {
this.outroCount -= 1;
});
}
};
</script>
Loading…
Cancel
Save