@schukai/monster 3.12.2 → 3.13.0
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -0,0 +1,122 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright schukai GmbH and contributors 2022. All Rights Reserved.
|
3
|
+
* Node module: @schukai/monster
|
4
|
+
* This file is licensed under the AGPLv3 License.
|
5
|
+
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
|
+
*/
|
7
|
+
|
8
|
+
import { instanceSymbol } from "../../constants.mjs";
|
9
|
+
import { isObject } from "../../types/is.mjs";
|
10
|
+
import { Datasource } from "../datasource.mjs";
|
11
|
+
|
12
|
+
export {DomStorage};
|
13
|
+
|
14
|
+
|
15
|
+
/**
|
16
|
+
* The DomStorage is a class that stores data in memory.
|
17
|
+
*
|
18
|
+
* @license AGPLv3
|
19
|
+
* @copyright schukai GmbH
|
20
|
+
* @memberOf Monster.Data.Datasource
|
21
|
+
*/
|
22
|
+
class DomStorage extends Datasource {
|
23
|
+
/**
|
24
|
+
* @param {Object} [options] options contains definitions for the datasource.
|
25
|
+
*/
|
26
|
+
constructor(options) {
|
27
|
+
super();
|
28
|
+
|
29
|
+
if (isObject(options)) {
|
30
|
+
this.setOptions(options);
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* This method is called by the `instanceof` operator.
|
36
|
+
* @returns {symbol}
|
37
|
+
*/
|
38
|
+
static get [instanceSymbol]() {
|
39
|
+
return Symbol.for("@schukai/monster/data/datasource/storage/dom-storage");
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* @property {Object} defaults
|
44
|
+
* @property {Object} defaults.read
|
45
|
+
* @property {string} defaults.read.selector
|
46
|
+
* @property {Object} defaults.write
|
47
|
+
* @property {string} defaults.write.selector
|
48
|
+
*/
|
49
|
+
get defaults() {
|
50
|
+
return Object.assign({}, super.defaults, {
|
51
|
+
read: {
|
52
|
+
selector: undefined,
|
53
|
+
},
|
54
|
+
write: {
|
55
|
+
selector: undefined,
|
56
|
+
}
|
57
|
+
});
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* @return {Promise}
|
62
|
+
* @throws {Error} The read selector is not defined
|
63
|
+
* @throws {Error} There are no storage element
|
64
|
+
*/
|
65
|
+
read() {
|
66
|
+
const self = this;
|
67
|
+
|
68
|
+
let selector = self.getOption("read.selector", undefined);
|
69
|
+
if (!selector) {
|
70
|
+
throw new Error("The read selector is not defined");
|
71
|
+
}
|
72
|
+
|
73
|
+
let storage = document.querySelector(selector);
|
74
|
+
if (!storage) {
|
75
|
+
throw new Error("There are no storage element");
|
76
|
+
}
|
77
|
+
|
78
|
+
return new Promise((resolve, reject) => {
|
79
|
+
try {
|
80
|
+
let data = JSON.parse(storage.innerHTML);
|
81
|
+
self.set(data)
|
82
|
+
resolve(data);
|
83
|
+
} catch (e) {
|
84
|
+
reject(e);
|
85
|
+
}
|
86
|
+
;
|
87
|
+
})
|
88
|
+
}
|
89
|
+
|
90
|
+
/**
|
91
|
+
* @return {Promise}
|
92
|
+
* @throws {Error} The write selector is not defined
|
93
|
+
* @throws {Error} There are no storage element
|
94
|
+
*/
|
95
|
+
write() {
|
96
|
+
|
97
|
+
const self = this;
|
98
|
+
|
99
|
+
let selector = self.getOption("write.selector");
|
100
|
+
if (!selector) {
|
101
|
+
throw new Error("The write selector is not defined");
|
102
|
+
}
|
103
|
+
|
104
|
+
let storage = document.querySelector(selector);
|
105
|
+
if (!storage) {
|
106
|
+
throw new Error("There are no storage element");
|
107
|
+
}
|
108
|
+
|
109
|
+
return new Promise((resolve, reject) => {
|
110
|
+
try {
|
111
|
+
storage.innerHTML = JSON.stringify(self.get());
|
112
|
+
resolve(storage);
|
113
|
+
} catch (e) {
|
114
|
+
reject(e);
|
115
|
+
}
|
116
|
+
})
|
117
|
+
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
package/source/types/version.mjs
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
import {expect} from "chai"
|
2
|
+
import {DomStorage} from "../../../../../application/source/data/datasource/dom.mjs";
|
3
|
+
|
4
|
+
|
5
|
+
describe('ServeDomr', function () {
|
6
|
+
|
7
|
+
it('should init', function () {
|
8
|
+
|
9
|
+
const dom = new DomStorage({
|
10
|
+
|
11
|
+
})
|
12
|
+
|
13
|
+
expect(dom).to.be.not.null
|
14
|
+
|
15
|
+
|
16
|
+
})
|
17
|
+
|
18
|
+
});
|
package/test/cases/monster.mjs
CHANGED