mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-24 01:06:07 +08:00
ea76b7c442
Summary: Until now, we've been hiding transactions beneath the surface. When you call persistModel, you're implicitly creating a transaction. You could explicitly create them with `atomically`..., but there were several critical problems that are fixed in this diff: - Calling persistModel / unpersistModel within a transaction could cause the DatabaseStore to trigger. This could result in other parts of the app making queries /during/ the transaction, potentially before the COMMIT occurred and saved the changes. The new, explicit inTransaction syntax holds all changes until after COMMIT and then triggers. - Calling atomically and then calling persistModel inside that resulted in us having to check whether a transaction was present and was gross. - Many parts of the code ran extensive logic inside a promise chained within `atomically`: BAD: ``` DatabaseStore.atomically => DatabaseStore.persistModel(draft) => GoMakeANetworkRequestThatReturnsAPromise ``` OVERWHELMINGLY BETTER: ``` DatabaseStore.inTransaction (t) => t.persistModel(draft) .then => GoMakeANetworkRequestThatReturnsAPromise ``` Having explicit transactions also puts us on equal footing with Sequelize and other ORMs. Note that you /have/ to call DatabaseStore.inTransaction (t) =>. There is no other way to access the methods that let you alter the database. :-) Other changes: - This diff removes Message.labels and the Message-Labels table. We weren't using Message-level labels anywhere, and the table could grow very large. - This diff changes the page size during initial sync from 250 => 200 in an effort to make transactions a bit faster. Test Plan: Run tests! Reviewers: juan, evan Reviewed By: juan, evan Differential Revision: https://phab.nylas.com/D2353
131 lines
4.2 KiB
CoffeeScript
131 lines
4.2 KiB
CoffeeScript
_ = require 'underscore'
|
|
|
|
{NylasAPI,
|
|
Actions,
|
|
AccountStore,
|
|
DatabaseStore,
|
|
DatabaseObjectRegistry} = require 'nylas-exports'
|
|
|
|
NylasLongConnection = require './nylas-long-connection'
|
|
NylasSyncWorker = require './nylas-sync-worker'
|
|
|
|
|
|
class NylasSyncWorkerPool
|
|
|
|
constructor: ->
|
|
@_workers = []
|
|
AccountStore.listen(@_onAccountsChanged, @)
|
|
@_onAccountsChanged()
|
|
|
|
_onAccountsChanged: ->
|
|
return if NylasEnv.inSpecMode()
|
|
|
|
accounts = AccountStore.items()
|
|
workers = _.map(accounts, @workerForAccount)
|
|
|
|
# Stop the workers that are not in the new workers list.
|
|
# These accounts are no longer in our database, so we shouldn't
|
|
# be listening.
|
|
old = _.without(@_workers, workers...)
|
|
worker.cleanup() for worker in old
|
|
|
|
@_workers = workers
|
|
|
|
workers: =>
|
|
@_workers
|
|
|
|
workerForAccount: (account) =>
|
|
worker = _.find @_workers, (c) -> c.account().id is account.id
|
|
return worker if worker
|
|
|
|
worker = new NylasSyncWorker(NylasAPI, account)
|
|
connection = worker.connection()
|
|
|
|
connection.onStateChange (state) ->
|
|
Actions.longPollStateChanged({accountId: account.id, state: state})
|
|
if state == NylasLongConnection.State.Connected
|
|
## TODO use OfflineStatusStore
|
|
Actions.longPollConnected()
|
|
else
|
|
## TODO use OfflineStatusStore
|
|
Actions.longPollOffline()
|
|
|
|
connection.onDeltas (deltas) =>
|
|
@_handleDeltas(deltas)
|
|
|
|
@_workers.push(worker)
|
|
worker.start()
|
|
worker
|
|
|
|
_cleanupAccountWorkers: ->
|
|
for worker in @_workers
|
|
worker.cleanup()
|
|
@_workers = []
|
|
|
|
_handleDeltas: (deltas) ->
|
|
Actions.longPollReceivedRawDeltas(deltas)
|
|
Actions.longPollReceivedRawDeltasPing(deltas.length)
|
|
|
|
# Create a (non-enumerable) reference from the attributes which we carry forward
|
|
# back to their original deltas. This allows us to mark the deltas that the
|
|
# app ignores later in the process.
|
|
deltas.forEach (delta) ->
|
|
if delta.attributes
|
|
Object.defineProperty(delta.attributes, '_delta', { get: -> delta })
|
|
|
|
{create, modify, destroy} = @_clusterDeltas(deltas)
|
|
|
|
# Apply all the deltas to create objects. Gets promises for handling
|
|
# each type of model in the `create` hash, waits for them all to resolve.
|
|
create[type] = NylasAPI._handleModelResponse(_.values(dict)) for type, dict of create
|
|
Promise.props(create).then (created) =>
|
|
# Apply all the deltas to modify objects. Gets promises for handling
|
|
# each type of model in the `modify` hash, waits for them all to resolve.
|
|
modify[type] = NylasAPI._handleModelResponse(_.values(dict)) for type, dict of modify
|
|
Promise.props(modify).then (modified) =>
|
|
|
|
# Now that we've persisted creates/updates, fire an action
|
|
# that allows other parts of the app to update based on new models
|
|
# (notifications)
|
|
if _.flatten(_.values(created)).length > 0
|
|
Actions.didPassivelyReceiveNewModels(created)
|
|
|
|
# Apply all of the deletions
|
|
destroyPromises = destroy.map(@_handleDeltaDeletion)
|
|
Promise.settle(destroyPromises).then =>
|
|
Actions.longPollProcessedDeltas()
|
|
|
|
_clusterDeltas: (deltas) ->
|
|
# Group deltas by object type so we can mutate the cache efficiently.
|
|
# NOTE: This code must not just accumulate creates, modifies and destroys
|
|
# but also de-dupe them. We cannot call "persistModels(itemA, itemA, itemB)"
|
|
# or it will throw an exception - use the last received copy of each model
|
|
# we see.
|
|
create = {}
|
|
modify = {}
|
|
destroy = []
|
|
for delta in deltas
|
|
if delta.event is 'create'
|
|
create[delta.object] ||= {}
|
|
create[delta.object][delta.attributes.id] = delta.attributes
|
|
else if delta.event is 'modify'
|
|
modify[delta.object] ||= {}
|
|
modify[delta.object][delta.attributes.id] = delta.attributes
|
|
else if delta.event is 'delete'
|
|
destroy.push(delta)
|
|
|
|
{create, modify, destroy}
|
|
|
|
_handleDeltaDeletion: (delta) =>
|
|
klass = NylasAPI._apiObjectToClassMap[delta.object]
|
|
return unless klass
|
|
|
|
DatabaseStore.inTransaction (t) =>
|
|
t.find(klass, delta.id).then (model) ->
|
|
return Promise.resolve() unless model
|
|
return t.unpersistModel(model)
|
|
|
|
pool = new NylasSyncWorkerPool()
|
|
window.NylasSyncWorkerPool = pool
|
|
module.exports = pool
|