Skip to content

Commit 7224e2b

Browse files
committed
Inserted variable scoping in to functions + destination functions
1 parent 97a58fd commit 7224e2b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/connections/functions/destination-functions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ To change which event type the handler listens to, you can rename it to the name
7979
> info ""
8080
> Functions' runtime includes a `fetch()` polyfill using a `node-fetch` package. Check out the [node-fetch documentation](https://www.npmjs.com/package/node-fetch){:target="_blank"} for usage examples.
8181
82+
### Variable scoping
83+
84+
When declaring settings variables, declare them in the function handler rather than globally in your function. This prevents you from leaking the settings values across other function instances.
85+
86+
The handler for destination functions is event-specific. For example, you might have an `onTrack()`or `onIdentify()` function handler.
87+
8288
### Errors and error handling
8389

8490
{% include content/functions/errors-and-error-handling.md %}

src/connections/functions/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ Functions let you create your own sources and destinations directly within your
1212
## What can you do with Functions?
1313
Functions can help you bring external data into Segment ([Source functions](/docs/connections/functions/source-functions)) and send data in Segment out to external destinations ([Destination functions](/docs/connections/functions/destination-functions)). Use [Insert functions](/docs/connections/functions/insert-functions) to transform data before it reaches your downstream destinations. Functions are scoped to your specific workspace. If you're a technology partner and want to build a new integration and publish it in Segment's catalog, see the [Developer Center documentation](/docs/partners/).
1414

15+
### Variable scoping in Functions
16+
Functions are powered by AWS Lambda functions on the backend. This means you will need to declare any settings variables you create in the function handler rather than globally in your function.
17+
18+
- For Source functions, the handler is onRequest().
19+
- For Destination functions and Insert functions, the handler is event-specfic. For example onTrack(), onIdentify(), etc.
20+
21+
If you declare functions globally in your function code, you risk leaking those settings values across function instances if you have more than one per function codebase. Make sure that you scope settings to the handler functions and pass those settings values as arguments to any helper functions that may need them to ensure context remains scoped to each function instance.
22+
23+
Learn more about this at in the [AWS Documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#:~:text=Avoid%20global%20variables,static%20initialization%20performance){:target="_blank"}.
24+
1525
#### Source functions
1626
Source functions receive external data from a webhook and can create Segment events, objects, or both. Source functions have access to the full power of JavaScript so you can validate and transform the incoming data and even make external API requests to annotate your data.
1727

0 commit comments

Comments
 (0)