Skip to content

Commit

Permalink
Merge pull request #482 from sveltejs/gh-481
Browse files Browse the repository at this point in the history
allow helpers to depend on one another
  • Loading branch information
Rich-Harris committed Apr 14, 2017
2 parents dba9401 + 51ff9ec commit 563d300
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { removeNode, removeObjectKey } from '../utils/removeNode.js';
import getIntro from './shared/utils/getIntro.js';
import getOutro from './shared/utils/getOutro.js';
import processCss from './shared/processCss.js';
import annotateWithScopes from './annotateWithScopes.js';
import annotateWithScopes from '../utils/annotateWithScopes.js';

const test = typeof global !== 'undefined' && global.__svelte_test;

Expand Down
37 changes: 35 additions & 2 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import MagicString from 'magic-string';
import { parse } from 'acorn';
import annotateWithScopes from '../../utils/annotateWithScopes.js';
import isReference from '../../utils/isReference.js';
import { walk } from 'estree-walker';
import deindent from '../../utils/deindent.js';
import CodeBuilder from '../../utils/CodeBuilder.js';
import visit from './visit.js';
Expand Down Expand Up @@ -296,8 +301,36 @@ export default function dom ( parsed, source, options ) {
);
} else {
generator.uses.forEach( key => {
const fn = shared[ key ]; // eslint-disable-line import/namespace
builders.main.addBlock( fn.toString().replace( /^function [^(]*/, 'function ' + generator.alias( key ) ) );
const str = shared[ key ].toString(); // eslint-disable-line import/namespace
const code = new MagicString( str );
const fn = parse( str ).body[0];

let scope = annotateWithScopes( fn );

walk( fn, {
enter ( node, parent ) {
if ( node._scope ) scope = node._scope;

if ( node.type === 'Identifier' && isReference( node, parent ) && !scope.has( node.name ) ) {
if ( node.name in shared ) {
// this helper function depends on another one
generator.uses.add( node.name );

const alias = generator.alias( node.name );
if ( alias !== node.name ) code.overwrite( node.start, node.end, alias );
}
}
},

leave ( node ) {
if ( node._scope ) scope = scope.parent;
}
});

const alias = generator.alias( fn.id.name );
if ( alias !== fn.id.name ) code.overwrite( fn.id.start, fn.id.end, alias );

builders.main.addBlock( code.toString() );
});
}

Expand Down
24 changes: 12 additions & 12 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ export function dispatchObservers ( component, group, newState, oldState ) {
var newValue = newState[ key ];
var oldValue = oldState[ key ];

if ( newValue === oldValue && typeof newValue !== 'object' ) continue;

var callbacks = group[ key ];
if ( !callbacks ) continue;

for ( var i = 0; i < callbacks.length; i += 1 ) {
var callback = callbacks[i];
if ( callback.__calling ) continue;

callback.__calling = true;
callback.call( component, newValue, oldValue );
callback.__calling = false;
if ( differs( newValue, oldValue ) ) {
var callbacks = group[ key ];
if ( !callbacks ) continue;

for ( var i = 0; i < callbacks.length; i += 1 ) {
var callback = callbacks[i];
if ( callback.__calling ) continue;

callback.__calling = true;
callback.call( component, newValue, oldValue );
callback.__calling = false;
}
}
}
}
27 changes: 2 additions & 25 deletions src/shared/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,7 @@ export function observeDev ( key, callback, options ) {
throw new Error( message );
}

var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;

( group[ key ] || ( group[ key ] = [] ) ).push( callback );

if ( !options || options.init !== false ) {
callback.__calling = true;
callback.call( this, this._state[ key ] );
callback.__calling = false;
}

return {
cancel: function () {
var index = group[ key ].indexOf( callback );
if ( ~index ) group[ key ].splice( index, 1 );
}
};
return observe.call( this, key, callback, options );
}

export function on ( eventName, handler ) {
Expand All @@ -77,15 +62,7 @@ export function onDev ( eventName, handler ) {
return this.on( 'destroy', handler );
}

var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
handlers.push( handler );

return {
cancel: function () {
var index = handlers.indexOf( handler );
if ( ~index ) handlers.splice( index, 1 );
}
};
return on.call( this, eventName, handler );
}

export function set ( newState ) {
Expand Down
File renamed without changes.

0 comments on commit 563d300

Please sign in to comment.