GeezerVison is showing github.com — diagnostics_channel: fix default callback context · nodejs/node@d7c9047
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/diagnostics_channel.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,7 @@ const { triggerUncaughtException } = internalBinding('errors'); | 35 | 35 | // must always be accessed through the binding instead of cached. | 36 | 36 | const dc_binding = internalBinding('diagnostics_channel'); | 37 | 37 |
| 38 | | -const { WeakReference, kEmptyObject } = require('internal/util'); | | 38 | +const { WeakReference } = require('internal/util'); | 39 | 39 |
| 40 | 40 | // Can't delete when weakref count reaches 0 as it could increment again. | 41 | 41 | // Only GC can be used as a valid time to clean up the channels map. | @@ -407,11 +407,15 @@ class TracingChannel { | 407 | 407 | }); | 408 | 408 | } | 409 | 409 |
| 410 | | - traceCallback(fn, position = -1, context = kEmptyObject, thisArg, ...args) { | | 410 | + traceCallback(fn, position = -1, context = undefined, thisArg, ...args) { | 411 | 411 | if (!this.hasSubscribers) { | 412 | 412 | return ReflectApply(fn, thisArg, args); | 413 | 413 | } | 414 | 414 |
| | 415 | + if (context === undefined) { | | 416 | + context = ObjectSetPrototypeOf({}, null); | | 417 | + } | | 418 | + | 415 | 419 | const { start, end, asyncStart, asyncEnd, error } = this; | 416 | 420 |
| 417 | 421 | function wrappedCallback(err, res) { | |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const dc = require('diagnostics_channel'); | | 6 | + | | 7 | +for (const expectedError of [null, new Error('test')]) { | | 8 | + const channel = dc.tracingChannel(`test-${expectedError ? 'error' : 'result'}`); | | 9 | + const contexts = []; | | 10 | + const results = [{ value: 1 }, { value: 2 }]; | | 11 | + let completed = 0; | | 12 | + | | 13 | + channel.subscribe({ | | 14 | + start: common.mustCall((context) => { | | 15 | + assert.strictEqual(context.result, undefined); | | 16 | + assert.strictEqual(context.error, undefined); | | 17 | + for (const previous of contexts) { | | 18 | + assert.notStrictEqual(context, previous); | | 19 | + } | | 20 | + contexts.push(context); | | 21 | + }, 2), | | 22 | + asyncStart: common.mustCall((context) => { | | 23 | + assert.strictEqual(context, contexts[completed]); | | 24 | + if (expectedError) { | | 25 | + assert.strictEqual(context.error, expectedError); | | 26 | + assert.strictEqual(context.result, undefined); | | 27 | + } else { | | 28 | + assert.strictEqual(context.result, results[completed]); | | 29 | + assert.strictEqual(context.error, undefined); | | 30 | + } | | 31 | + completed++; | | 32 | + }, 2), | | 33 | + asyncEnd: common.mustCall(2), | | 34 | + error: expectedError ? common.mustCall((context) => { | | 35 | + assert.strictEqual(context, contexts[completed]); | | 36 | + assert.strictEqual(context.error, expectedError); | | 37 | + }, 2) : common.mustNotCall(), | | 38 | + }); | | 39 | + | | 40 | + for (const result of results) { | | 41 | + channel.traceCallback(common.mustCall((callback) => { | | 42 | + setImmediate(callback, expectedError, expectedError ? undefined : result); | | 43 | + }), undefined, undefined, undefined, common.mustCall((err, value) => { | | 44 | + assert.strictEqual(err, expectedError); | | 45 | + assert.strictEqual(value, expectedError ? undefined : result); | | 46 | + })); | | 47 | + } | | 48 | + | | 49 | + setImmediate(common.mustCall(() => { | | 50 | + assert.strictEqual(completed, 2); | | 51 | + for (const [index, context] of contexts.entries()) { | | 52 | + assert.strictEqual(context.error, expectedError || undefined); | | 53 | + assert.strictEqual(context.result, expectedError ? undefined : results[index]); | | 54 | + } | | 55 | + })); | | 56 | +} |
|