@@ -142,6 +142,10 @@ export default class Block {
142
142
}
143
143
144
144
addVariable ( name : string , init ?: string ) {
145
+ if ( name [ 0 ] === '#' ) {
146
+ name = this . alias ( name . slice ( 1 ) ) ;
147
+ }
148
+
145
149
if ( this . variables . has ( name ) && this . variables . get ( name ) !== init ) {
146
150
throw new Error (
147
151
`Variable '${ name } ' already initialised with a different value`
@@ -166,18 +170,16 @@ export default class Block {
166
170
toString ( ) {
167
171
const { dev } = this . compiler . options ;
168
172
169
- let introing ;
170
- const hasIntros = ! this . builders . intro . isEmpty ( ) ;
171
- if ( hasIntros ) {
172
- introing = this . getUniqueName ( 'introing' ) ;
173
- this . addVariable ( introing ) ;
174
- }
173
+ if ( this . hasIntroMethod || this . hasOutroMethod ) {
174
+ this . addVariable ( '#current' ) ;
175
175
176
- let outroing ;
177
- const hasOutros = ! this . builders . outro . isEmpty ( ) ;
178
- if ( hasOutros ) {
179
- outroing = this . alias ( 'outroing' ) ;
180
- this . addVariable ( outroing ) ;
176
+ if ( ! this . builders . mount . isEmpty ( ) ) {
177
+ this . builders . mount . addLine ( `#current = true;` ) ;
178
+ }
179
+
180
+ if ( ! this . builders . outro . isEmpty ( ) ) {
181
+ this . builders . outro . addLine ( `#current = false;` ) ;
182
+ }
181
183
}
182
184
183
185
if ( this . autofocus ) {
@@ -275,46 +277,30 @@ export default class Block {
275
277
}
276
278
277
279
if ( this . hasIntroMethod || this . hasOutroMethod ) {
278
- if ( hasIntros ) {
280
+ if ( this . builders . mount . isEmpty ( ) ) {
281
+ properties . addBlock ( `i: @noop,` ) ;
282
+ } else {
279
283
properties . addBlock ( deindent `
280
284
${ dev ? 'i: function intro' : 'i' } (#target, anchor) {
281
- if (${ introing } ) return;
282
- ${ introing } = true;
283
- ${ hasOutros && `${ outroing } = false;` }
284
-
285
+ if (#current) return;
285
286
${ this . builders . intro }
286
-
287
287
this.m(#target, anchor);
288
288
},
289
289
` ) ;
290
- } else {
291
- if ( this . builders . mount . isEmpty ( ) ) {
292
- properties . addBlock ( `i: @noop,` ) ;
293
- } else {
294
- properties . addBlock ( deindent `
295
- ${ dev ? 'i: function intro' : 'i' } (#target, anchor) {
296
- this.m(#target, anchor);
297
- },
298
- ` ) ;
299
- }
300
290
}
301
291
302
- if ( hasOutros ) {
292
+ if ( this . builders . outro . isEmpty ( ) ) {
293
+ properties . addBlock ( `o: @run,` ) ;
294
+ } else {
303
295
properties . addBlock ( deindent `
304
296
${ dev ? 'o: function outro' : 'o' } (#outrocallback) {
305
- if (${ outroing } ) return;
306
- ${ outroing } = true;
307
- ${ hasIntros && `${ introing } = false;` }
297
+ if (!#current) return;
308
298
309
299
${ this . outros > 1 && `#outrocallback = @callAfter(#outrocallback, ${ this . outros } );` }
310
300
311
301
${ this . builders . outro }
312
302
},
313
303
` ) ;
314
- } else {
315
- properties . addBlock ( deindent `
316
- o: @run,
317
- ` ) ;
318
304
}
319
305
}
320
306
0 commit comments