Skip to content

Commit

Permalink
fix sync & import function
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Jan 29, 2019
1 parent d55eb2b commit ff186bd
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 126 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lodash": "^4.17.10",
"material-design-icons-iconfont": "^3.0.3",
"moment": "^2.22.1",
"socket.io-client": "^2.2.0",
"vue": "^2.5.21",
"vue-clipboard2": "^0.2.1",
"vue-router": "^3.0.1",
Expand Down
5 changes: 3 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ if (PRODUCTION) import(
if (DEBUG) {
window.tabs = tabs
window.browser = browser
window.listManger = listManger
window.boss = boss
browser.browserAction.setBadgeText({text: 'dev'})
}

Expand Down Expand Up @@ -301,8 +303,7 @@ const init = async () => {
}
})
await migrate()
await boss.refresh()
await boss.initTimer()
await boss.init()
}

init()
1 change: 1 addition & 0 deletions src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const END_FRONT = 'front'
export const END_BACKGROUND = 'background'

export const SYNC_SERVICE_URL = DEBUG ? 'http://127.0.0.1:3000' : 'https://boss.cnwangjie.com'
export const SYNC_MAX_INTERVAL = 864e5

export const ADD_LIST = 'addList'
export const UPDATE_LIST_BY_ID = 'updateListById'
Expand Down
5 changes: 3 additions & 2 deletions src/common/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {genObjectId} from './utils'
import {normalizeTab} from './tab'
import {PICKED_LIST_RPOPS} from './constants'

export const createNewTabList = ({tabs, title, tags, time, pinned, expand, color}) => ({
_id: genObjectId(),
export const createNewTabList = ({_id, tabs, title, tags, time, pinned, expand, color, updatedAt}) => ({
_id: _id || genObjectId(),
tabs: Array.isArray(tabs) ? tabs.map(normalizeTab) : [],
title: title || '',
tags: tags || [],
Expand All @@ -13,6 +13,7 @@ export const createNewTabList = ({tabs, title, tags, time, pinned, expand, color
pinned: pinned === true, // default is false
expand: expand !== false, // default is true
color: color || '',
updatedAt: updatedAt || time || Date.now(),
})

export const validateList = list => list != null && Array.isArray(list.tabs)
Expand Down
44 changes: 27 additions & 17 deletions src/common/listManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ const getStorage = async () => {
if (_readingStorage) {
await new Promise(resolve => {
const interval = setInterval(() => {
console.log('await reading storage')
if (_readingStorage) return
clearInterval(interval)
resolve()
}, 20)
}, 100)
})
}
if (cache.lists && cache.ops) return cache
Expand All @@ -40,23 +39,24 @@ const compressOps = ops => {
for (let i = ops.length - 1; i > -1; i -= 1) {
const op = ops[i]
// ignore all actions for the list if that list will be removed finally
if (op.args && typeof op.args[0] === 'object' && removed.includes(op.args[0]._id)
if (op.args && op.args[0] && removed.includes(op.args[0]._id)
|| typeof op.args[0] === 'string' && removed.includes(op.args[0])) continue

if (op.method === 'removeListById') {
removed.push(op.args[0])
finalOps.unshift(op)
} else if (op.method === 'updateListById') {
// keep the final result of every property if a list will be updated
if (updated[op.args[0]]) {
for (const key in op.args[1]) {
if (key in updated[op.args[0]]) continue
updated[op.args[0]][key] = op.args[1][key]
const [listId, newList, time] = op.args
if (updated[listId]) {
for (const key in newList) {
if (key in updated[listId]) continue
updated[listId][key] = newList[key]
}
continue
} else {
updated[op.args[0]] = Object.assign({}, op.args[1])
finalOps.unshift({method: 'updateListById', args: [op.args[0], updated[op.args[0]]]})
updated[listId] = Object.assign({}, newList)
finalOps.unshift({method: 'updateListById', args: [listId, updated[listId], time]})
}
} else if (op.method === 'changeListOrderRelatively') {
// combine the value if a list is reordered continuously
Expand All @@ -83,28 +83,30 @@ manager.modifiers = {
[ADD_LIST](lists, [list]) {
if (~lists.findIndex(i => i._id === list._id)) return
lists.unshift(list)
return true
return [list]
},
[UPDATE_LIST_BY_ID](lists, [listId, newList]) {
[UPDATE_LIST_BY_ID](lists, [listId, newList, time = Date.now()]) {
const normal = Object.keys(newList).some(k => SYNCED_LIST_PROPS.includes(k))
for (let i = 0; i < lists.length; i += 1) {
if (lists[i]._id !== listId) continue
for (const [k, v] in Object.entries(newList)) {
lists[i][k] = v
const list = lists[i]
for (const [k, v] of Object.entries(newList)) {
list[k] = v
}
return normal
if (normal) list.updatedAt = time
return normal ? [listId, newList, time] : null
}
},
[REMOVE_LIST_BY_ID](lists, [listId]) {
const index = lists.findIndex(list => list._id === listId)
lists.splice(index, 1)
return true
return [listId]
},
[CHANGE_LIST_ORDER](lists, [listId, diff]) {
const index = lists.findIndex(list => list._id === listId)
const [list] = lists.splice(index, 1)
lists.splice(index + diff, 0, list)
return true
return [listId, diff]
},
}

Expand All @@ -113,7 +115,8 @@ const _modifyQueue = []
const _startModifyWork = (lists, ops) => {
while (_modifyQueue.length !== 0) {
const [method, args] = _modifyQueue.shift()
if (manager.modifiers[method](lists, args)) ops.push({method, args, time: Date.now()})
const opArgs = manager.modifiers[method](lists, args)
if (opArgs) ops.push({method, args: opArgs, time: Date.now()})
}
saveStorage()
}
Expand Down Expand Up @@ -170,4 +173,11 @@ manager.createVuexPlugin = () => store => {
}
})
}
manager.idle = () => new Promise(resolve => {
const interval = setInterval(() => {
if (cache.lists) return
clearInterval(interval)
resolve()
}, 100)
})
export default manager
Loading

0 comments on commit ff186bd

Please sign in to comment.