pull/8356/head
Fred Martini 4 years ago
parent 301dbc3b8f
commit 14609492d1

@ -59,7 +59,7 @@ export default class Block {
destroy: Array<Node | Node[]>; destroy: Array<Node | Node[]>;
}; };
event_updaters: ({condition:Expression, snippet:Node, index:number})[] = []; event_updaters: ({condition:Expression, index:number})[] = [];
event_listeners: Node[] = []; event_listeners: Node[] = [];
maintain_context: boolean; maintain_context: boolean;
@ -484,12 +484,9 @@ export default class Block {
); );
if (this.event_updaters.length === 1) { if (this.event_updaters.length === 1) {
const {condition, snippet} = this.event_updaters[0]; const {condition} = this.event_updaters[0];
this.chunks.update.push(b` this.chunks.update.push(b`
if (${condition}) { if (${condition}) ${dispose}.p()`);
${dispose}.swap(${snippet})
}`
);
} }
this.chunks.destroy.push( this.chunks.destroy.push(
@ -505,12 +502,8 @@ export default class Block {
} }
`); `);
for (const {condition, snippet, index} of this.event_updaters) { for (const {condition, index} of this.event_updaters) {
this.chunks.update.push(b` this.chunks.update.push(b` if (${condition}) ${dispose}[${index}].p()`);
if (${condition}) {
${dispose}[${index}].swap(${snippet})
}`
);
} }
this.chunks.destroy.push( this.chunks.destroy.push(

@ -69,9 +69,9 @@ export default class EventHandlerWrapper {
const index = block.event_listeners.length; const index = block.event_listeners.length;
const condition = block.renderer.dirty(this.node.expression.dynamic_dependencies()); const condition = block.renderer.dirty(this.node.expression.dynamic_dependencies());
block.event_updaters.push({condition, snippet, index}); block.event_updaters.push({condition, index});
block.event_listeners.push( block.event_listeners.push(
x`@listen_swap(${snippet}, (h)=> ${listen}(${target}, "${this.node.name}", h, ${args}))` x`@listen_and_update( () => (${snippet}), (h) => ${listen}(${target}, "${this.node.name}", h, ${args}))`
); );
} else { } else {
block.event_listeners.push( block.event_listeners.push(

@ -273,16 +273,19 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO
return noop; return noop;
} }
export function listen_swap(handler: EventListenerOrEventListenerObject|null|undefined|false, factory: (handler:EventListenerOrEventListenerObject|null|undefined|false) => Function) { export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerObject|null|undefined|false, factory: (handler:EventListenerOrEventListenerObject|null|undefined|false) => Function) {
let disposeHandle: Function = factory(handler); let handler = get_handler();
let dispose_handle: Function = factory(handler);
const dispose = () => { const dispose = () => {
disposeHandle(); dispose_handle();
} }
dispose.swap = (new_handler:EventListenerOrEventListenerObject) => { // update :
dispose.p = () => {
const new_handler = get_handler();
if (new_handler !== handler) { if (new_handler !== handler) {
disposeHandle(); dispose_handle();
handler = new_handler; handler = new_handler;
disposeHandle = factory(handler); dispose_handle = factory(handler);
} }
} }
return dispose; return dispose;

Loading…
Cancel
Save