state-in-url 8.0.1 → 8.0.2

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.
@@ -1 +1 @@
1
- "use strict";var g=require("react");var h=require("next/navigation");var p=require("../../parseSPObj.cjs");var k=require("../../useUrlStateBase/useUrlStateBase.cjs");var a=require("../../utils.cjs");function q(t){return t&&t.__esModule?t:{default:t}}var s=q(g);function y(t,r){const n=s.default.useMemo(()=>r?.useHistory===void 0?true:!!r?.useHistory,[]);const u=h.useRouter();const d=s.default.useMemo(()=>({push:(...e)=>{if(n){a.routerHistory.push(...e)}else{u.push(...e)}},replace:(...e)=>{if(n){a.routerHistory.replace(...e)}else{u.replace(...e)}}}),[u]);const{state:S,updateState:c,updateUrl:i,reset:f,getState:U,pendingUrlUpdate:b}=k.useUrlStateBase(t,d,({parse:e})=>{return a.isSSR?p.parseSPObj(a.filterUnknownParams(t,r?.searchParams),t):e(a.filterUnknownParamsClient(t,r?.searchParams||a.getSearch()))});const o=s.default.useCallback(()=>{if(b())return;c(a.filterUnknownParams(t,p.parseSPObj(Object.fromEntries(a.getParams(a.getSearch()).entries()),t)))},[]);s.default.useEffect(()=>{o();return a.subscribeToUrl(o)},[o]);const l=s.default.useMemo(()=>{const e={..._};if(r?.scroll!==void 0)e.scroll=r.scroll;if(r?.replace!==void 0)e.replace=r.replace;return e},[r?.scroll,r?.replace]);const m=s.default.useCallback((e,P)=>i(e,{...l,...P}),[i,l]);const v=s.default.useCallback(e=>{f({...l,...e})},[f,l]);return{setState:c,setUrl:m,urlState:S,reset:v,getState:U}}const _={replace:true,scroll:false};exports.useUrlState=y;
1
+ "use strict";var h=require("react");var g=require("next/navigation");var p=require("../../parseSPObj.cjs");var k=require("../../useUrlStateBase/useUrlStateBase.cjs");var a=require("../../utils.cjs");function q(t){return t&&t.__esModule?t:{default:t}}var s=q(h);function y(t,r){const c=s.default.useMemo(()=>r?.useHistory===void 0?true:!!r?.useHistory,[]);const u=g.useRouter();const d=s.default.useMemo(()=>({push:(...e)=>{if(c){a.routerHistory.push(...e)}else{u.push(...e)}},replace:(...e)=>{if(c){a.routerHistory.replace(...e)}else{u.replace(...e)}}}),[u]);const{state:S,updateState:n,updateUrl:i,reset:f,getState:U,pendingUrlUpdate:v}=k.useUrlStateBase(t,d,({parse:e})=>{return a.isSSR?p.parseSPObj(a.filterUnknownParams(t,r?.searchParams),t):e(a.filterUnknownParamsClient(t,r?.searchParams||a.getSearch()))},void 0,r?.searchParams!==void 0);const o=s.default.useCallback(()=>{if(v())return;n(a.filterUnknownParams(t,p.parseSPObj(Object.fromEntries(a.getParams(a.getSearch()).entries()),t)))},[]);s.default.useEffect(()=>{o();return a.subscribeToUrl(o)},[o]);const l=s.default.useMemo(()=>{const e={..._};if(r?.scroll!==void 0)e.scroll=r.scroll;if(r?.replace!==void 0)e.replace=r.replace;return e},[r?.scroll,r?.replace]);const m=s.default.useCallback((e,b)=>i(e,{...l,...b}),[i,l]);const P=s.default.useCallback(e=>{f({...l,...e})},[f,l]);return{setState:n,setUrl:m,urlState:S,reset:P,getState:U}}const _={replace:true,scroll:false};exports.useUrlState=y;
@@ -1 +1 @@
1
- import r from"react";import{useRouter as y}from"next/navigation.js";import{parseSPObj as p}from"../../parseSPObj.mjs";import{useUrlStateBase as C}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as m,isSSR as v,filterUnknownParams as f,filterUnknownParamsClient as H,getSearch as S,getParams as M,subscribeToUrl as j}from"../../utils.mjs";function w(s,t){const c=r.useMemo(()=>t?.useHistory===void 0?true:!!t?.useHistory,[]);const a=y();const U=r.useMemo(()=>({push:(...e)=>{if(c){m.push(...e)}else{a.push(...e)}},replace:(...e)=>{if(c){m.replace(...e)}else{a.replace(...e)}}}),[a]);const{state:b,updateState:u,updateUrl:n,reset:i,getState:d,pendingUrlUpdate:h}=C(s,U,({parse:e})=>{return v?p(f(s,t?.searchParams),s):e(H(s,t?.searchParams||S()))});const l=r.useCallback(()=>{if(h())return;u(f(s,p(Object.fromEntries(M(S()).entries()),s)))},[]);r.useEffect(()=>{l();return j(l)},[l]);const o=r.useMemo(()=>{const e={...E};if(t?.scroll!==void 0)e.scroll=t.scroll;if(t?.replace!==void 0)e.replace=t.replace;return e},[t?.scroll,t?.replace]);const P=r.useCallback((e,k)=>n(e,{...o,...k}),[n,o]);const g=r.useCallback(e=>{i({...o,...e})},[i,o]);return{setState:u,setUrl:P,urlState:b,reset:g,getState:d}}const E={replace:true,scroll:false};export{w as useUrlState};
1
+ import r from"react";import{useRouter as v}from"next/navigation.js";import{parseSPObj as p}from"../../parseSPObj.mjs";import{useUrlStateBase as y}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as m,isSSR as C,filterUnknownParams as f,filterUnknownParamsClient as H,getSearch as S,getParams as M,subscribeToUrl as j}from"../../utils.mjs";function w(s,t){const c=r.useMemo(()=>t?.useHistory===void 0?true:!!t?.useHistory,[]);const a=v();const U=r.useMemo(()=>({push:(...e)=>{if(c){m.push(...e)}else{a.push(...e)}},replace:(...e)=>{if(c){m.replace(...e)}else{a.replace(...e)}}}),[a]);const{state:d,updateState:u,updateUrl:n,reset:i,getState:b,pendingUrlUpdate:h}=y(s,U,({parse:e})=>{return C?p(f(s,t?.searchParams),s):e(H(s,t?.searchParams||S()))},void 0,t?.searchParams!==void 0);const l=r.useCallback(()=>{if(h())return;u(f(s,p(Object.fromEntries(M(S()).entries()),s)))},[]);r.useEffect(()=>{l();return j(l)},[l]);const o=r.useMemo(()=>{const e={...E};if(t?.scroll!==void 0)e.scroll=t.scroll;if(t?.replace!==void 0)e.replace=t.replace;return e},[t?.scroll,t?.replace]);const P=r.useCallback((e,k)=>n(e,{...o,...k}),[n,o]);const g=r.useCallback(e=>{i({...o,...e})},[i,o]);return{setState:u,setUrl:P,urlState:d,reset:g,getState:b}}const E={replace:true,scroll:false};export{w as useUrlState};
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-in-url",
3
- "version": "8.0.1",
3
+ "version": "8.0.2",
4
4
  "description": "Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-16, react-router@6-7, remix@2, and Astro.",
