UNPKG

1.64 kBJavaScriptView Raw
1/**
2 * Context constructor.
3 * @memberof module:apeman-commons-invocating/lib
4 * @inner
5 * @constructor InvocationContext
6 * @param {object} values - InvocationContext values.
7 */
8
9"use strict";
10
11/** @lends module:apeman-commons-invocating/lib~InvocationContext */
12function InvocationContext() {
13 var s = this;
14 s.init.apply(s, arguments);
15}
16
17InvocationContext.prototype = {
18 /** @constructs InvocationContext */
19 init: function (values) {
20 var s = this;
21 s.set(values);
22 },
23 /**
24 * Clone this worker.
25 * @returns {InvocationContext} - Clone.
26 */
27 clone: function () {
28 var s = this;
29 var Constructor = s.constructor;
30 return new Constructor(s.get());
31 },
32 /**
33 * Set context values.
34 * @param {object} data - Values to set.
35 * @returns {InvocationContext}
36 */
37 set: function (data) {
38 var s = this;
39 Object.keys(data || {}).forEach(function (key) {
40 s[key] = data[key];
41 });
42 return s;
43 },
44 /**
45 * Get context values.
46 * @returns {*}
47 */
48 get: function () {
49 var s = this;
50 var data = {};
51 Object.keys(s).forEach(function (key) {
52 data[key] = s[key];
53 });
54 return data;
55 },
56 constructor: InvocationContext
57};
58
59/**
60 * Define a context.
61 */
62InvocationContext.define = function (values) {
63 function Defined() {
64 var s = this;
65 s.init.apply(s, arguments);
66 }
67
68 Defined.prototype = new InvocationContext(values);
69 Defined.prototype.constructor = Defined;
70
71 return Defined;
72};
73
74module.exports = InvocationContext;
\No newline at end of file