@schukai/monster 4.148.4 → 4.148.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.6"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.148.4"}
1
+ {"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.6"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.148.5"}
@@ -20,6 +20,22 @@ import { ATTRIBUTE_PREFIX } from "../../dom/constants.mjs";
20
20
  */
21
21
  const STYLE_DISPLAY_MODE_BLOCK = "block";
22
22
 
23
+ /**
24
+ * Datasource lifecycle operation for read requests.
25
+ *
26
+ * @since 4.148.5
27
+ * @type {string}
28
+ */
29
+ const DATASOURCE_OPERATION_READ = "read";
30
+
31
+ /**
32
+ * Datasource lifecycle operation for write requests.
33
+ *
34
+ * @since 4.148.5
35
+ * @type {string}
36
+ */
37
+ const DATASOURCE_OPERATION_WRITE = "write";
38
+
23
39
  /**
24
40
  * This attribute `data-monster-datasource` can be used to pass a datasource.
25
41
  *
@@ -105,6 +121,8 @@ const ATTRIBUTE_DATATABLE_MODE_HIDDEN = "hidden";
105
121
  const ATTRIBUTE_DATATABLE_MODE_VISIBLE = "visible";
106
122
 
107
123
  export {
124
+ DATASOURCE_OPERATION_READ,
125
+ DATASOURCE_OPERATION_WRITE,
108
126
  ATTRIBUTE_DATASOURCE,
109
127
  ATTRIBUTE_DATASOURCE_SELECTOR,
110
128
  ATTRIBUTE_DATASOURCE_ARGUMENTS,
@@ -31,6 +31,10 @@ import { Observer } from "../../../types/observer.mjs";
31
31
  import { Pathfinder } from "../../../data/pathfinder.mjs";
32
32
  import { fireCustomEvent } from "../../../dom/events.mjs";
33
33
  import { isArray, isFunction, isObject, isString } from "../../../types/is.mjs";
34
+ import {
35
+ DATASOURCE_OPERATION_READ,
36
+ DATASOURCE_OPERATION_WRITE,
37
+ } from "../constants.mjs";
34
38
 
35
39
  export { Rest };
36
40
 
@@ -318,6 +322,9 @@ class Rest extends Datasource {
318
322
  * @fires monster-datasource-fetch
319
323
  * @fires monster-datasource-fetched
320
324
  * @fires monster-datasource-error
325
+ * @fires monster-datasource-validation
326
+ * @description Lifecycle event details contain `datasource` and
327
+ * `operation: "read"` for this request.
321
328
  */
322
329
  read() {
323
330
  const opt = clone(this.getOption("read"));
@@ -372,6 +379,7 @@ class Rest extends Datasource {
372
379
  return new Promise((resolve, reject) => {
373
380
  fireCustomEvent(this, "monster-datasource-fetch", {
374
381
  datasource: this,
382
+ operation: DATASOURCE_OPERATION_READ,
375
383
  });
376
384
 
377
385
  queueMicrotask(() => {
@@ -381,15 +389,18 @@ class Rest extends Datasource {
381
389
  if (this[readRequestIdSymbol] === requestId) {
382
390
  fireCustomEvent(this, "monster-datasource-fetched", {
383
391
  datasource: this,
392
+ operation: DATASOURCE_OPERATION_READ,
384
393
  });
385
394
  }
386
395
 
387
396
  resolve(response);
388
397
  })
389
398
  .catch((error) => {
390
- handleValidationError.call(this, error);
399
+ handleValidationError.call(this, error, DATASOURCE_OPERATION_READ);
391
400
  if (this[readRequestIdSymbol] === requestId) {
392
401
  fireCustomEvent(this, "monster-datasource-error", {
402
+ datasource: this,
403
+ operation: DATASOURCE_OPERATION_READ,
393
404
  error: error,
394
405
  });
395
406
 
@@ -404,6 +415,12 @@ class Rest extends Datasource {
404
415
  /**
405
416
  * Writes the data to the rest api.
406
417
  * @return {Promise}
418
+ * @fires monster-datasource-fetch
419
+ * @fires monster-datasource-fetched
420
+ * @fires monster-datasource-error
421
+ * @fires monster-datasource-validation
422
+ * @description Lifecycle event details contain `datasource` and
423
+ * `operation: "write"` for this request.
407
424
  */
408
425
  write() {
409
426
  const opt = clone(this.getOption("write"));
@@ -435,6 +452,7 @@ class Rest extends Datasource {
435
452
  return new Promise((resolve, reject) => {
436
453
  fireCustomEvent(this, "monster-datasource-fetch", {
437
454
  datasource: this,
455
+ operation: DATASOURCE_OPERATION_WRITE,
438
456
  });
439
457
 
440
458
  queueMicrotask(() => {
@@ -444,15 +462,18 @@ class Rest extends Datasource {
444
462
  if (this[writeRequestIdSymbol] === requestId) {
445
463
  fireCustomEvent(this, "monster-datasource-fetched", {
446
464
  datasource: this,
465
+ operation: DATASOURCE_OPERATION_WRITE,
447
466
  });
448
467
  }
449
468
 
450
469
  resolve(response);
451
470
  })
452
471
  .catch((error) => {
453
- handleValidationError.call(this, error);
472
+ handleValidationError.call(this, error, DATASOURCE_OPERATION_WRITE);
454
473
  if (this[writeRequestIdSymbol] === requestId) {
455
474
  fireCustomEvent(this, "monster-datasource-error", {
475
+ datasource: this,
476
+ operation: DATASOURCE_OPERATION_WRITE,
456
477
  error: error,
457
478
  });
458
479
 
@@ -662,9 +683,10 @@ function initIntersectionObserver() {
662
683
  /**
663
684
  * @private
664
685
  * @param {Error} error
686
+ * @param {string} operation
665
687
  * @return {Promise<void>}
666
688
  */
667
- function handleValidationError(error) {
689
+ function handleValidationError(error, operation) {
668
690
  const path = this.getOption("response.path.validationErrors");
669
691
  if (!path) {
670
692
  return Promise.resolve();
@@ -723,6 +745,8 @@ function handleValidationError(error) {
723
745
  new CustomEvent("monster-datasource-validation", {
724
746
  bubbles: true,
725
747
  detail: {
748
+ datasource: this,
749
+ operation,
726
750
  errors: normalizedErrors,
727
751
  raw: rawErrors,
728
752
  message,
@@ -37,7 +37,10 @@ import { Observer } from "../../types/observer.mjs";
37
37
  import { TokenList } from "../../types/tokenlist.mjs";
38
38
  import { clone } from "../../util/clone.mjs";
39
39
  import { State } from "../form/types/state.mjs";
40
- import { ATTRIBUTE_DATASOURCE_SELECTOR } from "./constants.mjs";
40
+ import {
41
+ ATTRIBUTE_DATASOURCE_SELECTOR,
42
+ DATASOURCE_OPERATION_WRITE,
43
+ } from "./constants.mjs";
41
44
  import { Datasource } from "./datasource.mjs";
42
45
  import { Rest as RestDatasource } from "./datasource/rest.mjs";
43
46
  import { BadgeStyleSheet } from "../stylesheet/badge.mjs";
@@ -203,7 +206,10 @@ class SaveButton extends CustomElement {
203
206
  }
204
207
 
205
208
  if (element instanceof RestDatasource) {
206
- element.addEventListener("monster-datasource-fetch", () => {
209
+ element.addEventListener("monster-datasource-fetch", (event) => {
210
+ if (event?.detail?.operation === DATASOURCE_OPERATION_WRITE) {
211
+ return;
212
+ }
207
213
  if (self[saveInFlightSymbol]) {
208
214
  self[pendingResetSymbol] = true;
209
215
  return;
@@ -211,15 +217,20 @@ class SaveButton extends CustomElement {
211
217
  self[fetchInFlightSymbol] = true;
212
218
  clearOriginValues.call(self);
213
219
  });
214
- element.addEventListener("monster-datasource-fetched", () => {
215
- self[fetchInFlightSymbol] = false;
220
+ element.addEventListener("monster-datasource-fetched", (event) => {
221
+ if (event?.detail?.operation !== DATASOURCE_OPERATION_WRITE) {
222
+ self[fetchInFlightSymbol] = false;
223
+ }
216
224
  setOriginValues.call(
217
225
  self,
218
226
  clone(self[datasourceLinkedElementSymbol].data),
219
227
  );
220
228
  updateChangesState.call(self);
221
229
  });
222
- element.addEventListener("monster-datasource-error", () => {
230
+ element.addEventListener("monster-datasource-error", (event) => {
231
+ if (event?.detail?.operation === DATASOURCE_OPERATION_WRITE) {
232
+ return;
233
+ }
223
234
  self[fetchInFlightSymbol] = false;
224
235
  });
225
236
  }
@@ -0,0 +1,299 @@
1
+ import * as chai from "chai";
2
+ import { chaiDom } from "../../../util/chai-dom.mjs";
3
+ import { initJSDOM } from "../../../util/jsdom.mjs";
4
+
5
+ const expect = chai.expect;
6
+ chai.use(chaiDom);
7
+
8
+ let DataFetchError;
9
+
10
+ const waitForUpdates = () => new Promise((resolve) => setTimeout(resolve, 30));
11
+
12
+ function createDatasource(id) {
13
+ const datasource = document.createElement("monster-datasource-rest");
14
+ datasource.id = id;
15
+ datasource.setOption("features.autoInit", false);
16
+ datasource.setOption("read.url", "/records/1");
17
+ datasource.setOption("write.url", "/records/1");
18
+ return datasource;
19
+ }
20
+
21
+ function listenForLifecycle(datasource) {
22
+ const events = [];
23
+ for (const type of [
24
+ "monster-datasource-fetch",
25
+ "monster-datasource-fetched",
26
+ "monster-datasource-error",
27
+ "monster-datasource-validation",
28
+ ]) {
29
+ datasource.addEventListener(type, (event) => {
30
+ events.push({ type, detail: event.detail });
31
+ });
32
+ }
33
+ return events;
34
+ }
35
+
36
+ function createSaveButton(datasourceSelector) {
37
+ const button = document.createElement("monster-datasource-save-button");
38
+ button.setOption("datasource.selector", datasourceSelector);
39
+ button.setOption("disableWhenNoChanges", true);
40
+ return button;
41
+ }
42
+
43
+ function getStateButton(button) {
44
+ return button.shadowRoot?.querySelector("monster-state-button");
45
+ }
46
+
47
+ async function loadDatasource(datasource, data) {
48
+ datasource.datasource.read = () => {
49
+ datasource.data = data;
50
+ return Promise.resolve({ ok: true });
51
+ };
52
+ await datasource.read();
53
+ await waitForUpdates();
54
+ }
55
+
56
+ function clickSaveButton(button) {
57
+ const action = getStateButton(button)?.getOption("actions.click");
58
+ expect(action).to.be.a("function");
59
+ action();
60
+ }
61
+
62
+ function expectPendingChanges(button) {
63
+ expect(Number(button.getOption("changes"))).to.be.greaterThan(0);
64
+ expect(getStateButton(button)?.getOption("disabled")).to.equal(false);
65
+ }
66
+
67
+ function expectNoPendingChanges(button) {
68
+ expect(button.getOption("changes")).to.equal("");
69
+ expect(getStateButton(button)?.getOption("disabled")).to.equal(true);
70
+ }
71
+
72
+ describe("REST datasource lifecycle", function () {
73
+ before(async function () {
74
+ await initJSDOM();
75
+ globalThis.location = window.location;
76
+ await import("element-internals-polyfill").catch(() => {});
77
+ await import("../../../../source/components/datatable/datasource/rest.mjs");
78
+ await import("../../../../source/components/datatable/save-button.mjs");
79
+ ({ DataFetchError } = await import(
80
+ "../../../../source/data/datasource/server/restapi/data-fetch-error.mjs"
81
+ ));
82
+ });
83
+
84
+ beforeEach(() => {
85
+ document.getElementById("mocks").innerHTML = "";
86
+ });
87
+
88
+ afterEach(() => {
89
+ document.getElementById("mocks").innerHTML = "";
90
+ });
91
+
92
+ it("identifies read and write lifecycle events without changing event names", async function () {
93
+ const datasource = createDatasource("lifecycle-datasource");
94
+ const events = listenForLifecycle(datasource);
95
+ document.getElementById("mocks").appendChild(datasource);
96
+
97
+ datasource.datasource.read = () => Promise.resolve({ ok: true });
98
+ await datasource.read();
99
+
100
+ datasource.datasource.write = () => Promise.resolve({ ok: true });
101
+ await datasource.write();
102
+
103
+ expect(
104
+ events.map(({ type, detail }) => [type, detail.operation]),
105
+ ).to.deep.equal([
106
+ ["monster-datasource-fetch", "read"],
107
+ ["monster-datasource-fetched", "read"],
108
+ ["monster-datasource-fetch", "write"],
109
+ ["monster-datasource-fetched", "write"],
110
+ ]);
111
+ events.forEach(({ detail }) => {
112
+ expect(detail.datasource).to.equal(datasource);
113
+ });
114
+ });
115
+
116
+ it("identifies write validation and error events", async function () {
117
+ const datasource = createDatasource("validation-datasource");
118
+ datasource.setOption(
119
+ "response.path.validationErrors",
120
+ "meta.validationErrors",
121
+ );
122
+ datasource.setOption("response.path.validationMessage", "meta.message");
123
+ const events = listenForLifecycle(datasource);
124
+ document.getElementById("mocks").appendChild(datasource);
125
+
126
+ const response = new Response(
127
+ JSON.stringify({
128
+ meta: {
129
+ message: "Input data is invalid",
130
+ validationErrors: {
131
+ parentId: { code: "VAL_INVALID", message: "Invalid parent" },
132
+ },
133
+ },
134
+ }),
135
+ { status: 400, headers: { "content-type": "application/json" } },
136
+ );
137
+ datasource.datasource.write = () =>
138
+ Promise.reject(new DataFetchError("Input data is invalid", response));
139
+
140
+ await datasource.write().catch(() => {});
141
+ await waitForUpdates();
142
+
143
+ const validation = events.find(
144
+ ({ type }) => type === "monster-datasource-validation",
145
+ );
146
+ const error = events.find(
147
+ ({ type }) => type === "monster-datasource-error",
148
+ );
149
+ expect(validation?.detail.operation).to.equal("write");
150
+ expect(validation?.detail.datasource).to.equal(datasource);
151
+ expect(validation?.detail.errors.parentId).to.deep.equal({
152
+ code: "VAL_INVALID",
153
+ message: "Invalid parent",
154
+ });
155
+ expect(error?.detail.operation).to.equal("write");
156
+ expect(error?.detail.datasource).to.equal(datasource);
157
+ });
158
+
159
+ it("keeps every save button on the loaded baseline after a rejected write", async function () {
160
+ const datasource = createDatasource("rejected-write-datasource");
161
+ const loaded = {
162
+ data: [{ id: "area-1", name: "Inbound", parentAreaId: "" }],
163
+ };
164
+ const invalid = {
165
+ data: [{ id: "area-1", name: "Inbound changed", parentAreaId: "area-1" }],
166
+ };
167
+ const corrected = {
168
+ data: [{ id: "area-1", name: "Inbound changed", parentAreaId: "" }],
169
+ };
170
+ const first = createSaveButton("#rejected-write-datasource");
171
+ const second = createSaveButton("#rejected-write-datasource");
172
+ const mocks = document.getElementById("mocks");
173
+ mocks.append(datasource, first, second);
174
+ await loadDatasource(datasource, loaded);
175
+ expectNoPendingChanges(first);
176
+ expectNoPendingChanges(second);
177
+
178
+ datasource.data = invalid;
179
+ await waitForUpdates();
180
+ expectPendingChanges(first);
181
+ expectPendingChanges(second);
182
+
183
+ datasource.datasource.write = () =>
184
+ Promise.reject(new Error("Rejected write"));
185
+ clickSaveButton(first);
186
+ await waitForUpdates();
187
+ expectPendingChanges(first);
188
+ expectPendingChanges(second);
189
+
190
+ datasource.data = corrected;
191
+ await waitForUpdates();
192
+ expectPendingChanges(first);
193
+ expectPendingChanges(second);
194
+ });
195
+
196
+ it("establishes the current data as every save button baseline after a successful write", async function () {
197
+ const datasource = createDatasource("successful-write-datasource");
198
+ const loaded = { data: [{ id: "area-1", name: "Inbound" }] };
199
+ const edited = { data: [{ id: "area-1", name: "Inbound changed" }] };
200
+ const first = createSaveButton("#successful-write-datasource");
201
+ const second = createSaveButton("#successful-write-datasource");
202
+ const mocks = document.getElementById("mocks");
203
+ mocks.append(datasource, first, second);
204
+ await loadDatasource(datasource, loaded);
205
+ datasource.data = edited;
206
+ await waitForUpdates();
207
+ expectPendingChanges(first);
208
+ expectPendingChanges(second);
209
+
210
+ datasource.datasource.write = () => Promise.resolve({ ok: true });
211
+ clickSaveButton(first);
212
+ await waitForUpdates();
213
+
214
+ expectNoPendingChanges(first);
215
+ expectNoPendingChanges(second);
216
+ });
217
+
218
+ it("replaces every save button baseline after a read", async function () {
219
+ const datasource = createDatasource("read-baseline-datasource");
220
+ const loaded = { data: [{ id: "area-1", name: "Inbound" }] };
221
+ const edited = { data: [{ id: "area-1", name: "Inbound changed" }] };
222
+ const refreshed = { data: [{ id: "area-1", name: "Inbound from server" }] };
223
+ const first = createSaveButton("#read-baseline-datasource");
224
+ const second = createSaveButton("#read-baseline-datasource");
225
+ const mocks = document.getElementById("mocks");
226
+ mocks.append(datasource, first, second);
227
+ await loadDatasource(datasource, loaded);
228
+ datasource.data = edited;
229
+ await waitForUpdates();
230
+ expectPendingChanges(first);
231
+ expectPendingChanges(second);
232
+
233
+ datasource.datasource.read = () => {
234
+ datasource.data = refreshed;
235
+ return Promise.resolve({ ok: true });
236
+ };
237
+ await datasource.read();
238
+ await waitForUpdates();
239
+
240
+ expectNoPendingChanges(first);
241
+ expectNoPendingChanges(second);
242
+ });
243
+
244
+ it("does not let write completion end an in-flight read", async function () {
245
+ const datasource = createDatasource("concurrent-datasource");
246
+ const loaded = { data: [{ id: "area-1", name: "Inbound" }] };
247
+ const firstReadValue = {
248
+ data: [{ id: "area-1", name: "Inbound from server" }],
249
+ };
250
+ const finalReadValue = {
251
+ data: [{ id: "area-1", name: "Inbound from server (final)" }],
252
+ };
253
+ const first = createSaveButton("#concurrent-datasource");
254
+ const second = createSaveButton("#concurrent-datasource");
255
+ const mocks = document.getElementById("mocks");
256
+ mocks.append(datasource, first, second);
257
+ await loadDatasource(datasource, loaded);
258
+ expectNoPendingChanges(first);
259
+ expectNoPendingChanges(second);
260
+
261
+ let resolveRead;
262
+ datasource.datasource.read = () =>
263
+ new Promise((resolve) => {
264
+ resolveRead = resolve;
265
+ });
266
+ const read = datasource.read();
267
+ await waitForUpdates();
268
+ expectNoPendingChanges(first);
269
+ expectNoPendingChanges(second);
270
+
271
+ datasource.datasource.write = () => Promise.resolve({ ok: true });
272
+ await datasource.write();
273
+ expectNoPendingChanges(first);
274
+ expectNoPendingChanges(second);
275
+
276
+ datasource.data = firstReadValue;
277
+ await waitForUpdates();
278
+ expectNoPendingChanges(first);
279
+ expectNoPendingChanges(second);
280
+
281
+ datasource.datasource.write = () =>
282
+ Promise.reject(new Error("Rejected concurrent write"));
283
+ await datasource.write().catch(() => {});
284
+ expectNoPendingChanges(first);
285
+ expectNoPendingChanges(second);
286
+
287
+ datasource.data = finalReadValue;
288
+ await waitForUpdates();
289
+
290
+ expectNoPendingChanges(first);
291
+ expectNoPendingChanges(second);
292
+
293
+ resolveRead({ ok: true });
294
+ await read;
295
+ await waitForUpdates();
296
+ expectNoPendingChanges(first);
297
+ expectNoPendingChanges(second);
298
+ });
299
+ });