Mailspring/internal_packages/worker-ui/lib/developer-bar-store.coffee
Ben Gotow 1a576d92dc feat(work): Create the "Work" window, move TaskQueue, Nylas sync workers
Summary:
Move sync workers and Edgehill token checks to work window

Move the task queue and database setup to the work window

Move ContactStore background refresh to work window

Store the task queue in the database

WIP

The TaskQueue now puts tasks in the database instead of in a file, which also means it can be observed

Move all delta sync and initial sync to a package, make NylasSyncStore which exposes read-only sync state

DraftStore no longer reads task status. Once you set the "sending" bit on a draft, it never gets unset. But that's fine actually.

If your package lists windowTypes, you *only* get loaded in those windowTypes. If you specify no windowTypes, you get loaded in the root window.

This means that onboarding, worker-ui, worker-sync, etc. no longer get loaded into the main window

ActivitySidebar has a special little store that observes the task queue since it's no longer in the window

Move "toggle component regions" / "toggle react remote" to the Developer menu

Move sync worker specs, update draft store specs to not rely on TaskQueue at all

Test Plan: Run existing tests, all pass

Reviewers: dillon, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D1936
2015-08-27 16:39:40 -07:00

147 lines
4.1 KiB
CoffeeScript

Reflux = require 'reflux'
{Actions} = require 'nylas-exports'
qs = require 'querystring'
_ = require 'underscore'
curlItemId = 0
DeveloperBarStore = Reflux.createStore
init: ->
@_setStoreDefaults()
@_registerListeners()
########### PUBLIC #####################################################
curlHistory: -> @_curlHistory
longPollState: -> @_longPollState
longPollHistory: ->
# We can't use Utils.deepClone because the deltas contain circular references
# See delta.attributes._delta = delta
JSON.parse(JSON.stringify(@_longPollHistory))
visible: -> @_visible
########### PRIVATE ####################################################
triggerThrottled: ->
@_triggerThrottled ?= _.throttle(@trigger, 100)
@_triggerThrottled()
_setStoreDefaults: ->
@_curlHistory = []
@_longPollHistory = []
@_longPollState = {}
@_visible = atom.inDevMode()
_registerListeners: ->
@listenTo Actions.didMakeAPIRequest, @_onAPIRequest
@listenTo Actions.longPollReceivedRawDeltas, @_onLongPollDeltas
@listenTo Actions.longPollProcessedDeltas, @_onLongPollProcessedDeltas
@listenTo Actions.longPollStateChanged, @_onLongPollStateChange
@listenTo Actions.clearDeveloperConsole, @_onClear
@listenTo Actions.showDeveloperConsole, @_onShow
@listenTo Actions.sendFeedback, @_onSendFeedback
_onShow: ->
@_visible = true
@trigger(@)
_onClear: ->
@_curlHistory = []
@_longPollHistory = []
@trigger(@)
_onLongPollDeltas: (deltas) ->
# Add a local timestamp to deltas so we can display it
now = new Date()
delta.timestamp = now for delta in deltas
# Incoming deltas are [oldest...newest]. Append them to the beginning
# of our internal history which is [newest...oldest]
@_longPollHistory.unshift(deltas.reverse()...)
if @_longPollHistory.length > 200
@_longPollHistory.length = 200
@triggerThrottled(@)
_onLongPollProcessedDeltas: ->
@triggerThrottled(@)
_onLongPollStateChange: ({accountId, state}) ->
@_longPollState[accountId] = state
@triggerThrottled(@)
_onAPIRequest: ({request, response}) ->
url = request.url
if request.auth
url = url.replace('://', "://#{request.auth.user}:#{request.auth.pass}@")
if request.qs
url += "?#{qs.stringify(request.qs)}"
postBody = ""
postBody = JSON.stringify(request.body).replace(/'/g, '\\u0027') if request.body
data = ""
data = "-d '#{postBody}'" unless request.method == 'GET'
item =
id: "curlitemId:#{curlItemId}"
command: "curl -X #{request.method} #{data} \"#{url}\""
statusCode: response?.statusCode || 0
@_curlHistory.unshift(item)
curlItemId += 1
@triggerThrottled(@)
_onSendFeedback: ->
{AccountStore,
Contact,
Message,
DatabaseStore} = require 'nylas-exports'
user = AccountStore.current().name
debugData = JSON.stringify({
queries: @_curlHistory
}, null, '\t')
# Remove API tokens from URLs included in the debug data
# This regex detects ://user:pass@ and removes it.
debugData = debugData.replace(/:\/\/(\w)*:(\w)?@/g, '://')
draft = new Message
from: [AccountStore.current().me()]
to: [
new Contact
name: "Nylas Team"
email: "feedback@nylas.com"
]
date: (new Date)
draft: true
subject: "Feedback"
accountId: AccountStore.current().id
body: """
Hi, Nylas team! I have some feedback for you.<br/>
<br/>
<b>What happened:</b><br/>
<br/>
<br/>
<b>Impact:</b><br/>
<br/>
<br/>
<b>Feedback:</b><br/>
<br/>
<br/>
<b>Environment:</b><br/>
I'm using Nylas Mail #{atom.getVersion()} and my platform is #{process.platform}-#{process.arch}.<br/>
--<br/>
#{user}<br/>
-- Extra Debugging Data --<br/>
#{debugData}
"""
DatabaseStore.persistModel(draft).then ->
DatabaseStore.localIdForModel(draft).then (localId) ->
Actions.composePopoutDraft(localId)
module.exports = DeveloperBarStore