Skip to content

Commit 28b8eae

Browse files
committed
Emit events when the active Stacktrace is changed.
1 parent b7de299 commit 28b8eae

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/stacktrace.coffee

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
fs = require 'fs'
2+
3+
{Emitter} = require 'emissary'
4+
25
jsSHA = require 'jssha'
36
{chomp} = require 'line-chomper'
47
traceParser = null
@@ -12,6 +15,9 @@ ACTIVE = null
1215
#
1316
class Stacktrace
1417

18+
# Turn the Stacktrace class into an emitter.
19+
Emitter.extend this
20+
1521
constructor: (@frames = [], @message = '') ->
1622

1723
# Internal: Compute the SHA256 checksum of the normalized stacktrace.
@@ -45,11 +51,18 @@ class Stacktrace
4551
# Public: Mark this trace as the "active" one. The active trace is shown in the navigation view
4652
# and its frames are given a marker in an open {EditorView}.
4753
#
48-
activate: -> ACTIVE = this
54+
activate: ->
55+
former = ACTIVE
56+
ACTIVE = this
57+
if former isnt ACTIVE
58+
Stacktrace.emit 'active-changed', oldTrace: former, newTrace: ACTIVE
4959

5060
# Public: Deactivate this trace if it's active.
5161
#
52-
deactivate: -> ACTIVE = null if ACTIVE is this
62+
deactivate: ->
63+
if ACTIVE is this
64+
ACTIVE = null
65+
Stacktrace.emit 'active-changed', oldTrace: this, newTrace: null
5366

5467
# Public: Parse zero to many Stacktrace instances from a corpus of text.
5568
#

spec/stacktrace-spec.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe 'Stacktrace', ->
7979
afterEach ->
8080
activated = Stacktrace.getActivated()
8181
activated.deactivate() if activated?
82+
Stacktrace.off 'active-changed'
8283

8384
it 'can be activated', ->
8485
trace.activate()
@@ -93,6 +94,14 @@ describe 'Stacktrace', ->
9394
trace.deactivate()
9495
expect(Stacktrace.getActivated()).toBeNull()
9596

97+
it 'broadcasts a "active-changed" event', ->
98+
event = null
99+
Stacktrace.on 'active-changed', (e) -> event = e
100+
101+
trace.activate()
102+
expect(event.oldTrace).toBeNull()
103+
expect(event.newTrace).toBe(trace)
104+
96105
describe 'Frame', ->
97106
[frame] = []
98107

0 commit comments

Comments
 (0)