@stacksjs/composables 0.70.258 → 0.70.260

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 (167) hide show
  1. package/dist/_shared.js +1 -23
  2. package/dist/cloneFnJSON.js +1 -3
  3. package/dist/computedAsync.js +1 -20
  4. package/dist/computedEager.js +1 -10
  5. package/dist/createEventHook.js +1 -15
  6. package/dist/createFetch.js +1 -8
  7. package/dist/createGlobalState.js +1 -10
  8. package/dist/createSharedComposable.js +1 -9
  9. package/dist/extendRef.js +1 -6
  10. package/dist/formatTimeAgo.js +1 -33
  11. package/dist/index.js +1 -210
  12. package/dist/isDefined.js +1 -3
  13. package/dist/makeDestructurable.js +1 -16
  14. package/dist/onClickOutside.js +1 -25
  15. package/dist/onKeyStroke.js +1 -31
  16. package/dist/onLongPress.js +1 -43
  17. package/dist/onStartTyping.js +1 -23
  18. package/dist/refAutoReset.js +1 -15
  19. package/dist/refDefault.js +1 -8
  20. package/dist/resolveRef.js +1 -7
  21. package/dist/ssrHandlers.js +1 -8
  22. package/dist/syncRef.js +1 -35
  23. package/dist/tryOnMounted.js +1 -22
  24. package/dist/unrefElement.js +1 -9
  25. package/dist/until.js +1 -62
  26. package/dist/useActiveElement.js +1 -19
  27. package/dist/useAnimate.js +1 -48
  28. package/dist/useArrayUtils.js +1 -114
  29. package/dist/useAsyncQueue.js +1 -22
  30. package/dist/useAsyncState.js +1 -35
  31. package/dist/useBase64.js +1 -45
  32. package/dist/useBattery.js +1 -38
  33. package/dist/useBreakpoints.js +1 -97
  34. package/dist/useBroadcastChannel.js +1 -31
  35. package/dist/useBrowserLocation.js +1 -64
  36. package/dist/useClipboard.js +1 -32
  37. package/dist/useClipboardItems.js +1 -15
  38. package/dist/useCloned.js +1 -16
  39. package/dist/useColorMode.js +1 -60
  40. package/dist/useConfirmDialog.js +1 -34
  41. package/dist/useCounter.js +1 -28
  42. package/dist/useCssVar.js +1 -23
  43. package/dist/useCycleList.js +1 -27
  44. package/dist/useDark.js +1 -38
  45. package/dist/useDateFormat.js +1 -11
  46. package/dist/useDebounce.js +1 -16
  47. package/dist/useDebounceFn.js +1 -18
  48. package/dist/useDebouncedRef.js +1 -22
  49. package/dist/useDebouncedRefHistory.js +1 -36
  50. package/dist/useDeviceMotion.js +1 -53
  51. package/dist/useDeviceOrientation.js +1 -23
  52. package/dist/useDevicePixelRatio.js +1 -24
  53. package/dist/useDocumentVisibility.js +1 -16
  54. package/dist/useDraggable.js +1 -58
  55. package/dist/useDropZone.js +1 -52
  56. package/dist/useElementBounding.js +1 -59
  57. package/dist/useElementByPoint.js +1 -18
  58. package/dist/useElementHover.js +1 -28
  59. package/dist/useElementSize.js +1 -34
  60. package/dist/useElementVisibility.js +1 -10
  61. package/dist/useEventBus.js +1 -41
  62. package/dist/useEventListener.js +1 -29
  63. package/dist/useEventSource.js +1 -72
  64. package/dist/useEyeDropper.js +1 -17
  65. package/dist/useFavicon.js +1 -22
  66. package/dist/useFetch.js +1 -61
  67. package/dist/useFileDialog.js +1 -24
  68. package/dist/useFocus.js +1 -42
  69. package/dist/useFocusWithin.js +1 -29
  70. package/dist/useForm.js +1 -182
  71. package/dist/useFps.js +1 -22
  72. package/dist/useFullscreen.js +1 -67
  73. package/dist/useGeolocation.js +1 -55
  74. package/dist/useIdle.js +1 -46
  75. package/dist/useImage.js +1 -32
  76. package/dist/useInfiniteScroll.js +1 -50
  77. package/dist/useIntersectionObserver.js +1 -27
  78. package/dist/useInterval.js +1 -32
  79. package/dist/useIntervalFn.js +1 -24
  80. package/dist/useKeyModifier.js +1 -22
  81. package/dist/useLastChanged.js +1 -8
  82. package/dist/useLocalStorage.js +1 -4
  83. package/dist/useMagicKeys.js +1 -81
  84. package/dist/useManualRefHistory.js +1 -64
  85. package/dist/useMath.js +1 -52
  86. package/dist/useMediaQuery.js +1 -19
  87. package/dist/useMemoize.js +1 -24
  88. package/dist/useMounted.js +1 -8
  89. package/dist/useMouse.js +1 -67
  90. package/dist/useMouseInElement.js +1 -31
  91. package/dist/useMousePressed.js +1 -31
  92. package/dist/useMutationObserver.js +1 -27
  93. package/dist/useNavigatorLanguage.js +1 -17
  94. package/dist/useNetwork.js +1 -58
  95. package/dist/useNow.js +1 -10
  96. package/dist/useObjectUrl.js +1 -23
  97. package/dist/useOffsetPagination.js +1 -45
  98. package/dist/useOnline.js +1 -20
  99. package/dist/usePageLeave.js +1 -27
  100. package/dist/useParallax.js +1 -33
  101. package/dist/useParentElement.js +1 -8
  102. package/dist/usePermission.js +1 -21
  103. package/dist/usePointer.js +1 -32
  104. package/dist/usePointerLock.js +1 -33
  105. package/dist/usePointerSwipe.js +1 -52
  106. package/dist/usePreferredColorScheme.js +1 -25
  107. package/dist/usePreferredContrast.js +1 -29
  108. package/dist/usePreferredLanguages.js +1 -16
  109. package/dist/usePreferredReducedMotion.js +1 -18
  110. package/dist/usePrevious.js +1 -8
  111. package/dist/useQuery.js +1 -265
  112. package/dist/useRafFn.js +1 -37
  113. package/dist/useRefHistory.js +1 -72
  114. package/dist/useResizeObserver.js +1 -27
  115. package/dist/useScreenOrientation.js +1 -27
  116. package/dist/useScreenSafeArea.js +1 -25
  117. package/dist/useScriptTag.js +1 -52
  118. package/dist/useScroll.js +1 -116
  119. package/dist/useScrollLock.js +1 -39
  120. package/dist/useSessionStorage.js +1 -4
  121. package/dist/useShare.js +1 -11
  122. package/dist/useSorted.js +1 -10
  123. package/dist/useStepper.js +1 -46
  124. package/dist/useStorage.js +1 -54
  125. package/dist/useStyleTag.js +1 -39
  126. package/dist/useSupported.js +1 -10
  127. package/dist/useSwipe.js +1 -58
  128. package/dist/useTextDirection.js +1 -16
  129. package/dist/useTextSelection.js +1 -27
  130. package/dist/useTextareaAutosize.js +1 -20
  131. package/dist/useThrottle.js +1 -23
  132. package/dist/useThrottleFn.js +1 -36
  133. package/dist/useThrottledRef.js +1 -30
  134. package/dist/useThrottledRefHistory.js +1 -43
  135. package/dist/useTimeAgo.js +1 -58
  136. package/dist/useTimeout.js +1 -32
  137. package/dist/useTimeoutFn.js +1 -27
  138. package/dist/useTimeoutPoll.js +1 -33
  139. package/dist/useTimestamp.js +1 -43
  140. package/dist/useTitle.js +1 -12
  141. package/dist/useToNumber.js +1 -25
  142. package/dist/useToString.js +1 -10
  143. package/dist/useToggle.js +1 -12
  144. package/dist/useTransition.js +1 -89
  145. package/dist/useUrlSearchParams.js +1 -72
  146. package/dist/useVibrate.js +1 -14
  147. package/dist/useVirtualList.js +1 -49
  148. package/dist/useWakeLock.js +1 -32
  149. package/dist/useWebNotification.js +1 -39
  150. package/dist/useWebSocket.js +1 -103
  151. package/dist/useWebWorker.js +1 -56
  152. package/dist/useWindowFocus.js +1 -20
  153. package/dist/useWindowScroll.js +1 -17
  154. package/dist/useWindowSize.js +1 -28
  155. package/dist/watchArray.js +1 -9
  156. package/dist/watchAtMost.js +1 -12
  157. package/dist/watchDebounced.js +1 -20
  158. package/dist/watchDeep.js +1 -13
  159. package/dist/watchIgnorable.js +1 -19
  160. package/dist/watchImmediate.js +1 -7
  161. package/dist/watchOnce.js +1 -17
  162. package/dist/watchPausable.js +1 -23
  163. package/dist/watchThrottled.js +1 -37
  164. package/dist/watchTriggerable.js +1 -10
  165. package/dist/watchWithFilter.js +1 -6
  166. package/dist/whenever.js +1 -7
  167. package/package.json +2 -2
