Skip to content

Commit

Permalink
feat: add support to forward args in context.with (open-telemetry#1883)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
Flarna and dyladan committed Feb 18, 2021
1 parent 2d75a57 commit 46070ea
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
12 changes: 8 additions & 4 deletions api/src/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ export class ContextAPI {
*
* @param context context to be active during function execution
* @param fn function to execute in a context
* @param thisArg optional receiver to be used for calling fn
* @param args optional arguments forwarded to fn
*/
public with<T extends (...args: unknown[]) => ReturnType<T>>(
public with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
context: Context,
fn: T
): ReturnType<T> {
return this._getContextManager().with(context, fn);
fn: F,
thisArg?: ThisParameterType<F>,
...args: A
): ReturnType<F> {
return this._getContextManager().with(context, fn, thisArg, ...args);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions api/src/context-base/NoopContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export class NoopContextManager implements types.ContextManager {
return ROOT_CONTEXT;
}

with<T extends (...args: unknown[]) => ReturnType<T>>(
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
_context: types.Context,
fn: T
): ReturnType<T> {
return fn();
fn: F,
thisArg?: ThisParameterType<F>,
...args: A
): ReturnType<F> {
return fn.call(thisArg, ...args);
}

bind<T>(target: T, _context?: types.Context): T {
Expand Down
10 changes: 7 additions & 3 deletions api/src/context-base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ export interface ContextManager {
* Run the fn callback with object set as the current active context
* @param context Any object to set as the current active context
* @param fn A callback to be immediately run within a specific context
* @param thisArg optional receiver to be used for calling fn
* @param args optional arguments forwarded to fn
*/
with<T extends (...args: unknown[]) => ReturnType<T>>(
with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
context: Context,
fn: T
): ReturnType<T>;
fn: F,
thisArg?: ThisParameterType<F>,
...args: A
): ReturnType<F>;

/**
* Bind an object as the current context (or a specific one)
Expand Down
20 changes: 20 additions & 0 deletions api/test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ describe('API', () => {
assert.strictEqual(typeof tracer, 'object');
});

describe('Context', () => {
it('with should forward this, arguments and return value', () => {
function fnWithThis(this: string, a: string, b: number): string {
assert.strictEqual(this, 'that');
assert.strictEqual(arguments.length, 2);
assert.strictEqual(a, 'one');
assert.strictEqual(b, 2);
return 'done';
}

const res = context.with(ROOT_CONTEXT, fnWithThis, 'that', 'one', 2);
assert.strictEqual(res, 'done');

assert.strictEqual(
context.with(ROOT_CONTEXT, () => 3.14),
3.14
);
});
});

describe('GlobalTracerProvider', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
Expand Down
24 changes: 24 additions & 0 deletions api/test/context-base/NoopContextManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ describe('NoopContextManager', () => {
return done();
});
});

it('should forward this, arguments and return value', () => {
function fnWithThis(this: string, a: string, b: number): string {
assert.strictEqual(this, 'that');
assert.strictEqual(arguments.length, 2);
assert.strictEqual(a, 'one');
assert.strictEqual(b, 2);
return 'done';
}

const res = contextManager.with(
ROOT_CONTEXT,
fnWithThis,
'that',
'one',
2
);
assert.strictEqual(res, 'done');

assert.strictEqual(
contextManager.with(ROOT_CONTEXT, () => 3.14),
3.14
);
});
});

describe('.active()', () => {
Expand Down

0 comments on commit 46070ea

Please sign in to comment.