Skip to content

Commit

Permalink
refactor: storage & shared data
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau authored and iksim committed Apr 18, 2019
1 parent 9e95521 commit c45ab5b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/devtools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import './plugins'
import { parse } from '../util'
import { isChrome, initEnv } from './env'
import SharedData, { init as initSharedData, destroy as destroySharedData } from 'src/shared-data'
import storage from './storage'
import VuexResolve from './views/vuex/resolve'

for (const key in filters) {
Expand Down Expand Up @@ -100,7 +99,7 @@ function initApp (shell) {
initSharedData({
bridge,
Vue,
storage
persist: true
})

bridge.once('ready', version => {
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/views/events/module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import storage from '../../storage'
import storage from 'src/storage'
import { classify } from 'src/util'
import SharedData from 'src/shared-data'

Expand Down
2 changes: 1 addition & 1 deletion src/devtools/views/router/module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import storage from '../../storage'
import storage from 'src/storage'

const ENABLED_KEY = 'EVENTS_ENABLED'
const enabled = storage.get(ENABLED_KEY)
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/views/routes/module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import storage from '../../storage'
import storage from 'src/storage'

const ENABLED_KEY = 'EVENTS_ENABLED'
const enabled = storage.get(ENABLED_KEY)
Expand Down
28 changes: 12 additions & 16 deletions src/shared-data.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import storage from './storage'

// Initial state
const internalSharedData = {
openInEditorHost: '/',
Expand All @@ -24,8 +26,6 @@ const persisted = [

let Vue
let bridge
// Storage API
let storage = null
// List of fields to persist to storage (disabled if 'false')
// This should be unique to each shared data client to prevent conflicts
let persist = false
Expand All @@ -36,23 +36,19 @@ export function init (params) {
// Mandatory params
bridge = params.bridge
Vue = params.Vue

if (params.hasOwnProperty('storage')) {
storage = params.storage
persist = persisted
}
persist = !!params.persist

// Load persisted fields
if (persist) {
persist.forEach(key => {
const value = storage.get(`shared-data:${key}`)
if (value !== null) {
internalSharedData[key] = value
// Send to other shared data clients
persisted.forEach(key => {
const value = storage.get(`shared-data:${key}`)
if (value !== null) {
internalSharedData[key] = value
// Send to other shared data clients
if (persist) {
sendValue(key, value)
}
})
}
}
})

// Wrapper Vue instance
vm = new Vue({
Expand All @@ -72,7 +68,7 @@ export function destroy () {

function setValue (key, value) {
// Storage
if (persist && persist.includes(key)) {
if (persist && persisted.includes(key)) {
storage.set(`shared-data:${key}`, value)
}
vm[key] = value
Expand Down
File renamed without changes.

0 comments on commit c45ab5b

Please sign in to comment.