Add beginning of message system

This commit is contained in:
Ben Ogle
2014-11-20 13:59:49 -08:00
parent 5727865b94
commit d5d6422d28
3 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
MessageManager = require '../src/message-manager'
{$} = require '../src/space-pen-extensions'
fdescribe "MessageManager", ->
[manager] = []
beforeEach ->
manager = new MessageManager
describe "adding events", ->
addSpy = null
beforeEach ->
addSpy = jasmine.createSpy()
manager.onDidAddMessage(addSpy)
it "emits an event when a message has been added", ->
manager.add('error', 'Some error!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'error'
expect(message.getMessage()).toBe 'Some error!'
expect(message.getIcon()).toBe 'someIcon'
it "emits a fatal error ::addFatalError has been called", ->
manager.addFatalError('Some error!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'fatal'
it "emits an error ::addError has been called", ->
manager.addError('Some error!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'error'
it "emits a warning message ::addWarning has been called", ->
manager.addWarning('Something!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'warning'
it "emits an info message ::addInfo has been called", ->
manager.addInfo('Something!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'info'
it "emits a success message ::addSuccess has been called", ->
manager.addSuccess('Something!', icon: 'someIcon')
expect(addSpy).toHaveBeenCalled()
message = addSpy.mostRecentCall.args[0]
expect(message.getType()).toBe 'success'

View File

@@ -0,0 +1,40 @@
{Emitter, Disposable} = require 'event-kit'
Message = require '../src/message'
# Experimental: Allows messaging the user. This will likely change, dont use
# quite yet!
module.exports =
class MessageManager
constructor: ->
@messages = []
@emitter = new Emitter
###
Section: Events
###
onDidAddMessage: (callback) ->
@emitter.on 'did-add-message', callback
addSuccess: (messageString, options) ->
@addMessage(new Message('success', messageString, options))
addInfo: (messageString, options) ->
@addMessage(new Message('info', messageString, options))
addWarning: (messageString, options) ->
@addMessage(new Message('warning', messageString, options))
addError: (messageString, options) ->
@addMessage(new Message('error', messageString, options))
addFatalError: (messageString, options) ->
@addMessage(new Message('fatal', messageString, options))
add: (type, messageString, options) ->
@addMessage(new Message(type, messageString, options))
addMessage: (message) ->
@messages.push(message)
@emitter.emit('did-add-message', message)
message

38
src/message.coffee Normal file
View File

@@ -0,0 +1,38 @@
module.exports =
class Message
constructor: (@type, @message, @options={}) ->
getType: -> @type
getMessage: -> @message
isClosable: ->
!!@options.closable
getIcon: ->
return @options.icon if @options.icon?
switch @type
when 'fatal' then 'flame'
when 'error' then 'bug'
when 'warning' then 'alert'
when 'info' then 'info'
when 'success' then 'check'
getIssueUrl: ->
"https://github.com/atom/atom/issues/new?title=#{encodeURI(@getIssueTitle())}&body=#{encodeURI(@getIssueBody())}"
getIssueTitle: ->
@message
getIssueBody: ->
# TODO: crib version information from bug-report: https://github.com/lee-dohm/bug-report/blob/master/lib/bug-report.coffee#L69
"""
There was an unhandled error
Stack Trace
```
At #{@options.errorDetail}
#{@options.stack}
```
"""