Skip to content

Commit

Permalink
Rename idDedupeMode 'append' -> 'add'.
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasramo committed Jan 16, 2024
1 parent 531c3c2 commit 6f84579
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ emitter.off('a', 'foo');
// listener id again? Well, it's up to you and Eventti allows you to choose from
// four different options what the behaviour should be.

// Case #1: When the idDedupeMode mode is set to "append" (which it is by
// Case #1: When the idDedupeMode mode is set to "add" (which it is by
// default) the existing listener (matching the id) will be first completely
// removed and then the new listener will be appended to the listener queue.
const emitter1 = new Emitter({ idDedupeMode: EmitterIdDedupeMode.APPEND });
const emitter1 = new Emitter({ idDedupeMode: EmitterIdDedupeMode.ADD });
emitter1.on('a', () => console.log('foo 1'), 'foo');
emitter1.on('a', () => console.log('bar'), 'bar');
emitter1.on('a', () => console.log('foo 2'), 'foo');
Expand Down Expand Up @@ -185,13 +185,13 @@ emitter5.idDedupeMode = EmitterIdDedupeMode.THROW;
- **allowDuplicateListeners**  —  `boolean`
- When set to `false` `.on()` or `.once()` methods will throw an error if a duplicate event listener is added.
- Optional. Defaults to `true` if omitted.
- **idDedupeMode**  —  `"append" | "update" | "ignore" | "throw"`
- **idDedupeMode**  —  `"add" | "update" | "ignore" | "throw"`
- Defines how a duplicate event listener id is handled.
- `"append"`: the existing listener (of the id) is removed and the new listener is appended to the event's listener queue.
- `"add"`: the existing listener (of the id) is removed and the new listener is appended to the event's listener queue.
- `"update"`: the existing listener (of the id) is replaced with the new listener without changing the index of the listener in the event's listener queue.
- `"ignore"`: the new listener is silently ignored and not added to the event.
- `"throw"`: as the name suggests an error will be thrown.
- Optional. Defaults to `"append"` if omitted.
- Optional. Defaults to `"add"` if omitted.
- **createId**  —  `() => string | number | symbol`
- A function which is used to create listener ids. By default Eventti uses `Symbol()` to create unique ids, but you can provide your own function if you want to use something else.
- Optional. Defaults to `Symbol` if omitted.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";var o=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var h=(s,e)=>{for(var t in e)o(s,t,{get:e[t],enumerable:!0})},c=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of u(e))!f.call(s,i)&&i!==t&&o(s,i,{get:()=>e[i],enumerable:!(n=v(e,i))||n.enumerable});return s};var L=s=>c(o({},"__esModule",{value:!0}),s);var m={};h(m,{Emitter:()=>l,EmitterIdDedupeMode:()=>a});module.exports=L(m);var a={APPEND:"append",UPDATE:"update",IGNORE:"ignore",THROW:"throw"};function E(s,e){let t=s.get(e);return t||(t=new p,s.set(e,t)),t}var p=class{constructor(){this.idMap=new Map,this.fnMap=new Map,this.onceList=new Set,this.emitList=null}add(e,t,n,i,d){if(!d&&this.fnMap.has(e))throw new Error("Emitter: tried to add an existing event listener to an event!");if(this.idMap.has(n))switch(i){case a.THROW:throw new Error("Emitter: tried to add an existing event listener id to an event!");case a.IGNORE:return n;default:this.delId(n,i===a.UPDATE)}let r=this.fnMap.get(e);return r||(r=new Set,this.fnMap.set(e,r)),r.add(n),this.idMap.set(n,e),t&&this.onceList.add(n),this.emitList&&this.emitList.push(e),n}delId(e,t=!1){let n=this.idMap.get(e);if(!n)return;let i=this.fnMap.get(n);t||this.idMap.delete(e),this.onceList.delete(e),i.delete(e),i.size||this.fnMap.delete(n),this.emitList=null}delFn(e){let t=this.fnMap.get(e);t&&(t.forEach(n=>{this.onceList.delete(n),this.idMap.delete(n)}),this.fnMap.delete(e),this.emitList=null)}},l=class{constructor(e={}){let{idDedupeMode:t=a.APPEND,allowDuplicateListeners:n=!0}=e;this.idDedupeMode=t,this.createId=e.createId||Symbol,this.allowDuplicateListeners=n,this._events=new Map}_getListeners(e){let t=this._events.get(e);if(!t)return null;let{idMap:n,onceList:i}=t;if(!n.size)return null;let d=t.emitList||[...n.values()];if(i.size)if(i.size===n.size)this._events.delete(e);else for(let r of i)t.delId(r);else t.emitList=d;return d}on(e,t,n=this.createId()){return E(this._events,e).add(t,!1,n,this.idDedupeMode,this.allowDuplicateListeners)}once(e,t,n=this.createId()){return E(this._events,e).add(t,!0,n,this.idDedupeMode,this.allowDuplicateListeners)}off(e,t){if(e===void 0){this._events.clear();return}if(t===void 0){this._events.delete(e);return}let n=this._events.get(e);n&&(typeof t=="function"?n.delFn(t):n.delId(t),n.idMap.size||this._events.delete(e))}emit(e,...t){let n=this._getListeners(e);if(!n)return;let i=0,d=n.length;for(;i<d;i++)n[i](...t)}listenerCount(e){if(e===void 0){let t=0;return this._events.forEach((n,i)=>{t+=this.listenerCount(i)}),t}return this._events.get(e)?.idMap.size||0}};0&&(module.exports={Emitter,EmitterIdDedupeMode});
"use strict";var o=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var h=(s,e)=>{for(var t in e)o(s,t,{get:e[t],enumerable:!0})},c=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of u(e))!f.call(s,i)&&i!==t&&o(s,i,{get:()=>e[i],enumerable:!(n=v(e,i))||n.enumerable});return s};var L=s=>c(o({},"__esModule",{value:!0}),s);var m={};h(m,{Emitter:()=>p,EmitterIdDedupeMode:()=>a});module.exports=L(m);var a={ADD:"add",UPDATE:"update",IGNORE:"ignore",THROW:"throw"};function E(s,e){let t=s.get(e);return t||(t=new l,s.set(e,t)),t}var l=class{constructor(){this.idMap=new Map,this.fnMap=new Map,this.onceList=new Set,this.emitList=null}add(e,t,n,i,d){if(!d&&this.fnMap.has(e))throw new Error("Emitter: tried to add an existing event listener to an event!");if(this.idMap.has(n))switch(i){case a.THROW:throw new Error("Emitter: tried to add an existing event listener id to an event!");case a.IGNORE:return n;default:this.delId(n,i===a.UPDATE)}let r=this.fnMap.get(e);return r||(r=new Set,this.fnMap.set(e,r)),r.add(n),this.idMap.set(n,e),t&&this.onceList.add(n),this.emitList&&this.emitList.push(e),n}delId(e,t=!1){let n=this.idMap.get(e);if(!n)return;let i=this.fnMap.get(n);t||this.idMap.delete(e),this.onceList.delete(e),i.delete(e),i.size||this.fnMap.delete(n),this.emitList=null}delFn(e){let t=this.fnMap.get(e);t&&(t.forEach(n=>{this.onceList.delete(n),this.idMap.delete(n)}),this.fnMap.delete(e),this.emitList=null)}},p=class{constructor(e={}){let{idDedupeMode:t=a.ADD,allowDuplicateListeners:n=!0}=e;this.idDedupeMode=t,this.createId=e.createId||Symbol,this.allowDuplicateListeners=n,this._events=new Map}_getListeners(e){let t=this._events.get(e);if(!t)return null;let{idMap:n,onceList:i}=t;if(!n.size)return null;let d=t.emitList||[...n.values()];if(i.size)if(i.size===n.size)this._events.delete(e);else for(let r of i)t.delId(r);else t.emitList=d;return d}on(e,t,n=this.createId()){return E(this._events,e).add(t,!1,n,this.idDedupeMode,this.allowDuplicateListeners)}once(e,t,n=this.createId()){return E(this._events,e).add(t,!0,n,this.idDedupeMode,this.allowDuplicateListeners)}off(e,t){if(e===void 0){this._events.clear();return}if(t===void 0){this._events.delete(e);return}let n=this._events.get(e);n&&(typeof t=="function"?n.delFn(t):n.delId(t),n.idMap.size||this._events.delete(e))}emit(e,...t){let n=this._getListeners(e);if(!n)return;let i=0,d=n.length;for(;i<d;i++)n[i](...t)}listenerCount(e){if(e===void 0){let t=0;return this._events.forEach((n,i)=>{t+=this.listenerCount(i)}),t}return this._events.get(e)?.idMap.size||0}};0&&(module.exports={Emitter,EmitterIdDedupeMode});
2 changes: 1 addition & 1 deletion dist/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type EventListener = (...data: any) => any;
type EventListenerId = string | number | symbol;
type Events = Record<EventName, EventListener>;
declare const EmitterIdDedupeMode: {
readonly APPEND: "append";
readonly ADD: "add";
readonly UPDATE: "update";
readonly IGNORE: "ignore";
readonly THROW: "throw";
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type EventListener = (...data: any) => any;
type EventListenerId = string | number | symbol;
type Events = Record<EventName, EventListener>;
declare const EmitterIdDedupeMode: {
readonly APPEND: "append";
readonly ADD: "add";
readonly UPDATE: "update";
readonly IGNORE: "ignore";
readonly THROW: "throw";
Expand Down
2 changes: 1 addition & 1 deletion dist/index.global.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type EventListenerId = string | number | symbol;
export type Events = Record<EventName, EventListener>;

export const EmitterIdDedupeMode = {
APPEND: 'append',
ADD: 'add',
UPDATE: 'update',
IGNORE: 'ignore',
THROW: 'throw',
Expand Down Expand Up @@ -140,7 +140,7 @@ export class Emitter<T extends Events> {
protected _events: InternalEventMap;

constructor(options: EmitterOptions = {}) {
const { idDedupeMode = EmitterIdDedupeMode.APPEND, allowDuplicateListeners = true } = options;
const { idDedupeMode = EmitterIdDedupeMode.ADD, allowDuplicateListeners = true } = options;
this.idDedupeMode = idDedupeMode;
this.createId = options.createId || Symbol;
this.allowDuplicateListeners = allowDuplicateListeners;
Expand Down
Loading

0 comments on commit 6f84579

Please sign in to comment.