1
1
import Renderer from './Renderer' ;
2
2
import Wrapper from './wrappers/shared/Wrapper' ;
3
- import { b , x } from 'code-red' ;
3
+ import { b , x , p } from 'code-red' ;
4
4
import { Node , Identifier , ArrayPattern } from 'estree' ;
5
5
import { is_head } from './wrappers/shared/is_head' ;
6
6
@@ -47,6 +47,7 @@ export default class Block {
47
47
claim : Array < Node | Node [ ] > ;
48
48
hydrate : Array < Node | Node [ ] > ;
49
49
mount : Array < Node | Node [ ] > ;
50
+ bubble : Array < Node | Node [ ] > ;
50
51
measure : Array < Node | Node [ ] > ;
51
52
fix : Array < Node | Node [ ] > ;
52
53
animate : Array < Node | Node [ ] > ;
@@ -95,6 +96,7 @@ export default class Block {
95
96
claim : [ ] ,
96
97
hydrate : [ ] ,
97
98
mount : [ ] ,
99
+ bubble : [ ] ,
98
100
measure : [ ] ,
99
101
fix : [ ] ,
100
102
animate : [ ] ,
@@ -287,12 +289,27 @@ export default class Block {
287
289
}` ;
288
290
}
289
291
292
+ if ( this . chunks . bubble . length > 0 ) {
293
+ const bubble_fns : Identifier = {
294
+ type : 'Identifier' ,
295
+ name : '#bubble_fns'
296
+ } ;
297
+ this . add_variable ( bubble_fns , x `[]` ) ;
298
+
299
+ properties . bubble = x `function #bubble(type, callback) {
300
+ const local_dispose = [];
301
+ const fn = () => {
302
+ ${ this . chunks . bubble }
303
+ #dispose.push(...local_dispose);
304
+ }
305
+ if (#mounted) fn()
306
+ else ${ bubble_fns } .push(fn);
307
+ return () => @run_all(local_dispose);
308
+ }` ;
309
+ }
310
+
290
311
if ( this . chunks . mount . length === 0 ) {
291
312
properties . mount = noop ;
292
- } else if ( this . event_listeners . length === 0 ) {
293
- properties . mount = x `function #mount(#target, #anchor) {
294
- ${ this . chunks . mount }
295
- }` ;
296
313
} else {
297
314
properties . mount = x `function #mount(#target, #anchor) {
298
315
${ this . chunks . mount }
@@ -382,6 +399,10 @@ export default class Block {
382
399
d: ${ properties . destroy }
383
400
}` ;
384
401
402
+ if ( properties . bubble ) {
403
+ return_value . properties . push ( p `b: ${ properties . bubble } ` ) ;
404
+ }
405
+
385
406
const block = dev && this . get_unique_name ( 'block' ) ;
386
407
387
408
const body = b `
@@ -423,6 +444,7 @@ export default class Block {
423
444
this . chunks . hydrate . length > 0 ||
424
445
this . chunks . claim . length > 0 ||
425
446
this . chunks . mount . length > 0 ||
447
+ this . chunks . bubble . length > 0 ||
426
448
this . chunks . update . length > 0 ||
427
449
this . chunks . destroy . length > 0 ||
428
450
this . has_animation ;
@@ -446,7 +468,7 @@ export default class Block {
446
468
}
447
469
448
470
render_listeners ( chunk : string = '' ) {
449
- if ( this . event_listeners . length > 0 ) {
471
+ if ( this . event_listeners . length > 0 || this . chunks . bubble . length > 0 ) {
450
472
this . add_variable ( { type : 'Identifier' , name : '#mounted' } ) ;
451
473
this . chunks . destroy . push ( b `#mounted = false` ) ;
452
474
@@ -457,7 +479,7 @@ export default class Block {
457
479
458
480
this . add_variable ( dispose ) ;
459
481
460
- if ( this . event_listeners . length === 1 ) {
482
+ if ( this . event_listeners . length === 1 && this . chunks . bubble . length === 0 ) {
461
483
this . chunks . mount . push (
462
484
b `
463
485
if (!#mounted) {
@@ -476,6 +498,7 @@ export default class Block {
476
498
${ dispose } = [
477
499
${ this . event_listeners }
478
500
];
501
+ ${ this . chunks . bubble . length > 0 && b `@run_all(#bubble_fns)` }
479
502
#mounted = true;
480
503
}
481
504
` ) ;
0 commit comments