Revert reversion of DataBoard / forceSync changes.

This reverts commit ea08bc882230289d675c58c64572d6a7bd0c4a7a.
This commit is contained in:
ccd0 2018-01-23 22:17:31 -08:00
parent 719097340b
commit f24ebbd648
3 changed files with 64 additions and 39 deletions

View File

@ -185,9 +185,8 @@ 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() # calls forceSync
db.data.lastChecked = now
db.save()
ThreadWatcher.fetchAllStatus()
db.setLastChecked()
ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval
buttonFetchAll: ->
@ -197,13 +196,15 @@ ThreadWatcher =
ThreadWatcher.fetchAllStatus()
fetchAllStatus: ->
ThreadWatcher.db.forceSync()
ThreadWatcher.unreaddb.forceSync()
QuoteYou.db?.forceSync()
return unless (threads = ThreadWatcher.getAll()).length
for thread in threads
ThreadWatcher.fetchStatus thread
return
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
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) ->
$.forceSync 'Remember Your Posts'
if Conf['Remember Your Posts']
$.get 'Remember Your Posts', Conf['Remember Your Posts'], (items) ->
return unless items['Remember Your Posts']
{boardID, threadID, postID} = e.detail
(QuoteYou.db.set {boardID, threadID, postID, val: true})

View File

@ -13,21 +13,44 @@ class DataBoard
@sync = sync
$.on d, '4chanXInitFinished', init
save: (cb) -> $.set @key, @data, cb
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?()
delete: ({boardID, threadID, postID}) ->
$.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()
@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]
deleteIfEmpty: ({boardID, threadID}) ->
if threadID
@ -38,24 +61,29 @@ class DataBoard
delete @data.boards[boardID]
set: (data, cb) ->
$.forceSync @key
@setUnsafe data, cb
@save =>
@setUnsafe data
, cb
setUnsafe: ({boardID, threadID, postID, val}, cb) ->
setUnsafe: ({boardID, threadID, postID, val}) ->
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) ->
$.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
@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()
get: ({boardID, threadID, postID, defaultValue}) ->
if board = @data.boards[boardID]
@ -74,11 +102,7 @@ class DataBoard
thread
val or defaultValue
forceSync: ->
$.forceSync @key
clean: ->
$.forceSync @key
for boardID, val of @data.boards
@deleteIfEmpty {boardID}
@ -109,7 +133,7 @@ class DataBoard
threads[ID] = board[ID] if ID of board
@data.boards[boardID] = threads
@deleteIfEmpty {boardID}
@save()
$.set @key, @data
onSync: (data) =>
@data = data or boards: {}