cross-state 0.55.6 → 0.56.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.
Files changed (65) hide show
  1. package/dist/{diff-BQ8bB3Wk.d.cts → diff-D7XuntCn.d.cts} +2 -2
  2. package/dist/{diff-gZezL04N.d.ts → diff-qAkLCsnM.d.ts} +2 -2
  3. package/dist/index.cjs +3 -3
  4. package/dist/index.d.cts +4 -4
  5. package/dist/index.d.ts +4 -4
  6. package/dist/index.js +3 -3
  7. package/dist/mutative/index.d.cts +2 -2
  8. package/dist/mutative/index.d.ts +2 -2
  9. package/dist/mutative/register.cjs +1 -1
  10. package/dist/mutative/register.d.cts +2 -2
  11. package/dist/mutative/register.d.ts +2 -2
  12. package/dist/mutative/register.js +1 -1
  13. package/dist/{mutativeMethods-6QzygG3W.d.cts → mutativeMethods-Be_XPk5N.d.cts} +2 -2
  14. package/dist/{mutativeMethods-Br3xN2Iq.d.ts → mutativeMethods-DRYtjUcH.d.ts} +2 -2
  15. package/dist/{patchMethods-pw_OcPA8.d.cts → patchMethods-BlYtjRUU.d.cts} +3 -3
  16. package/dist/{patchMethods-CT9M507t.d.ts → patchMethods-Dr2OFh02.d.ts} +3 -3
  17. package/dist/patches/index.d.cts +3 -3
  18. package/dist/patches/index.d.ts +3 -3
  19. package/dist/patches/register.cjs +1 -1
  20. package/dist/patches/register.d.cts +3 -3
  21. package/dist/patches/register.d.ts +3 -3
  22. package/dist/patches/register.js +1 -1
  23. package/dist/persist/register.cjs +2 -2
  24. package/dist/persist/register.d.cts +2 -2
  25. package/dist/persist/register.d.ts +2 -2
  26. package/dist/persist/register.js +2 -2
  27. package/dist/{persist-CPjpg6D0.d.ts → persist-CIEYJ0rO.d.ts} +2 -2
  28. package/dist/{persist-CGLe7YUX.cjs → persist-CQhYPv4d.cjs} +2 -2
  29. package/dist/{persist-CGLe7YUX.cjs.map → persist-CQhYPv4d.cjs.map} +1 -1
  30. package/dist/{persist-D7MAsyyW.d.cts → persist-CR3mhti7.d.cts} +2 -2
  31. package/dist/{persist-Btgg3qGU.js → persist-DFzbvNoJ.js} +2 -2
  32. package/dist/{persist-Btgg3qGU.js.map → persist-DFzbvNoJ.js.map} +1 -1
  33. package/dist/react/index.cjs +128 -95
  34. package/dist/react/index.cjs.map +1 -1
  35. package/dist/react/index.d.cts +25 -47
  36. package/dist/react/index.d.ts +25 -47
  37. package/dist/react/index.js +128 -92
  38. package/dist/react/index.js.map +1 -1
  39. package/dist/react/register.cjs +3 -3
  40. package/dist/react/register.d.cts +3 -3
  41. package/dist/react/register.d.ts +3 -3
  42. package/dist/react/register.js +3 -3
  43. package/dist/{scope-BbUXgNW_.d.ts → scope-ByLCVzKk.d.ts} +2 -2
  44. package/dist/{scope-CoDFmhxK.cjs → scope-C9JXWddv.cjs} +6 -3
  45. package/dist/scope-C9JXWddv.cjs.map +1 -0
  46. package/dist/{scope-6B7-8lwa.d.cts → scope-DkO-RIMy.d.cts} +2 -2
  47. package/dist/{scope-CKXL9xoi.js → scope-Mhr133F_.js} +6 -3
  48. package/dist/scope-Mhr133F_.js.map +1 -0
  49. package/dist/{store-DKaeE840.d.ts → store-B3PWCx63.d.cts} +2 -1
  50. package/dist/{store-BEsiS8y7.d.cts → store-BJWoRQIs.d.ts} +2 -1
  51. package/dist/{store-BpMJxIHt.js → store-DOEc60aP.js} +9 -7
  52. package/dist/store-DOEc60aP.js.map +1 -0
  53. package/dist/{store-1TGlSyq4.cjs → store-DQcFfgKD.cjs} +9 -7
  54. package/dist/store-DQcFfgKD.cjs.map +1 -0
  55. package/dist/{storeMethods-CkvcMFoL.js → storeMethods-C7bswcgj.js} +2 -2
  56. package/dist/{storeMethods-CkvcMFoL.js.map → storeMethods-C7bswcgj.js.map} +1 -1
  57. package/dist/{storeMethods-Bg8nMBrf.d.cts → storeMethods-Dt_mJTe3.d.cts} +3 -3
  58. package/dist/{storeMethods-C_RzHBQL.cjs → storeMethods-YDnpa-6-.cjs} +2 -2
  59. package/dist/{storeMethods-C_RzHBQL.cjs.map → storeMethods-YDnpa-6-.cjs.map} +1 -1
  60. package/dist/{storeMethods-DWKSveN0.d.ts → storeMethods-wsbbfJzY.d.ts} +3 -3
  61. package/package.json +3 -3
  62. package/dist/scope-CKXL9xoi.js.map +0 -1
  63. package/dist/scope-CoDFmhxK.cjs.map +0 -1
  64. package/dist/store-1TGlSyq4.cjs.map +0 -1
  65. package/dist/store-BpMJxIHt.js.map +0 -1
