Skip to content

Commit d5d54f1

Browse files
committed
Merge pull request #48 from drauggres/master
big update. please check twice.
2 parents 740c55e + c9e941f commit d5d54f1

File tree

4 files changed

+94
-121
lines changed

4 files changed

+94
-121
lines changed

dist/longjohn.js

Lines changed: 42 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/longjohn.coffee

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{EventEmitter} = require 'events'
2+
if EventEmitter.prototype.on?['longjohn']
3+
return module.exports = EventEmitter.prototype.on['longjohn']
24
filename = __filename
35
current_trace_error = null
46
in_prepare = 0
@@ -67,11 +69,22 @@ prepareStackTrace = (error, structured_stack_trace) ->
6769
++in_prepare
6870

6971
unless error.__cached_trace__?
70-
error.__cached_trace__ = structured_stack_trace.filter (f) -> f.getFileName() isnt filename
71-
error.__previous__ = current_trace_error if !error.__previous__? and in_prepare is 1
72-
72+
Object.defineProperty(error, '__cached_trace__', {
73+
writable: true,
74+
enumerable: false,
75+
configurable: true,
76+
value: structured_stack_trace.filter (f) -> f.getFileName() isnt filename
77+
});
78+
if !error.__previous__? and in_prepare is 1
79+
Object.defineProperty(error, '__previous__', {
80+
writable: true,
81+
enumerable: false,
82+
configurable: true,
83+
value: current_trace_error
84+
});
85+
7386
if error.__previous__?
74-
previous_stack = error.__previous__.__cached_trace__
87+
previous_stack =prepareStackTrace(error.__previous__, error.__previous__.__stack__)
7588
if previous_stack?.length > 0
7689
error.__cached_trace__.push(create_callsite(exports.empty_frame))
7790
error.__cached_trace__.push(previous_stack...)
@@ -90,36 +103,25 @@ limit_frames = (stack) ->
90103
while previous? and count > 1
91104
previous = previous.__previous__
92105
--count
93-
if previous?
94-
which_previous_must_delete = previous
95-
if previous?.__previous__?.__cached_trace__
96-
len = previous.__previous__.__cached_trace__.length
97-
previous = stack
98-
while previous? and previous != which_previous_must_delete.__previous__ and previous.__cached_trace__.length >= (len+1)
99-
previous.__cached_trace__.length -= (len+1)
100-
previous = previous.__previous__
101-
delete previous.__previous__
106+
delete previous.__previous__ if previous?
102107

103108
ERROR_ID = 1
104109

105-
call_stack_location = ->
110+
wrap_callback = (callback, location) ->
106111
orig = Error.prepareStackTrace
107112
Error.prepareStackTrace = (x, stack) -> stack
108-
err = new Error()
109-
Error.captureStackTrace(err, arguments.callee)
110-
stack = err.stack
111-
Error.prepareStackTrace = orig
112-
return 'bad call_stack_location' unless stack[2]?
113-
"#{stack[2].getFunctionName()} (#{stack[2].getFileName()}:#{stack[2].getLineNumber()})"
114-
115-
wrap_callback = (callback, location) ->
116113
trace_error = new Error()
114+
Error.captureStackTrace(trace_error, arguments.callee)
115+
trace_error.__stack__ = trace_error.stack;
116+
Error.prepareStackTrace = orig
117117
trace_error.id = ERROR_ID++
118-
trace_error.location = call_stack_location()
118+
if trace_error.stack[1]
119+
trace_error.location = "#{trace_error.stack[1].getFunctionName()} (#{trace_error.stack[1].getFileName()}:#{trace_error.stack[1].getLineNumber()})";
120+
else
121+
trace_error.location = 'bad call_stack_location'
119122
trace_error.__location__ = location
120123
trace_error.__previous__ = current_trace_error
121124
trace_error.__trace_count__ = if current_trace_error? then current_trace_error.__trace_count__ + 1 else 1
122-
trace_error.stack
123125

124126
limit_frames(trace_error)
125127

@@ -137,15 +139,14 @@ wrap_callback = (callback, location) ->
137139
finally
138140
current_trace_error = null
139141

140-
new_callback.__original_callback__ = callback
142+
new_callback.listener = callback
141143
new_callback
142144

143145

144146

145147
_on = EventEmitter.prototype.on
146148
_addListener = EventEmitter.prototype.addListener
147149
_once = EventEmitter.prototype.once
148-
_removeListener = EventEmitter.prototype.removeListener
149150
_listeners = EventEmitter.prototype.listeners
150151

151152
EventEmitter.prototype.addListener = (event, callback) ->
@@ -163,37 +164,23 @@ EventEmitter.prototype.once = (event, callback) ->
163164
args[1] = wrap_callback(callback, 'EventEmitter.once')
164165
_once.apply(this, args)
165166

166-
EventEmitter.prototype.removeListener = (event, callback) ->
167-
find_listener = (callback) =>
168-
is_callback = (val) ->
169-
val.__original_callback__ is callback or
170-
val.__original_callback__?.listener?.__original_callback__ is callback or
171-
val.listener?.__original_callback__ is callback
172-
173-
return null unless @_events?[event]?
174-
return @_events[event] if is_callback(@_events[event])
175-
176-
if Array.isArray(@_events[event])
177-
listeners = @_events[event] ? []
178-
for l in listeners
179-
return l if is_callback(l)
180-
181-
null
182-
183-
listener = find_listener(callback)
184-
return @ unless listener? and typeof listener is 'function'
185-
_removeListener.call(@, event, listener)
186-
187167
EventEmitter.prototype.listeners = (event) ->
188168
listeners = _listeners.call(this, event)
189169
unwrapped = []
190170
for l in listeners
191-
if l.__original_callback__
192-
unwrapped.push l.__original_callback__
171+
if l.listener
172+
unwrapped.push l.listener
193173
else
194174
unwrapped.push l
195175
return unwrapped
196176

177+
Object.defineProperty(EventEmitter.prototype.on, 'longjohn', {
178+
writable: true,
179+
enumerable: false,
180+
configurable: true,
181+
value: this
182+
});
183+
197184
_nextTick = process.nextTick
198185

199186
process.nextTick = (callback) ->

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "longjohn",
33
"description": "Long stack traces for node.js inspired by https://github.com/tlrobinson/long-stack-traces",
4-
"version": "0.2.7",
4+
"version": "0.2.8",
55
"homepage": "https://github.com/mattinsler/longjohn",
66
"author": {
77
"name": "Matt Insler",

test/test-longjohn.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ describe 'longjohn', ->
8080
emitter.emit('foo')
8181

8282
assert.equal(count, 1)
83+
84+
it 'should work with removeAllListeners (issue #32)', ->
85+
{EventEmitter} = require 'events'
86+
87+
count = 0
88+
foo = -> ++count
89+
90+
emitter = new EventEmitter()
91+
92+
emitter.on('removeListener', foo)
93+
emitter.on('dummy', foo)
94+
95+
emitter.removeAllListeners('dummy', foo)
96+
97+
assert.equal(count, 1)
8398

8499
it 'should work with setTimeout', (done) ->
85100
setTimeout ->

0 commit comments

Comments
 (0)