@wixc3/patterns
Advanced tools
Comparing version 14.1.2 to 14.1.3
@@ -28,2 +28,3 @@ import { Signal } from './signal'; | ||
protected events: Map<EventId, Signal<any>>; | ||
protected emitOnce: Map<EventId, Signal<any>>; | ||
/** | ||
@@ -30,0 +31,0 @@ * Check if an event has subscribers |
@@ -32,2 +32,3 @@ "use strict"; | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
/** | ||
@@ -44,3 +45,3 @@ * Check if an event has subscribers | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new signal_1.Signal([handler])); | ||
bucket ? bucket.add(handler) : this.events.set(event, new signal_1.Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -57,4 +58,5 @@ }; | ||
this.once = (event, handler) => { | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new signal_1.Signal(undefined, [handler])); | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new signal_1.Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -66,7 +68,8 @@ }; | ||
this.unsubscribe = (event, handler) => { | ||
const bucket = this.events.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.unsubscribe(handler); | ||
if ((bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0) { | ||
this.events.delete(event); | ||
} | ||
let bucket = this.events.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.delete(handler); | ||
(bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0 && this.events.delete(event); | ||
bucket = this.emitOnce.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.delete(handler); | ||
(bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0 && this.events.delete(event); | ||
}; | ||
@@ -83,2 +86,3 @@ /** | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -89,4 +93,4 @@ /** | ||
this.clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
}; | ||
@@ -99,4 +103,6 @@ /** | ||
this.notify = (event, data) => { | ||
var _a; | ||
var _a, _b; | ||
(_a = this.events.get(event)) === null || _a === void 0 ? void 0 : _a.notify(data); | ||
(_b = this.emitOnce.get(event)) === null || _b === void 0 ? void 0 : _b.notify(data); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -103,0 +109,0 @@ /** |
@@ -33,6 +33,4 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export declare class Signal<T> { | ||
export declare class Signal<T> extends Set<Listener<T>> { | ||
private onceHandlers; | ||
private handlers; | ||
constructor(handlers?: Listener<T>[], once?: Listener<T>[]); | ||
/** | ||
@@ -45,10 +43,5 @@ * Subscribe a notification callback | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value: Listener<T>): boolean; | ||
/** | ||
* Unsubscribe an existing callback | ||
*/ | ||
unsubscribe: (handler: Listener<T>) => void; | ||
get size(): number; | ||
/** | ||
@@ -55,0 +48,0 @@ * Notify all subscribers with arg data |
@@ -35,6 +35,6 @@ "use strict"; | ||
*/ | ||
class Signal { | ||
constructor(handlers, once) { | ||
class Signal extends Set { | ||
constructor() { | ||
super(...arguments); | ||
this.onceHandlers = new Set(); | ||
this.handlers = new Set(); | ||
/** | ||
@@ -45,12 +45,6 @@ * Subscribe a notification callback | ||
this.subscribe = (handler) => { | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
this.add(handler); | ||
}; | ||
this.once = (handler) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
@@ -61,4 +55,4 @@ /** | ||
this.unsubscribe = (handler) => { | ||
this.handlers.delete(handler); | ||
this.onceHandlers.delete(handler); | ||
this.delete(handler); | ||
}; | ||
@@ -69,3 +63,3 @@ /** | ||
this.notify = (data) => { | ||
for (const handler of this.handlers) { | ||
for (const handler of this) { | ||
handler(data); | ||
@@ -78,16 +72,5 @@ } | ||
}; | ||
handlers === null || handlers === void 0 ? void 0 : handlers.forEach((handler) => this.subscribe(handler)); | ||
once === null || once === void 0 ? void 0 : once.forEach((handler) => this.once(handler)); | ||
} | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value) { | ||
return this.handlers.has(value) || this.onceHandlers.has(value); | ||
} | ||
get size() { | ||
return this.handlers.size + this.onceHandlers.size; | ||
} | ||
clear() { | ||
this.handlers.clear(); | ||
super.clear(); | ||
this.onceHandlers.clear(); | ||
@@ -94,0 +77,0 @@ } |
@@ -28,2 +28,3 @@ import { Signal } from './signal'; | ||
protected events: Map<EventId, Signal<any>>; | ||
protected emitOnce: Map<EventId, Signal<any>>; | ||
/** | ||
@@ -30,0 +31,0 @@ * Check if an event has subscribers |
@@ -29,2 +29,3 @@ import { Signal } from './signal'; | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
/** | ||
@@ -41,3 +42,3 @@ * Check if an event has subscribers | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new Signal([handler])); | ||
bucket ? bucket.add(handler) : this.events.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -54,4 +55,5 @@ }; | ||
this.once = (event, handler) => { | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new Signal(undefined, [handler])); | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -63,7 +65,8 @@ }; | ||
this.unsubscribe = (event, handler) => { | ||
const bucket = this.events.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.unsubscribe(handler); | ||
if ((bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0) { | ||
this.events.delete(event); | ||
} | ||
let bucket = this.events.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.delete(handler); | ||
(bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0 && this.events.delete(event); | ||
bucket = this.emitOnce.get(event); | ||
bucket === null || bucket === void 0 ? void 0 : bucket.delete(handler); | ||
(bucket === null || bucket === void 0 ? void 0 : bucket.size) === 0 && this.events.delete(event); | ||
}; | ||
@@ -80,2 +83,3 @@ /** | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -86,4 +90,4 @@ /** | ||
this.clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
}; | ||
@@ -96,4 +100,6 @@ /** | ||
this.notify = (event, data) => { | ||
var _a; | ||
var _a, _b; | ||
(_a = this.events.get(event)) === null || _a === void 0 ? void 0 : _a.notify(data); | ||
(_b = this.emitOnce.get(event)) === null || _b === void 0 ? void 0 : _b.notify(data); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -100,0 +106,0 @@ /** |
@@ -33,6 +33,4 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export declare class Signal<T> { | ||
export declare class Signal<T> extends Set<Listener<T>> { | ||
private onceHandlers; | ||
private handlers; | ||
constructor(handlers?: Listener<T>[], once?: Listener<T>[]); | ||
/** | ||
@@ -45,10 +43,5 @@ * Subscribe a notification callback | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value: Listener<T>): boolean; | ||
/** | ||
* Unsubscribe an existing callback | ||
*/ | ||
unsubscribe: (handler: Listener<T>) => void; | ||
get size(): number; | ||
/** | ||
@@ -55,0 +48,0 @@ * Notify all subscribers with arg data |
@@ -32,6 +32,6 @@ /** | ||
*/ | ||
export class Signal { | ||
constructor(handlers, once) { | ||
export class Signal extends Set { | ||
constructor() { | ||
super(...arguments); | ||
this.onceHandlers = new Set(); | ||
this.handlers = new Set(); | ||
/** | ||
@@ -42,12 +42,6 @@ * Subscribe a notification callback | ||
this.subscribe = (handler) => { | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
this.add(handler); | ||
}; | ||
this.once = (handler) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
@@ -58,4 +52,4 @@ /** | ||
this.unsubscribe = (handler) => { | ||
this.handlers.delete(handler); | ||
this.onceHandlers.delete(handler); | ||
this.delete(handler); | ||
}; | ||
@@ -66,3 +60,3 @@ /** | ||
this.notify = (data) => { | ||
for (const handler of this.handlers) { | ||
for (const handler of this) { | ||
handler(data); | ||
@@ -75,16 +69,5 @@ } | ||
}; | ||
handlers === null || handlers === void 0 ? void 0 : handlers.forEach((handler) => this.subscribe(handler)); | ||
once === null || once === void 0 ? void 0 : once.forEach((handler) => this.once(handler)); | ||
} | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value) { | ||
return this.handlers.has(value) || this.onceHandlers.has(value); | ||
} | ||
get size() { | ||
return this.handlers.size + this.onceHandlers.size; | ||
} | ||
clear() { | ||
this.handlers.clear(); | ||
super.clear(); | ||
this.onceHandlers.clear(); | ||
@@ -91,0 +74,0 @@ } |
{ | ||
"name": "@wixc3/patterns", | ||
"version": "14.1.2", | ||
"version": "14.1.3", | ||
"description": "A utility for saving objects to be disposed", | ||
@@ -23,4 +23,3 @@ "main": "dist/cjs/index.js", | ||
"promise-assist": "^2.0.1" | ||
}, | ||
"gitHead": "8066ec978e0d56cea3dc4897e22e7f35f211b3ce" | ||
} | ||
} |
@@ -29,2 +29,3 @@ import { Signal } from './signal'; | ||
protected events = new Map<EventId, Signal<any>>(); | ||
protected emitOnce = new Map<EventId, Signal<any>>(); | ||
@@ -43,3 +44,3 @@ /** | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new Signal([handler])); | ||
bucket ? bucket.add(handler) : this.events.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -58,4 +59,5 @@ }; | ||
once = <Event extends EventId>(event: Event, handler: (data: Events[Event]) => void) => { | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new Signal(undefined, [handler])); | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -68,7 +70,8 @@ }; | ||
unsubscribe = <Event extends EventId>(event: Event, handler: (data: Events[Event]) => void) => { | ||
const bucket = this.events.get(event); | ||
bucket?.unsubscribe(handler); | ||
if (bucket?.size === 0) { | ||
this.events.delete(event); | ||
} | ||
let bucket = this.events.get(event); | ||
bucket?.delete(handler); | ||
bucket?.size === 0 && this.events.delete(event); | ||
bucket = this.emitOnce.get(event); | ||
bucket?.delete(handler); | ||
bucket?.size === 0 && this.events.delete(event); | ||
}; | ||
@@ -87,2 +90,3 @@ | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -94,4 +98,4 @@ | ||
clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map<EventId, Signal<any>>(); | ||
this.emitOnce = new Map<EventId, Signal<any>>(); | ||
}; | ||
@@ -106,2 +110,4 @@ | ||
this.events.get(event)?.notify(data); | ||
this.emitOnce.get(event)?.notify(data); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -108,0 +114,0 @@ |
@@ -34,10 +34,4 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export class Signal<T> { | ||
export class Signal<T> extends Set<Listener<T>> { | ||
private onceHandlers = new Set<Listener<T>>(); | ||
private handlers = new Set<Listener<T>>(); | ||
constructor(handlers?: Listener<T>[], once?: Listener<T>[]) { | ||
handlers?.forEach((handler) => this.subscribe(handler)); | ||
once?.forEach((handler) => this.once(handler)); | ||
} | ||
/** | ||
@@ -48,33 +42,15 @@ * Subscribe a notification callback | ||
subscribe = (handler: Listener<T>) => { | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
this.add(handler); | ||
}; | ||
once = (handler: Listener<T>) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value: Listener<T>): boolean { | ||
return this.handlers.has(value) || this.onceHandlers.has(value); | ||
} | ||
/** | ||
* Unsubscribe an existing callback | ||
*/ | ||
unsubscribe = (handler: Listener<T>) => { | ||
this.handlers.delete(handler); | ||
this.onceHandlers.delete(handler); | ||
this.delete(handler); | ||
}; | ||
get size(): number { | ||
return this.handlers.size + this.onceHandlers.size; | ||
} | ||
/** | ||
@@ -84,3 +60,3 @@ * Notify all subscribers with arg data | ||
notify = (data: T) => { | ||
for (const handler of this.handlers) { | ||
for (const handler of this) { | ||
handler(data); | ||
@@ -94,6 +70,6 @@ } | ||
clear(): void { | ||
this.handlers.clear(); | ||
override clear(): void { | ||
super.clear(); | ||
this.onceHandlers.clear(); | ||
} | ||
} |
@@ -70,9 +70,2 @@ import chai, { expect } from 'chai'; | ||
}); | ||
// TODO: uncomment when engine COM is ready | ||
// it(`doesn't call listeners after unsubscribing using subscribe return value`, () => { | ||
// const unsubscribe = signal.subscribe(listener); | ||
// unsubscribe(); | ||
// signal.notify({ a: 'value', b: 5 }); | ||
// expect(listener.callCount, 'no new calls after unsubscribe').to.eql(0); | ||
// }); | ||
describe('clear', () => { | ||
@@ -79,0 +72,0 @@ it('removes all listeners', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
255136
4960