Skip to content

Commit c2da5fc

Browse files
author
Guillaume Chau
committed
feat(ui): improved IpcMessenger with new options
1 parent 6023c2e commit c2da5fc

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

docs/dev-guide/ui-api.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,9 +949,6 @@ const { IpcMessenger } = require('@vue/cli-shared-utils')
949949
// Create a new IpcMessenger instance
950950
const ipc = new IpcMessenger()
951951

952-
// Connect to the vue-cli IPC network
953-
ipc.connect()
954-
955952
function sendMessage (data) {
956953
// Send a message to the cli-ui server
957954
ipc.send({
@@ -978,6 +975,42 @@ function cleanup () {
978975
}
979976
```
980977

978+
Manual connection:
979+
980+
```js
981+
const ipc = new IpcMessenger({
982+
autoConnect: false
983+
})
984+
985+
// This message will be queued
986+
ipc.send({ ... })
987+
988+
ipc.connect()
989+
```
990+
991+
Auto disconnect on idle (after some time without sending any message):
992+
993+
```js
994+
const ipc = new IpcMessenger({
995+
disconnectOnIdle: true,
996+
idleTimeout: 3000 // Default
997+
})
998+
999+
ipc.send({ ... })
1000+
1001+
setTimeout(() => {
1002+
console.log(ipc.connected) // false
1003+
}, 3000)
1004+
```
1005+
1006+
Connect to another IPC network:
1007+
1008+
```js
1009+
const ipc = new IpcMessenger({
1010+
networkId: 'my-ipc-network'
1011+
})
1012+
```
1013+
9811014
In a vue-cli plugin `ui.js` file, you can use the `ipcOn`, `ipcOff` and `ipcSend` methods:
9821015

9831016
```js

packages/@vue/cli-service/lib/commands/serve.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ module.exports = (api, options) => {
209209
// Send final app URL
210210
if (args.dashboard) {
211211
const ipc = new IpcMessenger()
212-
ipc.connect()
213212
ipc.send({
214213
vueServe: {
215214
url: urls.localUrlForBrowser

packages/@vue/cli-service/lib/webpack/DashboardPlugin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class DashboardPlugin {
5252
let assetSources = new Map()
5353

5454
if (!sendData) {
55-
ipc.connect()
5655
sendData = data => ipc.send({
5756
webpackDashboardData: {
5857
type: this.type,

packages/@vue/cli-shared-utils/lib/ipc.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
const ipc = require('node-ipc')
22

3-
const defaultId = process.env.VUE_CLI_IPC || 'vue-cli'
3+
const DEFAULT_ID = process.env.VUE_CLI_IPC || 'vue-cli'
4+
const DEFAULT_IDLE_TIMEOUT = 3000
5+
const DEFAULT_OPTIONS = {
6+
networkId: DEFAULT_ID,
7+
autoConnect: true,
8+
disconnectOnIdle: false,
9+
idleTimeout: DEFAULT_IDLE_TIMEOUT
10+
}
411

512
exports.IpcMessenger = class IpcMessenger {
6-
constructor (id = defaultId) {
7-
ipc.config.id = this.id = id
13+
constructor (options = {}) {
14+
options = Object.assign({}, DEFAULT_OPTIONS, options)
15+
ipc.config.id = this.id = options.networkId
816
ipc.config.retry = 1500
917
ipc.config.silent = true
1018

1119
this.connected = false
1220
this.connecting = false
1321
this.disconnecting = false
1422
this.queue = null
23+
this.options = options
1524

1625
this.listeners = []
1726

1827
this.disconnectTimeout = 15000
28+
this.idleTimer = null
1929

2030
// Prevent forced process exit
2131
// (or else ipc messages may not be sent before kill)
@@ -29,8 +39,18 @@ exports.IpcMessenger = class IpcMessenger {
2939
send (data, type = 'message') {
3040
if (this.connected) {
3141
ipc.of[this.id].emit(type, data)
42+
43+
clearTimeout(this.idleTimer)
44+
if (this.options.disconnectOnIdle) {
45+
this.idleTimer = setTimeout(() => {
46+
this.disconnect()
47+
}, this.options.idleTimeout)
48+
}
3249
} else {
3350
this.queue.push(data)
51+
if (this.options.autoConnect && !this.connecting) {
52+
this.connect()
53+
}
3454
}
3555
}
3656

0 commit comments

Comments
 (0)