generated from Exabyte-io/template-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
85 lines (85 loc) · 3.39 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImportantSettingsProviderMixin = exports.DomainContextProviderMixin = exports.ContextAndRenderFieldsMixin = void 0;
const clone_1 = require("../../utils/clone");
function ContextAndRenderFieldsMixin(superclass) {
return class extends superclass {
/**
* @see https://stackoverflow.com/questions/64396668/why-do-typescript-mixins-require-a-constructor-with-a-single-rest-parameter-any
* */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(...params) {
super(...params);
this._context = params[0].context || {};
}
// in-memory, or "volatile" context that is reset when the `parent` object is destroyed
get context() {
return this._context;
}
set context(newContext) {
this._context = newContext;
}
updateContext(ctx = {}, executeRender = false) {
this._context = { ...this.context, ...ctx };
if (executeRender)
this.render();
}
// to get "persistent" context, that is stored in database and further should be provided to constructor
// when the `parent` object is re-created
getPersistentContext() {
return this.prop("context");
}
// to make context persistent in `_json`
updatePersistentContext(ctx = {}) {
this.setProp("context", { ...ctx });
}
// to get persistent and volatile context combined
getCombinedContext() {
return { ...this.getPersistentContext(), ...this.context };
}
// override in subclasses
// eslint-disable-next-line @typescript-eslint/no-unused-vars
render(_context = this.context) {
throw new Error("RenderInitMixin: render not implemented in derived class");
}
};
}
exports.ContextAndRenderFieldsMixin = ContextAndRenderFieldsMixin;
/*
* @summary Handles logic for domain-specific context, eg. "important settings".
* Important settings are stored inside "important" property and have context providers associated with it.
*/
function DomainContextProviderMixin(superclass) {
return class extends superclass {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(...args) {
super(...args);
this._contextProviders = [];
}
get contextProviders() {
// override in children
return this._contextProviders;
}
};
}
exports.DomainContextProviderMixin = DomainContextProviderMixin;
function ImportantSettingsProviderMixin(superclass) {
return class extends DomainContextProviderMixin(superclass) {
get important() {
return (0, clone_1.deepClone)(this._json.important || {});
}
setImportant(key, value) {
this.setProp("important", { [key]: value });
}
get importantSettingsProviders() {
return this.contextProviders.filter((p) => p.domain === "important");
}
get isImportantEdited() {
return this.prop("important.isEdited");
}
set isImportantEdited(bool) {
this.setProp("important", Object.assign(this.important, { isEdited: bool }));
}
};
}
exports.ImportantSettingsProviderMixin = ImportantSettingsProviderMixin;