UNPKG

2.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ActionContextKey = exports.ActionContext = void 0;
4const immutable_1 = require("immutable");
5/**
6 * Implementation of {@link IActionContext} using Immutable.js.
7 */
8class ActionContext {
9 constructor(data = {}) {
10 this.map = (0, immutable_1.Map)(data);
11 }
12 /**
13 * Will only set the value if the key is not already set.
14 */
15 setDefault(key, value) {
16 return this.has(key) ? this : this.set(key, value);
17 }
18 set(key, value) {
19 return this.setRaw(key.name, value);
20 }
21 setRaw(key, value) {
22 return new ActionContext(this.map.set(key, value));
23 }
24 delete(key) {
25 return new ActionContext(this.map.delete(key.name));
26 }
27 get(key) {
28 return this.getRaw(key.name);
29 }
30 getRaw(key) {
31 return this.map.get(key);
32 }
33 getSafe(key) {
34 if (!this.has(key)) {
35 throw new Error(`Context entry ${key.name} is required but not available`);
36 }
37 return this.get(key);
38 }
39 has(key) {
40 return this.hasRaw(key.name);
41 }
42 hasRaw(key) {
43 return this.map.has(key);
44 }
45 merge(...contexts) {
46 // eslint-disable-next-line @typescript-eslint/no-this-alias,consistent-this
47 let context = this;
48 for (const source of contexts) {
49 for (const key of source.keys()) {
50 context = context.set(key, source.get(key));
51 }
52 }
53 return context;
54 }
55 keys() {
56 return [...this.map.keys()]
57 .map(keyName => new ActionContextKey(keyName));
58 }
59 toJS() {
60 return this.map.toJS();
61 }
62 toString() {
63 return `ActionContext(${JSON.stringify(this.map.toJS())})`;
64 }
65 [Symbol.for('nodejs.util.inspect.custom')]() {
66 return `ActionContext(${JSON.stringify(this.map.toJS(), null, ' ')})`;
67 }
68 /**
69 * Convert the given object to an action context object if it is not an action context object yet.
70 * If it already is an action context object, return the object as-is.
71 * @param maybeActionContext An action context or record.
72 * @return {ActionContext} An action context object.
73 */
74 static ensureActionContext(maybeActionContext) {
75 return maybeActionContext instanceof ActionContext ?
76 maybeActionContext :
77 new ActionContext((0, immutable_1.Map)(maybeActionContext || {}));
78 }
79}
80exports.ActionContext = ActionContext;
81/**
82 * Simple implementation of {@link IActionContextKey}.
83 */
84class ActionContextKey {
85 constructor(name) {
86 this.name = name;
87 }
88}
89exports.ActionContextKey = ActionContextKey;
90//# sourceMappingURL=ActionContext.js.map
\No newline at end of file