@irrelon/emitter 3.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,52 +1,56 @@
1
1
  {
2
- "author": "Rob Evans - Irrelon Software Limited",
3
- "name": "@irrelon/emitter",
4
- "description": "Simple JavaScript event emitter.",
5
- "version": "3.0.0",
6
- "keywords": [
7
- "javascript",
8
- "browser",
9
- "node",
10
- "client-side",
11
- "server-side",
12
- "emitter",
13
- "events",
14
- "on",
15
- "off",
16
- "once",
17
- "listener"
18
- ],
19
- "dependencies": {
20
- "@babel/polyfill": "^7.2.5",
21
- "@babel/runtime": "^7.3.1",
22
- "irrelon-overload": "^1.0.3"
23
- },
24
- "devDependencies": {
25
- "@babel/cli": "^7.2.3",
26
- "@babel/core": "^7.2.2",
27
- "@babel/plugin-proposal-class-properties": "^7.3.0",
28
- "@babel/plugin-proposal-object-rest-spread": "^7.3.2",
29
- "@babel/plugin-transform-runtime": "^7.2.0",
30
- "@babel/preset-env": "^7.3.1",
31
- "@babel/preset-react": "^7.0.0",
32
- "@babel/register": "^7.0.0",
33
- "mocha": "^5.2.0",
34
- "rimraf": "^2.6.2"
35
- },
36
- "engines": {
37
- "node": "*"
38
- },
39
- "scripts": {
40
- "test": "mocha ./tests/*.test.js",
41
- "build": "rimraf dist && babel ./src/*.js --out-dir dist"
42
- },
43
- "repository": {
44
- "type": "git",
45
- "url": "git://github.com/irrelon/emitter.git"
46
- },
47
- "main": "./dist/Emitter.js",
48
- "browserify": "./dist/Emitter.js",
49
- "bugs": {
50
- "url": "http://github.com/irrelon/emitter/issues"
51
- }
2
+ "author": "Rob Evans - Irrelon Software Limited",
3
+ "name": "@irrelon/emitter",
4
+ "description": "Simple JavaScript event emitter.",
5
+ "version": "5.0.0",
6
+ "keywords": [
7
+ "javascript",
8
+ "browser",
9
+ "node",
10
+ "client-side",
11
+ "server-side",
12
+ "emitter",
13
+ "events",
14
+ "on",
15
+ "off",
16
+ "once",
17
+ "listener"
18
+ ],
19
+ "devDependencies": {
20
+ "@babel/cli": "^7.8.7",
21
+ "@babel/core": "^7.8.7",
22
+ "@babel/plugin-proposal-class-properties": "^7.8.7",
23
+ "@babel/plugin-proposal-object-rest-spread": "^7.8.7",
24
+ "@babel/plugin-transform-runtime": "^7.8.7",
25
+ "@babel/preset-env": "^7.8.7",
26
+ "@babel/preset-react": "^7.8.7",
27
+ "@babel/register": "^7.8.7",
28
+ "@types/node": "^16.11.6",
29
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
30
+ "@typescript-eslint/parser": "^5.4.0",
31
+ "eslint": "^7.32.0",
32
+ "eslint-config-irrelon": "^1.0.2",
33
+ "jest": "^27.3.1",
34
+ "nodemon": "^2.0.3",
35
+ "rimraf": "^2.7.1",
36
+ "ts-node": "^10.4.0",
37
+ "typescript": "^4.4.4"
38
+ },
39
+ "engines": {
40
+ "node": "*"
41
+ },
42
+ "scripts": {
43
+ "test": "NODE_ENV=test npm run build && jest ./tests",
44
+ "testMon": "nodemon --watch src --watch tests --exec \"NODE_ENV=test BABEL_DISABLE_CACHE=1 jest ./tests\"",
45
+ "build": "rimraf dist && tsc"
46
+ },
47
+ "types": "./dist/Emitter.d.ts",
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git://github.com/irrelon/emitter.git"
51
+ },
52
+ "main": "./dist/Emitter.js",
53
+ "bugs": {
54
+ "url": "https://github.com/irrelon/emitter/issues"
55
+ }
52
56
  }
package/readme.md CHANGED
@@ -3,10 +3,10 @@
3
3
  ## Usage
4
4
 
5
5
  ### Browser
6
- Include the ./dist/index.min.js file in your HTML
6
+ Include the ./dist/Emitter.js file in your HTML
7
7
 
8
8
  ```html
9
- <script src="./dist/index.min.js"></script>
9
+ <script src="./dist/Emitter.js"></script>
10
10
  ```
11
11
 
12
12
  ### Node.js
@@ -19,25 +19,58 @@ npm install @irrelon/emitter
19
19
  Include in your application:
20
20
 
21
21
  ```js
22
- var Emitter = require('@irrelon/emitter');
22
+ import {Emitter, makeEmitter} from "@irrelon/emitter";
23
23
  ```
24
24
 
25
- ## Add Emitter Functionality to Existing Class
26
- Given an example class:
25
+ ## Create a New Standalone Emitter
26
+ ```js
27
+ const emitter = new Emitter();
27
28
 
29
+ emitter.on("foo", () => {
30
+ console.log("foo emitted!");
31
+ });
32
+
33
+ emitter.emit("foo!");
34
+ ```
35
+
36
+ ## Add Emitter Functionality to an Object
28
37
  ```js
29
- var MyClass = function () {
30
- this.emit('myEvent', myData, myOtherData);
38
+ const obj = {
39
+ someExistingProp: "hello"
31
40
  };
32
- ```
41
+ makeEmitter(obj);
33
42
 
34
- Add emitter functionality:
43
+ obj.on("someEvent", function (data) {
44
+ // do something when the event is emitted
45
+ if (data.someData === true) {
46
+ console.log(this.someExistingProp); // Will output "hello" because `this` has been applied as `obj` in context
47
+ }
48
+ });
35
49
 
50
+ obj.emit("someEvent", {someData: true});
51
+ ```
52
+
53
+ ## Add Emitter Functionality to Existing Class
36
54
  ```js
37
- Emitter(MyClass);
55
+ class MyClass {
56
+ constructor () {
57
+ console.log("I created a class instance!");
58
+ }
59
+ }
60
+
61
+ makeEmitter(MyClass.prototype); // Notice we use prototype here!
62
+
63
+ const myNewClassInstance = new MyClass();
64
+
65
+ myNewClassInstance.on("someEvent", () => {
66
+ // do something when the event is emitted
67
+ });
68
+
69
+ myNewClassInstance.emit("someEvent", {someData: true});
38
70
  ```
39
71
 
40
- Your class now inherits the emitter methods:
72
+ ## The Emitter Methods
73
+ Your object or class is given these new methods/functions:
41
74
 
42
75
  * on
43
76
  * off