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

View File

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

View File

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