5
5
  "homepage": "https://state-in-url.dev",
6
6
  "repository": {
@@ -1 +1 @@
1
- "use strict";var M=require("react");var O=require("../constants/constants.cjs");var P=require("../useIsomorphicLayoutEffect.cjs");var R=require("../useSharedState/useSharedState.cjs");var j=require("../useUrlEncode/useUrlEncode.cjs");var n=require("../utils.cjs");function D(t){return t&&t.__esModule?t:{default:t}}var l=D(M);let f;let s;let a;let v;let m=0;const S=()=>window.location.pathname+window.location.search;function d(){a=void 0;v?.();v=void 0}function g(){if(!a)return void 0;const t=S();if(t===a.from&&t!==a.to){return a}d();return void 0}function H(t,e){const o=g()?.from??S();if(o===t){d();return}a={from:o,to:t,search:e};v??=n.subscribeToUrl(d)}function V(t){if(t.pathname!==window.location.pathname)return;const e=g();const o=t.pathname+t.search;if(e?.to===o)return;const c=o===S();if(c&&!e)return;const i=c?"replace":t.method;t.router[i](t.url,t.opts);H(o,t.search)}function W(t,e,o,c){const{parse:i,stringify:E}=j.useUrlEncode(t);const{state:L,getState:u,setState:h}=R.useSharedState(t,()=>o?.({parse:i})||t);const p=l.default.useRef();if(p.current===void 0)p.current=n.getSearch();P.useIsomorphicLayoutEffect(()=>{const r=()=>{h(i(n.filterUnknownParamsClient(t,n.getSearch())))};window.addEventListener(n.popstateEv,r);if(p.current!==n.getSearch())r();return()=>{window.removeEventListener(n.popstateEv,r)}},[]);l.default.useEffect(()=>{m+=1;return()=>{m-=1;if(m===0)d()}},[]);const w=l.default.useCallback((r,y)=>{const U=typeof r==="function"?r(u(),t):{...u(),...r};h(U);const $=s?.search??g()?.search??window.location.search;const q=E(U,n.getOtherParams(t,$));const T=q.length?`?${q}`:"";const _=x(window.location.pathname,c);const b=`${_}${T}${window.location.hash}`;if(b===`${_}${$}${window.location.hash}`)return;const{replace:C,...I}=y||{};clearTimeout(f);s={router:e,method:C?"replace":"push",url:b,pathname:window.location.pathname,search:T,opts:I};f=setTimeout(()=>{f=void 0;if(!s)return;const B=s;s=void 0;V(B)},O.TIMEOUT)},[e,E,u]);const k=l.default.useCallback(r=>w(t,r),[w]);return{updateState:h,updateUrl:w,state:L,reset:k,getState:u,pendingUrlUpdate:()=>f!==void 0}}function x(t,e){if(!e||e==="/"){return t}return t.slice(e.length)}exports.useUrlStateBase=W;
1
+ "use strict";var R=require("react");var A=require("../constants/constants.cjs");var S=require("../subscribers.cjs");var D=require("../useIsomorphicLayoutEffect.cjs");var F=require("../useSharedState/useSharedState.cjs");var H=require("../useUrlEncode/useUrlEncode.cjs");var o=require("../utils.cjs");function J(t){return t&&t.__esModule?t:{default:t}}var i=J(R);let g;let a;let s;let E;let U=0;const b=()=>window.location.pathname+window.location.search;function f(){s=void 0;E?.();E=void 0}function d(){if(!s)return void 0;const t=b();if(t===s.from&&t!==s.to){return s}f();return void 0}function K(t,e){const n=d()?.from??b();if(n===t){f();return}s={from:n,to:t,search:e};E??=o.subscribeToUrl(f)}function N(t){if(t.pathname!==window.location.pathname)return;const e=d();const n=t.pathname+t.search;if(e?.to===n)return;const u=n===b();if(u&&!e)return;const h=u?"replace":t.method;t.router[h](t.url,t.opts);K(n,t.search)}function y(){return!!a&&a.pathname===window.location.pathname}function W(t,e,n,u,h=true){const{parse:p,stringify:q}=H.useUrlEncode(t);i.default.useState(()=>{if(o.isSSR||!n||!h)return;const r=!S.subscribers.get(t).length;if(!r||y()||d())return;const l=n({parse:p});if(!o.isEqual(S.stateMap.get(t),l))S.stateMap.set(t,l)});const{state:C,getState:c,setState:w}=F.useSharedState(t,()=>n?.({parse:p})||t);const m=i.default.useRef();if(m.current===void 0)m.current=o.getSearch();D.useIsomorphicLayoutEffect(()=>{const r=()=>{w(p(o.filterUnknownParamsClient(t,o.getSearch())))};window.addEventListener(o.popstateEv,r);if(m.current!==o.getSearch())r();return()=>{window.removeEventListener(o.popstateEv,r)}},[]);i.default.useEffect(()=>{U+=1;return()=>{U-=1;if(U===0)f()}},[]);const v=i.default.useCallback((r,l)=>{const $=typeof r==="function"?r(c(),t):{...c(),...r};w($);const T=a?.search??d()?.search??window.location.search;const L=q($,o.getOtherParams(t,T));const M=L.length?`?${L}`:"";const _=j(window.location.pathname,u);const k=`${_}${M}${window.location.hash}`;if(k===`${_}${T}${window.location.hash}`)return;const{replace:B,...O}=l||{};clearTimeout(g);a={router:e,method:B?"replace":"push",url:k,pathname:window.location.pathname,search:M,opts:O};g=setTimeout(()=>{g=void 0;if(!a)return;const P=a;a=void 0;N(P)},A.TIMEOUT)},[e,q,c]);const I=i.default.useCallback(r=>v(t,r),[v]);return{updateState:w,updateUrl:v,state:C,reset:I,getState:c,pendingUrlUpdate:y}}function j(t,e){if(!e||e==="/"){return t}return t.slice(e.length)}exports.useUrlStateBase=W;
@@ -1,14 +1,15 @@
1
1
  import { useUrlEncode } from '../useUrlEncode';
2
2
  import { type JSONCompatible, type Router } from '../utils';
3
+ declare function isWriteWaitingHere(): boolean;
3
4
  export declare function useUrlStateBase<T extends JSONCompatible>(defaultState: T, router: Router, getInitialState?: ({ parse, }: {
4
5
  parse: ReturnType<typeof useUrlEncode<T>>['parse'];
5
- }) => T, basename?: string): {
6
+ }) => T, basename?: string, canSeedFromUrl?: boolean): {
6
7
  updateState: (value: Partial<T> | ((currState: T, defaultState: T) => T)) => void;
7
8
  updateUrl: (value?: Parameters<(value: Partial<T> | ((currState: T, defaultState: T) => T)) => void>[0], options?: Options) => void;
8
9
  state: T;
9
10
  reset: (options?: Options) => void;
10
11
  getState: () => T;
11
- pendingUrlUpdate: () => boolean;
12
+ pendingUrlUpdate: typeof isWriteWaitingHere;
12
13
  };
