document-process-manager
Advanced tools
Comparing version 1.1.6 to 1.1.7
import { URL } from 'node:url' | ||
import { Buffer } from 'node:buffer' | ||
import WebSocket, { WebSocketServer } from 'ws' | ||
import { WebSocketServer } from 'ws' | ||
import MessageAdapter from './messageAdapter/index.js' | ||
@@ -9,11 +9,6 @@ export default class Socket extends EventTarget { | ||
#webSocketServer | ||
#webSocket | ||
#_webSocket | ||
#active = false | ||
#messageAdapters | ||
#url | ||
#_boundConnection | ||
#boundOpen | ||
#boundClose | ||
#boundError | ||
#boundMessage | ||
constructor($settings, $sockets) { | ||
@@ -24,10 +19,5 @@ super() | ||
this.active = this.#settings.active | ||
this.#boundOpen = this.#open.bind(this) | ||
this.#boundClose = this.#close.bind(this) | ||
this.#boundError = this.#error.bind(this) | ||
this.#boundMessage = this.#message.bind(this) | ||
} | ||
get active() { return this.#active } | ||
set active($active) { | ||
if(this.#active === $active) { return } | ||
if($active === true) { | ||
@@ -37,3 +27,3 @@ this.webSocketServer | ||
else if($active === false) { | ||
this.webSocketServer.close() | ||
this.webSocketServer?.close() | ||
this.#webSocketServer = undefined | ||
@@ -65,26 +55,25 @@ this.#webSocket = undefined | ||
}) | ||
this.#webSocketServer.on('connection', this.#boundConnection) | ||
this.#webSocketServer.on('connection', this.#websocketServerConnection.bind(this)) | ||
this.#webSocketServer.on('close', this.#websocketServerClose.bind(this)) | ||
this.#webSocketServer.on('error', this.#websocketServerError.bind(this)) | ||
return this.#webSocketServer | ||
} | ||
get webSocket() { return this.#webSocket } | ||
set webSocket($webSocket) { | ||
if(this.#webSocket !== undefined) return | ||
this.#webSocket = $webSocket | ||
this.#webSocket.on('open', this.#boundOpen) | ||
this.#webSocket.on('close', this.#boundClose) | ||
this.#webSocket.on('error', this.#boundError) | ||
this.#webSocket.on('message', this.#boundMessage) | ||
get #webSocket() { return this.#_webSocket } | ||
set #webSocket($webSocket) { | ||
if($webSocket === undefined) { this.#_webSocket = undefined } | ||
else { | ||
this.#_webSocket = $webSocket | ||
this.#_webSocket.on('message', this.#websocketMessage.bind(this)) | ||
this.#_webSocket.on('error', this.#websocketError.bind(this)) | ||
this.#_webSocket.on('open', this.#websocketOpen.bind(this)) | ||
this.#_webSocket.on('close', this.#websocketClose.bind(this)) | ||
} | ||
} | ||
get #boundConnection() { | ||
if(this.#_boundConnection !== undefined) { return this.#_boundConnection } | ||
this.#_boundConnection = this.#connection.bind(this) | ||
return this.#_boundConnection | ||
} | ||
#connection($ws) { | ||
this.webSocket = $ws | ||
} | ||
#open($event) { console.log("#open", $event) } | ||
#close($event) { console.log("#close", $event) } | ||
#error() { console.error("#error", ...arguments) } | ||
#message($data, $isBinary) { | ||
#websocketServerConnection($ws) { this.#webSocket = $ws } | ||
#websocketServerClose() { this.#webSocket = undefined } | ||
#websocketServerError($error) { console.error($error) } | ||
#websocketOpen() {} | ||
#websocketClose() {} | ||
#websocketError($error) { console.error($error) } | ||
#websocketMessage($data, $isBinary) { | ||
iterateAdapters: | ||
@@ -96,3 +85,5 @@ for(const [ | ||
const message = $messageAdapter.message($data, $isBinary) | ||
return message(this.webSocket, $data, $isBinary) | ||
const { type, detail } = message(this.#webSocket, $data, $isBinary) | ||
const messageEvent = new CustomEvent(type, { detail }) | ||
this.dispatchEvent(messageEvent) | ||
} | ||
@@ -106,3 +97,5 @@ catch($err) { /* console.log($err) */ } | ||
for(const [$adapterName, $adapter] of this.#settings.messageAdapters) { | ||
const adapter = new MessageAdapter($adapter, this) | ||
let adapter | ||
if($adapter instanceof MessageAdapter) { adapter = adapter } | ||
else { adapter = new MessageAdapter($adapter, this) } | ||
messageAdapters.push([$adapterName, adapter]) | ||
@@ -109,0 +102,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Document Process Manager (DPM)", | ||
"version": "1.1.6", | ||
"version": "1.1.7", | ||
"license": "ISC", | ||
@@ -8,0 +8,0 @@ "type": "module", |
66700
1550