@wixc3/patterns
Advanced tools
Comparing version 14.1.1 to 14.1.2
@@ -28,3 +28,2 @@ import { Signal } from './signal'; | ||
protected events: Map<EventId, Signal<any>>; | ||
protected emitOnce: Map<EventId, Signal<any>>; | ||
/** | ||
@@ -31,0 +30,0 @@ * Check if an event has subscribers |
@@ -32,3 +32,2 @@ "use strict"; | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
/** | ||
@@ -45,3 +44,3 @@ * Check if an event has subscribers | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.add(handler) : this.events.set(event, new signal_1.Signal([handler])); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new signal_1.Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -58,5 +57,4 @@ }; | ||
this.once = (event, handler) => { | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new signal_1.Signal([handler])); | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new signal_1.Signal(undefined, [handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -68,8 +66,7 @@ }; | ||
this.unsubscribe = (event, handler) => { | ||
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); | ||
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); | ||
} | ||
}; | ||
@@ -86,3 +83,2 @@ /** | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -93,4 +89,4 @@ /** | ||
this.clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
}; | ||
@@ -103,6 +99,4 @@ /** | ||
this.notify = (event, data) => { | ||
var _a, _b; | ||
var _a; | ||
(_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); | ||
}; | ||
@@ -109,0 +103,0 @@ /** |
@@ -33,4 +33,6 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export declare class Signal<T> extends Set<Listener<T>> { | ||
export declare class Signal<T> { | ||
private onceHandlers; | ||
private handlers; | ||
constructor(handlers?: Listener<T>[], once?: Listener<T>[]); | ||
/** | ||
@@ -40,8 +42,13 @@ * Subscribe a notification callback | ||
*/ | ||
subscribe: (handler: Listener<T>) => () => void; | ||
once: (handler: Listener<T>) => () => void; | ||
subscribe: (handler: Listener<T>) => void; | ||
once: (handler: Listener<T>) => void; | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value: Listener<T>): boolean; | ||
/** | ||
* Unsubscribe an existing callback | ||
*/ | ||
unsubscribe: (handler: Listener<T>) => void; | ||
get size(): number; | ||
/** | ||
@@ -48,0 +55,0 @@ * Notify all subscribers with arg data |
@@ -35,6 +35,6 @@ "use strict"; | ||
*/ | ||
class Signal extends Set { | ||
constructor() { | ||
super(...arguments); | ||
class Signal { | ||
constructor(handlers, once) { | ||
this.onceHandlers = new Set(); | ||
this.handlers = new Set(); | ||
/** | ||
@@ -45,8 +45,12 @@ * Subscribe a notification callback | ||
this.subscribe = (handler) => { | ||
this.add(handler); | ||
return () => this.unsubscribe(handler); | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
this.once = (handler) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
return () => this.unsubscribe(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
@@ -57,4 +61,4 @@ /** | ||
this.unsubscribe = (handler) => { | ||
this.handlers.delete(handler); | ||
this.onceHandlers.delete(handler); | ||
this.delete(handler); | ||
}; | ||
@@ -65,3 +69,3 @@ /** | ||
this.notify = (data) => { | ||
for (const handler of this) { | ||
for (const handler of this.handlers) { | ||
handler(data); | ||
@@ -74,5 +78,16 @@ } | ||
}; | ||
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() { | ||
super.clear(); | ||
this.handlers.clear(); | ||
this.onceHandlers.clear(); | ||
@@ -79,0 +94,0 @@ } |
@@ -28,3 +28,2 @@ import { Signal } from './signal'; | ||
protected events: Map<EventId, Signal<any>>; | ||
protected emitOnce: Map<EventId, Signal<any>>; | ||
/** | ||
@@ -31,0 +30,0 @@ * Check if an event has subscribers |
@@ -29,3 +29,2 @@ import { Signal } from './signal'; | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
/** | ||
@@ -42,3 +41,3 @@ * Check if an event has subscribers | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.add(handler) : this.events.set(event, new Signal([handler])); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -55,5 +54,4 @@ }; | ||
this.once = (event, handler) => { | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new Signal([handler])); | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new Signal(undefined, [handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -65,8 +63,7 @@ }; | ||
this.unsubscribe = (event, handler) => { | ||
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); | ||
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); | ||
} | ||
}; | ||
@@ -83,3 +80,2 @@ /** | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -90,4 +86,4 @@ /** | ||
this.clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map(); | ||
this.emitOnce = new Map(); | ||
}; | ||
@@ -100,6 +96,4 @@ /** | ||
this.notify = (event, data) => { | ||
var _a, _b; | ||
var _a; | ||
(_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); | ||
}; | ||
@@ -106,0 +100,0 @@ /** |
@@ -33,4 +33,6 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export declare class Signal<T> extends Set<Listener<T>> { | ||
export declare class Signal<T> { | ||
private onceHandlers; | ||
private handlers; | ||
constructor(handlers?: Listener<T>[], once?: Listener<T>[]); | ||
/** | ||
@@ -40,8 +42,13 @@ * Subscribe a notification callback | ||
*/ | ||
subscribe: (handler: Listener<T>) => () => void; | ||
once: (handler: Listener<T>) => () => void; | ||
subscribe: (handler: Listener<T>) => void; | ||
once: (handler: Listener<T>) => void; | ||
/** | ||
* @returns true if a listener is subscribed | ||
*/ | ||
has(value: Listener<T>): boolean; | ||
/** | ||
* Unsubscribe an existing callback | ||
*/ | ||
unsubscribe: (handler: Listener<T>) => void; | ||
get size(): number; | ||
/** | ||
@@ -48,0 +55,0 @@ * Notify all subscribers with arg data |
@@ -32,6 +32,6 @@ /** | ||
*/ | ||
export class Signal extends Set { | ||
constructor() { | ||
super(...arguments); | ||
export class Signal { | ||
constructor(handlers, once) { | ||
this.onceHandlers = new Set(); | ||
this.handlers = new Set(); | ||
/** | ||
@@ -42,8 +42,12 @@ * Subscribe a notification callback | ||
this.subscribe = (handler) => { | ||
this.add(handler); | ||
return () => this.unsubscribe(handler); | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
this.once = (handler) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
return () => this.unsubscribe(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
@@ -54,4 +58,4 @@ /** | ||
this.unsubscribe = (handler) => { | ||
this.handlers.delete(handler); | ||
this.onceHandlers.delete(handler); | ||
this.delete(handler); | ||
}; | ||
@@ -62,3 +66,3 @@ /** | ||
this.notify = (data) => { | ||
for (const handler of this) { | ||
for (const handler of this.handlers) { | ||
handler(data); | ||
@@ -71,5 +75,16 @@ } | ||
}; | ||
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() { | ||
super.clear(); | ||
this.handlers.clear(); | ||
this.onceHandlers.clear(); | ||
@@ -76,0 +91,0 @@ } |
{ | ||
"name": "@wixc3/patterns", | ||
"version": "14.1.1", | ||
"version": "14.1.2", | ||
"description": "A utility for saving objects to be disposed", | ||
@@ -23,3 +23,4 @@ "main": "dist/cjs/index.js", | ||
"promise-assist": "^2.0.1" | ||
} | ||
}, | ||
"gitHead": "8066ec978e0d56cea3dc4897e22e7f35f211b3ce" | ||
} |
@@ -29,3 +29,2 @@ import { Signal } from './signal'; | ||
protected events = new Map<EventId, Signal<any>>(); | ||
protected emitOnce = new Map<EventId, Signal<any>>(); | ||
@@ -44,3 +43,3 @@ /** | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.add(handler) : this.events.set(event, new Signal([handler])); | ||
bucket ? bucket.subscribe(handler) : this.events.set(event, new Signal([handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -59,5 +58,4 @@ }; | ||
once = <Event extends EventId>(event: Event, handler: (data: Events[Event]) => void) => { | ||
this.off(event, handler); | ||
const bucket = this.emitOnce.get(event); | ||
bucket ? bucket.add(handler) : this.emitOnce.set(event, new Signal([handler])); | ||
const bucket = this.events.get(event); | ||
bucket ? bucket.once(handler) : this.events.set(event, new Signal(undefined, [handler])); | ||
return () => this.unsubscribe(event, handler); | ||
@@ -70,8 +68,7 @@ }; | ||
unsubscribe = <Event extends EventId>(event: Event, handler: (data: Events[Event]) => void) => { | ||
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); | ||
const bucket = this.events.get(event); | ||
bucket?.unsubscribe(handler); | ||
if (bucket?.size === 0) { | ||
this.events.delete(event); | ||
} | ||
}; | ||
@@ -90,3 +87,2 @@ | ||
this.events.delete(event); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -98,4 +94,4 @@ | ||
clear = () => { | ||
this.events.forEach((bucket) => bucket.clear()); | ||
this.events = new Map<EventId, Signal<any>>(); | ||
this.emitOnce = new Map<EventId, Signal<any>>(); | ||
}; | ||
@@ -110,4 +106,2 @@ | ||
this.events.get(event)?.notify(data); | ||
this.emitOnce.get(event)?.notify(data); | ||
this.emitOnce.delete(event); | ||
}; | ||
@@ -114,0 +108,0 @@ |
@@ -34,4 +34,10 @@ export type Listener<T> = (data: T) => void; | ||
*/ | ||
export class Signal<T> extends Set<Listener<T>> { | ||
export class Signal<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)); | ||
} | ||
/** | ||
@@ -42,17 +48,33 @@ * Subscribe a notification callback | ||
subscribe = (handler: Listener<T>) => { | ||
this.add(handler); | ||
return () => this.unsubscribe(handler); | ||
this.unsubscribe(handler); | ||
this.handlers.add(handler); | ||
// TODO: uncomment when engine COM is ready | ||
// return () => this.unsubscribe(handler); | ||
}; | ||
once = (handler: Listener<T>) => { | ||
this.unsubscribe(handler); | ||
this.onceHandlers.add(handler); | ||
return () => this.unsubscribe(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; | ||
} | ||
/** | ||
@@ -62,3 +84,3 @@ * Notify all subscribers with arg data | ||
notify = (data: T) => { | ||
for (const handler of this) { | ||
for (const handler of this.handlers) { | ||
handler(data); | ||
@@ -72,6 +94,6 @@ } | ||
override clear(): void { | ||
super.clear(); | ||
clear(): void { | ||
this.handlers.clear(); | ||
this.onceHandlers.clear(); | ||
} | ||
} |
@@ -70,8 +70,9 @@ import chai, { expect } from 'chai'; | ||
}); | ||
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); | ||
}); | ||
// 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', () => { | ||
@@ -78,0 +79,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
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
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
257911
5015
1