Constructor
new Context(objopt)
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
obj |
object |
<optional> |
{} |
The object for destructuring Properties
|
Properties:
Name | Type | Description |
---|---|---|
body |
bodyCallback |
function that does the main work, but which needs code that sets up or prepares before executing, and/or needs code that tears down or completes. doc |
head |
headTailCallback |
callback that does the set up or preparation. |
tail |
headTailCallback |
callback that does the tear down or post |
error |
errorCallback |
block of code that is invoked upon error occurring. By default does nothing. If defined and returns |
execute |
function |
invokes the |
state |
any |
holds state, represented by |
Examples
// create a context which has `object` as initial state
const context = ContextManager.create()
context.head = function (param) {
this.value = param
};
context.body = function (param) {
Logger.log(this);
}
context.execute('value'); // outputs {value: 'value'}
// same as above, except with callbacks
const context = ContextManager.create({}, {
head: function (param) {
this.value = param;
},
body: function (param) {
Logger.log(this);
}
});
context.execute('value'); // outputs {value: 'value'}
// creates a context where state is an array
const context = ContextManager.create([]); // send array as first parameter
context.head = function (param) {
this.push(param - 1)
};
context.body = function (param) {
this.push(param);
}
context.tail = function (param) {
this.push(param + 1);
}
context.execute(2);
Logger.log(context.state); // [1, 2, 3]