pull/8356/head
adiguba 4 years ago
parent ae1b3b4aeb
commit 6ce3d2fe94

@ -35,7 +35,7 @@ function is_valid_any_alias_name(alias: string) {
if (alias === '*') { if (alias === '*') {
return true; return true;
} }
let idx = alias.indexOf('*'); const idx = alias.indexOf('*');
if (idx < 0) return false; if (idx < 0) return false;
if (idx !== alias.lastIndexOf('*')) { if (idx !== alias.lastIndexOf('*')) {
return false; return false;

@ -98,7 +98,7 @@ export default class InlineComponent extends Node {
this.scope = scope; this.scope = scope;
} }
this.handlers.forEach(h => h.validate()) this.handlers.forEach(h => h.validate());
const children = []; const children = [];
for (let i = info.children.length - 1; i >= 0; i--) { for (let i = info.children.length - 1; i >= 0; i--) {

@ -1,10 +1,9 @@
import Renderer, { BindingGroup } from './Renderer'; import Renderer, { BindingGroup } from './Renderer';
import Wrapper from './wrappers/shared/Wrapper'; import Wrapper from './wrappers/shared/Wrapper';
import { b, x } from 'code-red'; import { b, x } from 'code-red';
import { Node, Identifier, ArrayPattern } from 'estree'; import { Node, Identifier, ArrayPattern, Expression } from 'estree';
import { is_head } from './wrappers/shared/is_head'; import { is_head } from './wrappers/shared/is_head';
import { regex_double_quotes } from '../../utils/patterns'; import { regex_double_quotes } from '../../utils/patterns';
import { Expression } from 'estree';
export interface Bindings { export interface Bindings {
object: Identifier; object: Identifier;
@ -60,7 +59,7 @@ export default class Block {
destroy: Array<Node | Node[]>; destroy: Array<Node | Node[]>;
}; };
event_updaters: ({condition:Expression, index:number})[] = []; event_updaters: Array<{condition:Expression, index:number}> = [];
event_listeners: Node[] = []; event_listeners: Node[] = [];
maintain_context: boolean; maintain_context: boolean;

@ -37,7 +37,7 @@ export default class EventHandlerWrapper {
const snippet = this.get_snippet(block); const snippet = this.get_snippet(block);
let wrappers = []; const wrappers = [];
if (this.node.modifiers.has('trusted')) wrappers.push(x`@trusted`); if (this.node.modifiers.has('trusted')) wrappers.push(x`@trusted`);
if (this.node.modifiers.has('self')) wrappers.push(x`@self`); if (this.node.modifiers.has('self')) wrappers.push(x`@self`);
if (this.node.modifiers.has('stopImmediatePropagation')) wrappers.push(x`@stop_immediate_propagation`); if (this.node.modifiers.has('stopImmediatePropagation')) wrappers.push(x`@stop_immediate_propagation`);

@ -74,7 +74,7 @@ export function listen_dev(node: Node, event: string, handler: EventListenerOrEv
}; };
} }
export function bubble_dev(component: SvelteComponent, listen_func: Function, node: EventTarget|SvelteComponent, type: string, typeName: string = type): Function { export function bubble_dev(component: SvelteComponent, listen_func: Function, node: EventTarget | SvelteComponent, type: string, typeName: string = type): Function {
dispatch_dev('SvelteComponentAddEventBubble', { component, listen_func, node, type, typeName }); dispatch_dev('SvelteComponentAddEventBubble', { component, listen_func, node, type, typeName });
const dispose = bubble(component, listen_func, node, type, typeName); const dispose = bubble(component, listen_func, node, type, typeName);
return () => { return () => {

@ -264,7 +264,7 @@ export function wrap_handler(handler: EventListenerOrEventListenerObject, wrappe
return result; return result;
} }
export function listen(node: EventTarget, event: string, handler: EventListenerOrEventListenerObject|null|undefined|false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { export function listen(node: EventTarget, event: string, handler: EventListenerOrEventListenerObject | null | undefined | false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) {
if (handler) { if (handler) {
const h = wrap_handler(handler, wrappers); const h = wrap_handler(handler, wrappers);
node.addEventListener(event, h, options); node.addEventListener(event, h, options);
@ -273,12 +273,10 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO
return noop; return noop;
} }
export function listen_and_update(get_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 handler = get_handler(); let handler = get_handler();
let dispose_handle: Function = factory(handler); let dispose_handle: Function = factory(handler);
const dispose = () => { const dispose = () => dispose_handle();
dispose_handle();
}
// update : // update :
dispose.p = () => { dispose.p = () => {
const new_handler = get_handler(); const new_handler = get_handler();
@ -287,7 +285,7 @@ export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerO
handler = new_handler; handler = new_handler;
dispose_handle = factory(handler); dispose_handle = factory(handler);
} }
} };
return dispose; return dispose;
} }

@ -155,7 +155,7 @@ function start_bubble(type: string, bubble: Bubble, callback: Callback) {
function start_bubbles(comp : SvelteComponent, bubble: Bubble) { function start_bubbles(comp : SvelteComponent, bubble: Bubble) {
for (const type of Object.keys(comp.$$.callbacks)) { for (const type of Object.keys(comp.$$.callbacks)) {
comp.$$.callbacks[type].forEach( callback => { start_bubble(type, bubble, callback); }) comp.$$.callbacks[type].forEach( callback => start_bubble(type, bubble, callback));
} }
} }
@ -170,7 +170,7 @@ export function restart_all_callback(comp : SvelteComponent) {
function start_callback(comp : SvelteComponent, type: string, callback: Callback) { function start_callback(comp : SvelteComponent, type: string, callback: Callback) {
for (const bubbles of [ comp.$$.bubbles[type], comp.$$.bubbles['*'] ]) { for (const bubbles of [ comp.$$.bubbles[type], comp.$$.bubbles['*'] ]) {
if (bubbles) { if (bubbles) {
for(const bubble of bubbles) { for (const bubble of bubbles) {
start_bubble(type, bubble, callback); start_bubble(type, bubble, callback);
} }
} }
@ -186,8 +186,8 @@ export function add_callback(comp : SvelteComponent, type: string, f: EventListe
const callback: Callback = {f, o}; const callback: Callback = {f, o};
if (o && (o as any)?.once === true) { if (o && (o as any)?.once === true) {
callback.f = function () { callback.f = function(this: any, ...args) {
const r = f.apply(this, arguments); const r = f.call(this, ...args);
remove_callback(comp, type, callback); remove_callback(comp, type, callback);
return r; return r;
}; };
@ -236,15 +236,20 @@ function add_bubble(comp: SvelteComponent, type: string, f: CallbackFactory): Fu
for (const dispose of bubble.r.values()) { for (const dispose of bubble.r.values()) {
dispose(); dispose();
} }
} };
} }
/**
* Schedules a callback to run when an handler is added to the component
*
* TODO : Docs
*/
export function onEventListener(type: string, fn: CallbackFactory) { export function onEventListener(type: string, fn: CallbackFactory) {
add_bubble(get_current_component(), type, fn); add_bubble(get_current_component(), type, fn);
} }
export function bubble(component: SvelteComponent, listen_func: Function, node: EventTarget|SvelteComponent, type: string, typeName: string = type): Function { export function bubble(component: SvelteComponent, listen_func: Function, node: EventTarget | SvelteComponent, type: string, typeName: string = type): Function {
if (type === '*') { if (type === '*') {
return add_bubble(component, type, (callback, options, eventType) => { return add_bubble(component, type, (callback, options, eventType) => {
let typeToListen: string = null; let typeToListen: string = null;
@ -253,12 +258,12 @@ export function bubble(component: SvelteComponent, listen_func: Function, node:
} else if (typeName.startsWith('*')) { } else if (typeName.startsWith('*')) {
const len = typeName.length; const len = typeName.length;
if (eventType.endsWith(typeName.substring(1))) { if (eventType.endsWith(typeName.substring(1))) {
typeToListen = eventType.substring(0, eventType.length - (len-1)); typeToListen = eventType.substring(0, eventType.length - (len - 1));
} }
} else if (typeName.endsWith('*')) { } else if (typeName.endsWith('*')) {
const len = typeName.length; const len = typeName.length;
if (eventType.startsWith(typeName.substring(0,len-1))) { if (eventType.startsWith(typeName.substring(0,len - 1))) {
typeToListen = eventType.substring(len-1); typeToListen = eventType.substring(len - 1);
} }
} }
if (typeToListen) { if (typeToListen) {
@ -271,7 +276,7 @@ export function bubble(component: SvelteComponent, listen_func: Function, node:
}); });
} }
export function listen_comp(comp: SvelteComponent, event: string, handler: EventListenerOrEventListenerObject|null|undefined|false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) { export function listen_comp(comp: SvelteComponent, event: string, handler: EventListenerOrEventListenerObject | null | undefined | false, options?: boolean | AddEventListenerOptions | EventListenerOptions, wrappers?: Function[]) {
if (handler) { if (handler) {
return comp.$on(event, wrap_handler(handler, wrappers), options); return comp.$on(event, wrap_handler(handler, wrappers), options);
} }

@ -25,7 +25,7 @@ export interface Callback {
o?: boolean | AddEventListenerOptions | EventListenerOptions; o?: boolean | AddEventListenerOptions | EventListenerOptions;
} }
export type CallbackFactory = (callback: EventListener, options: boolean | AddEventListenerOptions | EventListenerOptions | undefined, type: string) => Function|void; export type CallbackFactory = (callback: EventListener, options: boolean | AddEventListenerOptions | EventListenerOptions | undefined, type: string) => Function | undefined;
export interface Bubble { export interface Bubble {
f: CallbackFactory; f: CallbackFactory;

Loading…
Cancel
Save