@@ -1,7 +1,7 @@
1
- import "../scope-6B7-8lwa.cjs";
2
- import { Constrain, Duration, GetKeys, Join, MaybePromise, Object_, Path as Path$1, PathAsString, Selector, Store, Update, UpdateFunction, Value, WildcardPathAsString, WildcardValue } from "../store-BEsiS8y7.cjs";
3
- import { ScopeProps, ScopeProvider, UseCacheArray, UseCacheValue, UseStoreOptions, cacheMethods, scopeMethods, storeMethods, useCache, useScope, useStore } from "../storeMethods-Bg8nMBrf.cjs";
4
- import React$1, { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
1
+ import "../scope-DkO-RIMy.cjs";
2
+ import { Constrain, Duration, GetKeys, Join, MaybePromise, Object_, Path as Path$1, PathAsString, Selector, Store, Update, Value, WildcardPathAsString, WildcardValue } from "../store-B3PWCx63.cjs";
3
+ import { ScopeProps, ScopeProvider, UseCacheArray, UseCacheValue, UseStoreOptions, cacheMethods, scopeMethods, storeMethods, useCache, useScope, useStore } from "../storeMethods-Dt_mJTe3.cjs";
4
+ import { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
5
5
 
6
6
  //#region src/react/form/customInput.d.ts
7
7
  interface CustomInputProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -223,34 +223,6 @@ declare function LoadingBoundary({
223
223
  }: LoadingBoundaryProps): React.JSX.Element;
224
224
  declare function useLoadingBoundary(isLoading: boolean | undefined, label?: ReactNode): void;
225
225
  //#endregion
226
- //#region src/react/url/urlContext.d.ts
227
- type Location = string | {
228
- pathname: string;
229
- search: string;
230
- hash: string;
231
- };
232
- interface UrlContextType {
233
- location: Location;
234
- navigate: (navigate: (from: Location) => string) => void;
235
- }
236
- type UrlContextProviderProps = {
237
- children?: ReactNode;
238
- } & ({
239
- location: UrlContextType["location"];
240
- } | {
241
- locationHook: () => UrlContextType["location"];
242
- }) & ({
243
- navigate: UrlContextType["navigate"];
244
- } | {
245
- navigateHook: () => UrlContextType["navigate"];
246
- });
247
- declare const UrlContext: Context<UrlContextType | undefined>;
248
- declare function UrlProvider({
249
- children,
250
- ...props
251
- }: UrlContextProviderProps): React$1.JSX.Element;
252
- declare function useUrlContext(): UrlContextType;
253
- //#endregion
254
226
  //#region src/react/url/urlOptions.d.ts
255
227
  interface UrlOptions<T> {
256
228
  key: string;
@@ -263,6 +235,7 @@ interface UrlOptions<T> {
263
235
  persist?: {
264
236
  id: string;
265
237
  } | null;
238
+ path?: string | RegExp | (string | RegExp)[] | null;
266
239
  }
267
240
  interface UrlOptionsWithoutDefaults<T> extends Omit<UrlOptions<T | undefined>, "defaultValue"> {
268
241
  defaultValue?: T | undefined;
@@ -270,21 +243,26 @@ interface UrlOptionsWithoutDefaults<T> extends Omit<UrlOptions<T | undefined>, "
270
243
  declare function createUrlOptions<T>(options: UrlOptions<T>): Required<UrlOptions<T>>;
271
244
  declare function createUrlOptions<T>(options: UrlOptionsWithoutDefaults<T>): Required<UrlOptions<T | undefined>>;
272
245
  //#endregion
273
- //#region src/react/url/urlStore.d.ts
274
- declare class UrlStore<T> {
275
- readonly options: Required<UrlOptions<T>>;
276
- constructor(options: Required<UrlOptions<T>>);
277
- useStore(): T;
278
- useProp(): [T, update: UpdateFunction<T>];
279
- parse(location: Location): T | undefined;
246
+ //#region src/react/url/urlParamStore.d.ts
247
+ declare const urlStore: Store<string>;
248
+ declare class UrlParamStore<T> extends Store<T> {
249
+ readonly urlOptions: Required<UrlOptions<T>>;
250
+ private lastHref?;
251
+ private lastStorageValue?;
252
+ private lastValue?;
253
+ private storageKey;
254
+ constructor(urlOptions: Required<UrlOptions<T>>);
255
+ private getUrlValue;
256
+ private getStorageValue;
257
+ private isPathActive;
258
+ private calc;
259
+ private updateUrl;
260
+ private updateStorage;
261
+ set(update: Update<T>): void;
262
+ set<const P>(path: Constrain<P, Path$1<T>>, update: Update<Value<T, P>>): void;
280
263
  }
281
- declare function createUrlStore<T>(options: UrlOptions<T>): UrlStore<T>;
282
- declare function createUrlStore<T>(options: UrlOptionsWithoutDefaults<T>): UrlStore<T | undefined>;
283
- //#endregion
284
- //#region src/react/url/useUrlParam.d.ts
285
- declare function useUrlParam<T>(store: UrlStore<T>): [T, update: UpdateFunction<T>];
286
- declare function useUrlParam<T>(options: UrlOptions<T>): [T, update: UpdateFunction<T>];
287
- declare function useUrlParam<T>(options: UrlOptionsWithoutDefaults<T>): [T | undefined, update: UpdateFunction<T | undefined>];
264
+ declare function createUrlParam<T>(options: UrlOptions<T>): UrlParamStore<T>;
265
+ declare function createUrlParam<T>(options: UrlOptionsWithoutDefaults<T>): UrlParamStore<T | undefined>;
288
266
  //#endregion
289
267
  //#region src/react/useDecoupledState.d.ts
290
268
  interface UseDecoupledStateOptions<T> {
@@ -299,5 +277,5 @@ declare function useProp<T, S>(store: Store<T>, selector: Selector<T, S>, update
299
277
  declare function useProp<T, const P>(store: Store<T>, selector: Constrain<P, Path$1<T>>, options?: UseStoreOptions<Value<T, P>>): [value: Value<T, P>, setValue: Store<Value<T, P>>["set"]];
300
278
  declare function useProp<T>(store: Store<T>, options?: UseStoreOptions<T>): [value: T, setValue: Store<T>["set"]];
301
279
  //#endregion
302
- export { CustomInput, CustomInputProps, Field, Form, FormContext, FormDerivedState, FormFieldComponent, FormFieldComponentProps, FormFieldInfos, FormFieldProps, FormFieldPropsWithComponent, FormFieldPropsWithRender, FormInstance, FormOptions, FormState, LoadingBoundary, type LoadingBoundaryEntry, type LoadingBoundaryProps, type ScopeProps, ScopeProvider, Transform, UrlContext, type UrlContextProviderProps, type UrlContextType, type UrlOptions, type UrlOptionsWithoutDefaults, UrlProvider, type UrlStore, type UseCacheArray, type UseCacheValue, type UseDecoupledStateOptions, type UseStoreOptions, Validation, Validations, cacheMethods, createForm, createUrlOptions, createUrlStore, scopeMethods, storeMethods, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore, useUrlContext, useUrlParam };
280
+ export { CustomInput, CustomInputProps, Field, Form, FormContext, FormDerivedState, FormFieldComponent, FormFieldComponentProps, FormFieldInfos, FormFieldProps, FormFieldPropsWithComponent, FormFieldPropsWithRender, FormInstance, FormOptions, FormState, LoadingBoundary, type LoadingBoundaryEntry, type LoadingBoundaryProps, type ScopeProps, ScopeProvider, Transform, type UrlOptions, type UrlOptionsWithoutDefaults, type UrlParamStore, type UseCacheArray, type UseCacheValue, type UseDecoupledStateOptions, type UseStoreOptions, Validation, Validations, cacheMethods, createForm, createUrlOptions, createUrlParam, scopeMethods, storeMethods, urlStore, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore };
303
281
  //# sourceMappingURL=index.d.cts.map
@@ -1,7 +1,7 @@
1
- import "../scope-BbUXgNW_.js";
2
- import { Constrain, Duration, GetKeys, Join, MaybePromise, Object_, Path as Path$1, PathAsString, Selector, Store, Update, UpdateFunction, Value, WildcardPathAsString, WildcardValue } from "../store-DKaeE840.js";
3
- import { ScopeProps, ScopeProvider, UseCacheArray, UseCacheValue, UseStoreOptions, cacheMethods, scopeMethods, storeMethods, useCache, useScope, useStore } from "../storeMethods-DWKSveN0.js";
4
- import React$1, { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
1
+ import "../scope-ByLCVzKk.js";
2
+ import { Constrain, Duration, GetKeys, Join, MaybePromise, Object_, Path as Path$1, PathAsString, Selector, Store, Update, Value, WildcardPathAsString, WildcardValue } from "../store-BJWoRQIs.js";
3
+ import { ScopeProps, ScopeProvider, UseCacheArray, UseCacheValue, UseStoreOptions, cacheMethods, scopeMethods, storeMethods, useCache, useScope, useStore } from "../storeMethods-wsbbfJzY.js";
4
+ import { Component, ComponentPropsWithoutRef, Context, FormEvent, FunctionComponent, HTMLProps, ReactNode } from "react";
5
5
 
6
6
  //#region src/react/form/customInput.d.ts
7
7
  interface CustomInputProps extends React.HTMLAttributes<HTMLDivElement> {
@@ -223,34 +223,6 @@ declare function LoadingBoundary({
223
223
  }: LoadingBoundaryProps): React.JSX.Element;
224
224
  declare function useLoadingBoundary(isLoading: boolean | undefined, label?: ReactNode): void;
225
225
  //#endregion
226
- //#region src/react/url/urlContext.d.ts
227
- type Location = string | {
228
- pathname: string;
229
- search: string;
230
- hash: string;
231
- };
232
- interface UrlContextType {
233
- location: Location;
234
- navigate: (navigate: (from: Location) => string) => void;
235
- }
236
- type UrlContextProviderProps = {
237
- children?: ReactNode;
238
- } & ({
239
- location: UrlContextType["location"];
240
- } | {
241
- locationHook: () => UrlContextType["location"];
242
- }) & ({
243
- navigate: UrlContextType["navigate"];
244
- } | {
245
- navigateHook: () => UrlContextType["navigate"];
246
- });
247
- declare const UrlContext: Context<UrlContextType | undefined>;
248
- declare function UrlProvider({
249
- children,
250
- ...props
251
- }: UrlContextProviderProps): React$1.JSX.Element;
252
- declare function useUrlContext(): UrlContextType;
253
- //#endregion
254
226
  //#region src/react/url/urlOptions.d.ts
255
227
  interface UrlOptions<T> {
256
228
  key: string;
@@ -263,6 +235,7 @@ interface UrlOptions<T> {
263
235
  persist?: {
264
236
  id: string;
265
237
  } | null;
238
+ path?: string | RegExp | (string | RegExp)[] | null;
266
239
  }
267
240
  interface UrlOptionsWithoutDefaults<T> extends Omit<UrlOptions<T | undefined>, "defaultValue"> {
268
241
  defaultValue?: T | undefined;
@@ -270,21 +243,26 @@ interface UrlOptionsWithoutDefaults<T> extends Omit<UrlOptions<T | undefined>, "
270
243
  declare function createUrlOptions<T>(options: UrlOptions<T>): Required<UrlOptions<T>>;
271
244
  declare function createUrlOptions<T>(options: UrlOptionsWithoutDefaults<T>): Required<UrlOptions<T | undefined>>;
272
245
  //#endregion
273
- //#region src/react/url/urlStore.d.ts
274
- declare class UrlStore<T> {
275
- readonly options: Required<UrlOptions<T>>;
276
- constructor(options: Required<UrlOptions<T>>);
277
- useStore(): T;
278
- useProp(): [T, update: UpdateFunction<T>];
279
- parse(location: Location): T | undefined;
246
+ //#region src/react/url/urlParamStore.d.ts
247
+ declare const urlStore: Store<string>;
248
+ declare class UrlParamStore<T> extends Store<T> {
249
+ readonly urlOptions: Required<UrlOptions<T>>;
250
+ private lastHref?;
251
+ private lastStorageValue?;
252
+ private lastValue?;
253
+ private storageKey;
254
+ constructor(urlOptions: Required<UrlOptions<T>>);
255
+ private getUrlValue;
256
+ private getStorageValue;
257
+ private isPathActive;
258
+ private calc;
259
+ private updateUrl;
260
+ private updateStorage;
261
+ set(update: Update<T>): void;
262
+ set<const P>(path: Constrain<P, Path$1<T>>, update: Update<Value<T, P>>): void;
280
263
  }
281
- declare function createUrlStore<T>(options: UrlOptions<T>): UrlStore<T>;
282
- declare function createUrlStore<T>(options: UrlOptionsWithoutDefaults<T>): UrlStore<T | undefined>;
283
- //#endregion
284
- //#region src/react/url/useUrlParam.d.ts
285
- declare function useUrlParam<T>(store: UrlStore<T>): [T, update: UpdateFunction<T>];
286
- declare function useUrlParam<T>(options: UrlOptions<T>): [T, update: UpdateFunction<T>];
287
- declare function useUrlParam<T>(options: UrlOptionsWithoutDefaults<T>): [T | undefined, update: UpdateFunction<T | undefined>];
264
+ declare function createUrlParam<T>(options: UrlOptions<T>): UrlParamStore<T>;
265
+ declare function createUrlParam<T>(options: UrlOptionsWithoutDefaults<T>): UrlParamStore<T | undefined>;
288
266
  //#endregion
289
267
  //#region src/react/useDecoupledState.d.ts
290
268
  interface UseDecoupledStateOptions<T> {
@@ -299,5 +277,5 @@ declare function useProp<T, S>(store: Store<T>, selector: Selector<T, S>, update
299
277
  declare function useProp<T, const P>(store: Store<T>, selector: Constrain<P, Path$1<T>>, options?: UseStoreOptions<Value<T, P>>): [value: Value<T, P>, setValue: Store<Value<T, P>>["set"]];
300
278
  declare function useProp<T>(store: Store<T>, options?: UseStoreOptions<T>): [value: T, setValue: Store<T>["set"]];
301
279
  //#endregion
302
- export { CustomInput, CustomInputProps, Field, Form, FormContext, FormDerivedState, FormFieldComponent, FormFieldComponentProps, FormFieldInfos, FormFieldProps, FormFieldPropsWithComponent, FormFieldPropsWithRender, FormInstance, FormOptions, FormState, LoadingBoundary, type LoadingBoundaryEntry, type LoadingBoundaryProps, type ScopeProps, ScopeProvider, Transform, UrlContext, type UrlContextProviderProps, type UrlContextType, type UrlOptions, type UrlOptionsWithoutDefaults, UrlProvider, type UrlStore, type UseCacheArray, type UseCacheValue, type UseDecoupledStateOptions, type UseStoreOptions, Validation, Validations, cacheMethods, createForm, createUrlOptions, createUrlStore, scopeMethods, storeMethods, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore, useUrlContext, useUrlParam };
280
+ export { CustomInput, CustomInputProps, Field, Form, FormContext, FormDerivedState, FormFieldComponent, FormFieldComponentProps, FormFieldInfos, FormFieldProps, FormFieldPropsWithComponent, FormFieldPropsWithRender, FormInstance, FormOptions, FormState, LoadingBoundary, type LoadingBoundaryEntry, type LoadingBoundaryProps, type ScopeProps, ScopeProvider, Transform, type UrlOptions, type UrlOptionsWithoutDefaults, type UrlParamStore, type UseCacheArray, type UseCacheValue, type UseDecoupledStateOptions, type UseStoreOptions, Validation, Validations, cacheMethods, createForm, createUrlOptions, createUrlParam, scopeMethods, storeMethods, urlStore, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore };
303
281
  //# sourceMappingURL=index.d.ts.map
@@ -1,9 +1,9 @@
1
- import { autobind, calcDuration, createStore, debounce, queue, throttle } from "../store-BpMJxIHt.js";
1
+ import { Store, autobind, calcDuration, createStore, debounce, queue, throttle } from "../store-DOEc60aP.js";
2
2
  import { castArrayPath, deepEqual, get, isObject, join, set } from "../propAccess-B260LXN1.js";
3
3
  import { simpleHash } from "../hash-DNFM5y_h.js";
4
4
  import { fromExtendedJsonString, toExtendedJsonString } from "../extendedJson-BZkQBXEv.js";
5
- import { LoadingBoundary, ScopeProvider, cacheMethods, scopeMethods, storeMethods, useCache, useLoadingBoundary, useProp, useScope, useStore } from "../storeMethods-CkvcMFoL.js";
6
- import React, { Fragment, createContext, createElement, startTransition, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
5
+ import { LoadingBoundary, ScopeProvider, cacheMethods, scopeMethods, storeMethods, useCache, useLoadingBoundary, useProp, useScope, useStore } from "../storeMethods-C7bswcgj.js";
6
+ import { Fragment, createContext, createElement, startTransition, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
7
7
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
8
8
 
9
9
  //#region src/react/form/customInput.tsx
@@ -464,23 +464,9 @@ function createForm(options) {
464
464
  }
465
465
 
466
466
  //#endregion
467
- //#region src/react/url/urlContext.tsx
468
- const UrlContext = createContext(void 0);
469
- function UrlProvider({ children,...props }) {
470
- const location = "location" in props ? props.location : props.locationHook();
471
- const navigate = "navigate" in props ? props.navigate : props.navigateHook();
472
- return /* @__PURE__ */ jsx(UrlContext.Provider, {
473
- value: {
474
- location,
475
- navigate
476
- },
477
- children
478
- });
479
- }
480
- function useUrlContext() {
481
- const context = useContext(UrlContext);
482
- if (!context) throw new Error("useUrlContext must be used within a UrlContextProvider");
483
- return context;
467
+ //#region src/lib/castArray.ts
468
+ function castArray(value) {
469
+ return Array.isArray(value) ? value : [value];
484
470
  }
485
471
 
486
472
  //#endregion
@@ -496,17 +482,14 @@ function defaultDeserializer(value) {
496
482
  function defaultSerializer(value) {
497
483
  return toExtendedJsonString(value);
498
484
  }
499
- function parseLocation(location) {
500
- if (typeof location !== "string") location = `${location.pathname}${location.search}${location.hash}`;
501
- return new URL(location, window.location.origin);
502
- }
503
- function createStorageKey(id, key) {
504
- return `cross-state:url:${id}:${key}`;
485
+ function normalizePath(path) {
486
+ if (typeof path === "string") return path.replace(/^\//g, "").replace(/\/$/g, "");
487
+ return path;
505
488
  }
506
489
 
507
490
  //#endregion
508
491
  //#region src/react/url/urlOptions.ts
509
- function createUrlOptions({ key, type = "hash", serialize = defaultSerializer, deserialize = defaultDeserializer, defaultValue = void 0, writeDefaultValue = false, onCommit = () => void 0, persist = null }) {
492
+ function createUrlOptions({ key, type = "hash", serialize = defaultSerializer, deserialize = defaultDeserializer, defaultValue = void 0, writeDefaultValue = false, onCommit = () => void 0, persist = null, path = null }) {
510
493
  return {
511
494
  key,
512
495
  type,
@@ -515,81 +498,134 @@ function createUrlOptions({ key, type = "hash", serialize = defaultSerializer, d
515
498
  defaultValue,
516
499
  writeDefaultValue,
517
500
  onCommit,
518
- persist
501
+ persist,
502
+ path: path === null ? null : castArray(path).map(normalizePath)
519
503
  };
520
504
  }
521
505
 
522
506
  //#endregion
523
- //#region src/react/lib/useLatestRef.ts
524
- function useLatestRef(value) {
525
- const ref = useRef(value);
526
- useEffect(() => {
527
- ref.current = value;
528
- }, [value]);
529
- return ref;
530
- }
531
-
532
- //#endregion
533
- //#region src/react/url/useUrlParam.ts
534
- function useUrlParam(input) {
535
- const { key, type, serialize, deserialize, defaultValue, writeDefaultValue, onCommit, persist } = createUrlOptions("options" in input ? input.options : input);
536
- const { location, navigate } = useUrlContext();
537
- const url = parseLocation(location);
538
- const params = new URLSearchParams(url[type].slice(1));
539
- const urlValue = params.get(key);
540
- const storageKey = persist && createStorageKey(persist.id, key);
541
- const storageValue = storageKey ? localStorage.getItem(storageKey) : null;
542
- const value = useMemo(() => urlValue !== null ? deserialize(urlValue) : storageValue !== null ? deserialize(storageValue) : defaultValue, [urlValue]);
543
- function commit(value$1) {
544
- const serializedValue = serialize(value$1);
545
- navigate((location$1) => {
546
- const url$1 = parseLocation(location$1);
547
- const params$1 = new URLSearchParams(url$1[type].slice(1));
548
- if (!writeDefaultValue && serializedValue === serialize(defaultValue)) params$1.delete(key);
549
- else params$1.set(key, serializedValue);
550
- url$1[type] = params$1.toString();
551
- return url$1.toString().replace(window.location.origin, "");
507
+ //#region src/react/url/urlParamStore.ts
508
+ const urlStore = createStore(() => window.location.href, {
509
+ cacheValue: false,
510
+ effect() {
511
+ const update = () => {
512
+ if (window.location.href !== this.calculatedValue?.value) this.invalidate();
513
+ };
514
+ const interval = setInterval(update, 1);
515
+ window.addEventListener("popstate", update);
516
+ return () => {
517
+ clearInterval(interval);
518
+ window.removeEventListener("popstate", update);
519
+ };
520
+ }
521
+ });
522
+ var UrlParamStore = class UrlParamStore extends Store {
523
+ constructor(urlOptions) {
524
+ super(() => this.calc(), { cacheValue: false });
525
+ this.urlOptions = urlOptions;
526
+ autobind(UrlParamStore);
527
+ this.storageKey = urlOptions.persist && createStorageKey(urlOptions.persist.id, urlOptions.key);
528
+ this.addEffect(() => {
529
+ let isActive = false;
530
+ let urlValue = this.getUrlValue();
531
+ let storageValue = this.getStorageValue();
532
+ const update = () => {
533
+ const oldIsActive = isActive;
534
+ isActive = this.isPathActive();
535
+ const oldUrlValue = urlValue;
536
+ urlValue = this.getUrlValue();
537
+ const oldStorageValue = storageValue;
538
+ storageValue = this.getStorageValue();
539
+ if (!isActive) return;
540
+ if (isActive === oldIsActive && urlValue === oldUrlValue && storageValue === oldStorageValue) return;
541
+ this.invalidate();
542
+ if (!oldIsActive) {
543
+ if (urlValue !== null) this.updateStorage(this.urlOptions.deserialize(urlValue));
544
+ else if (storageValue !== null) this.updateUrl(this.urlOptions.deserialize(storageValue));
545
+ else if (this.urlOptions.writeDefaultValue) {
546
+ this.updateUrl(this.urlOptions.defaultValue);
547
+ this.updateStorage(this.urlOptions.defaultValue);
548
+ }
549
+ } else if (urlValue !== oldUrlValue) {
550
+ if (urlValue === null && this.urlOptions.writeDefaultValue) this.updateUrl(this.urlOptions.defaultValue);
551
+ this.updateStorage();
552
+ } else if (storageValue !== null) {
553
+ if (storageValue === null && this.urlOptions.writeDefaultValue) this.updateUrl(this.urlOptions.defaultValue);
554
+ this.updateUrl(this.urlOptions.deserialize(storageValue));
555
+ }
556
+ };
557
+ const cancel = urlStore.subscribe(update);
558
+ window.addEventListener("storage", update);
559
+ return () => {
560
+ cancel();
561
+ window.removeEventListener("storage", update);
562
+ };
552
563
  });
553
- if (storageKey) localStorage.setItem(storageKey, serializedValue);
554
- onCommit?.(value$1);
555
564
  }
556
- const latestValue = useLatestRef(value);
557
- const latestCommit = useLatestRef(commit);
558
- const update = useCallback((update$1) => {
559
- if (update$1 instanceof Function) update$1 = update$1(latestValue.current);
560
- latestCommit.current(update$1);
561
- }, []);
562
- useEffect(() => {
563
- if (urlValue !== null) commit(deserialize(urlValue));
564
- }, [urlValue]);
565
- useEffect(() => {
566
- if (urlValue === null && storageValue !== null) commit(deserialize(storageValue));
567
- else if (urlValue === null && writeDefaultValue) commit(defaultValue);
568
- }, []);
569
- return [value, update];
570
- }
571
-
572
- //#endregion
573
- //#region src/react/url/urlStore.ts
574
- var UrlStore = class {
575
- constructor(options) {
576
- this.options = options;
565
+ getUrlValue() {
566
+ const href = urlStore.get();
567
+ const url = new URL(href);
568
+ const params = new URLSearchParams(url[this.urlOptions.type].slice(1) || "");
569
+ return params.get(this.urlOptions.key);
577
570
  }
578
- useStore() {
579
- return useUrlParam(this)[0];
571
+ getStorageValue() {
572
+ return this.storageKey !== null ? localStorage.getItem(this.storageKey) : null;
580
573
  }
581
- useProp() {
582
- return useUrlParam(this);
574
+ isPathActive() {
575
+ if (this.urlOptions.path === null) return true;
576
+ const path = normalizePath(window.location.pathname);
577
+ return castArray(this.urlOptions.path).some((p) => {
578
+ if (typeof p === "string") return !p || p === path || path.startsWith(p + "/");
579
+ return p.test(path);
580
+ });
581
+ }
582
+ calc() {
583
+ let href = window.location.href;
584
+ const storageValue = this.storageKey !== null ? localStorage.getItem(this.storageKey) : null;
585
+ if (!this.isPathActive() && this.lastHref !== void 0) href = this.lastHref;
586
+ if (this.lastHref === href && this.lastStorageValue === storageValue) return this.lastValue;
587
+ const url = new URL(href);
588
+ const params = new URLSearchParams(url[this.urlOptions.type].slice(1));
589
+ const urlValue = params.get(this.urlOptions.key);
590
+ const value = urlValue !== null ? this.urlOptions.deserialize(urlValue) : this.storageKey !== null && storageValue !== null ? this.urlOptions.deserialize(storageValue) : this.urlOptions.defaultValue;
591
+ this.lastHref = href;
592
+ this.lastStorageValue = storageValue;
593
+ this.lastValue = value;
594
+ return value;
595
+ }
596
+ updateUrl(value = this.calc()) {
597
+ const serializedValue = this.urlOptions.serialize(value);
598
+ const url = new URL(window.location.href);
599
+ const params = new URLSearchParams(url[this.urlOptions.type].slice(1));
600
+ if (!this.urlOptions.writeDefaultValue && serializedValue === this.urlOptions.serialize(this.urlOptions.defaultValue)) params.delete(this.urlOptions.key);
601
+ else params.set(this.urlOptions.key, serializedValue);
602
+ url[this.urlOptions.type] = params.toString();
603
+ window.history.replaceState(window.history.state, "", url.toString());
604
+ window.dispatchEvent(new PopStateEvent("popstate"));
605
+ }
606
+ updateStorage(value = this.calc()) {
607
+ if (this.storageKey === null) return;
608
+ const serializedValue = this.urlOptions.serialize(value);
609
+ localStorage.setItem(this.storageKey, serializedValue);
583
610
  }
584
- parse(location) {
585
- const url = parseLocation(location);
586
- const params = new URLSearchParams(url[this.options.type].slice(1));
587
- const urlValue = params.get(this.options.key);
588
- return urlValue !== null ? this.options.deserialize(urlValue) : void 0;
611
+ set(...args) {
612
+ const path = args.length > 1 ? args[0] : [];
613
+ let update = args.length > 1 ? args[1] : args[0];
614
+ if (update instanceof Function) {
615
+ const before = this.get();
616
+ const valueBefore = get(before, path);
617
+ const valueAfter = update(valueBefore);
618
+ update = set(before, path, valueAfter);
619
+ } else if (path.length > 0) update = set(this.get(), path, update);
620
+ if (this.isPathActive()) this.updateUrl(update);
621
+ else this.updateStorage(update);
589
622
  }
590
623
  };
591
- function createUrlStore(options) {
592
- return new UrlStore(createUrlOptions(options));
624
+ function createUrlParam(options) {
625
+ return new UrlParamStore(createUrlOptions(options));
626
+ }
627
+ function createStorageKey(id, key) {
628
+ return `cross-state:url:${id}:${key}`;
593
629
  }
594
630
 
595
631
  //#endregion
@@ -626,5 +662,5 @@ function useDecoupledState(value, onChange, options = {}) {
626
662
  }
627
663
 
628
664
  //#endregion
629
- export { CustomInput, Form, LoadingBoundary, ScopeProvider, UrlContext, UrlProvider, cacheMethods, createForm, createUrlOptions, createUrlStore, scopeMethods, storeMethods, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore, useUrlContext, useUrlParam };
665
+ export { CustomInput, Form, LoadingBoundary, ScopeProvider, cacheMethods, createForm, createUrlOptions, createUrlParam, scopeMethods, storeMethods, urlStore, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore };
630
666
  //# sourceMappingURL=index.js.map