@faable/challenge 1.1.0 → 1.3.0

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/index.js CHANGED
@@ -303,11 +303,26 @@ var announce = (state, onChallenge) => {
303
303
  );
304
304
  }
305
305
  };
306
+ var announceBadge = (state) => {
307
+ if (typeof window !== "undefined" && typeof CustomEvent === "function") {
308
+ window.dispatchEvent(
309
+ new CustomEvent(CHALLENGE_EVENT, { detail: { state } })
310
+ );
311
+ }
312
+ };
306
313
  var postWithChallenge = async (send, onChallenge) => {
307
314
  var _a;
308
- const first = await send();
315
+ announceBadge("solving");
316
+ let first;
317
+ try {
318
+ first = await send();
319
+ } catch (e) {
320
+ announceBadge("idle");
321
+ throw e;
322
+ }
309
323
  const details = (_a = first.data) == null ? void 0 : _a.details;
310
324
  if (!first.error || first.code !== "challenge_required" && first.code !== "challenge_invalid" || !isChallengeDetails(details)) {
325
+ announceBadge(first.error ? "idle" : "done");
311
326
  return first;
312
327
  }
313
328
  announce("solving", onChallenge);
@@ -1422,7 +1437,7 @@ var mountChallengeWidget = (el, opts = {}, driver = {}) => {
1422
1437
  pending = null;
1423
1438
  }
1424
1439
  const shown = Date.now() - solvingSince;
1425
- if (state === "solving" && (next === "done" || next === "error") && shown < MIN_SOLVING_MS && !reducedMotion()) {
1440
+ if (state === "solving" && (next === "done" || next === "error" || next === "idle") && shown < MIN_SOLVING_MS && !reducedMotion()) {
1426
1441
  pending = setTimeout(() => {
1427
1442
  pending = null;
1428
1443
  apply(next);
@@ -1498,16 +1513,16 @@ var resetChallengeWidgets = (root = document) => {
1498
1513
  const els = root instanceof Element && root.matches("[data-faable-challenge]") ? [root] : Array.from(root.querySelectorAll("[data-faable-challenge]"));
1499
1514
  for (const el of els) (_a = MOUNTED.get(el)) == null ? void 0 : _a.reset();
1500
1515
  };
1501
- var mountChallengeWidgets = (root = document) => {
1502
- root.querySelectorAll("[data-faable-challenge]").forEach((el) => mountChallengeWidget(el));
1516
+ var mountChallengeWidgets = (root = document, driver = {}) => {
1517
+ root.querySelectorAll("[data-faable-challenge]").forEach((el) => mountChallengeWidget(el, {}, driver));
1503
1518
  };
1504
1519
  var watching = false;
1505
- var watchChallengeWidgets = () => {
1520
+ var watchChallengeWidgets = (driver = {}) => {
1506
1521
  if (watching || typeof document === "undefined") return;
1507
1522
  if (typeof window === "undefined" || typeof MutationObserver !== "function")
1508
1523
  return;
1509
1524
  watching = true;
1510
- const scan = () => mountChallengeWidgets(document);
1525
+ const scan = () => mountChallengeWidgets(document, driver);
1511
1526
  if (document.readyState === "loading") {
1512
1527
  document.addEventListener("DOMContentLoaded", scan, { once: true });
1513
1528
  } else {
@@ -1517,8 +1532,10 @@ var watchChallengeWidgets = () => {
1517
1532
  for (const r of records) {
1518
1533
  r.addedNodes.forEach((node) => {
1519
1534
  if (!(node instanceof Element)) return;
1520
- if (node.matches("[data-faable-challenge]")) mountChallengeWidget(node);
1521
- mountChallengeWidgets(node);
1535
+ if (node.matches("[data-faable-challenge]")) {
1536
+ mountChallengeWidget(node, {}, driver);
1537
+ }
1538
+ mountChallengeWidgets(node, driver);
1522
1539
  });
1523
1540
  }
1524
1541
  }).observe(document.documentElement, { childList: true, subtree: true });