@splidejs/utils
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -254,35 +254,30 @@ "use strict"; | ||
} | ||
function arrayRemove(array, predicate) { | ||
const removed = []; | ||
for (let i = array.length - 1; i >= 0; i--) { | ||
if (predicate(array[i])) { | ||
removed.push(...array.splice(i, 1)); | ||
} | ||
function assert(assertion) { | ||
if (!assertion) { | ||
throw new Error(); | ||
} | ||
return removed; | ||
} | ||
function EventBinder(removersRef) { | ||
const removers = removersRef || []; | ||
const removers = removersRef || /* @__PURE__ */ new Set(); | ||
const key = removersRef ? {} : void 0; | ||
let destroyed; | ||
function bind(target, events, callback, options) { | ||
assert(!destroyed); | ||
forEachEvent(events, (event) => { | ||
target.addEventListener(event, callback, options); | ||
removers.push([target.removeEventListener.bind(target, event, callback, options), key]); | ||
removers.add([target.removeEventListener.bind(target, event, callback, options), key]); | ||
}); | ||
} | ||
function destroy() { | ||
if (key) { | ||
arrayRemove(removers, (remover) => remover[1] === key).forEach((remover) => { | ||
function destroy(hard) { | ||
removers.forEach((remover) => { | ||
if (!key || remover[1] === key) { | ||
remover[0](); | ||
}); | ||
} else { | ||
removers.forEach((remover) => { | ||
remover[0](); | ||
}); | ||
empty(removers); | ||
} | ||
removers.delete(remover); | ||
} | ||
}); | ||
destroyed = hard; | ||
} | ||
return { | ||
bind, | ||
create: apply(EventBinder, removers), | ||
lock: apply(EventBinder, removers), | ||
destroy | ||
@@ -292,7 +287,9 @@ }; | ||
function EventBus(listenersRef) { | ||
const listeners = listenersRef || []; | ||
const listeners = listenersRef || {}; | ||
const key = listenersRef ? {} : void 0; | ||
let destroyed; | ||
function on(events, callback) { | ||
assert(!destroyed); | ||
forEachEvent(events, (event) => { | ||
listeners.push([event, callback, key]); | ||
listeners[event] = push(get(event), [[callback, key]]); | ||
}); | ||
@@ -302,20 +299,22 @@ } | ||
forEachEvent(events, (event) => { | ||
arrayRemove(listeners, (listener) => { | ||
return listener[0] === event && (!callback || listener[1] === callback) && listener[2] === key; | ||
}).forEach(empty); | ||
listeners[event] = get(event).filter((listener) => !((!callback || listener[0] === callback) && listener[1] === key)); | ||
}); | ||
} | ||
function emit(event, ...args) { | ||
listeners.slice().forEach((listener) => { | ||
listener[0] === event && listener[1](...args); | ||
get(event).forEach((listener) => listener[0] && listener[0](...args)); | ||
} | ||
function get(event) { | ||
return listeners[event] || []; | ||
} | ||
function destroy(hard) { | ||
forOwn(listeners, (entries, event) => { | ||
listeners[event] = entries.filter((listener) => { | ||
const unlocked = !key || listener[1] === key; | ||
unlocked && empty(listener); | ||
return !unlocked; | ||
}); | ||
}); | ||
!key && omit(listeners); | ||
destroyed = hard; | ||
} | ||
function destroy() { | ||
if (key) { | ||
arrayRemove(listeners, (listener) => listener[2] === key).forEach(empty); | ||
} else { | ||
listeners.forEach(empty); | ||
empty(listeners); | ||
} | ||
} | ||
return { | ||
@@ -325,3 +324,3 @@ on, | ||
emit, | ||
create: apply(EventBus, listeners), | ||
lock: apply(EventBus, listeners), | ||
destroy | ||
@@ -331,10 +330,10 @@ }; | ||
function EventInterface(binder = EventBinder(), bus = EventBus()) { | ||
function create2() { | ||
return EventInterface(binder.create(), bus.create()); | ||
function lock() { | ||
return EventInterface(binder.lock(), bus.lock()); | ||
} | ||
function destroy() { | ||
binder.destroy(); | ||
bus.destroy(); | ||
function destroy(hard) { | ||
binder.destroy(hard); | ||
bus.destroy(hard); | ||
} | ||
return assign({}, binder, bus, { create: create2, destroy }); | ||
return assign({}, binder, bus, { lock, destroy }); | ||
} | ||
@@ -341,0 +340,0 @@ function RequestInterval(interval, onInterval, onUpdate, limit) { |
@@ -252,35 +252,30 @@ function empty(array) { | ||
} | ||
function arrayRemove(array, predicate) { | ||
const removed = []; | ||
for (let i = array.length - 1; i >= 0; i--) { | ||
if (predicate(array[i])) { | ||
removed.push(...array.splice(i, 1)); | ||
} | ||
function assert(assertion) { | ||
if (!assertion) { | ||
throw new Error(); | ||
} | ||
return removed; | ||
} | ||
function EventBinder(removersRef) { | ||
const removers = removersRef || []; | ||
const removers = removersRef || /* @__PURE__ */ new Set(); | ||
const key = removersRef ? {} : void 0; | ||
let destroyed; | ||
function bind(target, events, callback, options) { | ||
assert(!destroyed); | ||
forEachEvent(events, (event) => { | ||
target.addEventListener(event, callback, options); | ||
removers.push([target.removeEventListener.bind(target, event, callback, options), key]); | ||
removers.add([target.removeEventListener.bind(target, event, callback, options), key]); | ||
}); | ||
} | ||
function destroy() { | ||
if (key) { | ||
arrayRemove(removers, (remover) => remover[1] === key).forEach((remover) => { | ||
function destroy(hard) { | ||
removers.forEach((remover) => { | ||
if (!key || remover[1] === key) { | ||
remover[0](); | ||
}); | ||
} else { | ||
removers.forEach((remover) => { | ||
remover[0](); | ||
}); | ||
empty(removers); | ||
} | ||
removers.delete(remover); | ||
} | ||
}); | ||
destroyed = hard; | ||
} | ||
return { | ||
bind, | ||
create: apply(EventBinder, removers), | ||
lock: apply(EventBinder, removers), | ||
destroy | ||
@@ -290,7 +285,9 @@ }; | ||
function EventBus(listenersRef) { | ||
const listeners = listenersRef || []; | ||
const listeners = listenersRef || {}; | ||
const key = listenersRef ? {} : void 0; | ||
let destroyed; | ||
function on(events, callback) { | ||
assert(!destroyed); | ||
forEachEvent(events, (event) => { | ||
listeners.push([event, callback, key]); | ||
listeners[event] = push(get(event), [[callback, key]]); | ||
}); | ||
@@ -300,20 +297,22 @@ } | ||
forEachEvent(events, (event) => { | ||
arrayRemove(listeners, (listener) => { | ||
return listener[0] === event && (!callback || listener[1] === callback) && listener[2] === key; | ||
}).forEach(empty); | ||
listeners[event] = get(event).filter((listener) => !((!callback || listener[0] === callback) && listener[1] === key)); | ||
}); | ||
} | ||
function emit(event, ...args) { | ||
listeners.slice().forEach((listener) => { | ||
listener[0] === event && listener[1](...args); | ||
get(event).forEach((listener) => listener[0] && listener[0](...args)); | ||
} | ||
function get(event) { | ||
return listeners[event] || []; | ||
} | ||
function destroy(hard) { | ||
forOwn(listeners, (entries, event) => { | ||
listeners[event] = entries.filter((listener) => { | ||
const unlocked = !key || listener[1] === key; | ||
unlocked && empty(listener); | ||
return !unlocked; | ||
}); | ||
}); | ||
!key && omit(listeners); | ||
destroyed = hard; | ||
} | ||
function destroy() { | ||
if (key) { | ||
arrayRemove(listeners, (listener) => listener[2] === key).forEach(empty); | ||
} else { | ||
listeners.forEach(empty); | ||
empty(listeners); | ||
} | ||
} | ||
return { | ||
@@ -323,3 +322,3 @@ on, | ||
emit, | ||
create: apply(EventBus, listeners), | ||
lock: apply(EventBus, listeners), | ||
destroy | ||
@@ -329,10 +328,10 @@ }; | ||
function EventInterface(binder = EventBinder(), bus = EventBus()) { | ||
function create2() { | ||
return EventInterface(binder.create(), bus.create()); | ||
function lock() { | ||
return EventInterface(binder.lock(), bus.lock()); | ||
} | ||
function destroy() { | ||
binder.destroy(); | ||
bus.destroy(); | ||
function destroy(hard) { | ||
binder.destroy(hard); | ||
bus.destroy(hard); | ||
} | ||
return assign({}, binder, bus, { create: create2, destroy }); | ||
return assign({}, binder, bus, { lock, destroy }); | ||
} | ||
@@ -339,0 +338,0 @@ function RequestInterval(interval, onInterval, onUpdate, limit) { |
@@ -9,4 +9,4 @@ import { AnyFunction } from '../../types'; | ||
bind(target: EventTarget, events: string | string[], callback: AnyFunction, options?: AddEventListenerOptions): void; | ||
create(): EventBinder; | ||
destroy(): void; | ||
lock(): EventBinder; | ||
destroy(hard?: boolean): void; | ||
} | ||
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
declare type Removers = [() => void, object?][]; | ||
declare type Removers = Set<[() => void, object?]>; | ||
/** | ||
@@ -21,0 +21,0 @@ * The constructor function to provide methods to subscribe native events. |
@@ -14,12 +14,18 @@ import { AnyFunction } from '../../types'; | ||
emit(event: string, ...args: any[]): void; | ||
create(): EventBus<M>; | ||
destroy(): void; | ||
lock(): EventBus<M>; | ||
destroy(hard?: boolean): void; | ||
} | ||
/** | ||
* The type for an array with listener data as `[ event, callback, key ]`. | ||
* The type for an array with a listener entry as `[ callback, key ]`. | ||
* | ||
* @since 0.0.1 | ||
*/ | ||
export declare type Listeners = [string, AnyFunction, object?][]; | ||
export declare type Listener = [AnyFunction, object?]; | ||
/** | ||
* The collection of listeners. | ||
* | ||
* @since 0.0.1 | ||
*/ | ||
export declare type Listeners = Record<string, Listener[]>; | ||
/** | ||
* Provides the simple event system. | ||
@@ -37,3 +43,3 @@ * Note that `M` - type for an event map - must have index signature, | ||
* | ||
* @param listenersRef - An array with listener data. Internal use only. | ||
* @param listenersRef | ||
* | ||
@@ -40,0 +46,0 @@ * @return An EventBus instance. |
@@ -9,8 +9,9 @@ import { EventBus } from '../EventBus/EventBus'; | ||
*/ | ||
export interface EventInterface<M extends Record<string, AnyFunction> = Record<string, AnyFunction>> extends Omit<EventBinder, 'create'>, Omit<EventBus<M>, 'create'> { | ||
create(): EventInterface<M>; | ||
destroy(): void; | ||
export interface EventInterface<M extends Record<string, AnyFunction> = Record<string, AnyFunction>> extends Omit<EventBinder, 'lock'>, Omit<EventBus<M>, 'lock'> { | ||
lock(): EventInterface<M>; | ||
destroy(hard?: boolean): void; | ||
} | ||
/** | ||
* The constructor function that provides interface for both internal and native events. | ||
* Only the root `EventInterface` instance can destroy all locked instances. | ||
* | ||
@@ -17,0 +18,0 @@ * @since 0.0.1 |
@@ -9,10 +9,6 @@ /** | ||
/** | ||
* Removes elements in the provided array with the predicate function. | ||
* This method modifies the array itself. | ||
* Throws an error when `assertion` is falsy. | ||
* | ||
* @param array - An array. | ||
* @param predicate - A predicate function. | ||
* | ||
* @return An array with removed elements. | ||
* @param assertion - Set a falsy value to throw an error. | ||
*/ | ||
export declare function arrayRemove<T>(array: T[], predicate: (value: T) => boolean): T[]; | ||
export declare function assert(assertion: boolean): void; |
{ | ||
"name": "@splidejs/utils", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"main": "dist/splide-utils.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/splide-utils.esm.js", |
70371
1783