Local Storage

Tyr.local provides continuation local storage functionality to Tyranid. Continuation local storage is roughly analogous to thread local storage seen in threaded systems. A sequence of callbacks can be thought of as being roughly similar to a green thread.

Tyranid's local storage is currently built on top of continuation-local-storage (CLP). However, this might change to support zones, asyncWrap, domains, etc. as this functionality will probably be migrated to node.js core. As a result, Tyranid provides a lightweight wrapper around CLP so that if the underlying implementation changes your code should remain as is.

For example, if you wanted to keep track of whether the current user was on a mobile device, you could do:

in startup:

Tyr.local.define('onMobile');

in an Express middleware you provide:

Tyr.local.onMobile = myIsOnMobileFunction();

later on in some code that doesn't have access to the request:

if (Tyr.local.onMobile) { ... }

Client-side Local

Tyr.local is also available on the client and contains data pertaining to the current session.

define(propertyName: string): void

This method defines a binding for a property that you wish to track across async code.

user: User

This returns a reference to the currently-logged-in user if there is a "user" collection defined.

org: Organization

This returns a reference to the currently-logged-in user's organization if there is a "organization" collection defined.

req: Req

This returns a reference to the current Express Request object.

req: Res

This returns a reference to the current Express Response object.