@live-change/dao-message
Advanced tools
Comparing version 0.5.15 to 0.5.16
28
index.js
@@ -11,2 +11,6 @@ const rd = require("@live-change/dao") | ||
this.target = settings.target | ||
this.messageChannel = settings.messageChannel ? new MessageChannel() : null | ||
this.port = this.messageChannel ? this.messageChannel.port1 : this.target | ||
this.decorator = settings.decorator || ((message) => message) | ||
this.filter = settings.filter || ((message) => true) | ||
this.initialize() | ||
@@ -16,5 +20,7 @@ } | ||
initialize() { | ||
this.target.addEventListener('message', (event) => { | ||
this.port.addEventListener('message', (event) => { | ||
debug("MSG IN:", event.data) | ||
this.handleMessage(JSON.parse(event.data)) | ||
const data = JSON.parse(event.data) | ||
if(!this.filter(data)) return | ||
this.handleMessage(data) | ||
}) | ||
@@ -24,3 +30,3 @@ this.target.addEventListener('error', (event) => { | ||
}) | ||
this.target.addEventListener('messageerror', (event) => { | ||
this.port.addEventListener('messageerror', (event) => { | ||
console.error("Worker", this.url, "message error", event) | ||
@@ -31,9 +37,14 @@ }) | ||
}) | ||
if(this.messageChannel) { | ||
this.port.start() | ||
this.target.postMessage({ type: 'connection', port: this.messageChannel.port2 }, [this.messageChannel.port2]) | ||
} | ||
this.handleConnect() | ||
console.log("MESSAGE CONNECTED", this.url) | ||
debug("MESSAGE CONNECTED", this.url) | ||
} | ||
send(message) { | ||
message = this.decorator(message) | ||
debug("MSG OUT:", message) | ||
this.target.postMessage(JSON.stringify(message)) | ||
this.port.postMessage(JSON.stringify(message)) | ||
} | ||
@@ -46,3 +57,8 @@ | ||
dispose() { | ||
if(this.target.terminate) this.target.terminate() | ||
if(this.messageChannel) { | ||
this.port.postMessage(JSON.stringify({ type: 'close' })) | ||
this.port.close() | ||
} else { | ||
if (this.target.terminate) this.target.terminate() | ||
} | ||
} | ||
@@ -49,0 +65,0 @@ |
{ | ||
"name": "@live-change/dao-message", | ||
"version": "0.5.15", | ||
"version": "0.5.16", | ||
"author": { | ||
@@ -13,3 +13,3 @@ "email": "m8@em8.pl", | ||
"dependencies": { | ||
"@live-change/dao": "^0.5.15", | ||
"@live-change/dao": "^0.5.16", | ||
"debug": "^4.3.4", | ||
@@ -45,3 +45,3 @@ "supports-color": "^7.2.0" | ||
}, | ||
"gitHead": "e68c06bf9175ca4f7794856f671def7d260f8930" | ||
"gitHead": "85e1b0cb9e16b320c38224b926df3e74015e920d" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4700
59
Updated@live-change/dao@^0.5.16