Skip to content

Commit 2616dc0

Browse files
committed
Remove closures from SvelteComponent
1 parent 53ad868 commit 2616dc0

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

compiler/generate/index.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ export default function generate ( parsed, source, options, names ) {
286286
const topLevelStatements = [];
287287

288288
const setStatements = [ deindent`
289-
var oldState = state;
290-
state = Object.assign( {}, oldState, newState );
289+
var oldState = this.state;
290+
this.state = Object.assign( {}, oldState, newState );
291291
` ];
292292

293293
if ( templateProperties.computed ) {
@@ -328,13 +328,13 @@ export default function generate ( parsed, source, options, names ) {
328328
}
329329
` );
330330

331-
setStatements.push( `applyComputations( state, newState, oldState )` );
331+
setStatements.push( `applyComputations( this.state, newState, oldState )` );
332332
}
333333

334334
setStatements.push( deindent`
335-
dispatchObservers( observers.immediate, newState, oldState );
336-
if ( mainFragment ) mainFragment.update( newState, state );
337-
dispatchObservers( observers.deferred, newState, oldState );
335+
this.dispatchObservers( this.observers.immediate, newState, oldState );
336+
if ( this.mainFragment ) this.mainFragment.update( newState, this.state );
337+
this.dispatchObservers( this.observers.deferred, newState, oldState );
338338
` );
339339

340340
const importBlock = imports
@@ -394,15 +394,15 @@ export default function generate ( parsed, source, options, names ) {
394394
if ( generator.hasComplexBindings ) {
395395
initStatements.push( deindent`
396396
this.__bindings = [];
397-
var mainFragment = renderMainFragment( state, this );
397+
this.mainFragment = renderMainFragment( this.state, this );
398398
if ( options.target ) this.mount( options.target );
399399
while ( this.__bindings.length ) this.__bindings.pop()();
400400
` );
401401

402402
setStatements.push( `while ( this.__bindings.length ) this.__bindings.pop()();` );
403403
} else {
404404
initStatements.push( deindent`
405-
var mainFragment = renderMainFragment( state, this );
405+
this.mainFragment = renderMainFragment( this.state, this );
406406
if ( options.target ) this.mount( options.target );
407407
` );
408408
}
@@ -433,17 +433,25 @@ export default function generate ( parsed, source, options, names ) {
433433

434434
topLevelStatements.push( deindent`
435435
function ${constructorName} ( options ) {
436-
var component = this;${generator.usesRefs ? `\nthis.refs = {}` : ``}
437-
var state = ${initialState};${templateProperties.computed ? `\napplyComputations( state, state, {} );` : ``}
436+
${generator.usesRefs ? `\nthis.refs = {}` : ``}
437+
this.state = ${initialState};${templateProperties.computed ? `\napplyComputations( this.state, this.state, {} );` : ``}
438438
439-
var observers = {
439+
this.observers = {
440440
immediate: Object.create( null ),
441441
deferred: Object.create( null )
442442
};
443443
444-
var callbacks = Object.create( null );
444+
this.callbacks = Object.create( null );
445445
446-
function dispatchObservers ( group, newState, oldState ) {
446+
this.root = options.root;
447+
this.yield = options.yield;
448+
449+
${initStatements.join( '\n\n' )}
450+
}
451+
452+
${constructorName}.prototype = {
453+
454+
dispatchObservers: function dispatchObservers ( group, newState, oldState ) {
447455
for ( var key in group ) {
448456
if ( !( key in newState ) ) continue;
449457
@@ -460,41 +468,41 @@ export default function generate ( parsed, source, options, names ) {
460468
if ( callback.__calling ) continue;
461469
462470
callback.__calling = true;
463-
callback.call( component, newValue, oldValue );
471+
callback.call( this, newValue, oldValue );
464472
callback.__calling = false;
465473
}
466474
}
467-
}
475+
},
468476
469-
this.fire = function fire ( eventName, data ) {
470-
var handlers = eventName in callbacks && callbacks[ eventName ].slice();
477+
fire: function fire ( eventName, data ) {
478+
var handlers = eventName in this.callbacks && this.callbacks[ eventName ].slice();
471479
if ( !handlers ) return;
472480
473481
for ( var i = 0; i < handlers.length; i += 1 ) {
474482
handlers[i].call( this, data );
475483
}
476-
};
484+
},
477485
478-
this.get = function get ( key ) {
479-
return key ? state[ key ] : state;
480-
};
486+
get: function get ( key ) {
487+
return key ? this.state[ key ] : this.state;
488+
},
481489
482-
this.set = function set ( newState ) {
490+
set: function set ( newState ) {
483491
${setStatements.join( '\n\n' )}
484-
};
492+
},
485493
486-
this.mount = function mount ( target, anchor ) {
487-
mainFragment.mount( target, anchor );
488-
}
494+
mount: function mount ( target, anchor ) {
495+
this.mainFragment.mount( target, anchor );
496+
},
489497
490-
this.observe = function ( key, callback, options ) {
491-
var group = ( options && options.defer ) ? observers.deferred : observers.immediate;
498+
observe: function observe ( key, callback, options ) {
499+
var group = ( options && options.defer ) ? this.observers.deferred : this.observers.immediate;
492500
493501
( group[ key ] || ( group[ key ] = [] ) ).push( callback );
494502
495503
if ( !options || options.init !== false ) {
496504
callback.__calling = true;
497-
callback.call( component, state[ key ] );
505+
callback.call( this, this.state[ key ] );
498506
callback.__calling = false;
499507
}
500508
@@ -504,10 +512,10 @@ export default function generate ( parsed, source, options, names ) {
504512
if ( ~index ) group[ key ].splice( index, 1 );
505513
}
506514
};
507-
};
515+
},
508516
509-
this.on = function on ( eventName, handler ) {
510-
var handlers = callbacks[ eventName ] || ( callbacks[ eventName ] = [] );
517+
on: function on ( eventName, handler ) {
518+
var handlers = this.callbacks[ eventName ] || ( this.callbacks[ eventName ] = [] );
511519
handlers.push( handler );
512520
513521
return {
@@ -516,26 +524,22 @@ export default function generate ( parsed, source, options, names ) {
516524
if ( ~index ) handlers.splice( index, 1 );
517525
}
518526
};
519-
};
527+
},
520528
521-
this.teardown = function teardown ( detach ) {
529+
teardown: function teardown ( detach ) {
522530
this.fire( 'teardown' );${templateProperties.onteardown ? `\ntemplate.onteardown.call( this );` : ``}
523531
524-
mainFragment.teardown( detach !== false );
525-
mainFragment = null;
532+
this.mainFragment.teardown( detach !== false );
533+
this.mainFragment = null;
526534
527-
state = {};
528-
};
535+
this.state = {};
536+
},
529537
530-
this.root = options.root;
531-
this.yield = options.yield;
532-
533-
${initStatements.join( '\n\n' )}
534538
}
535539
` );
536540

537541
if ( templateProperties.methods ) {
538-
topLevelStatements.push( `${constructorName}.prototype = template.methods;` );
542+
topLevelStatements.push( `Object.assign( ${constructorName}.prototype, template.methods );` );
539543
}
540544

541545
const result = topLevelStatements.join( '\n\n' );

0 commit comments

Comments
 (0)