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 === '*') {
return true;
}
let idx = alias.indexOf('*');
const idx = alias.indexOf('*');
if (idx < 0) return false;
if (idx !== alias.lastIndexOf('*')) {
return false;

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

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

@ -37,7 +37,7 @@ export default class EventHandlerWrapper {
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('self')) wrappers.push(x`@self`);
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 });
const dispose = bubble(component, listen_func, node, type, typeName);
return () => {

@ -264,7 +264,7 @@ export function wrap_handler(handler: EventListenerOrEventListenerObject, wrappe
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) {
const h = wrap_handler(handler, wrappers);
node.addEventListener(event, h, options);
@ -273,12 +273,10 @@ export function listen(node: EventTarget, event: string, handler: EventListenerO
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 dispose_handle: Function = factory(handler);
const dispose = () => {
dispose_handle();
}
const dispose = () => dispose_handle();
// update :
dispose.p = () => {
const new_handler = get_handler();
@ -287,7 +285,7 @@ export function listen_and_update(get_handler: ()=>EventListenerOrEventListenerO
handler = new_handler;
dispose_handle = factory(handler);
}
}
};
return dispose;
}

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

@ -25,7 +25,7 @@ export interface Callback {
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 {
f: CallbackFactory;

Loading…
Cancel
Save