@@ -1,49 +1 @@
1
- import { ref, watch } from "@stacksjs/stx";
2
- import { unref } from "./_shared";
3
- export function useVirtualList(source, options) {
4
- const { itemHeight, overscan = 5 } = options, list = ref([]), containerHeight = ref(0), scrollTop = ref(0), wrapperProps = ref({
5
- style: { width: "100%", height: "0px", marginTop: "0px" }
6
- });
7
- function getItems() {
8
- return unref(source);
9
- }
10
- function update() {
11
- const items = getItems(), totalHeight = items.length * itemHeight, startIndex = Math.max(0, Math.floor(scrollTop.value / itemHeight) - overscan), endIndex = Math.min(items.length, Math.ceil((scrollTop.value + containerHeight.value) / itemHeight) + overscan), visibleItems = [];
12
- for (let i = startIndex;i < endIndex; i++) {
13
- const data = items[i];
14
- if (data === void 0 && !(i in items))
15
- continue;
16
- visibleItems.push({ data, index: i });
17
- }
18
- list.value = visibleItems;
19
- wrapperProps.value = {
20
- style: {
21
- width: "100%",
22
- height: `${totalHeight - startIndex * itemHeight}px`,
23
- marginTop: `${startIndex * itemHeight}px`
24
- }
25
- };
26
- }
27
- const onScroll = (e) => {
28
- const target = e.target;
29
- scrollTop.value = target.scrollTop;
30
- containerHeight.value = target.clientHeight;
31
- update();
32
- };
33
- update();
34
- if (typeof source === "object" && source && "value" in source && "subscribe" in source)
35
- watch(source, () => update());
36
- function scrollTo(index) {
37
- scrollTop.value = index * itemHeight;
38
- update();
39
- }
40
- return {
41
- list,
42
- containerProps: {
43
- onScroll,
44
- style: { overflow: "auto", position: "relative" }
45
- },
46
- wrapperProps,
47
- scrollTo
48
- };
49
- }
1
+ import{ref,watch}from"@stacksjs/stx";import{unref}from"./_shared";export function useVirtualList(source,options){const{itemHeight,overscan=5}=options,list=ref([]),containerHeight=ref(0),scrollTop=ref(0),wrapperProps=ref({style:{width:"100%",height:"0px",marginTop:"0px"}});function getItems(){return unref(source)}function update(){const items=getItems(),totalHeight=items.length*itemHeight,startIndex=Math.max(0,Math.floor(scrollTop.value/itemHeight)-overscan),endIndex=Math.min(items.length,Math.ceil((scrollTop.value+containerHeight.value)/itemHeight)+overscan),visibleItems=[];for(let i=startIndex;i<endIndex;i++){const data=items[i];if(data===void 0&&!(i in items))continue;visibleItems.push({data,index:i})}list.value=visibleItems;wrapperProps.value={style:{width:"100%",height:`${totalHeight-startIndex*itemHeight}px`,marginTop:`${startIndex*itemHeight}px`}}}const onScroll=(e)=>{const target=e.target;scrollTop.value=target.scrollTop;containerHeight.value=target.clientHeight;update()};update();if(typeof source==="object"&&source&&"value"in source&&"subscribe"in source)watch(source,()=>update());function scrollTo(index){scrollTop.value=index*itemHeight;update()}return{list,containerProps:{onScroll,style:{overflow:"auto",position:"relative"}},wrapperProps,scrollTo}}
@@ -1,32 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- export function useWakeLock() {
3
- const isSupported = ref(typeof navigator < "u" && "wakeLock" in navigator), isActive = ref(!1);
4
- let sentinel = null;
5
- async function request() {
6
- if (!isSupported.value)
7
- return;
8
- try {
9
- sentinel = await navigator.wakeLock.request("screen");
10
- isActive.value = !0;
11
- sentinel.addEventListener("release", () => {
12
- isActive.value = !1;
13
- sentinel = null;
14
- });
15
- } catch {
16
- isActive.value = !1;
17
- }
18
- }
19
- async function release() {
20
- if (sentinel) {
21
- await sentinel.release();
22
- sentinel = null;
23
- isActive.value = !1;
24
- }
25
- }
26
- try {
27
- onUnmounted(() => {
28
- release();
29
- });
30
- } catch {}
31
- return { isSupported, isActive, request, release };
32
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useWakeLock(){const isSupported=ref(typeof navigator<"u"&&"wakeLock"in navigator),isActive=ref(!1);let sentinel=null;async function request(){if(!isSupported.value)return;try{sentinel=await navigator.wakeLock.request("screen");isActive.value=!0;sentinel.addEventListener("release",()=>{isActive.value=!1;sentinel=null})}catch{isActive.value=!1}}async function release(){if(sentinel){await sentinel.release();sentinel=null;isActive.value=!1}}try{onUnmounted(()=>{release()})}catch{}return{isSupported,isActive,request,release}}
@@ -1,39 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- export function useWebNotification(options = {}) {
3
- const isSupported = ref(typeof Notification < "u"), notification = ref(null), permissionGranted = ref(isSupported.value && Notification.permission === "granted"), onClick = ref(null), onShow = ref(null), onError = ref(null), onClose = ref(null);
4
- async function show(overrideOptions) {
5
- if (!isSupported.value)
6
- return;
7
- if (Notification.permission !== "granted") {
8
- const result = await Notification.requestPermission();
9
- permissionGranted.value = result === "granted";
10
- if (!permissionGranted.value)
11
- return;
12
- }
13
- const opts = { ...options, ...overrideOptions }, title = opts.title ?? "", n = new Notification(title, {
14
- body: opts.body,
15
- icon: opts.icon,
16
- dir: opts.dir,
17
- lang: opts.lang,
18
- tag: opts.tag,
19
- requireInteraction: opts.requireInteraction
20
- });
21
- notification.value = n;
22
- n.onclick = (e) => onClick.value?.(e);
23
- n.onshow = (e) => onShow.value?.(e);
24
- n.onerror = (e) => onError.value?.(e);
25
- n.onclose = (e) => {
26
- onClose.value?.(e);
27
- notification.value = null;
28
- };
29
- return n;
30
- }
31
- function close() {
32
- notification.value?.close();
33
- notification.value = null;
34
- }
35
- try {
36
- onUnmounted(() => close());
37
- } catch {}
38
- return { isSupported, notification, permissionGranted, show, close, onClick, onShow, onError, onClose };
39
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useWebNotification(options={}){const isSupported=ref(typeof Notification<"u"),notification=ref(null),permissionGranted=ref(isSupported.value&&Notification.permission==="granted"),onClick=ref(null),onShow=ref(null),onError=ref(null),onClose=ref(null);async function show(overrideOptions){if(!isSupported.value)return;if(Notification.permission!=="granted"){const result=await Notification.requestPermission();permissionGranted.value=result==="granted";if(!permissionGranted.value)return}const opts={...options,...overrideOptions},title=opts.title??"",n=new Notification(title,{body:opts.body,icon:opts.icon,dir:opts.dir,lang:opts.lang,tag:opts.tag,requireInteraction:opts.requireInteraction});notification.value=n;n.onclick=(e)=>onClick.value?.(e);n.onshow=(e)=>onShow.value?.(e);n.onerror=(e)=>onError.value?.(e);n.onclose=(e)=>{onClose.value?.(e);notification.value=null};return n}function close(){notification.value?.close();notification.value=null}try{onUnmounted(()=>close())}catch{}return{isSupported,notification,permissionGranted,show,close,onClick,onShow,onError,onClose}}
@@ -1,103 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- import { toValue } from "./_shared";
3
- export function useWebSocket(url, options = {}) {
4
- const {
5
- onConnected,
6
- onDisconnected,
7
- onError,
8
- onMessage,
9
- immediate = !0,
10
- autoReconnect = !1,
11
- heartbeat = !1,
12
- protocols
13
- } = options, data = ref(null), status = ref("CLOSED"), wsRef = ref(null);
14
- let retryCount = 0, retryTimer = null, heartbeatTimer = null, explicitlyClosed = !1;
15
- const heartbeatInterval = typeof heartbeat === "object" ? heartbeat.interval ?? 30000 : 30000, heartbeatMessage = typeof heartbeat === "object" ? heartbeat.message ?? "ping" : "ping", maxRetries = typeof autoReconnect === "object" ? autoReconnect.retries ?? 3 : autoReconnect ? 1 / 0 : 0, retryDelay = typeof autoReconnect === "object" ? autoReconnect.delay ?? 1000 : 1000;
16
- function startHeartbeat(ws) {
17
- stopHeartbeat();
18
- if (!heartbeat)
19
- return;
20
- heartbeatTimer = setInterval(() => {
21
- if (ws.readyState === WebSocket.OPEN)
22
- ws.send(heartbeatMessage);
23
- }, heartbeatInterval);
24
- }
25
- function stopHeartbeat() {
26
- if (heartbeatTimer !== null) {
27
- clearInterval(heartbeatTimer);
28
- heartbeatTimer = null;
29
- }
30
- }
31
- function open() {
32
- if (typeof WebSocket > "u")
33
- return;
34
- close();
35
- explicitlyClosed = !1;
36
- retryCount = 0;
37
- const resolvedUrl = toValue(url);
38
- status.value = "CONNECTING";
39
- const ws = protocols ? new WebSocket(resolvedUrl, protocols) : new WebSocket(resolvedUrl);
40
- wsRef.value = ws;
41
- ws.onopen = () => {
42
- status.value = "OPEN";
43
- retryCount = 0;
44
- startHeartbeat(ws);
45
- onConnected?.(ws);
46
- };
47
- ws.onclose = (event) => {
48
- status.value = "CLOSED";
49
- wsRef.value = null;
50
- stopHeartbeat();
51
- onDisconnected?.(ws, event);
52
- if (!explicitlyClosed && autoReconnect && retryCount < maxRetries) {
53
- retryCount++;
54
- retryTimer = setTimeout(() => {
55
- open();
56
- }, retryDelay);
57
- }
58
- };
59
- ws.onerror = (event) => {
60
- onError?.(ws, event);
61
- };
62
- ws.onmessage = (event) => {
63
- data.value = typeof event.data === "string" ? event.data : String(event.data);
64
- onMessage?.(ws, event);
65
- };
66
- }
67
- function close(code, reason) {
68
- explicitlyClosed = !0;
69
- stopHeartbeat();
70
- if (retryTimer !== null) {
71
- clearTimeout(retryTimer);
72
- retryTimer = null;
73
- }
74
- const ws = wsRef.value;
75
- if (ws) {
76
- if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
77
- status.value = "CLOSING";
78
- ws.close(code, reason);
79
- }
80
- wsRef.value = null;
81
- }
82
- }
83
- function send(sendData) {
84
- const ws = wsRef.value;
85
- if (ws && ws.readyState === WebSocket.OPEN)
86
- ws.send(sendData);
87
- }
88
- if (immediate)
89
- open();
90
- try {
91
- onUnmounted(() => {
92
- close();
93
- });
94
- } catch {}
95
- return {
96
- data,
97
- status,
98
- ws: wsRef,
99
- open,
100
- close,
101
- send
102
- };
103
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";import{toValue}from"./_shared";export function useWebSocket(url,options={}){const{onConnected,onDisconnected,onError,onMessage,immediate=!0,autoReconnect=!1,heartbeat=!1,protocols}=options,data=ref(null),status=ref("CLOSED"),wsRef=ref(null);let retryCount=0,retryTimer=null,heartbeatTimer=null,explicitlyClosed=!1;const heartbeatInterval=typeof heartbeat==="object"?heartbeat.interval??30000:30000,heartbeatMessage=typeof heartbeat==="object"?heartbeat.message??"ping":"ping",maxRetries=typeof autoReconnect==="object"?autoReconnect.retries??3:autoReconnect?1/0:0,retryDelay=typeof autoReconnect==="object"?autoReconnect.delay??1000:1000;function startHeartbeat(ws){stopHeartbeat();if(!heartbeat)return;heartbeatTimer=setInterval(()=>{if(ws.readyState===WebSocket.OPEN)ws.send(heartbeatMessage)},heartbeatInterval)}function stopHeartbeat(){if(heartbeatTimer!==null){clearInterval(heartbeatTimer);heartbeatTimer=null}}function open(){if(typeof WebSocket>"u")return;close();explicitlyClosed=!1;retryCount=0;const resolvedUrl=toValue(url);status.value="CONNECTING";const ws=protocols?new WebSocket(resolvedUrl,protocols):new WebSocket(resolvedUrl);wsRef.value=ws;ws.onopen=()=>{status.value="OPEN";retryCount=0;startHeartbeat(ws);onConnected?.(ws)};ws.onclose=(event)=>{status.value="CLOSED";wsRef.value=null;stopHeartbeat();onDisconnected?.(ws,event);if(!explicitlyClosed&&autoReconnect&&retryCount<maxRetries){retryCount++;retryTimer=setTimeout(()=>{open()},retryDelay)}};ws.onerror=(event)=>{onError?.(ws,event)};ws.onmessage=(event)=>{data.value=typeof event.data==="string"?event.data:String(event.data);onMessage?.(ws,event)}}function close(code,reason){explicitlyClosed=!0;stopHeartbeat();if(retryTimer!==null){clearTimeout(retryTimer);retryTimer=null}const ws=wsRef.value;if(ws){if(ws.readyState===WebSocket.OPEN||ws.readyState===WebSocket.CONNECTING){status.value="CLOSING";ws.close(code,reason)}wsRef.value=null}}function send(sendData){const ws=wsRef.value;if(ws&&ws.readyState===WebSocket.OPEN)ws.send(sendData)}if(immediate)open();try{onUnmounted(()=>{close()})}catch{}return{data,status,ws:wsRef,open,close,send}}
@@ -1,56 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- export function useWebWorker(url, workerOptions) {
3
- const data = ref(null), worker = ref(void 0);
4
- if (typeof Worker < "u") {
5
- const w = new Worker(url, workerOptions);
6
- worker.value = w;
7
- w.onmessage = (e) => {
8
- data.value = e.data;
9
- };
10
- try {
11
- onUnmounted(() => w.terminate());
12
- } catch {}
13
- }
14
- function post(message) {
15
- worker.value?.postMessage(message);
16
- }
17
- function terminate() {
18
- worker.value?.terminate();
19
- worker.value = void 0;
20
- }
21
- return { data, post, terminate, worker };
22
- }
23
- export function useWebWorkerFn(fn) {
24
- let worker;
25
- function createWorker() {
26
- const blob = new Blob([`self.onmessage = function(e) { self.postMessage((${fn.toString()})(...e.data)); }`], { type: "text/javascript" }), url = URL.createObjectURL(blob);
27
- return new Worker(url);
28
- }
29
- function workerFn(...args) {
30
- return new Promise((resolve, reject) => {
31
- if (typeof Worker > "u") {
32
- try {
33
- resolve(fn(...args));
34
- } catch (e) {
35
- reject(e);
36
- }
37
- return;
38
- }
39
- worker = createWorker();
40
- worker.onmessage = (e) => {
41
- resolve(e.data);
42
- worker?.terminate();
43
- };
44
- worker.onerror = (e) => {
45
- reject(e);
46
- worker?.terminate();
47
- };
48
- worker.postMessage(args);
49
- });
50
- }
51
- function terminate() {
52
- worker?.terminate();
53
- worker = void 0;
54
- }
55
- return { workerFn, terminate };
56
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useWebWorker(url,workerOptions){const data=ref(null),worker=ref(void 0);if(typeof Worker<"u"){const w=new Worker(url,workerOptions);worker.value=w;w.onmessage=(e)=>{data.value=e.data};try{onUnmounted(()=>w.terminate())}catch{}}function post(message){worker.value?.postMessage(message)}function terminate(){worker.value?.terminate();worker.value=void 0}return{data,post,terminate,worker}}export function useWebWorkerFn(fn){let worker;function createWorker(){const blob=new Blob([`self.onmessage = function(e) { self.postMessage((${fn.toString()})(...e.data)); }`],{type:"text/javascript"}),url=URL.createObjectURL(blob);return new Worker(url)}function workerFn(...args){return new Promise((resolve,reject)=>{if(typeof Worker>"u"){try{resolve(fn(...args))}catch(e){reject(e)}return}worker=createWorker();worker.onmessage=(e)=>{resolve(e.data);worker?.terminate()};worker.onerror=(e)=>{reject(e);worker?.terminate()};worker.postMessage(args)})}function terminate(){worker?.terminate();worker=void 0}return{workerFn,terminate}}
@@ -1,20 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- export function useWindowFocus() {
3
- const focused = ref(typeof document < "u" ? document.hasFocus() : !1);
4
- if (typeof window > "u")
5
- return focused;
6
- const onFocus = () => {
7
- focused.value = !0;
8
- }, onBlur = () => {
9
- focused.value = !1;
10
- };
11
- window.addEventListener("focus", onFocus, { passive: !0 });
12
- window.addEventListener("blur", onBlur, { passive: !0 });
13
- try {
14
- onUnmounted(() => {
15
- window.removeEventListener("focus", onFocus);
16
- window.removeEventListener("blur", onBlur);
17
- });
18
- } catch {}
19
- return focused;
20
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useWindowFocus(){const focused=ref(typeof document<"u"?document.hasFocus():!1);if(typeof window>"u")return focused;const onFocus=()=>{focused.value=!0},onBlur=()=>{focused.value=!1};window.addEventListener("focus",onFocus,{passive:!0});window.addEventListener("blur",onBlur,{passive:!0});try{onUnmounted(()=>{window.removeEventListener("focus",onFocus);window.removeEventListener("blur",onBlur)})}catch{}return focused}
@@ -1,17 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- export function useWindowScroll() {
3
- const x = ref(typeof window < "u" ? window.scrollX : 0), y = ref(typeof window < "u" ? window.scrollY : 0);
4
- if (typeof window > "u")
5
- return { x, y };
6
- const handler = () => {
7
- x.value = window.scrollX;
8
- y.value = window.scrollY;
9
- };
10
- window.addEventListener("scroll", handler, { passive: !0 });
11
- try {
12
- onUnmounted(() => {
13
- window.removeEventListener("scroll", handler);
14
- });
15
- } catch {}
16
- return { x, y };
17
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";export function useWindowScroll(){const x=ref(typeof window<"u"?window.scrollX:0),y=ref(typeof window<"u"?window.scrollY:0);if(typeof window>"u")return{x,y};const handler=()=>{x.value=window.scrollX;y.value=window.scrollY};window.addEventListener("scroll",handler,{passive:!0});try{onUnmounted(()=>{window.removeEventListener("scroll",handler)})}catch{}return{x,y}}
@@ -1,28 +1 @@
1
- import { onUnmounted, ref } from "@stacksjs/stx";
2
- import { defaultWindow } from "./_shared";
3
- export function useWindowSize(options) {
4
- const {
5
- initialWidth = 0,
6
- initialHeight = 0,
7
- listenOrientation = !0
8
- } = options ?? {}, win = defaultWindow(), width = ref(win ? win.innerWidth : initialWidth), height = ref(win ? win.innerHeight : initialHeight);
9
- function update() {
10
- if (win) {
11
- width.value = win.innerWidth;
12
- height.value = win.innerHeight;
13
- }
14
- }
15
- if (win) {
16
- win.addEventListener("resize", update);
17
- if (listenOrientation)
18
- win.addEventListener("orientationchange", update);
19
- try {
20
- onUnmounted(() => {
21
- win.removeEventListener("resize", update);
22
- if (listenOrientation)
23
- win.removeEventListener("orientationchange", update);
24
- });
25
- } catch {}
26
- }
27
- return { width, height };
28
- }
1
+ import{onUnmounted,ref}from"@stacksjs/stx";import{defaultWindow}from"./_shared";export function useWindowSize(options){const{initialWidth=0,initialHeight=0,listenOrientation=!0}=options??{},win=defaultWindow(),width=ref(win?win.innerWidth:initialWidth),height=ref(win?win.innerHeight:initialHeight);function update(){if(win){width.value=win.innerWidth;height.value=win.innerHeight}}if(win){win.addEventListener("resize",update);if(listenOrientation)win.addEventListener("orientationchange",update);try{onUnmounted(()=>{win.removeEventListener("resize",update);if(listenOrientation)win.removeEventListener("orientationchange",update)})}catch{}}return{width,height}}
@@ -1,9 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchArray(source, callback) {
3
- let oldList = [...source.value];
4
- return watch(source, (newValue) => {
5
- const newList = [...newValue], added = newList.filter((item) => !oldList.includes(item)), removed = oldList.filter((item) => !newList.includes(item));
6
- callback(newList, oldList, added, removed);
7
- oldList = newList;
8
- });
9
- }
1
+ import{watch}from"@stacksjs/stx";export function watchArray(source,callback){let oldList=[...source.value];return watch(source,(newValue)=>{const newList=[...newValue],added=newList.filter((item)=>!oldList.includes(item)),removed=oldList.filter((item)=>!newList.includes(item));callback(newList,oldList,added,removed);oldList=newList})}
@@ -1,12 +1 @@
1
- import { ref, watch } from "@stacksjs/stx";
2
- export function watchAtMost(source, callback, count) {
3
- const remaining = ref(count), unwatch = watch(source, (newValue) => {
4
- if (remaining.value > 0) {
5
- remaining.value--;
6
- callback(newValue);
7
- if (remaining.value <= 0)
8
- unwatch();
9
- }
10
- });
11
- return { stop: unwatch, remaining };
12
- }
1
+ import{ref,watch}from"@stacksjs/stx";export function watchAtMost(source,callback,count){const remaining=ref(count),unwatch=watch(source,(newValue)=>{if(remaining.value>0){remaining.value--;callback(newValue);if(remaining.value<=0)unwatch()}});return{stop:unwatch,remaining}}
@@ -1,20 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchDebounced(source, callback, options) {
3
- const delay = options?.debounce ?? 200;
4
- let timeoutId = null;
5
- const unwatch = watch(source, (newValue) => {
6
- if (timeoutId !== null)
7
- clearTimeout(timeoutId);
8
- timeoutId = setTimeout(() => {
9
- timeoutId = null;
10
- callback(newValue);
11
- }, delay);
12
- });
13
- return () => {
14
- if (timeoutId !== null) {
15
- clearTimeout(timeoutId);
16
- timeoutId = null;
17
- }
18
- unwatch();
19
- };
20
- }
1
+ import{watch}from"@stacksjs/stx";export function watchDebounced(source,callback,options){const delay=options?.debounce??200;let timeoutId=null;const unwatch=watch(source,(newValue)=>{if(timeoutId!==null)clearTimeout(timeoutId);timeoutId=setTimeout(()=>{timeoutId=null;callback(newValue)},delay)});return()=>{if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}unwatch()}}
package/dist/watchDeep.js CHANGED
@@ -1,13 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchDeep(source, callback, options) {
3
- let prev = JSON.stringify(source.value);
4
- if (options?.immediate)
5
- callback(source.value);
6
- return watch(source, (newValue) => {
7
- const next = JSON.stringify(newValue);
8
- if (next !== prev) {
9
- prev = next;
10
- callback(newValue);
11
- }
12
- });
13
- }
1
+ import{watch}from"@stacksjs/stx";export function watchDeep(source,callback,options){let prev=JSON.stringify(source.value);if(options?.immediate)callback(source.value);return watch(source,(newValue)=>{const next=JSON.stringify(newValue);if(next!==prev){prev=next;callback(newValue)}})}
@@ -1,19 +1 @@
1
- import { ref, watch } from "@stacksjs/stx";
2
- export function watchIgnorable(source, callback) {
3
- const isIgnoring = ref(!1);
4
- let lastValue;
5
- const unwatch = watch(source, (newValue) => {
6
- if (!isIgnoring.value)
7
- callback(newValue, lastValue);
8
- lastValue = newValue;
9
- });
10
- function ignoreUpdates(updater) {
11
- isIgnoring.value = !0;
12
- updater();
13
- Promise.resolve().then(() => {
14
- isIgnoring.value = !1;
15
- });
16
- }
17
- return { stop: unwatch, ignoreUpdates, isIgnoring };
18
- }
19
- export const ignorableWatch = watchIgnorable;
1
+ import{ref,watch}from"@stacksjs/stx";export function watchIgnorable(source,callback){const isIgnoring=ref(!1);let lastValue;const unwatch=watch(source,(newValue)=>{if(!isIgnoring.value)callback(newValue,lastValue);lastValue=newValue});function ignoreUpdates(updater){isIgnoring.value=!0;updater();Promise.resolve().then(()=>{isIgnoring.value=!1})}return{stop:unwatch,ignoreUpdates,isIgnoring}}export const ignorableWatch=watchIgnorable;
@@ -1,7 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchImmediate(source, callback) {
3
- callback(source.value);
4
- return watch(source, (newValue) => {
5
- callback(newValue);
6
- });
7
- }
1
+ import{watch}from"@stacksjs/stx";export function watchImmediate(source,callback){callback(source.value);return watch(source,(newValue)=>{callback(newValue)})}
package/dist/watchOnce.js CHANGED
@@ -1,17 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchOnce(source, callback) {
3
- let stopped = !1;
4
- const unwatch = watch(source, (newValue) => {
5
- if (stopped)
6
- return;
7
- stopped = !0;
8
- unwatch();
9
- callback(newValue);
10
- });
11
- return () => {
12
- if (!stopped) {
13
- stopped = !0;
14
- unwatch();
15
- }
16
- };
17
- }
1
+ import{watch}from"@stacksjs/stx";export function watchOnce(source,callback){let stopped=!1;const unwatch=watch(source,(newValue)=>{if(stopped)return;stopped=!0;unwatch();callback(newValue)});return()=>{if(!stopped){stopped=!0;unwatch()}}}
@@ -1,23 +1 @@
1
- import { ref, watch } from "@stacksjs/stx";
2
- export function watchPausable(source, callback) {
3
- const isActive = ref(!0);
4
- let lastValue;
5
- const unwatch = watch(source, (newValue) => {
6
- if (isActive.value)
7
- callback(newValue, lastValue);
8
- lastValue = newValue;
9
- });
10
- function pause() {
11
- isActive.value = !1;
12
- }
13
- function resume() {
14
- isActive.value = !0;
15
- }
16
- return {
17
- stop: unwatch,
18
- pause,
19
- resume,
20
- isActive
21
- };
22
- }
23
- export const pausableWatch = watchPausable;
1
+ import{ref,watch}from"@stacksjs/stx";export function watchPausable(source,callback){const isActive=ref(!0);let lastValue;const unwatch=watch(source,(newValue)=>{if(isActive.value)callback(newValue,lastValue);lastValue=newValue});function pause(){isActive.value=!1}function resume(){isActive.value=!0}return{stop:unwatch,pause,resume,isActive}}export const pausableWatch=watchPausable;
@@ -1,37 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchThrottled(source, callback, options) {
3
- const interval = options?.throttle ?? 200;
4
- let lastExec = 0, timeoutId = null, latestValue, hasPending = !1;
5
- const unwatch = watch(source, (newValue) => {
6
- const now = Date.now(), elapsed = now - lastExec;
7
- if (elapsed >= interval) {
8
- lastExec = now;
9
- if (timeoutId !== null) {
10
- clearTimeout(timeoutId);
11
- timeoutId = null;
12
- }
13
- hasPending = !1;
14
- callback(newValue);
15
- } else {
16
- latestValue = newValue;
17
- hasPending = !0;
18
- if (timeoutId === null)
19
- timeoutId = setTimeout(() => {
20
- lastExec = Date.now();
21
- timeoutId = null;
22
- if (hasPending) {
23
- hasPending = !1;
24
- callback(latestValue);
25
- }
26
- }, interval - elapsed);
27
- }
28
- });
29
- return () => {
30
- if (timeoutId !== null) {
31
- clearTimeout(timeoutId);
32
- timeoutId = null;
33
- }
34
- hasPending = !1;
35
- unwatch();
36
- };
37
- }
1
+ import{watch}from"@stacksjs/stx";export function watchThrottled(source,callback,options){const interval=options?.throttle??200;let lastExec=0,timeoutId=null,latestValue,hasPending=!1;const unwatch=watch(source,(newValue)=>{const now=Date.now(),elapsed=now-lastExec;if(elapsed>=interval){lastExec=now;if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}hasPending=!1;callback(newValue)}else{latestValue=newValue;hasPending=!0;if(timeoutId===null)timeoutId=setTimeout(()=>{lastExec=Date.now();timeoutId=null;if(hasPending){hasPending=!1;callback(latestValue)}},interval-elapsed)}});return()=>{if(timeoutId!==null){clearTimeout(timeoutId);timeoutId=null}hasPending=!1;unwatch()}}
@@ -1,10 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchTriggerable(source, callback) {
3
- const unwatch = watch(source, (newValue) => {
4
- callback(newValue);
5
- });
6
- function trigger() {
7
- callback(source.value);
8
- }
9
- return { stop: unwatch, trigger };
10
- }
1
+ import{watch}from"@stacksjs/stx";export function watchTriggerable(source,callback){const unwatch=watch(source,(newValue)=>{callback(newValue)});function trigger(){callback(source.value)}return{stop:unwatch,trigger}}
@@ -1,6 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function watchWithFilter(source, callback, eventFilter) {
3
- return watch(source, (newValue) => {
4
- eventFilter(() => callback(newValue));
5
- });
6
- }
1
+ import{watch}from"@stacksjs/stx";export function watchWithFilter(source,callback,eventFilter){return watch(source,(newValue)=>{eventFilter(()=>callback(newValue))})}
package/dist/whenever.js CHANGED
@@ -1,7 +1 @@
1
- import { watch } from "@stacksjs/stx";
2
- export function whenever(source, callback) {
3
- return watch(source, (newValue) => {
4
- if (newValue)
5
- callback(newValue);
6
- });
7
- }
1
+ import{watch}from"@stacksjs/stx";export function whenever(source,callback){return watch(source,(newValue)=>{if(newValue)callback(newValue)})}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/composables",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.258",
5
+ "version": "0.70.260",
6
6
  "description": "Stacks composables powered by stx reactivity.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@stacksjs/stx": "^0.2.148",
55
- "@stacksjs/datetime": "0.70.258"
55
+ "@stacksjs/datetime": "0.70.260"
56
56
  },
57
57
  "devDependencies": {
58
58
  "better-dx": "^0.2.17"