@optionfactory/ful 1.0.2 → 1.0.3

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/dist/ful.iife.js CHANGED
@@ -950,6 +950,65 @@ var ful = (function (exports, ftl) {
950
950
  }
951
951
  }
952
952
 
953
+ class AsyncEvents {
954
+ static async fireAsync(el, evt) {
955
+ el.dispatchEvent(evt);
956
+ return await evt.async?.promise;
957
+ }
958
+ /**
959
+ *
960
+ * @param {*} el
961
+ * @param {*} type
962
+ * @param {*} fn returning the result
963
+ * @param {*} options
964
+ * @returns
965
+ */
966
+ static asyncOn(el, type, fn, options) {
967
+ const listener = async (event) => {
968
+ let resolve, reject;
969
+ const promise = new Promise((res, rej) => {
970
+ resolve = res;
971
+ reject = rej;
972
+ });
973
+ event.async = { promise };
974
+ try {
975
+ //@ts-ignore
976
+ resolve(await fn(event));
977
+ } catch (e) {
978
+ //@ts-ignore
979
+ reject(e);
980
+ }
981
+ };
982
+ el.addEventListener(type, listener, options);
983
+ return listener;
984
+ }
985
+ /**
986
+ *
987
+ * @param {*} el
988
+ * @param {*} type
989
+ * @param {*} listener the listener returned by asyncOn
990
+ * @param {*} options
991
+ */
992
+ static asyncOff(el, type, listener, options) {
993
+ el.removeEventListener(type, listener, options);
994
+ }
995
+ static mixInto(...classes) {
996
+ for (const k of classes) {
997
+ Object.assign(k.prototype, {
998
+ async fireAsync(evt) {
999
+ return await AsyncEvents.fireAsync(this, evt);
1000
+ },
1001
+ asyncOn(type, fn, options) {
1002
+ return AsyncEvents.asyncOn(this, type, fn, options);
1003
+ },
1004
+ asyncOff(type, listener, options) {
1005
+ return AsyncEvents.asyncOff(this, type, listener, options);
1006
+ }
1007
+ });
1008
+ }
1009
+ }
1010
+ }
1011
+
953
1012
  const timing = {
954
1013
  sleep(ms) {
955
1014
  return new Promise(resolve => setTimeout(resolve, ms));
@@ -1244,8 +1303,8 @@ var ful = (function (exports, ftl) {
1244
1303
  async prepare(values, form) {
1245
1304
  return await this.#requestMapper(values, form);
1246
1305
  }
1247
- async submit(values, form) {
1248
- return values;
1306
+ async submit(values, form, response) {
1307
+ return response;
1249
1308
  }
1250
1309
  async transform(response, form) {
1251
1310
  return await this.#responseMapper(response, form);
@@ -1287,13 +1346,17 @@ var ful = (function (exports, ftl) {
1287
1346
  try {
1288
1347
  const loader = Loaders.fromAttributes(this, 'loaders:form');
1289
1348
  const values = this.values;
1290
- const request = await loader.prepare(values, this);
1291
- const se = new CustomEvent('submit', { bubbles: true, cancelable: true, detail: { values, request } });
1292
- if (!this.dispatchEvent(se)) {
1293
- return;
1294
- }
1349
+ let request = await loader.prepare(values, this);
1295
1350
  try {
1296
- const response = await loader.submit(se.detail.request, this);
1351
+ const se = new CustomEvent('submit', { bubbles: true, cancelable: true, detail: { values, request } });
1352
+ if (!this.dispatchEvent(se)) {
1353
+ return;
1354
+ }
1355
+ const sre = new CustomEvent('submit:requested', { bubbles: true, cancelable: false, detail: { values: se.detail.values, request: se.detail.request} });
1356
+ let response = await AsyncEvents.fireAsync(this, sre);
1357
+ request = sre.detail.request;
1358
+
1359
+ response = await loader.submit(request, this, response);
1297
1360
  const mapped = await loader.transform(response, this);
1298
1361
  this.dispatchEvent(new CustomEvent('submit:success', { bubbles: true, cancelable: false, detail: { values, request, response: mapped } }));
1299
1362
  } catch (e) {
@@ -1301,12 +1364,13 @@ var ful = (function (exports, ftl) {
1301
1364
  if (e instanceof Failure) {
1302
1365
  this.errors = e.problems;
1303
1366
  }
1304
- throw e;
1367
+ console.warn("failed to submit form", this, "reason:", e);
1305
1368
  }
1306
1369
  } finally {
1307
1370
  this.spinner(false);
1308
1371
  }
1309
1372
  }
1373
+
1310
1374
  spinner(spin) {
1311
1375
  this.querySelectorAll('ful-spinner').forEach(el => {
1312
1376
  const hel = /** @type HTMLElement */ (el);
@@ -2579,6 +2643,7 @@ var ful = (function (exports, ftl) {
2579
2643
  }
2580
2644
  }
2581
2645
 
2646
+ exports.AsyncEvents = AsyncEvents;
2582
2647
  exports.AuthorizationCodeFlow = AuthorizationCodeFlow;
2583
2648
  exports.AuthorizationCodeFlowInterceptor = AuthorizationCodeFlowInterceptor;
2584
2649
  exports.AuthorizationCodeFlowSession = AuthorizationCodeFlowSession;