mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-09 06:04:33 +08:00
fix(streaming): A few minor fixes for login/logout
This commit is contained in:
parent
cd8afcadf6
commit
e8dff2005d
4 changed files with 63 additions and 36 deletions
|
@ -55,6 +55,8 @@ AccountSidebarStore = Reflux.createStore
|
||||||
|
|
||||||
_populateUnreadCounts: ->
|
_populateUnreadCounts: ->
|
||||||
namespace = NamespaceStore.current()
|
namespace = NamespaceStore.current()
|
||||||
|
return unless namespace
|
||||||
|
|
||||||
@_sections.forEach (section) =>
|
@_sections.forEach (section) =>
|
||||||
section.tags.forEach (tag) =>
|
section.tags.forEach (tag) =>
|
||||||
# Some tags don't have unread counts
|
# Some tags don't have unread counts
|
||||||
|
|
|
@ -27,6 +27,11 @@ ActivityBarStore = Reflux.createStore
|
||||||
@listenTo Actions.didMakeAPIRequest, @_onAPIRequest
|
@listenTo Actions.didMakeAPIRequest, @_onAPIRequest
|
||||||
@listenTo Actions.developerPanelSelectSection, @_onSelectSection
|
@listenTo Actions.developerPanelSelectSection, @_onSelectSection
|
||||||
@listenTo Actions.longPollStateChanged, @_onLongPollStateChange
|
@listenTo Actions.longPollStateChanged, @_onLongPollStateChange
|
||||||
|
@listenTo Actions.logout, @_onLogout
|
||||||
|
|
||||||
|
_onLogout: ->
|
||||||
|
@_setStoreDefaults()
|
||||||
|
@trigger(@)
|
||||||
|
|
||||||
_onSelectSection: (section) ->
|
_onSelectSection: (section) ->
|
||||||
@_section = section
|
@_section = section
|
||||||
|
|
|
@ -3,6 +3,7 @@ request = require 'request'
|
||||||
Actions = require './actions'
|
Actions = require './actions'
|
||||||
{APIError} = require './errors'
|
{APIError} = require './errors'
|
||||||
DatabaseStore = require './stores/database-store'
|
DatabaseStore = require './stores/database-store'
|
||||||
|
NamespaceStore = require './stores/namespace-store'
|
||||||
InboxLongConnection = require './inbox-long-connection'
|
InboxLongConnection = require './inbox-long-connection'
|
||||||
{modelFromJSON, modelClassMap} = require './models/utils'
|
{modelFromJSON, modelClassMap} = require './models/utils'
|
||||||
async = require 'async'
|
async = require 'async'
|
||||||
|
@ -10,10 +11,13 @@ async = require 'async'
|
||||||
class InboxAPI
|
class InboxAPI
|
||||||
|
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@APILongConnections = {}
|
@_streamingConnections = []
|
||||||
atom.config.onDidChange('inbox.env', @_onConfigChanged)
|
atom.config.onDidChange('inbox.env', @_onConfigChanged)
|
||||||
atom.config.onDidChange('inbox.token', @_onConfigChanged)
|
atom.config.onDidChange('inbox.token', @_onConfigChanged)
|
||||||
@_onConfigChanged()
|
@_onConfigChanged()
|
||||||
|
|
||||||
|
NamespaceStore.listen(@_onNamespacesChanged, @)
|
||||||
|
@_onNamespacesChanged()
|
||||||
@
|
@
|
||||||
|
|
||||||
_onConfigChanged: =>
|
_onConfigChanged: =>
|
||||||
|
@ -30,50 +34,60 @@ class InboxAPI
|
||||||
|
|
||||||
current = {@APIToken, @AppID, @APIRoot}
|
current = {@APIToken, @AppID, @APIRoot}
|
||||||
|
|
||||||
if atom.state.mode is 'editor' and not _.isEqual(prev, current)
|
if atom.state.mode is 'editor'
|
||||||
@makeRequest
|
if not @APIToken?
|
||||||
path: "/n"
|
@_closeStreamingConnections()
|
||||||
returnsModel: true
|
|
||||||
success: =>
|
|
||||||
@_startLongPolling()
|
|
||||||
error: =>
|
|
||||||
@_startLongPolling()
|
|
||||||
|
|
||||||
_stopLongPolling: ->
|
if not _.isEqual(prev, current)
|
||||||
for namespace, connection of @APILongConnections
|
@makeRequest
|
||||||
connection.end()
|
path: "/n"
|
||||||
@APILongConnections = {}
|
returnsModel: true
|
||||||
|
|
||||||
_startLongPolling: ->
|
console.log(@_streamingConnections.length)
|
||||||
return unless atom.state.mode == 'editor'
|
|
||||||
|
_onNamespacesChanged: ->
|
||||||
|
return unless atom.state.mode is 'editor'
|
||||||
return if atom.getLoadSettings().isSpec
|
return if atom.getLoadSettings().isSpec
|
||||||
|
|
||||||
|
namespaces = NamespaceStore.items()
|
||||||
|
connections = _.map(namespaces, @_streamingConnectionForNamespace)
|
||||||
|
|
||||||
DatabaseStore = require './stores/database-store'
|
# Close the connections that are not in the new connections list.
|
||||||
Namespace = require './models/namespace'
|
# These namespaces are no longer in our database, so we shouldn't
|
||||||
|
# be listening.
|
||||||
|
old = _.without(@_streamingConnections, connections...)
|
||||||
|
conn.end() for conn in old
|
||||||
|
|
||||||
DatabaseStore.findAll(Namespace).then (namespaces) =>
|
@_streamingConnections = connections
|
||||||
@_stopLongPolling()
|
|
||||||
|
|
||||||
namespaces.forEach (namespace) =>
|
_closeStreamingConnections: ->
|
||||||
connection = new InboxLongConnection(@, namespace.id)
|
for conn in @_streamingConnections
|
||||||
@APILongConnections[namespace.id] = connection
|
conn.end()
|
||||||
|
@_streamingConnections = []
|
||||||
|
|
||||||
if !connection.hasCursor()
|
_streamingConnectionForNamespace: (namespace) =>
|
||||||
@getThreads(namespace.id)
|
connection = _.find @_streamingConnections, (c) ->
|
||||||
@getCalendars(namespace.id)
|
c.namespaceId() is namespace.id
|
||||||
|
console.log('Found existing connection') if connection
|
||||||
|
return connection if connection
|
||||||
|
|
||||||
connection.onStateChange (state) ->
|
connection = new InboxLongConnection(@, namespace.id)
|
||||||
Actions.longPollStateChanged(state)
|
|
||||||
if state == InboxLongConnection.State.Connected
|
|
||||||
Actions.restartTaskQueue()
|
|
||||||
|
|
||||||
connection.onDeltas (deltas) =>
|
if !connection.hasCursor()
|
||||||
@_handleDeltas(namespace.id, deltas)
|
@getThreads(namespace.id)
|
||||||
Actions.restartTaskQueue()
|
@getCalendars(namespace.id)
|
||||||
|
|
||||||
connection.start()
|
connection.onStateChange (state) ->
|
||||||
|
Actions.longPollStateChanged(state)
|
||||||
|
if state == InboxLongConnection.State.Connected
|
||||||
|
Actions.restartTaskQueue()
|
||||||
|
|
||||||
.catch (error) -> console.error(error)
|
connection.onDeltas (deltas) =>
|
||||||
|
@_handleDeltas(namespace.id, deltas)
|
||||||
|
Actions.restartTaskQueue()
|
||||||
|
|
||||||
|
connection.start()
|
||||||
|
connection
|
||||||
|
|
||||||
# Delegates to node's request object.
|
# Delegates to node's request object.
|
||||||
# On success, it will call the passed in success callback with options.
|
# On success, it will call the passed in success callback with options.
|
||||||
|
|
|
@ -6,6 +6,7 @@ class InboxLongConnection
|
||||||
|
|
||||||
@State =
|
@State =
|
||||||
Idle: 'idle'
|
Idle: 'idle'
|
||||||
|
Ended: 'ended'
|
||||||
Connecting: 'connecting'
|
Connecting: 'connecting'
|
||||||
Connected: 'connected'
|
Connected: 'connected'
|
||||||
Retrying: 'retrying'
|
Retrying: 'retrying'
|
||||||
|
@ -27,7 +28,10 @@ class InboxLongConnection
|
||||||
, 1000
|
, 1000
|
||||||
|
|
||||||
@
|
@
|
||||||
|
|
||||||
|
namespaceId: ->
|
||||||
|
@_namespaceId
|
||||||
|
|
||||||
hasCursor: ->
|
hasCursor: ->
|
||||||
!!atom.config.get(@_cursorKey)
|
!!atom.config.get(@_cursorKey)
|
||||||
|
|
||||||
|
@ -85,7 +89,9 @@ class InboxLongConnection
|
||||||
throw (new Error 'Cannot start polling without auth token.') unless @_inbox.APIToken
|
throw (new Error 'Cannot start polling without auth token.') unless @_inbox.APIToken
|
||||||
return if @_req
|
return if @_req
|
||||||
|
|
||||||
|
console.log("Long Polling Connection: Starting....")
|
||||||
@withCursor (cursor) =>
|
@withCursor (cursor) =>
|
||||||
|
return if @state is InboxLongConnection.State.Ended
|
||||||
console.log("Long Polling Connection: Starting for namespace #{@_namespaceId}, token #{@_inbox.APIToken}, with cursor #{cursor}")
|
console.log("Long Polling Connection: Starting for namespace #{@_namespaceId}, token #{@_inbox.APIToken}, with cursor #{cursor}")
|
||||||
options = url.parse("#{@_inbox.APIRoot}/n/#{@_namespaceId}/delta/streaming?cursor=#{cursor}&exclude_types=event")
|
options = url.parse("#{@_inbox.APIRoot}/n/#{@_namespaceId}/delta/streaming?cursor=#{cursor}&exclude_types=event")
|
||||||
options.auth = "#{@_inbox.APIToken}:"
|
options.auth = "#{@_inbox.APIToken}:"
|
||||||
|
@ -129,7 +135,7 @@ class InboxLongConnection
|
||||||
|
|
||||||
end: ->
|
end: ->
|
||||||
console.log("Long Polling Connection: Closed.")
|
console.log("Long Polling Connection: Closed.")
|
||||||
@setState(InboxLongConnection.State.Idle)
|
@setState(InboxLongConnection.State.Ended)
|
||||||
clearInterval(@_reqPingInterval) if @_reqPingInterval
|
clearInterval(@_reqPingInterval) if @_reqPingInterval
|
||||||
@_reqPingInterval = null
|
@_reqPingInterval = null
|
||||||
@_req.end() if @_req
|
@_req.end() if @_req
|
||||||
|
|
Loading…
Add table
Reference in a new issue