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
@@ -52,6 +52,7 @@ export default class Block {
52
52
claim : Array < Node | Node [ ] > ;
53
53
hydrate : Array < Node | Node [ ] > ;
54
54
mount : Array < Node | Node [ ] > ;
55
+ bubble : Array < Node | Node [ ] > ;
55
56
measure : Array < Node | Node [ ] > ;
56
57
fix : Array < Node | Node [ ] > ;
57
58
animate : Array < Node | Node [ ] > ;
@@ -100,6 +101,7 @@ export default class Block {
100
101
claim : [ ] ,
101
102
hydrate : [ ] ,
102
103
mount : [ ] ,
104
+ bubble : [ ] ,
103
105
measure : [ ] ,
104
106
fix : [ ] ,
105
107
animate : [ ] ,
@@ -292,12 +294,27 @@ export default class Block {
292
294
}` ;
293
295
}
294
296
297
+ if ( this . chunks . bubble . length > 0 ) {
298
+ const bubble_fns : Identifier = {
299
+ type : 'Identifier' ,
300
+ name : '#bubble_fns'
301
+ } ;
302
+ this . add_variable ( bubble_fns , x `[]` ) ;
303
+
304
+ properties . bubble = x `function #bubble(type, callback) {
305
+ const local_dispose = [];
306
+ const fn = () => {
307
+ ${ this . chunks . bubble }
308
+ #dispose.push(...local_dispose);
309
+ }
310
+ if (#mounted) fn()
311
+ else ${ bubble_fns } .push(fn);
312
+ return () => @run_all(local_dispose);
313
+ }` ;
314
+ }
315
+
295
316
if ( this . chunks . mount . length === 0 ) {
296
317
properties . mount = noop ;
297
- } else if ( this . event_listeners . length === 0 ) {
298
- properties . mount = x `function #mount(#target, #anchor) {
299
- ${ this . chunks . mount }
300
- }` ;
301
318
} else {
302
319
properties . mount = x `function #mount(#target, #anchor) {
303
320
${ this . chunks . mount }
@@ -387,6 +404,10 @@ export default class Block {
387
404
d: ${ properties . destroy }
388
405
}` ;
389
406
407
+ if ( properties . bubble ) {
408
+ return_value . properties . push ( p `b: ${ properties . bubble } ` ) ;
409
+ }
410
+
390
411
const block = dev && this . get_unique_name ( 'block' ) ;
391
412
392
413
const body = b `
@@ -428,6 +449,7 @@ export default class Block {
428
449
this . chunks . hydrate . length > 0 ||
429
450
this . chunks . claim . length > 0 ||
430
451
this . chunks . mount . length > 0 ||
452
+ this . chunks . bubble . length > 0 ||
431
453
this . chunks . update . length > 0 ||
432
454
this . chunks . destroy . length > 0 ||
433
455
this . has_animation ;
@@ -451,7 +473,7 @@ export default class Block {
451
473
}
452
474
453
475
render_listeners ( chunk : string = '' ) {
454
- if ( this . event_listeners . length > 0 ) {
476
+ if ( this . event_listeners . length > 0 || this . chunks . bubble . length > 0 ) {
455
477
this . add_variable ( { type : 'Identifier' , name : '#mounted' } ) ;
456
478
this . chunks . destroy . push ( b `#mounted = false` ) ;
457
479
@@ -462,7 +484,7 @@ export default class Block {
462
484
463
485
this . add_variable ( dispose ) ;
464
486
465
- if ( this . event_listeners . length === 1 ) {
487
+ if ( this . event_listeners . length === 1 && this . chunks . bubble . length === 0 ) {
466
488
this . chunks . mount . push (
467
489
b `
468
490
if (!#mounted) {
@@ -481,6 +503,7 @@ export default class Block {
481
503
${ dispose } = [
482
504
${ this . event_listeners }
483
505
];
506
+ ${ this . chunks . bubble . length > 0 && b `@run_all(#bubble_fns)` }
484
507
#mounted = true;
485
508
}
486
509
` ) ;
0 commit comments