@irrelon/emitter 5.0.0 → 5.0.2
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/.eslintignore +1 -3
- package/.eslintrc.json +35 -18
- package/.prettierignore +9 -0
- package/.prettierrc.yml +9 -0
- package/fixPaths.cjs +183 -0
- package/package.json +31 -24
- package/readme.md +16 -45
- package/src/Emitter.d.ts +190 -0
- package/src/Emitter.js +580 -0
- package/src/Emitter.ts +294 -247
- package/tests/Emitter.test.d.ts +1 -0
- package/tests/Emitter.test.js +267 -309
- package/tests/Emitter.test.ts +335 -0
- package/tsconfig.json +33 -12
- package/tsconfig.tsbuildinfo +1 -0
- package/.babelrc +0 -11
- package/dist/Emitter.d.ts +0 -205
- package/dist/Emitter.js +0 -415
package/.eslintignore
CHANGED
package/.eslintrc.json
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
1
|
{
|
|
2
|
-
"parser": "@typescript-eslint/parser",
|
|
3
|
-
"plugins": ["@typescript-eslint"],
|
|
4
2
|
"extends": [
|
|
5
|
-
"
|
|
6
|
-
"eslint-config-irrelon",
|
|
7
|
-
"plugin:@typescript-eslint/eslint-recommended",
|
|
3
|
+
"prettier",
|
|
8
4
|
"plugin:@typescript-eslint/recommended"
|
|
9
5
|
],
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"browser": true,
|
|
17
|
-
"node": true,
|
|
18
|
-
"es6": true
|
|
19
|
-
},
|
|
6
|
+
"plugins": [
|
|
7
|
+
"prettier",
|
|
8
|
+
"@typescript-eslint",
|
|
9
|
+
"unused-imports",
|
|
10
|
+
"tree-shaking"
|
|
11
|
+
],
|
|
20
12
|
"rules": {
|
|
21
|
-
"
|
|
22
|
-
"@typescript-eslint/
|
|
13
|
+
"@typescript-eslint/no-unnecessary-type-assertion": "off",
|
|
14
|
+
"@typescript-eslint/consistent-type-definitions": "off",
|
|
15
|
+
"@typescript-eslint/no-empty-interface": "off",
|
|
16
|
+
"@typescript-eslint/no-empty-arrow-function": "off",
|
|
17
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
18
|
+
"@typescript-eslint/no-inferrable-types": "off",
|
|
19
|
+
"@typescript-eslint/strict-boolean-expressions": "off",
|
|
20
|
+
"@typescript-eslint/ban-ts-comment": "off",
|
|
21
|
+
"@typescript-eslint/no-this-alias": "off",
|
|
22
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
23
|
+
"@typescript-eslint/no-shadow": "error",
|
|
24
|
+
"unused-imports/no-unused-imports": "warn",
|
|
25
|
+
"indent": ["error", "tab"],
|
|
26
|
+
"space-before-function-paren": [
|
|
27
|
+
"error",
|
|
28
|
+
"always"
|
|
29
|
+
],
|
|
30
|
+
"no-shadow": "off",
|
|
31
|
+
"complexity": [
|
|
32
|
+
"warn",
|
|
33
|
+
20
|
|
34
|
+
],
|
|
35
|
+
"max-depth": [
|
|
36
|
+
"warn",
|
|
37
|
+
4
|
|
38
|
+
],
|
|
39
|
+
"prefer-rest-params": "off"
|
|
23
40
|
}
|
|
24
|
-
}
|
|
41
|
+
}
|
package/.prettierignore
ADDED
package/.prettierrc.yml
ADDED
package/fixPaths.cjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A very basic search and replace function to find import and export statements
|
|
3
|
+
* in .js files where the path of the file being specified does not have a .js
|
|
4
|
+
* extension and replace those paths with one that has a .js extension.
|
|
5
|
+
*
|
|
6
|
+
* This would be much better written using abstract source trees to do the
|
|
7
|
+
* re-writing which would be "safer", however this is a fully working function
|
|
8
|
+
* and doesn't seem to mess anything up at present.
|
|
9
|
+
*/
|
|
10
|
+
const chokidar = require("chokidar");
|
|
11
|
+
const fs = require("fs");
|
|
12
|
+
const { readFile, writeFile, readdir } = require("fs").promises;
|
|
13
|
+
const { resolve } = require("path");
|
|
14
|
+
const { minimatch } = require("minimatch");
|
|
15
|
+
|
|
16
|
+
const matchPattern = ["**/*.js", "**/*.jsx"];
|
|
17
|
+
const excludePattern = ["node_modules", "react", ".git"];
|
|
18
|
+
|
|
19
|
+
const expressions = [{
|
|
20
|
+
exp: /import(.*)?["']([.]+)(((?!\.js).)*?)["'];/g,
|
|
21
|
+
update: (match, isIndex) => {
|
|
22
|
+
if (isIndex) {
|
|
23
|
+
return `import${match[1]}"${match[2]}${match[3]}/index.js";`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return `import${match[1]}"${match[2]}${match[3]}.js";`;
|
|
27
|
+
}
|
|
28
|
+
}, {
|
|
29
|
+
exp: /export\s(.*)?\sfrom\s["']([.]+)(((?!\.js).)*?)["'];/g,
|
|
30
|
+
update: (match, isIndex) => {
|
|
31
|
+
if (isIndex) {
|
|
32
|
+
return `export ${match[1]} from "${match[2]}${match[3]}/index.js";`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return `export ${match[1]} from "${match[2]}${match[3]}.js";`;
|
|
36
|
+
}
|
|
37
|
+
}];
|
|
38
|
+
|
|
39
|
+
let tsPaths = {};
|
|
40
|
+
let tsResolvedPaths = {};
|
|
41
|
+
|
|
42
|
+
const readTsConfig = () => {
|
|
43
|
+
const config = require("./tsconfig.json");
|
|
44
|
+
tsPaths = config.compilerOptions.paths;
|
|
45
|
+
|
|
46
|
+
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
|
|
47
|
+
tsResolvedPaths[key] = replacementArr.map((dir) => {
|
|
48
|
+
return resolve(__dirname, dir);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
async function getFiles (dir, gitIgnoreArr) {
|
|
54
|
+
const dirArr = await readdir(dir, { withFileTypes: true });
|
|
55
|
+
|
|
56
|
+
const finalList = dirArr.filter((filePath) => {
|
|
57
|
+
return excludePattern.every((glob) => !minimatch(filePath.name, glob));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const files = await Promise.all(finalList.map((tmpDir) => {
|
|
61
|
+
const res = resolve(dir, tmpDir.name);
|
|
62
|
+
return tmpDir.isDirectory() ? getFiles(res) : [res];
|
|
63
|
+
})).then((results) => {
|
|
64
|
+
return results.reduce((arr, item) => {
|
|
65
|
+
arr.push(...item);
|
|
66
|
+
return arr;
|
|
67
|
+
}, []);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const finalFiles = files.filter((file) => {
|
|
71
|
+
if (!file || !file.trim()) return false;
|
|
72
|
+
return matchPattern.some((glob) => minimatch(file, glob));
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return Array.prototype.concat(...finalFiles);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const processMatches = (regExp, fileFolder, content, updateFunc) => {
|
|
79
|
+
let match;
|
|
80
|
+
while (match = regExp.exec(content)) {
|
|
81
|
+
const pathToItemFile = resolve(fileFolder, match[2] + match[3] + ".js");
|
|
82
|
+
const pathToItemFolderIndexFile = resolve(fileFolder, match[2] + match[3] + "/index.js");
|
|
83
|
+
let pathStatFile;
|
|
84
|
+
let pathStatFolderIndexFile;
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
pathStatFile = fs.lstatSync(pathToItemFile);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
pathStatFolderIndexFile = undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
pathStatFolderIndexFile = fs.lstatSync(pathToItemFolderIndexFile);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
pathStatFolderIndexFile = undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (pathStatFile) {
|
|
99
|
+
// Just add .js to the end of the path
|
|
100
|
+
content = content.replace(match[0], updateFunc(match, false));
|
|
101
|
+
} else if (pathStatFolderIndexFile) {
|
|
102
|
+
content = content.replace(match[0], updateFunc(match, true));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return content;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const processFile = (file) => {
|
|
110
|
+
if (!file) return;
|
|
111
|
+
|
|
112
|
+
//console.log(`Processing ${file}...`);
|
|
113
|
+
|
|
114
|
+
readFile(file).then((fileContentsBuffer) => {
|
|
115
|
+
const fileFolderArr = file.split("/");
|
|
116
|
+
fileFolderArr.pop();
|
|
117
|
+
|
|
118
|
+
const fileFolder = fileFolderArr.join("/");
|
|
119
|
+
|
|
120
|
+
const fileContent = fileContentsBuffer.toString();
|
|
121
|
+
let updatedContent = fileContent;
|
|
122
|
+
|
|
123
|
+
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
|
|
124
|
+
const find = key.replace("*", "");
|
|
125
|
+
const replace = replacementArr[0].replace("*", "");
|
|
126
|
+
|
|
127
|
+
const relativeSteps = new Array(file.replace(__dirname, "").split("/").length - 2).fill("../").join("");
|
|
128
|
+
const replacementPath = relativeSteps + replace;
|
|
129
|
+
|
|
130
|
+
updatedContent = updatedContent.replaceAll(find, replacementPath);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expressions.forEach((expression) => {
|
|
134
|
+
updatedContent = processMatches(expression.exp, fileFolder, updatedContent, expression.update);
|
|
135
|
+
expression.exp.lastIndex = 0;
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
if (fileContent === updatedContent) {
|
|
139
|
+
//console.log(`No change ${file}`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
writeFile(file, updatedContent).then(() => {
|
|
144
|
+
console.log(`Updated ${file}`);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const runSearchReplace = () => {
|
|
150
|
+
// Scan all folders
|
|
151
|
+
readFile(resolve(__dirname, ".gitignore")).then((result) => {
|
|
152
|
+
return result.toString().split("\n").filter((line) => {
|
|
153
|
+
return !line.startsWith("#");
|
|
154
|
+
}).filter((line) => {
|
|
155
|
+
return Boolean(line.trim());
|
|
156
|
+
});
|
|
157
|
+
}).then((gitIgnoreArr) => {
|
|
158
|
+
return getFiles(__dirname, gitIgnoreArr);
|
|
159
|
+
}).then((files) => {
|
|
160
|
+
// Now scan the files for matching regular expressions
|
|
161
|
+
// of imports without extensions
|
|
162
|
+
files.forEach((file) => {
|
|
163
|
+
processFile(file);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
console.log(`Processed ${files.length} files`);
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
readTsConfig();
|
|
171
|
+
runSearchReplace();
|
|
172
|
+
|
|
173
|
+
const watcher = chokidar.watch(__dirname, { persistent: true });
|
|
174
|
+
|
|
175
|
+
watcher.on("change", (filename) => {
|
|
176
|
+
if (filename.indexOf("tsconfig.json") > -1) {
|
|
177
|
+
console.log("Updating tsconfig.json data");
|
|
178
|
+
readTsConfig();
|
|
179
|
+
}
|
|
180
|
+
if (!matchPattern.some((glob) => minimatch(filename, glob))) return;
|
|
181
|
+
//console.log("Changed", filename);
|
|
182
|
+
processFile(filename);
|
|
183
|
+
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"author": "Rob Evans - Irrelon Software Limited",
|
|
3
3
|
"name": "@irrelon/emitter",
|
|
4
4
|
"description": "Simple JavaScript event emitter.",
|
|
5
|
-
"version": "5.0.
|
|
5
|
+
"version": "5.0.2",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"keywords": [
|
|
7
8
|
"javascript",
|
|
8
9
|
"browser",
|
|
@@ -17,39 +18,45 @@
|
|
|
17
18
|
"listener"
|
|
18
19
|
],
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"@babel/
|
|
21
|
-
"@babel/
|
|
22
|
-
"@
|
|
23
|
-
"@
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"eslint": "^
|
|
32
|
-
"eslint-
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
21
|
+
"@babel/polyfill": "^7.2.5",
|
|
22
|
+
"@babel/runtime": "^7.3.1",
|
|
23
|
+
"@types/jest": "^29.5.0",
|
|
24
|
+
"@types/node": "^18.7.23",
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
|
26
|
+
"@typescript-eslint/parser": "^5.38.1",
|
|
27
|
+
"chokidar": "^3.5.3",
|
|
28
|
+
"eslint": "8.22.0",
|
|
29
|
+
"eslint-config-prettier": "^8.5.0",
|
|
30
|
+
"eslint-config-standard-with-typescript": "^23.0.0",
|
|
31
|
+
"eslint-plugin-import": "^2.26.0",
|
|
32
|
+
"eslint-plugin-jest": "^27.0.4",
|
|
33
|
+
"eslint-plugin-node": "^11.1.0",
|
|
34
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
35
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
36
|
+
"eslint-plugin-standard": "^5.0.0",
|
|
37
|
+
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
38
|
+
"eslint-plugin-unused-imports": "^2.0.0",
|
|
39
|
+
"jest": "^29.1.1",
|
|
40
|
+
"minimatch": "^7.4.2",
|
|
41
|
+
"prettier": "^2.7.1",
|
|
42
|
+
"ts-jest": "^29.0.2",
|
|
43
|
+
"typescript": "^4.8.4"
|
|
38
44
|
},
|
|
39
45
|
"engines": {
|
|
40
46
|
"node": "*"
|
|
41
47
|
},
|
|
42
48
|
"scripts": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
49
|
+
"build": "tsc",
|
|
50
|
+
"build-watch": "tsc --watch",
|
|
51
|
+
"fixPaths": "node fixPaths.cjs",
|
|
52
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --testMatch \"**/*.test.js\"",
|
|
53
|
+
"test-watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --testMatch \"**/*.test.js\" --watchAll"
|
|
46
54
|
},
|
|
47
|
-
"types": "./dist/Emitter.d.ts",
|
|
48
55
|
"repository": {
|
|
49
56
|
"type": "git",
|
|
50
57
|
"url": "git://github.com/irrelon/emitter.git"
|
|
51
58
|
},
|
|
52
|
-
"main": "
|
|
59
|
+
"main": "src/Emitter.js",
|
|
53
60
|
"bugs": {
|
|
54
61
|
"url": "https://github.com/irrelon/emitter/issues"
|
|
55
62
|
}
|
package/readme.md
CHANGED
|
@@ -3,74 +3,45 @@
|
|
|
3
3
|
## Usage
|
|
4
4
|
|
|
5
5
|
### Browser
|
|
6
|
-
Include the ./dist/
|
|
6
|
+
Include the ./dist/index.min.js file in your HTML
|
|
7
7
|
|
|
8
8
|
```html
|
|
9
|
-
<script src="./dist/
|
|
9
|
+
<script src="./dist/index.min.js"></script>
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
### Node.js
|
|
13
|
-
Install via NPM:
|
|
13
|
+
Install via NPM / Yarn:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
npm install @irrelon/emitter
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
```bash
|
|
20
|
+
yarn add @irrelon/emitter
|
|
21
|
+
```
|
|
22
|
+
|
|
19
23
|
Include in your application:
|
|
20
24
|
|
|
21
25
|
```js
|
|
22
|
-
|
|
26
|
+
var Emitter = require('@irrelon/emitter');
|
|
23
27
|
```
|
|
24
28
|
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
const emitter = new Emitter();
|
|
28
|
-
|
|
29
|
-
emitter.on("foo", () => {
|
|
30
|
-
console.log("foo emitted!");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
emitter.emit("foo!");
|
|
34
|
-
```
|
|
29
|
+
## Add Emitter Functionality to Existing Class
|
|
30
|
+
Given an example class:
|
|
35
31
|
|
|
36
|
-
## Add Emitter Functionality to an Object
|
|
37
32
|
```js
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
var MyClass = function () {
|
|
34
|
+
this.emit('myEvent', myData, myOtherData);
|
|
40
35
|
};
|
|
41
|
-
makeEmitter(obj);
|
|
42
|
-
|
|
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
|
-
});
|
|
49
|
-
|
|
50
|
-
obj.emit("someEvent", {someData: true});
|
|
51
36
|
```
|
|
52
37
|
|
|
53
|
-
|
|
54
|
-
```js
|
|
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();
|
|
38
|
+
Add emitter functionality:
|
|
64
39
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
myNewClassInstance.emit("someEvent", {someData: true});
|
|
40
|
+
```js
|
|
41
|
+
Emitter(MyClass);
|
|
70
42
|
```
|
|
71
43
|
|
|
72
|
-
|
|
73
|
-
Your object or class is given these new methods/functions:
|
|
44
|
+
Your class now inherits the emitter methods:
|
|
74
45
|
|
|
75
46
|
* on
|
|
76
47
|
* off
|
package/src/Emitter.d.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
export declare enum EventReturnFlag {
|
|
2
|
+
none = 0,
|
|
3
|
+
cancel = 1
|
|
4
|
+
}
|
|
5
|
+
export declare type EventListenerCallback = (...args: any[]) => any | Promise<any>;
|
|
6
|
+
export interface EventStaticEmitterObject {
|
|
7
|
+
id: string;
|
|
8
|
+
args: any[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new class with the capability to emit events.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Emitter {
|
|
14
|
+
_eventsEmitting: boolean;
|
|
15
|
+
_eventRemovalQueue: any[];
|
|
16
|
+
_eventListeners?: Record<string, Record<string, EventListenerCallback[]>>;
|
|
17
|
+
_eventStaticEmitters: Record<string, EventStaticEmitterObject[]>;
|
|
18
|
+
_eventsAllowDefer: boolean;
|
|
19
|
+
_eventsDeferTimeouts: Record<any, number>;
|
|
20
|
+
/**
|
|
21
|
+
* Attach an event listener to the passed event only if the passed
|
|
22
|
+
* id matches the id for the event being fired.
|
|
23
|
+
* @param {string} eventName The name of the event to listen for.
|
|
24
|
+
* @param {string} id The id to match against.
|
|
25
|
+
* @param {function} listener The method to call when the event is fired.
|
|
26
|
+
* @returns {Emitter} The emitter instance.
|
|
27
|
+
*/
|
|
28
|
+
_on(eventName: string, id: string, listener: EventListenerCallback): this;
|
|
29
|
+
/**
|
|
30
|
+
* Attach an event listener to the passed event only if the passed
|
|
31
|
+
* id matches the document id for the event being fired.
|
|
32
|
+
* @param {String} eventName The name of the event to listen for.
|
|
33
|
+
* @param {*} id The id to match against.
|
|
34
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
35
|
+
* @returns {Emitter} The emitter instance.
|
|
36
|
+
*/
|
|
37
|
+
_once(eventName: string, id: string, listener: EventListenerCallback): this;
|
|
38
|
+
/**
|
|
39
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
40
|
+
* @param {String} eventName The event to cancel listener for.
|
|
41
|
+
* @param {String} id The ID of the event to cancel listening for.
|
|
42
|
+
* @param {Function} listener The event listener function used in the on()
|
|
43
|
+
* or once() call to cancel.
|
|
44
|
+
* @returns {Emitter} The emitter instance.
|
|
45
|
+
*/
|
|
46
|
+
_off(eventName: string, id: string, listener?: EventListenerCallback): this;
|
|
47
|
+
/**
|
|
48
|
+
* Attach an event listener to the passed event only if the passed
|
|
49
|
+
* id matches the document id for the event being fired.
|
|
50
|
+
* @param {String} eventName The name of the event to listen for.
|
|
51
|
+
* @param {*} id The id to match against.
|
|
52
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
53
|
+
* @returns {Emitter} The emitter instance.
|
|
54
|
+
*/
|
|
55
|
+
on(eventName: string, id: string, listener: EventListenerCallback): this;
|
|
56
|
+
on(eventName: string, listener: EventListenerCallback): this;
|
|
57
|
+
/**
|
|
58
|
+
* Attach an event listener to the passed event only if the passed
|
|
59
|
+
* id matches the document id for the event being fired.
|
|
60
|
+
* @param {String} eventName The name of the event to listen for.
|
|
61
|
+
* @param id The id to match against.
|
|
62
|
+
* @param listener
|
|
63
|
+
* @returns {Emitter} The emitter instance.
|
|
64
|
+
*/
|
|
65
|
+
once(eventName: string, id: string, listener: EventListenerCallback): this;
|
|
66
|
+
once(eventName: string, listener: EventListenerCallback): this;
|
|
67
|
+
/**
|
|
68
|
+
* Overwrites any previous event listeners so that if the event fires, only
|
|
69
|
+
* the listener you pass will be called. If you pass an id along with the
|
|
70
|
+
* listener, only listeners for the id will be overwritten. This is similar
|
|
71
|
+
* to calling `.off(eventName)` then `on(eventName, listener)` since
|
|
72
|
+
* `off(eventName)` will cancel any previous event listeners for the passed
|
|
73
|
+
* event name.
|
|
74
|
+
* @param {String} eventName The name of the event to listen for.
|
|
75
|
+
* @param {*} id The id to match against.
|
|
76
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
77
|
+
* @returns {Emitter} The emitter instance.
|
|
78
|
+
*/
|
|
79
|
+
overwrite(eventName: string, id: string, listener: EventListenerCallback): this;
|
|
80
|
+
overwrite(eventName: string, listener: EventListenerCallback): this;
|
|
81
|
+
/**
|
|
82
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
83
|
+
* @param {String} eventName The event to cancel listener for.
|
|
84
|
+
* @param {String} id The ID of the event to cancel listening for.
|
|
85
|
+
* @param {Function} listener The event listener function used in the on()
|
|
86
|
+
* or once() call to cancel.
|
|
87
|
+
* @returns {Emitter} The emitter instance.
|
|
88
|
+
*/
|
|
89
|
+
off(eventName: string, id: string, listener?: EventListenerCallback): this;
|
|
90
|
+
off(eventName: string, listener?: EventListenerCallback): this;
|
|
91
|
+
off(eventName: string): this;
|
|
92
|
+
/**
|
|
93
|
+
* Emit an event by name.
|
|
94
|
+
* @param {Object} eventName The name of the event to emit.
|
|
95
|
+
* @param {...any} data The arguments to send to any listening methods.
|
|
96
|
+
* If you are sending multiple arguments, separate them with a comma so
|
|
97
|
+
* that they are received by the function as separate arguments.
|
|
98
|
+
* @return {number}
|
|
99
|
+
* @example #Emit an Event
|
|
100
|
+
* // Emit the event named "hello"
|
|
101
|
+
* myEntity.emit('hello');
|
|
102
|
+
* @example #Emit an Event With Data Object
|
|
103
|
+
* // Emit the event named "hello"
|
|
104
|
+
* myEntity.emit('hello', {moo: true});
|
|
105
|
+
* @example #Emit an Event With Multiple Data Values
|
|
106
|
+
* // Emit the event named "hello"
|
|
107
|
+
* myEntity.emit('hello', {moo: true}, 'someString');
|
|
108
|
+
* @example #Listen for Event Data
|
|
109
|
+
* // Set a listener to listen for the data (multiple values emitted
|
|
110
|
+
* // from an event are passed as function arguments)
|
|
111
|
+
* myEntity.on('hello', function (arg1, arg2) {
|
|
112
|
+
* console.log(arg1, arg2);
|
|
113
|
+
* }
|
|
114
|
+
*
|
|
115
|
+
* // Emit the event named "hello"
|
|
116
|
+
* myEntity.emit('hello', 'data1', 'data2');
|
|
117
|
+
*
|
|
118
|
+
* // The console output is:
|
|
119
|
+
* // data1, data2
|
|
120
|
+
*/
|
|
121
|
+
emit(eventName: string, ...data: any[]): Promise<EventReturnFlag>;
|
|
122
|
+
emitId(eventName: string, id: string, ...data: any[]): EventReturnFlag;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
125
|
+
* one is added for this event after the emitStatic() call has been
|
|
126
|
+
* made.
|
|
127
|
+
* @param {String} eventName The name of the event to emit.
|
|
128
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
129
|
+
* @returns {Emitter} The emitter instance.
|
|
130
|
+
* @private
|
|
131
|
+
*/
|
|
132
|
+
emitStatic(eventName: string, ...data: any[]): this;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
135
|
+
* one is added for this event after the emitStatic() call has been
|
|
136
|
+
* made.
|
|
137
|
+
* @param {String} eventName The name of the event to emit.
|
|
138
|
+
* @param {String} id The id of the event to emit.
|
|
139
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
140
|
+
* @returns {Emitter} The emitter instance.
|
|
141
|
+
* @private
|
|
142
|
+
*/
|
|
143
|
+
emitStaticId(eventName: string, id: string, ...data: any[]): this;
|
|
144
|
+
/**
|
|
145
|
+
* Handles removing emitters, is an internal method not called directly.
|
|
146
|
+
* @param {String} eventName The event to remove static emitter for.
|
|
147
|
+
* @returns {Emitter} The emitter instance.
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
150
|
+
cancelStatic(eventName: string): this;
|
|
151
|
+
/**
|
|
152
|
+
* Checks if an event has any event listeners or not.
|
|
153
|
+
* @param {String} eventName The name of the event to check for.
|
|
154
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
155
|
+
* the event. False if none are found.
|
|
156
|
+
*/
|
|
157
|
+
willEmit(eventName: string): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Checks if an event has any event listeners or not based on the passed id.
|
|
160
|
+
* @param {String} eventName The name of the event to check for.
|
|
161
|
+
* @param {String} id The event ID to check for.
|
|
162
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
163
|
+
* the event. False if none are found.
|
|
164
|
+
*/
|
|
165
|
+
willEmitId(eventName: string, id: string): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
168
|
+
* events of the same type that occur within 100 milliseconds of a previous
|
|
169
|
+
* one will all be wrapped into a single emit rather than emitting tons of
|
|
170
|
+
* events for lots of chained inserts etc. Only the data from the last
|
|
171
|
+
* de-bounced event will be emitted.
|
|
172
|
+
* @param {String} eventName The name of the event to emit.
|
|
173
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
174
|
+
* @returns {Emitter} The emitter instance.
|
|
175
|
+
*/
|
|
176
|
+
deferEmit(eventName: string, ...data: any[]): this;
|
|
177
|
+
/**
|
|
178
|
+
* If events are cleared with the off() method while the event emitter is
|
|
179
|
+
* actively processing any events then the off() calls get added to a
|
|
180
|
+
* queue to be executed after the event emitter is finished. This stops
|
|
181
|
+
* errors that might occur by potentially modifying the event queue while
|
|
182
|
+
* the emitter is running through them. This method is called after the
|
|
183
|
+
* event emitter is finished processing.
|
|
184
|
+
* @private
|
|
185
|
+
*/
|
|
186
|
+
_processRemovalQueue(): void;
|
|
187
|
+
}
|
|
188
|
+
export declare function makeEmitter(obj: new (...args: any[]) => any, prototypeMode: boolean): Emitter;
|
|
189
|
+
export declare function makeEmitter(obj: boolean): Emitter;
|
|
190
|
+
export declare function makeEmitter(obj: Record<string, unknown>, prototypeMode: boolean): Emitter;
|