Spaces:
Sleeping
Sleeping
File size: 3,183 Bytes
4cadbaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
const ipc = require('@achrinza/node-ipc')
const DEFAULT_ID = process.env.VUE_CLI_IPC || 'vue-cli'
const DEFAULT_IDLE_TIMEOUT = 3000
const DEFAULT_OPTIONS = {
networkId: DEFAULT_ID,
autoConnect: true,
disconnectOnIdle: false,
idleTimeout: DEFAULT_IDLE_TIMEOUT,
namespaceOnProject: true
}
const PROJECT_ID = process.env.VUE_CLI_PROJECT_ID
exports.IpcMessenger = class IpcMessenger {
constructor (options = {}) {
options = Object.assign({}, DEFAULT_OPTIONS, options)
ipc.config.id = this.id = options.networkId
ipc.config.retry = 1500
ipc.config.silent = true
this.connected = false
this.connecting = false
this.disconnecting = false
this.queue = null
this.options = options
this.listeners = []
this.disconnectTimeout = 15000
this.idleTimer = null
// Prevent forced process exit
// (or else ipc messages may not be sent before kill)
process.exit = code => {
process.exitCode = code
}
this._reset()
}
checkConnection () {
if (!ipc.of[this.id]) {
this.connected = false
}
}
send (data, type = 'message') {
this.checkConnection()
if (this.connected) {
if (this.options.namespaceOnProject && PROJECT_ID) {
data = {
_projectId: PROJECT_ID,
_data: data
}
}
ipc.of[this.id].emit(type, data)
clearTimeout(this.idleTimer)
if (this.options.disconnectOnIdle) {
this.idleTimer = setTimeout(() => {
this.disconnect()
}, this.options.idleTimeout)
}
} else {
this.queue.push(data)
if (this.options.autoConnect && !this.connecting) {
this.connect()
}
}
}
connect () {
this.checkConnection()
if (this.connected || this.connecting) return
this.connecting = true
this.disconnecting = false
ipc.connectTo(this.id, () => {
this.connected = true
this.connecting = false
this.queue && this.queue.forEach(data => this.send(data))
this.queue = null
ipc.of[this.id].on('message', this._onMessage)
})
}
disconnect () {
this.checkConnection()
if (!this.connected || this.disconnecting) return
this.disconnecting = true
this.connecting = false
const ipcTimer = setTimeout(() => {
this._disconnect()
}, this.disconnectTimeout)
this.send({ done: true }, 'ack')
ipc.of[this.id].on('ack', data => {
if (data.ok) {
clearTimeout(ipcTimer)
this._disconnect()
}
})
}
on (listener) {
this.listeners.push(listener)
}
off (listener) {
const index = this.listeners.indexOf(listener)
if (index !== -1) this.listeners.splice(index, 1)
}
_reset () {
this.queue = []
this.connected = false
}
_disconnect () {
this.connected = false
this.disconnecting = false
ipc.disconnect(this.id)
this._reset()
}
_onMessage (data) {
this.listeners.forEach(fn => {
if (this.options.namespaceOnProject && data._projectId) {
if (data._projectId === PROJECT_ID) {
data = data._data
} else {
return
}
}
fn(data)
})
}
}
|