@live-change/dao-message 0.5.14 → 0.5.16
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/index.js +22 -6
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -8,30 +8,41 @@ class MessageConnection extends Connection {
|
|
|
8
8
|
super(credentials, settings)
|
|
9
9
|
this.url = url
|
|
10
10
|
this.target = settings.target
|
|
11
|
+
this.messageChannel = settings.messageChannel ? new MessageChannel() : null
|
|
12
|
+
this.port = this.messageChannel ? this.messageChannel.port1 : this.target
|
|
13
|
+
this.decorator = settings.decorator || ((message) => message)
|
|
14
|
+
this.filter = settings.filter || ((message) => true)
|
|
11
15
|
this.initialize()
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
initialize() {
|
|
15
|
-
this.
|
|
19
|
+
this.port.addEventListener('message', (event) => {
|
|
16
20
|
debug("MSG IN:", event.data)
|
|
17
|
-
|
|
21
|
+
const data = JSON.parse(event.data)
|
|
22
|
+
if(!this.filter(data)) return
|
|
23
|
+
this.handleMessage(data)
|
|
18
24
|
})
|
|
19
25
|
this.target.addEventListener('error', (event) => {
|
|
20
26
|
console.error("Worker", this.url, "error", event)
|
|
21
27
|
})
|
|
22
|
-
this.
|
|
28
|
+
this.port.addEventListener('messageerror', (event) => {
|
|
23
29
|
console.error("Worker", this.url, "message error", event)
|
|
24
30
|
})
|
|
25
31
|
this.target.addEventListener('unhandledrejection', (event) => {
|
|
26
32
|
console.error("Worker", this.url, "unhandled rejection", event)
|
|
27
33
|
})
|
|
34
|
+
if(this.messageChannel) {
|
|
35
|
+
this.port.start()
|
|
36
|
+
this.target.postMessage({ type: 'connection', port: this.messageChannel.port2 }, [this.messageChannel.port2])
|
|
37
|
+
}
|
|
28
38
|
this.handleConnect()
|
|
29
|
-
|
|
39
|
+
debug("MESSAGE CONNECTED", this.url)
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
send(message) {
|
|
43
|
+
message = this.decorator(message)
|
|
33
44
|
debug("MSG OUT:", message)
|
|
34
|
-
this.
|
|
45
|
+
this.port.postMessage(JSON.stringify(message))
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
reconnect() {
|
|
@@ -39,7 +50,12 @@ class MessageConnection extends Connection {
|
|
|
39
50
|
}
|
|
40
51
|
|
|
41
52
|
dispose() {
|
|
42
|
-
if(this.
|
|
53
|
+
if(this.messageChannel) {
|
|
54
|
+
this.port.postMessage(JSON.stringify({ type: 'close' }))
|
|
55
|
+
this.port.close()
|
|
56
|
+
} else {
|
|
57
|
+
if (this.target.terminate) this.target.terminate()
|
|
58
|
+
}
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
closeConnection() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/dao-message",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.16",
|
|
4
4
|
"author": {
|
|
5
5
|
"email": "m8@em8.pl",
|
|
6
6
|
"name": "Michał Łaszczewski",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "https://github.com/live-change/live-change-dao/issues"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@live-change/dao": "^0.5.
|
|
13
|
+
"@live-change/dao": "^0.5.16",
|
|
14
14
|
"debug": "^4.3.4",
|
|
15
15
|
"supports-color": "^7.2.0"
|
|
16
16
|
},
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"scripts": {
|
|
42
42
|
"compileTests": "webpack test/*.js tests-bundle.js"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "85e1b0cb9e16b320c38224b926df3e74015e920d"
|
|
45
45
|
}
|