1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ActionContextKey = exports.ActionContext = void 0;
|
4 | const immutable_1 = require("immutable");
|
5 |
|
6 |
|
7 |
|
8 | class ActionContext {
|
9 | constructor(data = {}) {
|
10 | this.map = (0, immutable_1.Map)(data);
|
11 | }
|
12 | |
13 |
|
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 |
|
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 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | static ensureActionContext(maybeActionContext) {
|
75 | return maybeActionContext instanceof ActionContext ?
|
76 | maybeActionContext :
|
77 | new ActionContext((0, immutable_1.Map)(maybeActionContext || {}));
|
78 | }
|
79 | }
|
80 | exports.ActionContext = ActionContext;
|
81 |
|
82 |
|
83 |
|
84 | class ActionContextKey {
|
85 | constructor(name) {
|
86 | this.name = name;
|
87 | }
|
88 | }
|
89 | exports.ActionContextKey = ActionContextKey;
|
90 |
|
\ | No newline at end of file |