@very-simple/ajax 1.0.0-beta.5 → 1.0.0-beta.6

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.
Files changed (2) hide show
  1. package/dist/index.js +37 -24
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -13,14 +13,16 @@ var config = {
13
13
  // src/load.ts
14
14
  var parser = new DOMParser();
15
15
  var currentLoadController;
16
+ var progressDelayTimeout;
16
17
  var progress = 0;
17
18
  var trickleInterval;
18
19
  var emit = (type, payload) => document.dispatchEvent(new CustomEvent(`ajax:${type}`, { detail: payload }));
19
20
  var cache = /* @__PURE__ */ new Map();
20
21
  var load = async (url, regions, options) => {
21
- let progressDelayTimeout;
22
+ let aborted = false;
22
23
  try {
23
24
  currentLoadController?.abort();
25
+ clearTimeout(progressDelayTimeout);
24
26
  currentLoadController = new AbortController();
25
27
  setProgress(0);
26
28
  progressDelayTimeout = window.setTimeout(() => {
@@ -41,16 +43,18 @@ var load = async (url, regions, options) => {
41
43
  const document2 = parseHtml(html);
42
44
  return { document: document2, response };
43
45
  } catch (e) {
44
- setProgress(0);
45
- const isAbortError = e instanceof DOMException && e.name === "AbortError";
46
- if (!isAbortError) {
46
+ aborted = e instanceof DOMException && e.name === "AbortError";
47
+ if (!aborted) {
48
+ setProgress(0);
47
49
  window.location.reload();
48
50
  }
49
51
  } finally {
50
- clearTimeout(progressDelayTimeout);
51
- stopTrickle();
52
- setProgress(1);
53
- setTimeout(() => emit("progress-end", {}), options.progressHideDelay);
52
+ if (!aborted) {
53
+ clearTimeout(progressDelayTimeout);
54
+ stopTrickle();
55
+ setProgress(1);
56
+ setTimeout(() => emit("progress-end", {}), options.progressHideDelay);
57
+ }
54
58
  }
55
59
  };
56
60
  var parseHtml = (html) => parser.parseFromString(html, "text/html");
@@ -58,8 +62,14 @@ var trickle = () => {
58
62
  const amount = -0.095 * progress + 0.1;
59
63
  setProgress(progress + Math.random() * amount);
60
64
  };
61
- var startTrickle = () => trickleInterval = window.setInterval(trickle, 300);
62
- var stopTrickle = () => clearInterval(trickleInterval);
65
+ var startTrickle = () => {
66
+ stopTrickle();
67
+ trickleInterval = window.setInterval(trickle, 300);
68
+ };
69
+ var stopTrickle = () => {
70
+ clearInterval(trickleInterval);
71
+ trickleInterval = void 0;
72
+ };
63
73
  var setProgress = (value) => {
64
74
  progress = value;
65
75
  emit("progress", { progress: value });
@@ -76,7 +86,7 @@ var merge = (region, newRegion, strategy) => {
76
86
  Array.from(
77
87
  newRegion.querySelectorAll(`[${escapedPrefix}permanent][id]`)
78
88
  ).forEach((el) => {
79
- const original = region.querySelector(`#${el.id}`);
89
+ const original = region.querySelector(`#${CSS.escape(el.id)}`);
80
90
  if (original) {
81
91
  permanents.set(el.id, original);
82
92
  original.remove();
@@ -85,13 +95,12 @@ var merge = (region, newRegion, strategy) => {
85
95
  unmount?.(region);
86
96
  if (strategy === "replace") region.replaceWith(newRegion);
87
97
  else region.replaceChildren(...Array.from(newRegion.children));
88
- Array.from(
89
- newRegion.querySelectorAll(`[${escapedPrefix}permanent][id]`)
90
- ).forEach((el) => {
98
+ const target = strategy === "replace" ? newRegion : region;
99
+ target.querySelectorAll(`[${escapedPrefix}permanent][id]`).forEach((el) => {
91
100
  const original = permanents.get(el.id);
92
101
  if (original) el.replaceWith(original);
93
102
  });
94
- mount?.(strategy === "replace" ? newRegion : region);
103
+ mount?.(target);
95
104
  } else if (strategy === "before") {
96
105
  region.insertAdjacentElement("beforebegin", newRegion);
97
106
  mount?.(newRegion);
@@ -108,6 +117,7 @@ var merge = (region, newRegion, strategy) => {
108
117
  const result = morph(region, newRegion);
109
118
  autoFocusEl = result.autoFocusEl;
110
119
  }
120
+ if (strategy === "morph") return { autoFocusEl };
111
121
  if (newRegion.hasAttribute(`${prefix}autofocus`) || newRegion.autofocus) {
112
122
  autoFocusEl ??= newRegion;
113
123
  }
@@ -117,7 +127,7 @@ var merge = (region, newRegion, strategy) => {
117
127
  var morph = (container, newContainer) => {
118
128
  const { prefix, mount, unmount } = config;
119
129
  const manualReplacements = [];
120
- let autoFocusEls = [];
130
+ const autoFocusEls = [];
121
131
  let currentNodeKeepAttributes = [];
122
132
  Idiomorph.morph(container, newContainer, {
123
133
  callbacks: {
@@ -132,6 +142,7 @@ var morph = (container, newContainer) => {
132
142
  const isSameComponent = oldIsComponent && newIsComponent && oldNode.getAttribute(`${prefix}component`) === newNode.getAttribute(`${prefix}component`);
133
143
  if (!oldIsComponent || isSameComponent) return true;
134
144
  manualReplacements.push([oldNode, newNode]);
145
+ return false;
135
146
  },
136
147
  beforeAttributeUpdated: (name) => {
137
148
  return !currentNodeKeepAttributes.includes(name);
@@ -147,9 +158,9 @@ var morph = (container, newContainer) => {
147
158
  mount?.(node);
148
159
  }
149
160
  },
150
- afterNodeMorphed(_, newNode) {
151
- if (newNode instanceof HTMLElement && (newNode.autofocus || newNode.hasAttribute(`${prefix}autofocus`))) {
152
- autoFocusEls.push(newNode);
161
+ afterNodeMorphed(oldNode) {
162
+ if (oldNode instanceof HTMLElement && (oldNode.autofocus || oldNode.hasAttribute(`${prefix}autofocus`))) {
163
+ autoFocusEls.push(oldNode);
153
164
  }
154
165
  }
155
166
  }
@@ -207,7 +218,7 @@ var emit2 = (type, payload) => {
207
218
  document.dispatchEvent(event);
208
219
  if (waiting.length) return Promise.allSettled(waiting);
209
220
  };
210
- var currentUrl = window.location.pathname;
221
+ var currentUrl = window.location.pathname + window.location.search;
211
222
  var currentState = history.state;
212
223
  var currentVisitController;
213
224
  var scrollPositions = /* @__PURE__ */ new Map();
@@ -254,6 +265,7 @@ var visit = async (url, {
254
265
  }
255
266
  }
256
267
  if (!newDocument) return;
268
+ if (signal.aborted) return;
257
269
  currentUrl = url;
258
270
  currentState = state;
259
271
  if (historyMode === "replace") history.replaceState(state, "", url);
@@ -272,8 +284,9 @@ var visit = async (url, {
272
284
  if (hasMatchingRegions) {
273
285
  for (const id of regions) {
274
286
  const region = document.querySelector(id);
275
- const newRegion = newDocument.querySelector(id);
276
- if (!region || !newRegion) continue;
287
+ const parsedRegion = newDocument.querySelector(id);
288
+ if (!region || !parsedRegion) continue;
289
+ const newRegion = document.importNode(parsedRegion, true);
277
290
  const strategy = getMergeStrategy(region, newRegion);
278
291
  const result = merge(region, newRegion, strategy);
279
292
  autoFocusEl ??= result.autoFocusEl;
@@ -284,7 +297,7 @@ var visit = async (url, {
284
297
  }
285
298
  } else {
286
299
  const region = document.body;
287
- const newRegion = newDocument.body;
300
+ const newRegion = document.importNode(newDocument.body, true);
288
301
  const strategy = getMergeStrategy(region, newRegion);
289
302
  const result = merge(region, newRegion, strategy);
290
303
  autoFocusEl = result.autoFocusEl;
@@ -315,7 +328,7 @@ var visit = async (url, {
315
328
  await mergeRegions();
316
329
  await emit2("after-swap", { from, to, isBackForward, signal });
317
330
  applyScrollBehavior();
318
- }).ready;
331
+ }).updateCallbackDone;
319
332
  } else {
320
333
  if (morphHeads) Idiomorph2.morph(document.head, newDocument.head);
321
334
  await mergeRegions();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@very-simple/ajax",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.6",
4
4
  "description": "A very simple turbolinks inspired ajax library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",