@inertiajs/core 3.1.1 → 3.2.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
@@ -1701,14 +1701,21 @@ var InitialVisit = class {
1701
1701
 
1702
1702
  // src/poll.ts
1703
1703
  var Poll = class {
1704
- id = null;
1704
+ intervalId = null;
1705
+ timeoutId = null;
1705
1706
  throttle = false;
1706
1707
  keepAlive = false;
1707
1708
  cb;
1708
1709
  interval;
1709
1710
  cbCount = 0;
1711
+ mode;
1712
+ inFlight = false;
1713
+ currentCancel = null;
1714
+ stopped = true;
1715
+ instanceId = 0;
1710
1716
  constructor(interval, cb, options) {
1711
1717
  this.keepAlive = options.keepAlive ?? false;
1718
+ this.mode = options.mode ?? "overlap";
1712
1719
  this.cb = cb;
1713
1720
  this.interval = interval;
1714
1721
  if (options.autoStart ?? true) {
@@ -1716,8 +1723,17 @@ var Poll = class {
1716
1723
  }
1717
1724
  }
1718
1725
  stop() {
1719
- if (this.id) {
1720
- clearInterval(this.id);
1726
+ this.stopped = true;
1727
+ this.instanceId++;
1728
+ this.inFlight = false;
1729
+ this.currentCancel = null;
1730
+ if (this.intervalId) {
1731
+ clearInterval(this.intervalId);
1732
+ this.intervalId = null;
1733
+ }
1734
+ if (this.timeoutId) {
1735
+ clearTimeout(this.timeoutId);
1736
+ this.timeoutId = null;
1721
1737
  }
1722
1738
  }
1723
1739
  start() {
@@ -1725,14 +1741,12 @@ var Poll = class {
1725
1741
  return;
1726
1742
  }
1727
1743
  this.stop();
1728
- this.id = window.setInterval(() => {
1729
- if (!this.throttle || this.cbCount % 10 === 0) {
1730
- this.cb();
1731
- }
1732
- if (this.throttle) {
1733
- this.cbCount++;
1734
- }
1735
- }, this.interval);
1744
+ this.stopped = false;
1745
+ if (this.mode === "rest") {
1746
+ this.scheduleNext();
1747
+ return;
1748
+ }
1749
+ this.intervalId = window.setInterval(() => this.tick(), this.interval);
1736
1750
  }
1737
1751
  isInBackground(hidden2) {
1738
1752
  this.throttle = this.keepAlive ? false : hidden2;
@@ -1740,6 +1754,50 @@ var Poll = class {
1740
1754
  this.cbCount = 0;
1741
1755
  }
1742
1756
  }
1757
+ scheduleNext() {
1758
+ if (this.stopped) {
1759
+ return;
1760
+ }
1761
+ this.timeoutId = window.setTimeout(() => {
1762
+ this.timeoutId = null;
1763
+ this.tick();
1764
+ }, this.interval);
1765
+ }
1766
+ tick() {
1767
+ if (!this.throttle || this.cbCount % 10 === 0) {
1768
+ this.fire();
1769
+ } else if (this.mode === "rest") {
1770
+ this.scheduleNext();
1771
+ }
1772
+ if (this.throttle) {
1773
+ this.cbCount++;
1774
+ }
1775
+ }
1776
+ fire() {
1777
+ if (this.inFlight && this.mode === "cancel") {
1778
+ this.currentCancel?.();
1779
+ }
1780
+ const instance = this.instanceId;
1781
+ this.cb({
1782
+ onStart: (cancel) => {
1783
+ if (instance !== this.instanceId) {
1784
+ return;
1785
+ }
1786
+ this.inFlight = true;
1787
+ this.currentCancel = cancel;
1788
+ },
1789
+ onFinish: () => {
1790
+ if (instance !== this.instanceId) {
1791
+ return;
1792
+ }
1793
+ this.inFlight = false;
1794
+ this.currentCancel = null;
1795
+ if (this.mode === "rest") {
1796
+ this.scheduleNext();
1797
+ }
1798
+ }
1799
+ });
1800
+ }
1743
1801
  };
1744
1802
 
1745
1803
  // src/polls.ts
@@ -1748,12 +1806,19 @@ var Polls = class {
1748
1806
  constructor() {
1749
1807
  this.setupVisibilityListener();
1750
1808
  }
1809
+ get count() {
1810
+ return this.polls.length;
1811
+ }
1751
1812
  add(interval, cb, options) {
1752
1813
  const poll = new Poll(interval, cb, options);
1753
1814
  this.polls.push(poll);
1754
1815
  return {
1755
1816
  stop: () => poll.stop(),
1756
- start: () => poll.start()
1817
+ start: () => poll.start(),
1818
+ destroy: () => {
1819
+ poll.stop();
1820
+ this.polls = this.polls.filter((p) => p !== poll);
1821
+ }
1757
1822
  };
1758
1823
  }
1759
1824
  clear() {
@@ -2928,9 +2993,23 @@ var Router = class {
2928
2993
  }
2929
2994
  return eventHandler.onGlobalEvent(type, callback);
2930
2995
  }
2996
+ once(type, callback) {
2997
+ if (typeof window === "undefined") {
2998
+ return () => {
2999
+ };
3000
+ }
3001
+ const remove2 = this.on(type, (event) => {
3002
+ remove2();
3003
+ return callback(event);
3004
+ });
3005
+ return remove2;
3006
+ }
2931
3007
  hasPendingOptimistic() {
2932
3008
  return this.asyncRequestStream.hasPendingOptimistic();
2933
3009
  }
3010
+ get activePolls() {
3011
+ return polls.count;
3012
+ }
2934
3013
  cancelAll({ async = true, prefetch = true, sync = true } = {}) {
2935
3014
  if (async) {
2936
3015
  this.asyncRequestStream.cancelInFlight({ prefetch });
@@ -2940,10 +3019,29 @@ var Router = class {
2940
3019
  }
2941
3020
  }
2942
3021
  poll(interval, requestOptions = {}, options = {}) {
2943
- return polls.add(interval, () => this.reload({ preserveErrors: true, ...requestOptions }), {
2944
- autoStart: options.autoStart ?? true,
2945
- keepAlive: options.keepAlive ?? false
2946
- });
3022
+ return polls.add(
3023
+ interval,
3024
+ ({ onStart, onFinish }) => {
3025
+ const resolved = typeof requestOptions === "function" ? requestOptions() : requestOptions;
3026
+ this.reload({
3027
+ preserveErrors: true,
3028
+ ...resolved,
3029
+ onCancelToken: (token) => {
3030
+ onStart(token.cancel);
3031
+ resolved.onCancelToken?.(token);
3032
+ },
3033
+ onFinish: (visit) => {
3034
+ onFinish();
3035
+ resolved.onFinish?.(visit);
3036
+ }
3037
+ });
3038
+ },
3039
+ {
3040
+ autoStart: options.autoStart ?? true,
3041
+ keepAlive: options.keepAlive ?? false,
3042
+ mode: options.mode
3043
+ }
3044
+ );
2947
3045
  }
2948
3046
  visit(href, options = {}) {
2949
3047
  options.optimistic = options.optimistic ?? this.pendingOptimisticCallback;
@@ -3868,7 +3966,9 @@ var useInfiniteScrollData = (options) => {
3868
3966
  },
3869
3967
  onFinish: (visit) => {
3870
3968
  state.loading = false;
3871
- side === "next" ? options.onCompleteNextRequest(state.lastLoadedPage) : options.onCompletePreviousRequest(state.lastLoadedPage);
3969
+ const completed = visit.completed;
3970
+ const page3 = completed ? state.lastLoadedPage : null;
3971
+ side === "next" ? options.onCompleteNextRequest(page3, { page: page3, completed }) : options.onCompletePreviousRequest(page3, { page: page3, completed });
3872
3972
  reloadOptions.onFinish?.(visit);
3873
3973
  }
3874
3974
  });
@@ -4220,13 +4320,17 @@ function useInfiniteScroll(options) {
4220
4320
  // so they don't get confused with server-loaded content
4221
4321
  onBeforeUpdate: elementManager.processManuallyAddedElements,
4222
4322
  // After successful request, tag new server content
4223
- onCompletePreviousRequest: (loadedPage) => {
4224
- options.onCompletePreviousRequest();
4225
- requestAnimationFrame(() => elementManager.processServerLoadedElements(loadedPage), 2);
4323
+ onCompletePreviousRequest: (_, details) => {
4324
+ options.onCompletePreviousRequest(details);
4325
+ if (details.completed) {
4326
+ requestAnimationFrame(() => elementManager.processServerLoadedElements(details.page), 2);
4327
+ }
4226
4328
  },
4227
- onCompleteNextRequest: (loadedPage) => {
4228
- options.onCompleteNextRequest();
4229
- requestAnimationFrame(() => elementManager.processServerLoadedElements(loadedPage), 2);
4329
+ onCompleteNextRequest: (_, details) => {
4330
+ options.onCompleteNextRequest(details);
4331
+ if (details.completed) {
4332
+ requestAnimationFrame(() => elementManager.processServerLoadedElements(details.page), 2);
4333
+ }
4230
4334
  },
4231
4335
  onReset: options.onDataReset
4232
4336
  });