Skip to content

Commit

Permalink
fix: add timeout for websocket emit method to fix data cannot be change
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Aug 8, 2019
1 parent 7d84ce5 commit f8e20cf
Showing 1 changed file with 13 additions and 43 deletions.
56 changes: 13 additions & 43 deletions src/common/service/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const setWSToken = token => {
}
}

const _socketEmitTimeout = (socket, event, arg) => timeout(new Promise((resolve, reject) => {
const cb = result => result && result.err ? reject(result.err) : resolve(result)
if (arg) socket.emit(event, arg, cb)
else socket.emit(event, cb)
}), 5000)

const uploadOpsViaWS = async () => {
const socket = window._socket
if (!socket || !socket.connected) throw new Error('socket not connected')
Expand All @@ -86,44 +92,24 @@ const uploadOpsViaWS = async () => {
const changes = ops.sort((a, b) => a.time - b.time)
while (changes && changes.length) {
const change = changes.shift()
await new Promise(resolve => {
socket.emit('list.update', change, ({err}) => {
if (err) {
logger.log(change)
logger.error(err)
}
resolve()
})
})
await _socketEmitTimeout(socket, 'list.update', change)
}
}
}

const downloadRemoteLists = async () => {
const socket = window._socket
if (!socket || !socket.connected) throw new Error('socket not connected')
const remoteTime = await new Promise(resolve => {
socket.emit('list.time', time => {
resolve(time)
})
})
const remoteTime = await _socketEmitTimeout(socket, 'list.time')
const {listsUpdatedAt: localTime} = await browser.storage.local.get('listsUpdatedAt')
if (remoteTime === localTime) return
const remoteLists = await new Promise(resolve => {
socket.emit('list.all', lists => {
resolve(lists)
})
})
const remoteLists = await _socketEmitTimeout(socket, 'list.all')
const localLists = _.keyBy(await storage.getLists(), list => list._id)
const finallyLists = []
const fetching = {}
remoteLists.forEach(list => {
if (!(list._id in localLists) || localLists.updatedAt < list.updatedAt) {
fetching[list._id] = new Promise(resolve => {
socket.emit('list.get', list._id, remoteList => {
resolve(remoteList)
})
})
fetching[list._id] = _socketEmitTimeout(socket, 'list.get', list._id)
finallyLists.push(list._id)
} else {
finallyLists.push(localLists[list._id])
Expand Down Expand Up @@ -153,27 +139,11 @@ const syncLists = async () => {
}
}

const getRemoteOptionsUpdatedTimeViaWS = () => new Promise(resolve => {
const socket = window._socket
socket.emit('opts.time', time => {
resolve(time)
})
})
const getRemoteOptionsUpdatedTimeViaWS = () => _socketEmitTimeout(window._socket, 'opts.time')

const getRemoteOptions = () => new Promise(resolve => {
const socket = window._socket
socket.emit('opts.all', opts => {
resolve(opts)
})
})
const getRemoteOptions = () => _socketEmitTimeout(window._socket, 'opts.all')

const setRemoteOptions = (opts, time) => new Promise((resolve, reject) => {
const socket = window._socket
socket.emit('opts.set', ({opts, time}), ({err}) => {
if (err) reject(err)
resolve()
})
})
const setRemoteOptions = (opts, time) => _socketEmitTimeout(window._socket, 'opts.set', { opts, time })

const syncOptions = async () => {
const remoteTime = await getRemoteOptionsUpdatedTimeViaWS()
Expand Down

0 comments on commit f8e20cf

Please sign in to comment.