Skip to content

Commit

Permalink
fire intro.start and outro.start events (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 8, 2017
1 parent 5eff188 commit e9f17f3
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 73 deletions.
1 change: 0 additions & 1 deletion mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--bail
test/test.js
26 changes: 12 additions & 14 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ 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.hasComponents || generator.hasIntroTransitions) &&
${(generator.hasComponents || generator.hasComplexBindings) &&
`this._flush();`}
${generator.hasComplexBindings &&
`while ( this._bindings.length ) this._bindings.pop()();`}
${generator.hasIntroTransitions && `@callAll(this._postcreate);`}
`;

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

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

block.builders.intro.addBlock(deindent`
#component._oncreate.push( function () {
${introName} = @wrapTransition( ${state.name}, ${fn}, ${snippet}, true, null );
#component._postcreate.push( function () {
${introName} = @wrapTransition( #component, ${state.name}, ${fn}, ${snippet}, true, null );
${introName}.run( true, function () {
#component.fire( 'intro.end', { node: ${state.name} });
});
Expand All @@ -78,7 +78,7 @@ export default function addTransitions(
// TODO hide elements that have outro'd (unless they belong to a still-outroing
// group) prior to their removal from the DOM
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 () {
#component.fire( 'outro.end', { node: ${state.name} });
if ( --#outros === 0 ) #outrocallback();
Expand Down
11 changes: 6 additions & 5 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ export function set(newState) {
this._root._flush();
}

export function _flush() {
if (!this._oncreate) return;
export function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
export function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

export var proto = {
Expand Down
8 changes: 5 additions & 3 deletions src/shared/transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function hash(str) {
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 duration = obj.duration || 300;
var ease = obj.easing || linear;
Expand Down Expand Up @@ -78,6 +78,8 @@ export function wrapTransition(node, fn, params, intro, outgroup) {
}
},
start: function(program) {
component.fire(program.intro ? 'intro.start' : 'outro.start', { node: node });

program.a = this.t;
program.b = program.intro ? 1 : 0;
program.delta = program.b - program.a;
Expand Down Expand Up @@ -149,7 +151,7 @@ export var transitionManager = {

if (!this.running) {
this.running = true;
this.next();
requestAnimationFrame(this.bound || (this.bound = this.next.bind(this)));
}
},

Expand Down Expand Up @@ -186,7 +188,7 @@ export var transitionManager = {
}

if (this.running) {
requestAnimationFrame(this.bound || (this.bound = this.next.bind(this)));
requestAnimationFrame(this.bound);
} else if (this.stylesheet) {
var i = this.stylesheet.cssRules.length;
while (i--) this.stylesheet.deleteRule(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/computed-collapsed-if/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/each-block-changed-check/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/event-handlers-custom/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/if-block-no-update/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/if-block-simple/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
11 changes: 6 additions & 5 deletions test/js/samples/non-imported-component/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
13 changes: 8 additions & 5 deletions test/js/samples/onrender-onteardown-rewritten/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ function set(newState) {
this._root._flush();
}

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down Expand Up @@ -155,6 +156,8 @@ function SvelteComponent ( options ) {
this._fragment.mount( options.target, null );
}

this._flush();

if ( options._root ) {
options._root._oncreate.push( template.oncreate.bind( this ) );
} else {
Expand Down
2 changes: 2 additions & 0 deletions test/js/samples/onrender-onteardown-rewritten/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function SvelteComponent ( options ) {
this._fragment.mount( options.target, null );
}

this._flush();

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

function _flush() {
if (!this._oncreate) return;
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}

while (this._oncreate.length) {
this._oncreate.pop()();
}
function _flush() {
callAll(this._oncreate);
callAll(this._bindings);
}

var proto = {
Expand Down
35 changes: 35 additions & 0 deletions test/runtime/samples/transition-js-events/_config.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
Loading

0 comments on commit e9f17f3

Please sign in to comment.