@nichoth/events
Advanced tools
+18
-15
@@ -7,2 +7,3 @@ interface Listener { | ||
| } | ||
| type ValuesOf<T extends any[]> = T[number]; | ||
| export type Events = (({ | ||
@@ -16,13 +17,12 @@ _?: string[]; | ||
| }; | ||
| export declare class Bus { | ||
| export declare class Bus<T extends Array<string>> { | ||
| _starListeners: StarListener[]; | ||
| _listeners: Record<string, Listener[]>; | ||
| _validEvents: string[] | null; | ||
| _validEvents: T | null; | ||
| /** | ||
| * @constructor | ||
| * @param {string[]} [validEvents] Optional -- pass in an array of event | ||
| * names that are valid. Will throw if you emit or listen for an event | ||
| * not in the list. | ||
| * @param {EvNames} [validEvents] An array of valid event names. | ||
| * Will throw if you emit or listen for an event not in the list. | ||
| */ | ||
| constructor(validEvents?: string[] | NamespacedEvents); | ||
| constructor(validEvents?: T | NamespacedEvents); | ||
| /** | ||
@@ -35,24 +35,27 @@ * Create an object with leaf nodes as fully namespaced strings. | ||
| /** | ||
| * Return an array of leaf nodes of an object | ||
| * Return an array of leaf nodes of an object. Use this to get an array | ||
| * of event names from a given nested object of event names. | ||
| * | ||
| * @param events Namespaced events (the return value of `Bus.createEvents`) | ||
| * @param {[string]} [existing] Previous array to concat with | ||
| * @param {string[]} [existing] Previous array to concat with | ||
| * @returns {string[]} | ||
| */ | ||
| static flatten(events: NamespacedEvents | string, existing?: string[]): string[]; | ||
| static flatten<T extends string[]>(events: NamespacedEvents | string, existing?: string[]): T; | ||
| /** | ||
| * Listen for an event | ||
| * @param {string} evName Name of the event, or '*' for all events | ||
| * @param {T} evName Name of the event, or '*' for all events | ||
| * @param listener Function to call with the event | ||
| * @returns {()=>void} function `off` -- call this to remove the listener | ||
| */ | ||
| on(evName: string, listener: Listener | StarListener): () => void; | ||
| on(evName: ValuesOf<T> | '*', listener: Listener | StarListener): () => void; | ||
| /** | ||
| * Emit an event. | ||
| * @param {string} evName The event name to emit | ||
| * Emit an event, or return a function that is curried with an event name. | ||
| * | ||
| * @param {T[number]} evName The event name to emit | ||
| * @param {any?} data The data to pass to event listeners | ||
| */ | ||
| emit(evName: string, data?: any): any; | ||
| _emit(arr: Listener[] | StarListener[], evName: string, data: any, isStar: boolean): void; | ||
| emit(evName: T[number], data?: any): any; | ||
| _emit(arr: (Listener | StarListener)[], evName: string, data: any, isStar: boolean): void; | ||
| } | ||
| export {}; | ||
| //# sourceMappingURL=index.d.ts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,UAAU,QAAQ;IACd,CAAC,GAAG,KAAA,GAAG,GAAG,CAAA;CACb;AAED,UAAU,YAAY;IAClB,CAAC,MAAM,EAAC,MAAM,EAAE,IAAI,EAAC,GAAG,GAAG,GAAG,CAAA;CACjC;AAED,MAAM,MAAM,MAAM,GAAG,CAAC,CAAA;IAAE,CAAC,CAAC,EAAC,MAAM,EAAE,CAAA;CAAE,GAAG;IAAE,CAAC,CAAC,EAAC,MAAM,GAAE,MAAM,CAAA;CAAE,IAAC,MAAM,EAAE,CAAC,CAAA;AAIvE,MAAM,MAAM,gBAAgB,GAAG;IAC3B,CAAC,GAAG,EAAC,MAAM,GAAE,MAAM,GAAC,gBAAgB,CAAA;CACvC,CAAA;AAMD,qBAAa,GAAG;IACZ,cAAc,EAAC,YAAY,EAAE,CAAC;IAC9B,UAAU,EAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAA;IACrC,YAAY,EAAC,MAAM,EAAE,GAAC,IAAI,CAAA;IAE1B;;;;;OAKG;gBACU,WAAW,CAAC,EAAC,MAAM,EAAE,GAAC,gBAAgB;IAWnD;;;;OAIG;IACH,MAAM,CAAC,YAAY,CAAE,MAAM,EAAC,MAAM,EAAE,MAAM,CAAC,EAAC,MAAM,GAAE,gBAAgB;IA4BpE;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CAAE,MAAM,EAAC,gBAAgB,GAAC,MAAM,EAAE,QAAQ,GAAC,MAAM,EAAO,GAAE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B;IAW5C;;;;OAIG;IACH,MAAM,CAAC,YAAY,CAAE,MAAM,EAAC,MAAM,EAAE,MAAM,CAAC,EAAC,MAAM,GAAE,gBAAgB;IA4BpE;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,EAC7B,MAAM,EAAC,gBAAgB,GAAC,MAAM,EAC9B,QAAQ,GAAC,MAAM,EAAO,GACxB,CAAC;IAUH;;;;;OAKG;IAEH,EAAE,CAAE,MAAM,EAAC,QAAQ,CAAC,CAAC,CAAC,GAAC,GAAG,EAAE,QAAQ,EAAC,QAAQ,GAAC,YAAY,GAAE,MAAM,IAAI;IA2BtE;;;;;OAKG;IACH,IAAI,CAAE,MAAM,EAAC,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,EAAC,GAAG,GAAE,GAAG;IA2BtC,KAAK,CACD,GAAG,EAAC,CAAC,QAAQ,GAAC,YAAY,CAAC,EAAE,EAC7B,MAAM,EAAC,MAAM,EACb,IAAI,EAAC,GAAG,EACR,MAAM,EAAC,OAAO,GAChB,IAAI;CAcT"} |
+14
-13
@@ -1,4 +0,3 @@ | ||
| // myEvents = Bus.createEvents({ ... }) | ||
| // bus = Bus()<myEvents> | ||
| // export class Bus<T extends NamespacedEvents> { | ||
| // type Flatten<T> = T extends string[] ? T[number] : T; | ||
| // export class Bus<T extends Array<string>> /* implements Emitter<T> */ { | ||
| export class Bus { | ||
@@ -10,5 +9,4 @@ _starListeners; | ||
| * @constructor | ||
| * @param {string[]} [validEvents] Optional -- pass in an array of event | ||
| * names that are valid. Will throw if you emit or listen for an event | ||
| * not in the list. | ||
| * @param {EvNames} [validEvents] An array of valid event names. | ||
| * Will throw if you emit or listen for an event not in the list. | ||
| */ | ||
@@ -50,5 +48,7 @@ constructor(validEvents) { | ||
| /** | ||
| * Return an array of leaf nodes of an object | ||
| * Return an array of leaf nodes of an object. Use this to get an array | ||
| * of event names from a given nested object of event names. | ||
| * | ||
| * @param events Namespaced events (the return value of `Bus.createEvents`) | ||
| * @param {[string]} [existing] Previous array to concat with | ||
| * @param {string[]} [existing] Previous array to concat with | ||
| * @returns {string[]} | ||
@@ -58,3 +58,3 @@ */ | ||
| if (typeof events === 'string') { | ||
| return existing.concat([events]); | ||
| return ((existing).concat([events])); | ||
| } | ||
@@ -67,7 +67,7 @@ return Object.keys(events).reduce((acc, key) => { | ||
| * Listen for an event | ||
| * @param {string} evName Name of the event, or '*' for all events | ||
| * @param {T} evName Name of the event, or '*' for all events | ||
| * @param listener Function to call with the event | ||
| * @returns {()=>void} function `off` -- call this to remove the listener | ||
| */ | ||
| // on (evName:ValueOf<T>, listener:Listener|StarListener):() => void { | ||
| // on (evName:string, listener:Listener|StarListener):() => void { | ||
| on(evName, listener) { | ||
@@ -100,4 +100,5 @@ if (evName === '*') { | ||
| /** | ||
| * Emit an event. | ||
| * @param {string} evName The event name to emit | ||
| * Emit an event, or return a function that is curried with an event name. | ||
| * | ||
| * @param {T[number]} evName The event name to emit | ||
| * @param {any?} data The data to pass to event listeners | ||
@@ -104,0 +105,0 @@ */ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA,uCAAuC;AACvC,wBAAwB;AAExB,iDAAiD;AACjD,MAAM,OAAO,GAAG;IACZ,cAAc,CAAgB;IAC9B,UAAU,CAA2B;IACrC,YAAY,CAAc;IAE1B;;;;;OAKG;IACH,YAAa,WAAsC;QAC/C,IAAI,CAAC,cAAc,GAAG,EAAE,CAAA;QACxB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAA;QACpB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAA;QACxB,IAAI,WAAW,EAAE;YACb,IAAI,CAAC,YAAY,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;gBAC7C,WAAW,CAAC,CAAC;gBACb,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CA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AI,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;SAC7C;QAED,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;YAChC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;SACtD;QAED,OAAO,IAAI,CAAA;IACf,CAAC;IAED,KAAK,CACD,GAA6B,EAC7B,MAAa,EACb,IAAQ,EACR,MAAc;QAEd,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;YAAE,OAAM;QAE5B,IAAI,MAAM,EAAE;YACR,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;gBAClC,QAAyB,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,CAAA;YAC3D,CAAC,CAAC,CAAA;YACF,OAAM;SACT;QAED,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YAClB,QAAqB,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;QAC/C,CAAC,CAAC,CAAA;IACN,CAAC;CACJ"} |
@@ -1,1 +0,1 @@ | ||
| "use strict";var l=Object.defineProperty;var i=(a,t)=>l(a,"name",{value:t,configurable:!0});export class Bus{_starListeners;_listeners;_validEvents;constructor(t){this._starListeners=[],this._listeners={},this._validEvents=null,t&&(this._validEvents=Array.isArray(t)?t:Bus.flatten(t))}static createEvents(t,s){return Array.isArray(t)?t.reduce((n,r)=>(n[r]=s?s+"."+r:r,n),{}):Object.keys(t).reduce((n,r)=>r==="_"?Object.assign(n,Bus.createEvents(t[r],s||"")):(n[r]=Bus.createEvents(t[r],s?s+"."+r:r),n),{})}static flatten(t,s=[]){return typeof t=="string"?s.concat([t]):Object.keys(t).reduce((n,r)=>Bus.flatten(t[r],n),s)}on(t,s){if(t==="*")this._starListeners.push(s);else{if(this._validEvents&&!this._validEvents.includes(t))throw new Error("Invalid event name subscribed to");this._listeners[t]||(this._listeners[t]=[]),this._listeners[t].push(s)}const n=this;return i(function(){t==="*"?n._starListeners=n._starListeners.filter(e=>e!==s):n._listeners[t]=n._listeners[t].filter(e=>e!==s)},"off")}emit(t,s){const n=this;if(this._validEvents&&!this._validEvents.includes(t))throw new Error("Invalid event name emitted");if(s===void 0)return function(e){return n.emit(t,e)};const r=this._listeners[t]||[];return r&&r.length>0&&this._emit(r,t,s,!1),this._starListeners.length>0&&this._emit(this._starListeners,t,s,!0),this}_emit(t,s,n,r){if(t.length!==0){if(r){this._starListeners.forEach(e=>{e.call(e,s,n)});return}t.forEach(e=>{e.call(e,n)})}}}i(Bus,"Bus"); | ||
| "use strict";var l=Object.defineProperty;var i=(a,t)=>l(a,"name",{value:t,configurable:!0});export class Bus{_starListeners;_listeners;_validEvents;constructor(t){this._starListeners=[],this._listeners={},this._validEvents=null,t&&(this._validEvents=Array.isArray(t)?t:Bus.flatten(t))}static createEvents(t,s){return Array.isArray(t)?t.reduce((e,r)=>(e[r]=s?s+"."+r:r,e),{}):Object.keys(t).reduce((e,r)=>r==="_"?Object.assign(e,Bus.createEvents(t[r],s||"")):(e[r]=Bus.createEvents(t[r],s?s+"."+r:r),e),{})}static flatten(t,s=[]){return typeof t=="string"?s.concat([t]):Object.keys(t).reduce((e,r)=>Bus.flatten(t[r],e),s)}on(t,s){if(t==="*")this._starListeners.push(s);else{if(this._validEvents&&!this._validEvents.includes(t))throw new Error("Invalid event name subscribed to");this._listeners[t]||(this._listeners[t]=[]),this._listeners[t].push(s)}const e=this;return i(function(){t==="*"?e._starListeners=e._starListeners.filter(n=>n!==s):e._listeners[t]=e._listeners[t].filter(n=>n!==s)},"off")}emit(t,s){const e=this;if(this._validEvents&&!this._validEvents.includes(t))throw new Error("Invalid event name emitted");if(s===void 0)return function(n){return e.emit(t,n)};const r=this._listeners[t]||[];return r&&r.length>0&&this._emit(r,t,s,!1),this._starListeners.length>0&&this._emit(this._starListeners,t,s,!0),this}_emit(t,s,e,r){if(t.length!==0){if(r){this._starListeners.forEach(n=>{n.call(n,s,e)});return}t.forEach(n=>{n.call(n,e)})}}}i(Bus,"Bus"); |
+6
-5
| { | ||
| "name": "@nichoth/events", | ||
| "version": "3.1.9", | ||
| "version": "3.2.0", | ||
| "description": "Event emitter and helpers", | ||
@@ -28,4 +28,5 @@ "directories": { | ||
| "lint": "standardx -v \"./**/*.ts\"", | ||
| "build-tests": "esbuild test/*.ts --platform=node --format=esm --outdir=test --keep-names", | ||
| "test": "tsc --project tsconfig.build.json && npm run build-tests && NODE_ENV=test node ./test/index.js | tap-arc", | ||
| "build-tests": "esbuild test/*.ts --bundle --platform=node --format=esm --outdir=test --keep-names", | ||
| "//test": "tsc --project tsconfig.build.json && npm run build-tests && NODE_ENV=test node ./test/index.js | tap-arc", | ||
| "test": "esbuild ./test/index.ts --bundle --platform=node --format=esm | node --input-type=module | tap-arc", | ||
| "build-cjs": "esbuild src/*.ts --tsconfig=./tsconfig.build.json --format=cjs --keep-names --outdir=./dist --out-extension:.js=.cjs", | ||
@@ -40,2 +41,3 @@ "build": "mkdir -p ./dist && rm -rf ./dist/* && npm run build-cjs && tsc --project tsconfig.build.json && npm run build:min", | ||
| "devDependencies": { | ||
| "@nichoth/tapzero": "^0.8.4", | ||
| "@typescript-eslint/eslint-plugin": "^6.7.0", | ||
@@ -46,7 +48,6 @@ "@typescript-eslint/parser": "^6.0.0", | ||
| "tap-arc": "^0.3.5", | ||
| "tape": "^5.6.6", | ||
| "typescript": "^5.0.2" | ||
| }, | ||
| "author": "nichoth <nichoth@gmail.com> (https://nichoth.com)", | ||
| "license": "AGPL-3.0-or-later", | ||
| "license": "MIT", | ||
| "types": "./dist/index.d.ts", | ||
@@ -53,0 +54,0 @@ "repository": { |
+23
-1
@@ -8,3 +8,3 @@ # events | ||
| An event emitter and helpers | ||
| An extra minimal event emitter | ||
@@ -22,3 +22,25 @@ __featuring__ | ||
| ## example | ||
| You can pass in an array of valid event names. If you subscribe to or emit an | ||
| event not in the list, this will throw a runtime error. Also, you can create | ||
| a typed event bus because `Bus` takes a type argument. | ||
| ### create an event bus, with types | ||
| ```ts | ||
| import { Bus } from '@nichoth/events' | ||
| const eventTree = Bus.createEvents({ | ||
| a: ['b', 'c', 'd'], | ||
| b: { | ||
| _: ['e', 'f'], | ||
| c: ['1', '2', '3'] | ||
| } | ||
| }) | ||
| const events = Bus.flatten(eventTree) | ||
| const bus = new Bus<Array<typeof events[number]>>(events) | ||
| bus.on(eventTree.a) | ||
| ``` | ||
| ### create an event bus | ||
@@ -25,0 +47,0 @@ ```js |
+43
-26
| interface Listener { | ||
| (any): any | ||
| (any):any | ||
| } | ||
@@ -9,6 +9,14 @@ | ||
| type ValuesOf<T extends any[]>= T[number]; | ||
| // type AllowedEvent<T extends Array<string>> = ValuesOf<T> | ||
| // interface Emitter<T extends string[]> { | ||
| // on<K extends AllowedEvent<T>> | ||
| // (eventName: K, fn: Listener): any; | ||
| // emit<K extends AllowedEvent<T>> | ||
| // (eventName:K, params:any): void; | ||
| // } | ||
| export type Events = ({ _?:string[] } & { [k:string]:Events }|string[]) | ||
| // type ValueOf<T> = T[keyof T]; | ||
| export type NamespacedEvents = { | ||
@@ -18,18 +26,16 @@ [key:string]:string|NamespacedEvents | ||
| // myEvents = Bus.createEvents({ ... }) | ||
| // bus = Bus()<myEvents> | ||
| // type Flatten<T> = T extends string[] ? T[number] : T; | ||
| // export class Bus<T extends NamespacedEvents> { | ||
| export class Bus { | ||
| // export class Bus<T extends Array<string>> /* implements Emitter<T> */ { | ||
| export class Bus<T extends Array<string>> /* implements Emitter<T> */ { | ||
| _starListeners:StarListener[]; | ||
| _listeners:Record<string, Listener[]> | ||
| _validEvents:string[]|null | ||
| _listeners:Record<string, Listener[]>; | ||
| _validEvents:T|null; | ||
| /** | ||
| * @constructor | ||
| * @param {string[]} [validEvents] Optional -- pass in an array of event | ||
| * names that are valid. Will throw if you emit or listen for an event | ||
| * not in the list. | ||
| * @param {EvNames} [validEvents] An array of valid event names. | ||
| * Will throw if you emit or listen for an event not in the list. | ||
| */ | ||
| constructor (validEvents?:string[]|NamespacedEvents) { | ||
| constructor (validEvents?:T|NamespacedEvents) { | ||
| this._starListeners = [] | ||
@@ -41,3 +47,3 @@ this._listeners = {} | ||
| validEvents : | ||
| Bus.flatten(validEvents)) | ||
| Bus.flatten<T>(validEvents)) | ||
| } | ||
@@ -80,10 +86,15 @@ } | ||
| /** | ||
| * Return an array of leaf nodes of an object | ||
| * Return an array of leaf nodes of an object. Use this to get an array | ||
| * of event names from a given nested object of event names. | ||
| * | ||
| * @param events Namespaced events (the return value of `Bus.createEvents`) | ||
| * @param {[string]} [existing] Previous array to concat with | ||
| * @param {string[]} [existing] Previous array to concat with | ||
| * @returns {string[]} | ||
| */ | ||
| static flatten (events:NamespacedEvents|string, existing:string[] = []):string[] { | ||
| static flatten<T extends string[]> ( | ||
| events:NamespacedEvents|string, | ||
| existing:string[] = [] | ||
| ):T { | ||
| if (typeof events === 'string') { | ||
| return existing.concat([events]) | ||
| return ((existing).concat([events as T[number]])) as T | ||
| } | ||
@@ -93,3 +104,3 @@ | ||
| return Bus.flatten(events[key], acc) | ||
| }, existing) | ||
| }, existing as T) | ||
| } | ||
@@ -99,8 +110,8 @@ | ||
| * Listen for an event | ||
| * @param {string} evName Name of the event, or '*' for all events | ||
| * @param {T} evName Name of the event, or '*' for all events | ||
| * @param listener Function to call with the event | ||
| * @returns {()=>void} function `off` -- call this to remove the listener | ||
| */ | ||
| // on (evName:ValueOf<T>, listener:Listener|StarListener):() => void { | ||
| on (evName:string, listener:Listener|StarListener):() => void { | ||
| // on (evName:string, listener:Listener|StarListener):() => void { | ||
| on (evName:ValuesOf<T>|'*', listener:Listener|StarListener):() => void { | ||
| if (evName === '*') { | ||
@@ -133,7 +144,8 @@ this._starListeners.push(listener) | ||
| /** | ||
| * Emit an event. | ||
| * @param {string} evName The event name to emit | ||
| * Emit an event, or return a function that is curried with an event name. | ||
| * | ||
| * @param {T[number]} evName The event name to emit | ||
| * @param {any?} data The data to pass to event listeners | ||
| */ | ||
| emit (evName:string, data?:any):any { | ||
| emit (evName:T[number], data?:any):any { | ||
| const self = this | ||
@@ -165,3 +177,8 @@ | ||
| _emit (arr:Listener[]|StarListener[], evName:string, data:any, isStar:boolean) { | ||
| _emit ( | ||
| arr:(Listener|StarListener)[], | ||
| evName:string, | ||
| data:any, | ||
| isStar:boolean | ||
| ):void { | ||
| if (arr.length === 0) return | ||
@@ -168,0 +185,0 @@ |
+49
-12
@@ -1,3 +0,3 @@ | ||
| import test from 'tape' | ||
| import { Bus } from '../dist/index.js' | ||
| import { test } from '@nichoth/tapzero' | ||
| import { Bus } from '../src/index.js' | ||
| let bus | ||
@@ -10,4 +10,2 @@ | ||
| t.equal(typeof bus.emit, 'function', 'should have .emit') | ||
| t.end() | ||
| }) | ||
@@ -116,10 +114,12 @@ | ||
| const bus = new Bus(events) | ||
| t.doesNotThrow(() => bus.emit('foo.bar', 'testing'), null, | ||
| 'should not throw error emitting a valid event') | ||
| bus.emit('foo.bar', 'testing') | ||
| t.ok('should not throw with a valid event name') | ||
| t.throws(() => bus.emit('bla', 'test data'), null, | ||
| 'should throw emitting a bad event name') | ||
| t.doesNotThrow(() => bus.on('foo.bar', () => null), null, | ||
| 'should not throw subscribing to a valid event name') | ||
| t.throws(() => bus.on('baloney', () => null), null, | ||
| bus.on('foo.bar', () => null) | ||
| t.ok('should not throw subscribing to a valid event name') | ||
| t.throws(() => bus.on('baloney', () => null), | ||
| 'should throw subscribing to a bad even name') | ||
@@ -129,6 +129,7 @@ }) | ||
| test('emit a null event', t => { | ||
| t.plan(1) | ||
| t.plan(2) | ||
| const bus = new Bus() | ||
| bus.on('foo', () => { | ||
| t.pass('event listener was called') | ||
| bus.on('foo', (ev) => { | ||
| t.ok(!ev, 'event listener was called') | ||
| t.equal(ev, null, 'should get null as the event') | ||
| }) | ||
@@ -149,1 +150,37 @@ bus.emit('foo', null) | ||
| }) | ||
| // type ValuesOf<T extends any[]>= T[number]; | ||
| test('event types', t => { | ||
| const eventTree = Bus.createEvents({ | ||
| a: ['b', 'c', 'd'], | ||
| b: { | ||
| _: ['e', 'f'], | ||
| c: ['1', '2', '3'] | ||
| } | ||
| }) | ||
| const flat = Bus.flatten(eventTree) | ||
| const bus2 = new Bus<typeof flat>(flat) | ||
| // const bus2 = new Bus<Array<typeof flat[number]>>(flat) | ||
| // const tester = new Bus<typeof flat>(flat) | ||
| // tester.on('qcb', () => {}) | ||
| t.throws(() => { | ||
| bus2.on('qqqqq', (data) => console.log(data)) | ||
| }, 'should throw because the event name is invalid') | ||
| const arr = ['a', 'b', 'c'] | ||
| const bus3 = new Bus<typeof arr>() | ||
| bus3.emit('aaaa', 'data') | ||
| const bus = new Bus<['a', 'b', 'c']>() | ||
| // should see TS errors in vscode | ||
| // but should not throw, because we did not pass events as an argument | ||
| bus.emit('bad event', { data: 'data' }) | ||
| bus.on('aaaaa', data => console.log(data)) | ||
| // should not see TS error here | ||
| bus.emit('a', 'test') | ||
| }) |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Copyleft License
LicenseCopyleft license information was found.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Mixed license
LicensePackage contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
LicenseA license not known to be considered permissive was found.
Found 1 instance in 1 package
Copyleft License
LicenseCopyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
LicenseA license not known to be considered permissive was found.
Found 1 instance in 1 package
248628
254.02%5876
657.22%151
17.05%3
50%2
Infinity%3
Infinity%