Revert "Asynchronous version of DataBoard.forceSync."

This reverts commit fbee3c2df256b5152efd0682904837f0e13b6411.

Revert "Eliminate $.forceSync from QuoteYou."

This reverts commit f924beb2801b232dcbfa04e6d9d1abb2f6810ec3.

Revert "Better protection against race conditions that can lead to data loss in DataBoard."

This reverts commit 8d0a6cf97f263e0735b3002466328285e5a37454.
This commit is contained in:
ccd0 2018-01-23 11:02:46 -08:00
parent 568c08d09b
commit ea08bc8822
3 changed files with 39 additions and 64 deletions

View File

@ -185,8 +185,9 @@ ThreadWatcher =
interval = if ThreadWatcher.unreadEnabled and Conf['Show Unread Count'] then 5 * $.MINUTE else 2 * $.HOUR
now = Date.now()
unless now - interval < (db.data.lastChecked or 0) <= now
ThreadWatcher.fetchAllStatus()
db.setLastChecked()
ThreadWatcher.fetchAllStatus() # calls forceSync
db.data.lastChecked = now
db.save()
ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval
buttonFetchAll: ->
@ -196,15 +197,13 @@ ThreadWatcher =
ThreadWatcher.fetchAllStatus()
fetchAllStatus: ->
dbs = [ThreadWatcher.db, ThreadWatcher.unreaddb, QuoteYou.db].filter((x) -> x)
n = 0
for db in dbs
db.forceSync ->
if (++n) is dbs.length
threads = ThreadWatcher.getAll()
for thread in threads
ThreadWatcher.fetchStatus thread
return
ThreadWatcher.db.forceSync()
ThreadWatcher.unreaddb.forceSync()
QuoteYou.db?.forceSync()
return unless (threads = ThreadWatcher.getAll()).length
for thread in threads
ThreadWatcher.fetchStatus thread
return
fetchStatus: (thread, force) ->
{boardID, threadID, data} = thread

View File

@ -5,8 +5,8 @@ QuoteYou =
@db = new DataBoard 'yourPosts'
$.sync 'Remember Your Posts', (enabled) -> Conf['Remember Your Posts'] = enabled
$.on d, 'QRPostSuccessful', (e) ->
$.get 'Remember Your Posts', Conf['Remember Your Posts'], (items) ->
return unless items['Remember Your Posts']
$.forceSync 'Remember Your Posts'
if Conf['Remember Your Posts']
{boardID, threadID, postID} = e.detail
(QuoteYou.db.set {boardID, threadID, postID, val: true})

View File

@ -13,44 +13,21 @@ class DataBoard
@sync = sync
$.on d, '4chanXInitFinished', init
changes: []
save: (change, cb) ->
snapshot1 = JSON.stringify @data
change()
{changes} = @
changes.push change
$.get @key, {boards: {}}, (items) =>
@data = items[@key]
snapshot2 = JSON.stringify @data
c() for c in changes
$.set @key, @data, =>
@changes = []
@sync?() if snapshot1 isnt snapshot2
cb?()
forceSync: (cb) ->
snapshot1 = JSON.stringify @data
{changes} = @
$.get @key, {boards: {}}, (items) =>
@data = items[@key]
snapshot2 = JSON.stringify @data
c() for c in changes
@sync?() if snapshot1 isnt snapshot2
cb?()
save: (cb) -> $.set @key, @data, cb
delete: ({boardID, threadID, postID}) ->
@save =>
if postID
return unless @data.boards[boardID]?[threadID]
delete @data.boards[boardID][threadID][postID]
@deleteIfEmpty {boardID, threadID}
else if threadID
return unless @data.boards[boardID]
delete @data.boards[boardID][threadID]
@deleteIfEmpty {boardID}
else
delete @data.boards[boardID]
$.forceSync @key
if postID
return unless @data.boards[boardID]?[threadID]
delete @data.boards[boardID][threadID][postID]
@deleteIfEmpty {boardID, threadID}
else if threadID
return unless @data.boards[boardID]
delete @data.boards[boardID][threadID]
@deleteIfEmpty {boardID}
else
delete @data.boards[boardID]
@save()
deleteIfEmpty: ({boardID, threadID}) ->
if threadID
@ -61,29 +38,24 @@ class DataBoard
delete @data.boards[boardID]
set: (data, cb) ->
@save =>
@setUnsafe data
, cb
$.forceSync @key
@setUnsafe data, cb
setUnsafe: ({boardID, threadID, postID, val}) ->
setUnsafe: ({boardID, threadID, postID, val}, cb) ->
if postID isnt undefined
((@data.boards[boardID] or= {})[threadID] or= {})[postID] = val
else if threadID isnt undefined
(@data.boards[boardID] or= {})[threadID] = val
else
@data.boards[boardID] = val
@save cb
extend: ({boardID, threadID, postID, val, rm}, cb) ->
@save =>
oldVal = @get {boardID, threadID, postID, val: {}}
delete oldVal[key] for key in rm or []
$.extend oldVal, val
@setUnsafe {boardID, threadID, postID, val: oldVal}
, cb
setLastChecked: ->
@save =>
@data.lastChecked = Date.now()
$.forceSync @key
oldVal = @get {boardID, threadID, postID, val: {}}
delete oldVal[key] for key in rm or []
$.extend oldVal, val
@setUnsafe {boardID, threadID, postID, val: oldVal}, cb
get: ({boardID, threadID, postID, defaultValue}) ->
if board = @data.boards[boardID]
@ -102,7 +74,11 @@ class DataBoard
thread
val or defaultValue
forceSync: ->
$.forceSync @key
clean: ->
$.forceSync @key
for boardID, val of @data.boards
@deleteIfEmpty {boardID}
@ -133,7 +109,7 @@ class DataBoard
threads[ID] = board[ID] if ID of board
@data.boards[boardID] = threads
@deleteIfEmpty {boardID}
$.set @key, @data
@save()
onSync: (data) =>
@data = data or boards: {}