Merge pull request #970 from vmarchesin/fix-memory-leak-issue-952
[FIX] Resolve #952 - memory leak in createEventHub
This commit is contained in:
@ -965,8 +965,8 @@
|
|||||||
"fileName": "createEventHub.md",
|
"fileName": "createEventHub.md",
|
||||||
"text": "Creates a pub/sub ([publish–subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)) event hub with `emit`, `on`, and `off` methods.\n\nUse `Object.create(null)` to create an empty `hub` object that does not inherit properties from `Object.prototype`.\nFor `emit`, resolve the array of handlers based on the `event` argument and then run each one with `Array.prototype.forEach()` by passing in the data as an argument.\nFor `on`, create an array for the event if it does not yet exist, then use `Array.prototype.push()` to add the handler\nto the array.\nFor `off`, use `Array.prototype.findIndex()` to find the index of the handler in the event array and remove it using `Array.prototype.splice()`.",
|
"text": "Creates a pub/sub ([publish–subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)) event hub with `emit`, `on`, and `off` methods.\n\nUse `Object.create(null)` to create an empty `hub` object that does not inherit properties from `Object.prototype`.\nFor `emit`, resolve the array of handlers based on the `event` argument and then run each one with `Array.prototype.forEach()` by passing in the data as an argument.\nFor `on`, create an array for the event if it does not yet exist, then use `Array.prototype.push()` to add the handler\nto the array.\nFor `off`, use `Array.prototype.findIndex()` to find the index of the handler in the event array and remove it using `Array.prototype.splice()`.",
|
||||||
"codeBlocks": {
|
"codeBlocks": {
|
||||||
"es6": "const createEventHub = () => ({\n hub: Object.create(null),\n emit(event, data) {\n (this.hub[event] || []).forEach(handler => handler(data));\n },\n on(event, handler) {\n if (!this.hub[event]) this.hub[event] = [];\n this.hub[event].push(handler);\n },\n off(event, handler) {\n const i = (this.hub[event] || []).findIndex(h => h === handler);\n if (i > -1) this.hub[event].splice(i, 1);\n }\n});",
|
"es6": "const createEventHub = () => ({\n hub: Object.create(null),\n emit(event, data) {\n (this.hub[event] || []).forEach(handler => handler(data));\n },\n on(event, handler) {\n if (!this.hub[event]) this.hub[event] = [];\n this.hub[event].push(handler);\n },\n off(event, handler) {\n const i = (this.hub[event] || []).findIndex(h => h === handler);\n if (i > -1) this.hub[event].splice(i, 1);\n if (this.hub[event].length === 0) delete this.hub[event]\n }\n});",
|
||||||
"es5": "var createEventHub = function createEventHub() {\n return {\n hub: Object.create(null),\n emit: function emit(event, data) {\n (this.hub[event] || []).forEach(function (handler) {\n return handler(data);\n });\n },\n on: function on(event, handler) {\n if (!this.hub[event]) this.hub[event] = [];\n this.hub[event].push(handler);\n },\n off: function off(event, handler) {\n var i = (this.hub[event] || []).findIndex(function (h) {\n return h === handler;\n });\n if (i > -1) this.hub[event].splice(i, 1);\n }\n };\n};",
|
"es5": "var createEventHub = function createEventHub() {\n return {\n hub: Object.create(null),\n emit: function emit(event, data) {\n (this.hub[event] || []).forEach(function (handler) {\n return handler(data);\n });\n },\n on: function on(event, handler) {\n if (!this.hub[event]) this.hub[event] = [];\n this.hub[event].push(handler);\n },\n off: function off(event, handler) {\n var i = (this.hub[event] || []).findIndex(function (h) {\n return h === handler;\n });\n if (i > -1) this.hub[event].splice(i, 1);\n if (this.hub[event].length === 0) delete this.hub[event]\n }\n };\n};",
|
||||||
"example": "const handler = data => console.log(data);\nconst hub = createEventHub();\nlet increment = 0;\n\n// Subscribe: listen for different types of events\nhub.on('message', handler);\nhub.on('message', () => console.log('Message event fired'));\nhub.on('increment', () => increment++);\n\n// Publish: emit events to invoke all handlers subscribed to them, passing the data to them as an argument\nhub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired'\nhub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired'\nhub.emit('increment'); // `increment` variable is now 1\n\n// Unsubscribe: stop a specific handler from listening to the 'message' event\nhub.off('message', handler);"
|
"example": "const handler = data => console.log(data);\nconst hub = createEventHub();\nlet increment = 0;\n\n// Subscribe: listen for different types of events\nhub.on('message', handler);\nhub.on('message', () => console.log('Message event fired'));\nhub.on('increment', () => increment++);\n\n// Publish: emit events to invoke all handlers subscribed to them, passing the data to them as an argument\nhub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired'\nhub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired'\nhub.emit('increment'); // `increment` variable is now 1\n\n// Unsubscribe: stop a specific handler from listening to the 'message' event\nhub.off('message', handler);"
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
|
|||||||
@ -21,6 +21,7 @@ const createEventHub = () => ({
|
|||||||
off(event, handler) {
|
off(event, handler) {
|
||||||
const i = (this.hub[event] || []).findIndex(h => h === handler);
|
const i = (this.hub[event] || []).findIndex(h => h === handler);
|
||||||
if (i > -1) this.hub[event].splice(i, 1);
|
if (i > -1) this.hub[event].splice(i, 1);
|
||||||
|
if (this.hub[event].length === 0) delete this.hub[event];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
@ -423,6 +423,7 @@
|
|||||||
" off(event, handler) {",
|
" off(event, handler) {",
|
||||||
" const i = (this.hub[event] || []).findIndex(h => h === handler);",
|
" const i = (this.hub[event] || []).findIndex(h => h === handler);",
|
||||||
" if (i > -1) this.hub[event].splice(i, 1);",
|
" if (i > -1) this.hub[event].splice(i, 1);",
|
||||||
|
" if (this.hub[event].length === 0) delete this.hub[event];",
|
||||||
" }",
|
" }",
|
||||||
"});"
|
"});"
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user