Skip to content

Commit d24c7bb

Browse files
author
Guillaume Chau
committed
refactor(plugin api): scope shared data per-project
1 parent ca7ad70 commit d24c7bb

File tree

16 files changed

+134
-70
lines changed

16 files changed

+134
-70
lines changed

packages/@vue/cli-ui/apollo-server/api/PluginApi.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class PluginApi {
442442
* @returns {any} Shared data value
443443
*/
444444
getSharedData (id) {
445-
return sharedData.get(id, this.context)
445+
return sharedData.get({ id, projectId: this.project.id }, this.context)
446446
}
447447

448448
/**
@@ -452,7 +452,7 @@ class PluginApi {
452452
* @param {any} value Value of the Shared data
453453
*/
454454
setSharedData (id, value) {
455-
sharedData.set({ id, value }, this.context)
455+
sharedData.set({ id, projectId: this.project.id, value }, this.context)
456456
}
457457

458458
/**
@@ -461,7 +461,7 @@ class PluginApi {
461461
* @param {string} id Id of the Shared data
462462
*/
463463
removeSharedData (id) {
464-
sharedData.remove(id, this.context)
464+
sharedData.remove({ id, projectId: this.project.id }, this.context)
465465
}
466466

467467
/**
@@ -471,7 +471,7 @@ class PluginApi {
471471
* @param {function} handler Callback
472472
*/
473473
watchSharedData (id, handler) {
474-
sharedData.watch(id, handler)
474+
sharedData.watch({ id, projectId: this.project.id }, handler)
475475
}
476476

477477
/**
@@ -481,7 +481,7 @@ class PluginApi {
481481
* @param {function} handler Callback
482482
*/
483483
unwatchSharedData (id, handler) {
484-
sharedData.unwatch(id, handler)
484+
sharedData.unwatch({ id, projectId: this.project.id }, handler)
485485
}
486486

487487
/**

packages/@vue/cli-ui/apollo-server/connectors/plugins.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ function resetPluginApi ({ file, lightApi }, context) {
133133

134134
// Clean up
135135
if (pluginApi) {
136-
projectId = pluginApi.project && pluginApi.project.id
136+
projectId = pluginApi.project.id
137137
pluginApi.views.forEach(r => views.remove(r.id, context))
138138
pluginApi.ipcHandlers.forEach(fn => ipc.off(fn))
139139
}
140140
if (!lightApi) {
141-
sharedData.unWatchAll(context)
141+
if (projectId) sharedData.unWatchAll({ projectId }, context)
142142
clientAddons.clear(context)
143143
suggestions.clear(context)
144144
}
@@ -147,6 +147,12 @@ function resetPluginApi ({ file, lightApi }, context) {
147147
setTimeout(() => {
148148
const projects = require('./projects')
149149
const project = projects.findByPath(file, context)
150+
151+
if (!project) {
152+
resolve(false)
153+
return
154+
}
155+
150156
const plugins = getPlugins(file)
151157

152158
if (project && projects.getType(project, context) !== 'vue') {
@@ -180,7 +186,7 @@ function resetPluginApi ({ file, lightApi }, context) {
180186
// Add views
181187
pluginApi.views.forEach(view => views.add(view, context))
182188

183-
if (!project || lightApi) {
189+
if (lightApi) {
184190
resolve(true)
185191
return
186192
}

packages/@vue/cli-ui/apollo-server/connectors/shared-data.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const { log } = require('../util/logger')
66
const sharedData = new Map()
77
let watchers = new Map()
88

9-
function get (id, context) {
10-
const value = sharedData.get(id)
9+
function get ({ id, projectId }, context) {
10+
const store = sharedData.get(projectId)
11+
if (!store) return null
1112

13+
const value = store.get(id)
1214
if (typeof value === 'undefined') return null
1315

1416
return {
@@ -17,48 +19,71 @@ function get (id, context) {
1719
}
1820
}
1921

20-
function set ({ id, value }, context) {
21-
sharedData.set(id, value)
22+
function set ({ id, projectId, value }, context) {
23+
let store = sharedData.get(projectId)
24+
if (!store) {
25+
store = new Map()
26+
sharedData.set(projectId, store)
27+
}
28+
store.set(id, value)
29+
2230
context.pubsub.publish(channels.SHARED_DATA_UPDATED, {
23-
sharedDataUpdated: { id, value }
31+
sharedDataUpdated: { id, projectId, value }
2432
})
25-
const watchers = notify(id, value)
33+
34+
const watchers = notify({ id, projectId, value }, context)
2635
log('SharedData set', id, value, `(${watchers.length} watchers)`)
2736
return { id, value }
2837
}
2938

30-
function remove (id, context) {
31-
sharedData.delete(id)
39+
function remove ({ id, projectId }, context) {
40+
const store = sharedData.get(projectId)
41+
if (store) {
42+
store.delete(id)
43+
}
44+
3245
context.pubsub.publish(channels.SHARED_DATA_UPDATED, {
33-
sharedDataUpdated: { id, value: undefined }
46+
sharedDataUpdated: { id, projectId, value: undefined }
3447
})
35-
notify(id, undefined)
48+
49+
notify({ id, projectId, value: undefined }, context)
3650
log('SharedData remove', id)
3751
}
3852

39-
function watch (id, handler) {
40-
let handlers = watchers.get(id)
53+
function watch ({ id, projectId }, handler) {
54+
let store = watchers.get(projectId)
55+
if (!store) {
56+
store = new Map()
57+
watchers.set(projectId, store)
58+
}
59+
let handlers = store.get(id)
4160
if (!handlers) {
4261
handlers = []
43-
watchers.set(id, handlers)
62+
store.set(id, handlers)
4463
}
4564
handlers.push(handler)
4665
}
4766

48-
function unwatch (id, handler) {
49-
const handlers = watchers.get(id)
50-
if (handlers) {
51-
const index = handlers.indexOf(handler)
52-
if (index !== -1) handlers.splice(index, 1)
53-
}
67+
function unwatch ({ id, projectId }, handler) {
68+
const store = watchers.get(projectId)
69+
if (!store) return
70+
71+
const handlers = store.get(id)
72+
if (!handlers) return
73+
74+
const index = handlers.indexOf(handler)
75+
if (index !== -1) handlers.splice(index, 1)
5476
}
5577

56-
function unWatchAll (context) {
57-
watchers = new Map()
78+
function unWatchAll ({ projectId }, context) {
79+
watchers.delete(projectId)
5880
}
5981

60-
function notify (id, value) {
61-
const handlers = watchers.get(id)
82+
function notify ({ id, projectId, value }, context) {
83+
let handlers = watchers.get(projectId)
84+
if (handlers) {
85+
handlers = handlers.get(id)
86+
}
6287
if (handlers) {
6388
handlers.forEach(fn => fn(value, id))
6489
}

packages/@vue/cli-ui/apollo-server/resolvers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const resolvers = [{
3434
cwd: () => cwd.get(),
3535
progress: (root, { id }, context) => progress.get(id, context),
3636
clientAddons: (root, args, context) => clientAddons.list(context),
37-
sharedData: (root, { id }, context) => sharedData.get(id, context),
37+
sharedData: (root, args, context) => sharedData.get(args, context),
3838
locales: (root, args, context) => locales.list(context)
3939
},
4040

@@ -69,7 +69,7 @@ const resolvers = [{
6969
sharedDataUpdated: {
7070
subscribe: withFilter(
7171
(parent, args, { pubsub }) => pubsub.asyncIterator(channels.SHARED_DATA_UPDATED),
72-
(payload, vars) => payload.sharedDataUpdated.id === vars.id
72+
(payload, vars) => payload.sharedDataUpdated.id === vars.id && payload.sharedDataUpdated.projectId === vars.projectId
7373
)
7474
},
7575
localeAdded: {

packages/@vue/cli-ui/apollo-server/type-defs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ type Query {
6363
progress (id: ID!): Progress
6464
cwd: String!
6565
clientAddons: [ClientAddon]
66-
sharedData (id: ID!): SharedData
66+
sharedData (id: ID!, projectId: ID!): SharedData
6767
locales: [Locale]
6868
}
6969
7070
type Mutation {
7171
fileOpenInEditor (input: OpenInEditorInput!): Boolean
72-
sharedDataUpdate (id: ID!, value: JSON!): SharedData
72+
sharedDataUpdate (id: ID!, projectId: ID!, value: JSON!): SharedData
7373
}
7474
7575
type Subscription {
7676
progressChanged (id: ID!): Progress
7777
progressRemoved (id: ID!): ID
7878
cwdChanged: String!
7979
clientAddonAdded: ClientAddon
80-
sharedDataUpdated (id: ID!): SharedData
80+
sharedDataUpdated (id: ID!, projectId: ID!): SharedData
8181
localeAdded: Locale
8282
}
8383
`]

packages/@vue/cli-ui/src/components/TopBar.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import PROJECTS from '../graphql/projects.gql'
101101
import PROJECT_OPEN from '../graphql/projectOpen.gql'
102102
import PROJECT_SET_FAVORITE from '../graphql/projectSetFavorite.gql'
103103
import OPEN_IN_EDITOR from '../graphql/fileOpenInEditor.gql'
104+
import CURRENT_PROJECT_ID_SET from '../graphql/currentProjectIdSet.gql'
104105
105106
export default {
106107
apollo: {
@@ -136,6 +137,13 @@ export default {
136137
})
137138
138139
await resetApollo()
140+
141+
await this.$apollo.mutate({
142+
mutation: CURRENT_PROJECT_ID_SET,
143+
variables: {
144+
projectId: project.id
145+
}
146+
})
139147
},
140148
141149
async toggleCurrentFavorite () {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query currentProjectId {
2+
currentProjectId @client
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mutation currentProjectIdSet ($projectId: ID!) {
2+
currentProjectIdSet(projectId: $projectId) @client
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "./sharedDataFragment.gql"
22

3-
query sharedData ($id: ID!) {
4-
sharedData (id: $id) {
3+
query sharedData ($id: ID!, $projectId: ID!) {
4+
sharedData (id: $id, projectId: $projectId) {
55
...sharedData
66
}
77
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "./sharedDataFragment.gql"
22

3-
mutation sharedDataUpdate ($id: ID!, $value: JSON!) {
4-
sharedDataUpdate (id: $id, value: $value) {
3+
mutation sharedDataUpdate ($id: ID!, $projectId: ID!, $value: JSON!) {
4+
sharedDataUpdate (id: $id, projectId: $projectId, value: $value) {
55
...sharedData
66
}
77
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import "./sharedDataFragment.gql"
22

3-
subscription sharedDataUpdated ($id: ID!) {
4-
sharedDataUpdated (id: $id) {
3+
subscription sharedDataUpdated ($id: ID!, $projectId: ID!) {
4+
sharedDataUpdated (id: $id, projectId: $projectId) {
55
...sharedData
66
}
77
}

packages/@vue/cli-ui/src/router.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import About from './views/About.vue'
2121
import NotFound from './views/NotFound.vue'
2222

2323
import PROJECT_CURRENT from './graphql/projectCurrent.gql'
24+
import CURRENT_PROJECT_ID_SET from './graphql/currentProjectIdSet.gql'
2425

2526
Vue.use(Router)
2627

@@ -131,6 +132,13 @@ router.beforeEach(async (to, from, next) => {
131132
if (!result.data.projectCurrent) {
132133
next({ name: 'project-select' })
133134
return
135+
} else {
136+
await apolloClient.mutate({
137+
mutation: CURRENT_PROJECT_ID_SET,
138+
variables: {
139+
projectId: result.data.projectCurrent.id
140+
}
141+
})
134142
}
135143
}
136144
next()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
connected: true,
33
loading: 0,
4-
darkMode: false
4+
darkMode: false,
5+
currentProjectId: null
56
}

packages/@vue/cli-ui/src/state/resolvers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default {
99
cache.writeData({ data })
1010
return null
1111
},
12+
1213
loadingChange: (root, { mod }, { cache }) => {
1314
const { loading } = cache.readQuery({ query: LOADING })
1415
const data = {
@@ -17,6 +18,7 @@ export default {
1718
cache.writeData({ data })
1819
return null
1920
},
21+
2022
darkModeSet: (root, { enabled }, { cache }) => {
2123
const data = {
2224
darkMode: enabled
@@ -29,6 +31,14 @@ export default {
2931
el.classList.remove('vue-ui-dark-mode')
3032
}
3133
return null
34+
},
35+
36+
currentProjectIdSet: (root, { projectId }, { cache }) => {
37+
const data = {
38+
currentProjectId: projectId
39+
}
40+
cache.writeData({ data })
41+
return null
3242
}
3343
}
3444
}

0 commit comments

Comments
 (0)