13
14
  interface OptionsObject {
14
15
  [key: string]: unknown;
@@ -1 +1 @@
1
- import u from"react";import{TIMEOUT as x}from"../constants/constants.mjs";import{useIsomorphicLayoutEffect as B}from"../useIsomorphicLayoutEffect.mjs";import{useSharedState as H}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as M}from"../useUrlEncode/useUrlEncode.mjs";import{getSearch as h,popstateEv as L,filterUnknownParamsClient as V,getOtherParams as W,subscribeToUrl as _}from"../utils.mjs";let f;let a;let n;let w;let v=0;const g=()=>window.location.pathname+window.location.search;function l(){n=void 0;w?.();w=void 0}function S(){if(!n)return void 0;const t=g();if(t===n.from&&t!==n.to){return n}l();return void 0}function q(t,o){const r=S()?.from??g();if(r===t){l();return}n={from:r,to:t,search:o};w??=_(l)}function z(t){if(t.pathname!==window.location.pathname)return;const o=S();const r=t.pathname+t.search;if(o?.to===r)return;const i=r===g();if(i&&!o)return;const s=i?"replace":t.method;t.router[s](t.url,t.opts);q(r,t.search)}function A(t,o,r,i){const{parse:s,stringify:U}=M(t);const{state:y,getState:c,setState:m}=H(t,()=>r?.({parse:s})||t);const p=u.useRef();if(p.current===void 0)p.current=h();B(()=>{const e=()=>{m(s(V(t,h())))};window.addEventListener(L,e);if(p.current!==h())e();return()=>{window.removeEventListener(L,e)}},[]);u.useEffect(()=>{v+=1;return()=>{v-=1;if(v===0)l()}},[]);const d=u.useCallback((e,O)=>{const $=typeof e==="function"?e(c(),t):{...c(),...e};m($);const E=a?.search??S()?.search??window.location.search;const T=U($,W(t,E));const b=T.length?`?${T}`:"";const k=D(window.location.pathname,i);const C=`${k}${b}${window.location.hash}`;if(C===`${k}${E}${window.location.hash}`)return;const{replace:P,...R}=O||{};clearTimeout(f);a={router:o,method:P?"replace":"push",url:C,pathname:window.location.pathname,search:b,opts:R};f=setTimeout(()=>{f=void 0;if(!a)return;const j=a;a=void 0;z(j)},x)},[o,U,c]);const I=u.useCallback(e=>d(t,e),[d]);return{updateState:m,updateUrl:d,state:y,reset:I,getState:c,pendingUrlUpdate:()=>f!==void 0}}function D(t,o){if(!o||o==="/"){return t}return t.slice(o.length)}export{A as useUrlStateBase};
1
+ import i from"react";import{TIMEOUT as A}from"../constants/constants.mjs";import{subscribers as F,stateMap as I}from"../subscribers.mjs";import{useIsomorphicLayoutEffect as H}from"../useIsomorphicLayoutEffect.mjs";import{useSharedState as J}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as K}from"../useUrlEncode/useUrlEncode.mjs";import{isSSR as N,isEqual as W,getSearch as v,popstateEv as M,filterUnknownParamsClient as j,getOtherParams as z,subscribeToUrl as D}from"../utils.mjs";let g;let r;let a;let S;let E=0;const U=()=>window.location.pathname+window.location.search;function f(){a=void 0;S?.();S=void 0}function l(){if(!a)return void 0;const t=U();if(t===a.from&&t!==a.to){return a}f();return void 0}function G(t,o){const n=l()?.from??U();if(n===t){f();return}a={from:n,to:t,search:o};S??=D(f)}function Q(t){if(t.pathname!==window.location.pathname)return;const o=l();const n=t.pathname+t.search;if(o?.to===n)return;const s=n===U();if(s&&!o)return;const p=s?"replace":t.method;t.router[p](t.url,t.opts);G(n,t.search)}function q(){return!!r&&r.pathname===window.location.pathname}function V(t,o,n,s,p=true){const{parse:m,stringify:$}=K(t);i.useState(()=>{if(N||!n||!p)return;const e=!F.get(t).length;if(!e||q()||l())return;const u=n({parse:m});if(!W(I.get(t),u))I.set(t,u)});const{state:B,getState:c,setState:h}=J(t,()=>n?.({parse:m})||t);const d=i.useRef();if(d.current===void 0)d.current=v();H(()=>{const e=()=>{h(m(j(t,v())))};window.addEventListener(M,e);if(d.current!==v())e();return()=>{window.removeEventListener(M,e)}},[]);i.useEffect(()=>{E+=1;return()=>{E-=1;if(E===0)f()}},[]);const w=i.useCallback((e,u)=>{const b=typeof e==="function"?e(c(),t):{...c(),...e};h(b);const T=r?.search??l()?.search??window.location.search;const k=$(b,z(t,T));const C=k.length?`?${k}`:"";const L=X(window.location.pathname,s);const y=`${L}${C}${window.location.hash}`;if(y===`${L}${T}${window.location.hash}`)return;const{replace:P,...R}=u||{};clearTimeout(g);r={router:o,method:P?"replace":"push",url:y,pathname:window.location.pathname,search:C,opts:R};g=setTimeout(()=>{g=void 0;if(!r)return;const x=r;r=void 0;Q(x)},A)},[o,$,c]);const O=i.useCallback(e=>w(t,e),[w]);return{updateState:h,updateUrl:w,state:B,reset:O,getState:c,pendingUrlUpdate:q}}function X(t,o){if(!o||o==="/"){return t}return t.slice(o.length)}export{V as useUrlStateBase};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-in-url",
3
- "version": "8.0.1",
3
+ "version": "8.0.2",
4
4
  "description": "Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-16, react-router@6-7, remix@2, and Astro.",
