Skip to content

Commit 559ba6f

Browse files
committed
docs: .bind changes
1 parent f9185d6 commit 559ba6f

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

Diff for: docs/core/tracer.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
143143
}
144144
145145
export const handlerClass = new Lambda();
146-
export const handler = handlerClass.handler;
146+
export const handler = handlerClass.handler.bind(handlerClass); // (1)
147147
```
148+
149+
1. Binding your handler method allows your handler to access `this`.
150+
148151
=== "Manual"
149152

150153
```typescript hl_lines="6 8-9 12-13 19 22 26 28"
@@ -253,8 +256,11 @@ You can trace other Class methods using the `captureMethod` decorator or any arb
253256
}
254257
255258
export const myFunction = new Lambda();
256-
export const handler = myFunction.handler;
259+
export const handler = myFunction.handler.bind(myFunction); // (1)
257260
```
261+
262+
1. Binding your handler method allows your handler to access `this`.
263+
258264
=== "Manual"
259265

260266
```typescript hl_lines="6 8-9 15 18 23 25"

Diff for: packages/tracer/src/Tracer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
6767
* }
6868
*
6969
* export const handlerClass = new Lambda();
70-
* export const handler = handlerClass.handler;
70+
* export const handler = handlerClass.handler.bind(handlerClass);
7171
* ```
7272
*
7373
* ### Functions usage with manual instrumentation
@@ -334,7 +334,7 @@ class Tracer extends Utility implements TracerInterface {
334334
* }
335335
*
336336
* export const handlerClass = new Lambda();
337-
* export const handler = handlerClass.handler;
337+
* export const handler = handlerClass.handler.bind(handlerClass);
338338
* ```
339339
*
340340
* @decorator Class

Diff for: packages/tracer/tests/unit/Tracer.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ describe('Class: Tracer', () => {
881881

882882
// Act / Assess
883883
const lambda = new Lambda('someValue');
884-
expect(await lambda.handler({}, context, () => console.log('Lambda invoked!'))).toEqual('memberVariable:someValue');
884+
const handler = lambda.handler.bind(lambda);
885+
expect(await handler({}, context, () => console.log('Lambda invoked!'))).toEqual('memberVariable:someValue');
885886

886887
});
887888
});

0 commit comments

Comments
 (0)