Global

Methods

create(stateopt, callbacksopt) → {Context}

Creates and returns a context object

Parameters:
Name Type Attributes Default Description
state any <optional>
{}

the initial value of the context's state property available as this in callbacks

callbacks object <optional>
{}
Properties
Name Type Attributes Description
body bodyCallback <optional>

see callback

head headTailCallback <optional>

see callback

tail headTailCallback <optional>

see callback

tail errorCallback <optional>

see callback

Source:
See:

execute(obj) → {any}

A convenience function that creates and executes a context. See create for parameter callbacks specification. Since it executes immediately, callbacks and callbacks.body is required.

Parameters:
Name Type Description
obj object

The object to be destructured

Properties
Name Type Attributes Default Description
param any <optional>
null

The parameter to pass on to callbacks

state any <optional>
{}

Initial state

callbacks object

Callback, which at least must have body defined

Properties
Name Type Description
body bodyCallback

body callback

Source:
See:

klass() → {ContextManager}

Returns the class for advanced usage patterns

Source:
Example
// get the class object so we can potentially override functionality
const Context = ContextManager.klass();
class MyContext(Context) {
    ...
}
const context = new MyContext();

usingWaitLock(timeoutopt, guardopt, dependenciesopt) → {Context}

Creates a context manager with predefined head and tail, useful for using lock service in tandem with spreadsheet services

Parameters:
Name Type Attributes Description
timeout Number <optional>

the parameter passed to waitLock in the head method

guard String <optional>

a "guard" is referring to methods of LockService; value of user converts to getUserLock, script converts to getScriptLock and document converts to getDocumentLock

dependencies Object <optional>

For mock tests using dependency injection

Properties
Name Type Description
Lock_Service Object

for mocking .getScriptLock and .waitLock

Source:
See:
Example
// same as this:
const ctx = ContextManager.usingWaitLock(500, 'script');
ctx.body = function (param) {
  // do your work here
  return param + 1;
};
// run it
// first creates lock for you with timeout of 500
// then executes your body function above
// then releases lock for you
const result = ctx.execute(1);  X
Logger.log(result);  // 2

Type Definitions

bodyCallback(paramopt) → {any}

The body callback for an instance of Context. The value returned via the return value of execute. State can be changed by using the this property.

NOTE: If this callback is defined as an arrow function, this will be undefined

Parameters:
Name Type Attributes Description
param any <optional>

the value passed to from execute

Source:
Example
const context = ContextManager.create();
context.body = function (param) {
    Logger.log(param);
};
context.execute('hello from body callback');

errorCallback(error) → {any}

The error callback is invoked whenever an error is encountered in the head, body, or tail callbacks of an instance of Context. Returning null will swallow the error, and the error object will be returned via execute. If the body was executed and returned before the error was encountered, the body's result is available via the ctx.body.result property on the Error object.

Parameters:
Name Type Description
error Error

The error object that was encountered

Source:
Example
// defining an error callback and returning `null` instructs the context to not re-raise it, to "swallow" it
const context = ContextManager.create();
context.error = function (err) {
    return null;
};
context.body = function (param) {
    if (param === "bad")
        throw new Error("some fake error");
};
const result = context.execute("bad");
result instanceof Error;  // true
result.message;  // "some fake error"

headTailCallback(paramopt) → {void}

The head or tail callback for an instance of Context. The value returned is ignored and has no effect. State can be changed by using the this property.

NOTE: If this callback is defined as an arrow function, this will be undefined

Parameters:
Name Type Attributes Description
param any <optional>

the value passed to from execute

Source: