@masx200/event-emitter-target
Advanced tools
Comparing version 1.1.5 to 1.1.7
@@ -1,9 +0,2 @@ | ||
type EventEmitterTarget = ReturnType<typeof createEventEmitterTarget>; | ||
interface EventEmitterTargetConstructor { | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
} | ||
declare const EventEmitterTargetClass: EventEmitterTargetConstructor; | ||
type EVENTNAME = string | symbol; | ||
type EVENTLISTENER = (event?: any) => void; | ||
declare function toprimitive(): string; | ||
declare function createEventEmitterTarget(): { | ||
@@ -27,3 +20,10 @@ [Symbol.toPrimitive]: typeof toprimitive; | ||
}; | ||
declare function toprimitive(): string; | ||
type EventEmitterTarget = ReturnType<typeof createEventEmitterTarget>; | ||
interface EventEmitterTargetConstructor { | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
} | ||
declare const EventEmitterTargetClass: EventEmitterTargetConstructor; | ||
type EVENTNAME = string | symbol; | ||
type EVENTLISTENER = (event?: any) => void; | ||
export { EventEmitterTargetClass as default, EventEmitterTarget, EventEmitterTargetConstructor, EVENTNAME, EVENTLISTENER }; |
@@ -1,2 +0,2 @@ | ||
function t(t){if("string"!=typeof t&&"symbol"!=typeof t)throw new TypeError(" EVENTNAME expected: string | symbol;but invalid :"+t)}function e(t){if("function"!=typeof t)throw new TypeError(" EVENTLISTENER expected: (event?: any) => void;but invalid:"+t)}const n="EventEmitterTarget";function o(){return{}.toString.call({[Symbol.toStringTag]:n})}export default function i(){const r=function(){const i=new Map,r=new WeakMap;function s(t){let e=i.get(t);return e||(e=new Set,i.set(t,e)),e}function c(e){t(e),i.has(e)&&s(e).clear()}function u(e,n){t(e),i.has(e)&&s(e).forEach(t=>{Promise.resolve().then(()=>{t(n)})})}function f(n,o){t(n),e(o),s(n).add(o)}function a(t,e){s(t).delete(e)}function l(n,o){t(n),e(o),a(n,o),function(t,e){const n=s(t);let o=r.get(e);o&&n.delete(o)}(n,o)}function m(){return[...i].map(([t,e])=>[t,[...e]])[Symbol.iterator]()}return{[Symbol.toPrimitive]:o,[Symbol.toStringTag]:n,[Symbol.iterator]:m,entries:m,listenerCount:function(e){return t(e),i.has(e)?s(e).size:0},clear:c,removeAllListeners:c,on:f,addListener:f,off:l,removeListener:l,once:function(n,o){t(n),e(o);let i=!1,s=r.get(o);if(!s){const t=e=>{a(n,t),a(n,o),i||(i=!0,o(e))};s=t,r.set(o,s)}a(n,o),f(n,s)},emit:u,dispatch:u,eventNames:function(){return[...i.keys()]},listeners:function(e){return t(e),i.has(e)?[...s(e)]:[]}}}();return this&&this instanceof i?(Object.assign(this,r),this):r} | ||
function t(){return{}.toString.call({[Symbol.toStringTag]:"EventEmitterTarget"})}function e(t){if("string"!=typeof t&&"symbol"!=typeof t)throw new TypeError(" EVENTNAME expected: string | symbol;but invalid :"+t)}function n(t){if("function"!=typeof t)throw new TypeError(" EVENTLISTENER expected: (event?: any) => void;but invalid:"+t)}function o(){const o=new Map,r=new WeakMap;function i(t){let e=o.get(t);return e||(e=new Set,o.set(t,e)),e}function c(t){e(t),o.has(t)&&i(t).clear()}function s(t,n){e(t),o.has(t)&&i(t).forEach(t=>{Promise.resolve().then(()=>{t(n)})})}function u(t,o){e(t),n(o),i(t).add(o)}function f(t,e){i(t).delete(e)}function a(t,o){e(t),n(o),f(t,o),function(t,e){const n=i(t);let o=r.get(e);o&&n.delete(o)}(t,o)}function l(){return[...o].map(([t,e])=>[t,[...e]])[Symbol.iterator]()}return{[Symbol.toPrimitive]:t,[Symbol.toStringTag]:"EventEmitterTarget",[Symbol.iterator]:l,entries:l,listenerCount:function(t){return e(t),o.has(t)?i(t).size:0},clear:c,removeAllListeners:c,on:u,addListener:u,off:a,removeListener:a,once:function(t,o){e(t),n(o);let i=!1,c=r.get(o);if(!c){const e=n=>{f(t,e),f(t,o),i||(i=!0,o(n))};c=e,r.set(o,c)}f(t,o),u(t,c)},emit:s,dispatch:s,eventNames:function(){return[...o.keys()]},listeners:function(t){return e(t),o.has(t)?[...i(t)]:[]}}}const r=(t=>{var e=Symbol(),n=new Function("return async()=>{}")()();function r(){const t=o();return this&&this instanceof r?(Object.assign(this,t),this):Reflect.construct(r,[])}return Reflect.set(r,e,n),r})();export default r; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"version": "1.1.5", | ||
"version": "1.1.7", | ||
"typings": "./dist/index.d.ts", | ||
@@ -7,11 +7,14 @@ "types": "./dist/index.d.ts", | ||
"devDependencies": { | ||
"@babel/plugin-proposal-optional-catch-binding": "7.8.3", | ||
"@babel/core": "^7.8.4", | ||
"@babel/core": "^7.9.0", | ||
"@babel/plugin-proposal-optional-catch-binding": "^7.8.3", | ||
"@babel/preset-env": "^7.9.0", | ||
"@rollup/plugin-commonjs": "^11.0.2", | ||
"@rollup/plugin-json": "^4.0.2", | ||
"@rollup/plugin-node-resolve": "^7.1.1", | ||
"@types/node": "^13.7.0", | ||
"prettier": "^1.19.1", | ||
"rollup": "^1.31.0", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"@types/node": "^13.9.8", | ||
"acorn": "^7.1.1", | ||
"minimist": "^1.2.5", | ||
"prettier": "^2.0.2", | ||
"rollup": "^2.3.1", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
@@ -21,8 +24,7 @@ "rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-sourcemaps": "^0.5.0", | ||
"rollup-plugin-terser": "^5.2.0", | ||
"rollup-plugin-ts": "^1.2.12", | ||
"terser": "^4.6.3", | ||
"tslib": "^1.10.0", | ||
"@babel/preset-env": "^7.8.4", | ||
"typescript": "^3.8.0-dev.20200204" | ||
"rollup-plugin-terser": "^5.3.0", | ||
"rollup-plugin-ts": "^1.2.23", | ||
"terser": "^4.6.9", | ||
"tslib": "^1.11.1", | ||
"typescript": "^3.8.3" | ||
}, | ||
@@ -34,3 +36,3 @@ "type": "module", | ||
"scripts": { | ||
"prettier": "prettier *.js --write&&prettier *.json --write && prettier *.md --write&&prettier --parser=typescript --write ./src/**/*.ts", | ||
"prettier": "prettier *.js --write *.json *.md ./**/*.ts", | ||
"watch": "rollup -c rollup.config.js --watch", | ||
@@ -51,2 +53,2 @@ "build": "rollup -c rollup.config.js" | ||
"dependencies": {} | ||
} | ||
} |
@@ -50,3 +50,3 @@ # event-emitter-target | ||
emitter.on("event", () => { | ||
console.log("an event occurred!"); | ||
console.log("an event occurred!"); | ||
}); | ||
@@ -62,4 +62,4 @@ emitter.emit("event"); | ||
interface EventEmitterTargetConstructor { | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
} | ||
@@ -71,19 +71,19 @@ type EventEmitterTarget = ReturnType<typeof createEventEmitterTarget>; | ||
declare function createEventEmitterTarget(): { | ||
[Symbol.toPrimitive]: typeof toprimitive; | ||
[Symbol.toStringTag]: string; | ||
[Symbol.iterator]: () => IterableIterator<[EVENTNAME, EVENTLISTENER[]]>; | ||
entries: () => IterableIterator<[EVENTNAME, EVENTLISTENER[]]>; | ||
[Symbol.toPrimitive]: typeof toprimitive; | ||
[Symbol.toStringTag]: string; | ||
[Symbol.iterator]: () => IterableIterator<[EVENTNAME, EVENTLISTENER[]]>; | ||
entries: () => IterableIterator<[EVENTNAME, EVENTLISTENER[]]>; | ||
listenerCount: (name: EVENTNAME) => number; | ||
clear: (name: EVENTNAME) => void; | ||
removeAllListeners: (name: EVENTNAME) => void; | ||
on: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
addListener: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
off: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
removeListener: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
once: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
emit: (name: EVENTNAME, event?: any) => void; | ||
dispatch: (name: EVENTNAME, event?: any) => void; | ||
eventNames: () => EVENTNAME[]; | ||
listeners: (name: EVENTNAME) => EVENTLISTENER[]; | ||
listenerCount: (name: EVENTNAME) => number; | ||
clear: (name: EVENTNAME) => void; | ||
removeAllListeners: (name: EVENTNAME) => void; | ||
on: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
addListener: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
off: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
removeListener: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
once: (name: EVENTNAME, callback: EVENTLISTENER) => void; | ||
emit: (name: EVENTNAME, event?: any) => void; | ||
dispatch: (name: EVENTNAME, event?: any) => void; | ||
eventNames: () => EVENTNAME[]; | ||
listeners: (name: EVENTNAME) => EVENTLISTENER[]; | ||
}; | ||
@@ -90,0 +90,0 @@ declare function toprimitive(): string; |
import babel from "rollup-plugin-babel"; | ||
import sourcemaps from "rollup-plugin-sourcemaps"; | ||
// import sourcemaps from "rollup-plugin-sourcemaps"; | ||
import resolve from "@rollup/plugin-node-resolve"; | ||
@@ -9,74 +9,74 @@ import commonjs from "@rollup/plugin-commonjs"; | ||
const mybabelplugin = babel({ | ||
sourcemap: true, | ||
// extensions: [".jsx", ".ts", ".js", ".tsx"], | ||
plugins: ["@babel/plugin-proposal-optional-catch-binding"], | ||
presets: [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
targets: [ | ||
"last 1 edge version", | ||
"last 1 safari version", | ||
"last 1 chrome version", | ||
"last 1 firefox version" | ||
] | ||
} | ||
] | ||
] | ||
sourcemap: true, | ||
// extensions: [".jsx", ".ts", ".js", ".tsx"], | ||
plugins: ["@babel/plugin-proposal-optional-catch-binding"], | ||
presets: [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
targets: [ | ||
"last 1 edge version", | ||
"last 1 safari version", | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
], | ||
}, | ||
], | ||
], | ||
}); | ||
const myterserplugin = terser({ | ||
sourcemap: true, | ||
toplevel: true, | ||
output: { | ||
comments: !1, | ||
ascii_only: !0 | ||
}, | ||
compress: { | ||
sourcemap: true, | ||
toplevel: true, | ||
unused: true, | ||
drop_console: true, | ||
drop_debugger: true, | ||
pure_funcs: ["console.log"] | ||
}, | ||
mangle: { properties: false } | ||
output: { | ||
comments: !1, | ||
ascii_only: !0, | ||
}, | ||
compress: { | ||
toplevel: true, | ||
unused: true, | ||
drop_console: true, | ||
drop_debugger: true, | ||
pure_funcs: ["console.log"], | ||
}, | ||
mangle: { properties: false }, | ||
}); | ||
export default [ | ||
{ | ||
input: "./src/index.ts", | ||
output: [ | ||
{ | ||
file: "./dist/index.js", | ||
format: "esm", | ||
sourcemap: true | ||
} | ||
], | ||
plugins: [ | ||
sourcemaps(), | ||
json(), | ||
resolve(), | ||
commonjs(), | ||
typescript({}), | ||
// mybabelplugin, | ||
myterserplugin | ||
] | ||
}, | ||
{ | ||
input: "./dist/index.js", | ||
output: [ | ||
{ | ||
file: "./dist/index.js", | ||
format: "esm", | ||
sourcemap: true | ||
} | ||
], | ||
plugins: [ | ||
sourcemaps(), | ||
json(), | ||
resolve(), | ||
commonjs(), | ||
// typescript({}), | ||
mybabelplugin, | ||
myterserplugin | ||
] | ||
} | ||
{ | ||
input: "./src/index.ts", | ||
output: [ | ||
{ | ||
file: "./dist/index.js", | ||
format: "esm", | ||
sourcemap: true, | ||
}, | ||
], | ||
plugins: [ | ||
// sourcemaps(), | ||
json(), | ||
resolve(), | ||
commonjs(), | ||
typescript({}), | ||
// mybabelplugin, | ||
myterserplugin, | ||
], | ||
}, | ||
{ | ||
input: "./dist/index.js", | ||
output: [ | ||
{ | ||
file: "./dist/index.js", | ||
format: "esm", | ||
sourcemap: true, | ||
}, | ||
], | ||
plugins: [ | ||
// sourcemaps(), | ||
json(), | ||
resolve(), | ||
commonjs(), | ||
// typescript({}), | ||
mybabelplugin, | ||
myterserplugin, | ||
], | ||
}, | ||
]; |
180
src/index.ts
@@ -0,167 +1,27 @@ | ||
import { createEventEmitterTarget } from "./createEventEmitterTarget"; | ||
export type EventEmitterTarget = ReturnType<typeof createEventEmitterTarget>; | ||
export interface EventEmitterTargetConstructor { | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
new (): EventEmitterTarget; | ||
(): EventEmitterTarget; | ||
} | ||
const EventEmitterTargetClass: EventEmitterTargetConstructor = function EventEmitterTargetClass( | ||
this: any | ||
): EventEmitterTarget { | ||
const eventemittertarget = createEventEmitterTarget(); | ||
if (this && this instanceof EventEmitterTargetClass) { | ||
Object.assign(this, eventemittertarget); | ||
return this as EventEmitterTarget; | ||
} else { | ||
return eventemittertarget; | ||
//return Reflect.construct(EventEmitterTargetClass, []) as EventEmitterTarget; | ||
} | ||
} as EventEmitterTargetConstructor; | ||
export type EVENTNAME = string | symbol; | ||
export type EVENTLISTENER = (event?: any) => void; | ||
export default EventEmitterTargetClass; | ||
const EventEmitterTargetClass: EventEmitterTargetConstructor = ((noop) => { | ||
var a = noop(); | ||
var b = new Function("return async()=>{}")()(); | ||
function createEventEmitterTarget() { | ||
const 监听器回调映射 = new Map<EVENTNAME, Set<EVENTLISTENER>>(); | ||
const 源回调到一次包装 = new WeakMap<EVENTLISTENER, EVENTLISTENER>(); | ||
function 获取监听器集合(name: EVENTNAME): Set<EVENTLISTENER> { | ||
let 监听器集合: Set<EVENTLISTENER> | undefined = 监听器回调映射.get(name); | ||
if (!监听器集合) { | ||
监听器集合 = new Set(); | ||
监听器回调映射.set(name, 监听器集合); | ||
} | ||
return 监听器集合; | ||
} | ||
function clear(name: EVENTNAME) { | ||
assertEVENTNAME(name); | ||
if (监听器回调映射.has(name)) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
监听器集合.clear(); | ||
} | ||
} | ||
function emit(name: EVENTNAME, event?: any) { | ||
assertEVENTNAME(name); | ||
if (监听器回调映射.has(name)) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
监听器集合.forEach(listener => { | ||
Promise.resolve().then(() => { | ||
listener(event); | ||
}); | ||
}); | ||
} | ||
} | ||
function once(name: EVENTNAME, callback: EVENTLISTENER) { | ||
assertEVENTNAME(name); | ||
assertEVENTLISTENER(callback); | ||
let fired = false; | ||
let wrapped = 源回调到一次包装.get(callback); | ||
if (!wrapped) { | ||
const 一次包装 = (event?: any) => { | ||
offraw(name, 一次包装); | ||
offraw(name, callback); | ||
if (!fired) { | ||
fired = true; | ||
callback(event); | ||
function EventEmitterTargetClass(this: any): EventEmitterTarget { | ||
const eventemittertarget = createEventEmitterTarget(); | ||
if (this && this instanceof EventEmitterTargetClass) { | ||
Object.assign(this, eventemittertarget); | ||
return this as EventEmitterTarget; | ||
} else { | ||
return Reflect.construct(EventEmitterTargetClass, []); | ||
} | ||
}; | ||
wrapped = 一次包装; | ||
源回调到一次包装.set(callback, wrapped); | ||
} | ||
offraw(name, callback); | ||
on(name, wrapped); | ||
} | ||
function on(name: EVENTNAME, callback: EVENTLISTENER) { | ||
assertEVENTNAME(name); | ||
assertEVENTLISTENER(callback); | ||
const 监听器集合 = 获取监听器集合(name); | ||
监听器集合.add(callback); | ||
} | ||
function offraw(name: EVENTNAME, callback: EVENTLISTENER) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
监听器集合.delete(callback); | ||
} | ||
function offwrap(name: EVENTNAME, callback: EVENTLISTENER) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
let 一次包装 = 源回调到一次包装.get(callback); | ||
if (一次包装) { | ||
监听器集合.delete(一次包装); | ||
} | ||
} | ||
function off(name: EVENTNAME, callback: EVENTLISTENER) { | ||
assertEVENTNAME(name); | ||
assertEVENTLISTENER(callback); | ||
Reflect.set(EventEmitterTargetClass, a, b); | ||
offraw(name, callback); | ||
offwrap(name, callback); | ||
} | ||
function eventNames(): EVENTNAME[] { | ||
return [...监听器回调映射.keys()]; | ||
} | ||
function listeners(name: EVENTNAME): EVENTLISTENER[] { | ||
assertEVENTNAME(name); | ||
if (监听器回调映射.has(name)) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
return [...监听器集合]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
function listenerCount(name: EVENTNAME): number { | ||
assertEVENTNAME(name); | ||
if (监听器回调映射.has(name)) { | ||
const 监听器集合 = 获取监听器集合(name); | ||
return 监听器集合.size; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
function iterator(): IterableIterator<[EVENTNAME, EVENTLISTENER[]]> { | ||
let resultarr: Array<[EVENTNAME, EVENTLISTENER[]]> = [ | ||
...监听器回调映射 | ||
].map(([key, value]) => { | ||
return [key, [...value]] as [EVENTNAME, EVENTLISTENER[]]; | ||
}); | ||
return resultarr[Symbol.iterator](); | ||
} | ||
const eventtarget = { | ||
[Symbol.toPrimitive]: toprimitive, | ||
[Symbol.toStringTag]: toStringTag, | ||
[Symbol.iterator]: iterator, | ||
entries: iterator, | ||
listenerCount, | ||
clear, | ||
removeAllListeners: clear, | ||
on, | ||
addListener: on, | ||
off, | ||
removeListener: off, | ||
once, | ||
emit, | ||
dispatch: emit, | ||
eventNames, | ||
listeners | ||
}; | ||
return eventtarget; | ||
} | ||
function assertEVENTNAME(name: any): asserts name is EVENTNAME { | ||
if ("string" !== typeof name && "symbol" !== typeof name) { | ||
throw new TypeError( | ||
" EVENTNAME expected: string | symbol;but invalid :" + name | ||
); | ||
} | ||
} | ||
function assertEVENTLISTENER(callback: any): asserts callback is EVENTLISTENER { | ||
if ("function" !== typeof callback) { | ||
throw new TypeError( | ||
" EVENTLISTENER expected: (event?: any) => void;but invalid:" + callback | ||
); | ||
} | ||
} | ||
const toStringTag = "EventEmitterTarget"; | ||
function toprimitive(): string { | ||
return {}.toString.call({ [Symbol.toStringTag]: toStringTag }); | ||
} | ||
return EventEmitterTargetClass as EventEmitterTargetConstructor; | ||
})(Symbol); | ||
export type EVENTNAME = string | symbol; | ||
export type EVENTLISTENER = (event?: any) => void; | ||
export default EventEmitterTargetClass; |
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"sourceMap": true, | ||
"moduleResolution": "node", | ||
"pretty": true, | ||
"resolveJsonModule": true, | ||
"declaration": true, | ||
"noUnusedLocals": true, | ||
"checkJs": true, | ||
"importHelpers": true, | ||
"removeComments": true, | ||
"allowJs": true, | ||
"target": "es2019", | ||
"module": "esnext", | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true | ||
} | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"sourceMap": true, | ||
"moduleResolution": "node", | ||
"pretty": true, | ||
"resolveJsonModule": true, | ||
"declaration": true, | ||
"noUnusedLocals": true, | ||
"checkJs": false, | ||
"importHelpers": true, | ||
"removeComments": true, | ||
"allowJs": true, | ||
"target": "es2019", | ||
"module": "esnext", | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
56374
16
315
21
1