5
5
  "homepage": "https://state-in-url.dev",
6
6
  "repository": {
@@ -18,7 +18,7 @@ sources:
18
18
  metadata:
19
19
  type: framework
20
20
  library: state-in-url
21
- library_version: '8.0.1'
21
+ library_version: '8.0.2'
22
22
  framework: astro
23
23
  ---
24
24
 
@@ -16,7 +16,7 @@ sources:
16
16
  metadata:
17
17
  type: core
18
18
  library: state-in-url
19
- library_version: '8.0.1'
19
+ library_version: '8.0.2'
20
20
  ---
21
21
 
22
22
  # state-in-url — Feature state hook
@@ -15,7 +15,7 @@ sources:
15
15
  metadata:
16
16
  type: composition
17
17
  library: state-in-url
18
- library_version: '8.0.1'
18
+ library_version: '8.0.2'
19
19
  ---
20
20
 
21
21
  This skill builds on `state-in-url/feature-state-hook`. Read it first for the module-scoped default-state rule.
@@ -16,7 +16,7 @@ sources:
16
16
  metadata:
17
17
  type: core
18
18
  library: state-in-url
19
- library_version: '8.0.1'
19
+ library_version: '8.0.2'
20
20
  ---
21
21
 
22
22
  This skill builds on `state-in-url/feature-state-hook`. Read it first for the module-scoped default-state rule.
@@ -17,7 +17,7 @@ sources:
17
17
  metadata:
18
18
  type: framework
19
19
  library: state-in-url
20
- library_version: '8.0.1'
20
+ library_version: '8.0.2'
21
21
  framework: react
22
22
  ---
23
23
 
@@ -18,7 +18,7 @@ sources:
18
18
  metadata:
19
19
  type: framework
20
20
  library: state-in-url
21
- library_version: '8.0.1'
21
+ library_version: '8.0.2'
22
22
  framework: react
23
23
  ---
24
24
 
@@ -17,7 +17,7 @@ sources:
17
17
  metadata:
18
18
  type: framework
19
19
  library: state-in-url
20
- library_version: '8.0.1'
20
+ library_version: '8.0.2'
21
21
  framework: react
22
22
  ---
23
23
 
@@ -14,7 +14,7 @@ sources:
14
14
  metadata:
15
15
  type: core
16
16
  library: state-in-url
17
- library_version: '8.0.1'
17
+ library_version: '8.0.2'
18
18
  ---
19
19
 
20
20
  # state-in-url — Shared state without URL sync