Skip to content
Permalink
Browse files

Update: Export as commonjs

  • Loading branch information
jacoborus committed Feb 3, 2020
1 parent b7a3c31 commit c20a9b02356863657cc23135304c1e3ae04114d5
Showing with 53 additions and 63 deletions.
  1. +46 −56 lib/weak-emitter.js
  2. +3 −3 package.json
  3. +4 −4 tsconfig.json
@@ -1,57 +1,47 @@
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function weakEmitter() {
const events = new WeakMap();
const newEvent = (key) => {
const handlers = new Map();
events.set(key, handlers);
return handlers;
};
return {
on(key, handler) {
(events.get(key) || newEvent(key))
.set(handler, handler);
},
once(key, handler) {
const handlers = events.get(key) || newEvent(key);
handlers.set(handler, function () {
handlers.delete(handler);
handler(...arguments);
});
},
emit(key, ...args) {
const handlers = events.get(key);
handlers && handlers.forEach((handler) => handler(...args));
},
clear(key) {
events.delete(key);
},
off(key, handler) {
const handlers = events.get(key);
if (!handlers)
return;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function weakEmitter() {
const events = new WeakMap();
const newEvent = (key) => {
const handlers = new Map();
events.set(key, handlers);
return handlers;
};
return {
on(key, handler) {
(events.get(key) || newEvent(key))
.set(handler, handler);
},
once(key, handler) {
const handlers = events.get(key) || newEvent(key);
handlers.set(handler, function () {
handlers.delete(handler);
},
transfer(origin, destination) {
const handlers = events.get(origin);
if (!handlers)
return;
const event = (events.get(destination) || newEvent(destination));
handlers.forEach((handler) => {
event.set(handler, handler);
});
events.delete(origin);
}
};
}
exports.weakEmitter = weakEmitter;
});
handler(...arguments);
});
},
emit(key, ...args) {
const handlers = events.get(key);
handlers && handlers.forEach((handler) => handler(...args));
},
clear(key) {
events.delete(key);
},
off(key, handler) {
const handlers = events.get(key);
if (!handlers)
return;
handlers.delete(handler);
},
transfer(origin, destination) {
const handlers = events.get(origin);
if (!handlers)
return;
const event = (events.get(destination) || newEvent(destination));
handlers.forEach((handler) => {
event.set(handler, handler);
});
events.delete(origin);
}
};
}
exports.weakEmitter = weakEmitter;
@@ -1,9 +1,9 @@
{
"name": "weak-emitter",
"version": "4.1.2",
"version": "4.2.0",
"description": "ES6 WeakMap based event emitter (~350 bytes)",
"main": "./lib/weak-emitter.js",
"types": "./lib/weak-emitter.d.ts",
"main": "lib/weak-emitter.js",
"types": "lib/weak-emitter.d.ts",
"scripts": {
"linter": "eslint src/*.js",
"test": "tape -r ts-node/register/transpile-only src/tests.ts",
@@ -2,8 +2,8 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "umd", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"target": "ESNEXT", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
@@ -12,8 +12,8 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */

0 comments on commit c20a9b0

Please sign in to comment.
You can’t perform that action at this time.