fix(plugin api): IPC now namspaced per project by default, closes #2189

This commit is contained in:
Guillaume Chau
2018-08-23 00:57:47 +02:00
parent 8dd0b11747
commit f2614103a1
5 changed files with 43 additions and 10 deletions
+21 -2
View File
@@ -6,9 +6,12 @@ const DEFAULT_OPTIONS = {
networkId: DEFAULT_ID,
autoConnect: true,
disconnectOnIdle: false,
idleTimeout: DEFAULT_IDLE_TIMEOUT
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)
@@ -45,6 +48,13 @@ exports.IpcMessenger = class IpcMessenger {
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)
@@ -118,6 +128,15 @@ exports.IpcMessenger = class IpcMessenger {
}
_onMessage (data) {
this.listeners.forEach(fn => fn(data))
this.listeners.forEach(fn => {
if (this.options.namespaceOnProject && data._projectId) {
if (data._projectId === PROJECT_ID) {
data = data._data
} else {
return
}
}
fn(data)
})
}
}
@@ -318,8 +318,19 @@ class PluginApi {
* @param {function} cb Callback with 'data' param
*/
ipcOn (cb) {
this.ipcHandlers.push(cb)
return ipc.on(cb)
const handler = cb._handler = ({ data, emit }) => {
if (data._projectId) {
if (data._projectId === this.project.id) {
data = data._data
} else {
return
}
}
// eslint-disable-next-line standard/no-callback-literal
cb({ data, emit })
}
this.ipcHandlers.push(handler)
return ipc.on(handler)
}
/**
@@ -328,9 +339,11 @@ class PluginApi {
* @param {any} cb Callback to be removed
*/
ipcOff (cb) {
const index = this.ipcHandlers.indexOf(cb)
const handler = cb._handler
if (!handler) return
const index = this.ipcHandlers.indexOf(handler)
if (index !== -1) this.ipcHandlers.splice(index, 1)
ipc.off(cb)
ipc.off(handler)
}
/**
@@ -32,7 +32,7 @@ function set ({ id, projectId, value }, context) {
})
const watchers = notify({ id, projectId, value }, context)
log('SharedData set', id, value, `(${watchers.length} watchers)`)
log('SharedData set', id, projectId, value, `(${watchers.length} watchers)`)
return { id, value }
}
@@ -47,7 +47,7 @@ function remove ({ id, projectId }, context) {
})
notify({ id, projectId, value: undefined }, context)
log('SharedData remove', id)
log('SharedData remove', id, projectId)
}
function watch ({ id, projectId }, handler) {
@@ -292,8 +292,9 @@ async function run (id, context) {
task.time = Date.now()
// Task env
process.env.VUE_CLI_CONTEXT = cwd.get()
process.env.VUE_CLI_PROJECT_ID = projects.getCurrent(context).id
const nodeEnv = process.env.NODE_ENV
delete process.env.NODE_ENV
+1 -1
View File
@@ -71,7 +71,7 @@ module.exports = api => {
if (data.type === 'stats') {
// Stats are read from a file
const statsFile = path.resolve(process.cwd(), `./node_modules/.stats-${type}.json`)
const statsFile = path.resolve(api.getCwd(), `./node_modules/.stats-${type}.json`)
const value = await fs.readJson(statsFile)
setSharedData(id, value)
await fs.remove(statsFile)