cross-state 0.44.0 → 0.45.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.
@@ -33,32 +33,49 @@ function connectUrl(store$1, {
33
33
  serialize = defaultSerializer,
34
34
  deserialize = defaultDeserializer,
35
35
  defaultValue = undefined,
36
- onCommit
36
+ onCommit,
37
+ debounce: debounceTime = 500
37
38
  }) {
38
39
  const serializedDefaultValue = serialize(defaultValue);
40
+ let isDirty = false;
41
+ const commit = store.debounce(() => {
42
+ if (isDirty) {
43
+ const value = store$1.get();
44
+ const url = new URL(window.location.href);
45
+ const parameters = new URLSearchParams(url[type].slice(1));
46
+ const serializedValue = value !== undefined ? serialize(value) : undefined;
47
+ if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
48
+ parameters.delete(key);
49
+ } else {
50
+ parameters.set(key, serializedValue);
51
+ }
52
+ url[type] = parameters.toString();
53
+ window.history.replaceState(window.history.state, "", url.toString());
54
+ window.dispatchEvent(new PopStateEvent("popstate"));
55
+ onCommit?.(value);
56
+ isDirty = false;
57
+ }
58
+ }, debounceTime);
39
59
  const cancelUrlListener = urlStore.subscribe((_url) => {
60
+ if (isDirty) {
61
+ return;
62
+ }
40
63
  const url = new URL(_url);
41
64
  const parameters = new URLSearchParams(url[type].slice(1));
42
65
  const urlValue = parameters.get(key);
43
66
  store$1.set(urlValue !== null ? deserialize(urlValue) : defaultValue);
44
67
  });
45
- const cancelSubscription = store$1.subscribe((value) => {
46
- const url = new URL(window.location.href);
47
- const parameters = new URLSearchParams(url[type].slice(1));
48
- const serializedValue = value !== undefined ? serialize(value) : undefined;
49
- if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
50
- parameters.delete(key);
51
- } else {
52
- parameters.set(key, serializedValue);
53
- }
54
- url[type] = parameters.toString();
55
- window.history.replaceState(window.history.state, "", url.toString());
56
- window.dispatchEvent(new PopStateEvent("popstate"));
57
- onCommit?.(value);
58
- });
68
+ const cancelSubscription = store$1.subscribe(
69
+ () => {
70
+ isDirty = true;
71
+ commit();
72
+ },
73
+ { runNow: false }
74
+ );
59
75
  return store.disposable(() => {
60
76
  cancelUrlListener();
61
77
  cancelSubscription();
78
+ commit.flush();
62
79
  });
63
80
  }
64
81
  function defaultDeserializer(value) {
@@ -1 +1 @@
1
- {"version":3,"file":"urlStore.cjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import disposable from '@lib/disposable';\nimport { type DisposableCancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {\n key: string;\n type?: 'search' | 'hash';\n serialize?: (value: T) => string;\n deserialize?: (value: string) => T;\n defaultValue?: T;\n onCommit?: (value: T | undefined) => void;\n}\n\nexport interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {\n defaultValue: T;\n}\n\nexport type UrlStoreOptionsRequired<T> = UrlStoreOptions<T> &\n Required<Pick<UrlStoreOptions<T>, 'type' | 'serialize' | 'deserialize' | 'defaultValue'>>;\n\nconst urlStore = createStore(() => (typeof window !== 'undefined' ? window.location.href : ''));\n\nurlStore.addEffect(() => {\n const originalPushState = window.history.pushState;\n const originalReplaceState = window.history.replaceState;\n\n const update = () => {\n urlStore.set(window.location.href);\n };\n\n window.history.pushState = (...args) => {\n originalPushState.apply(window.history, args);\n update();\n };\n\n window.history.replaceState = (...args) => {\n originalReplaceState.apply(window.history, args);\n update();\n };\n\n window.addEventListener('popstate', update);\n\n return () => {\n window.history.pushState = originalPushState;\n window.history.replaceState = originalReplaceState;\n window.removeEventListener('popstate', update);\n };\n});\n\nexport function updateUrlStore(): void {\n urlStore.set(window.location.href);\n}\n\nexport function connectUrl<T>(\n store: Store<T>,\n options: UrlStoreOptionsWithDefaults<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T | undefined>,\n options: UrlStoreOptions<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T>,\n {\n key,\n type = 'search',\n serialize = defaultSerializer,\n deserialize = defaultDeserializer,\n defaultValue = undefined as T,\n onCommit,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = serialize(defaultValue);\n\n const cancelUrlListener = urlStore.subscribe((_url) => {\n const url = new URL(_url);\n const parameters = new URLSearchParams(url[type].slice(1));\n const urlValue = parameters.get(key);\n\n store.set(urlValue !== null ? deserialize(urlValue) : defaultValue);\n });\n\n const cancelSubscription = store.subscribe((value) => {\n const url = new URL(window.location.href);\n const parameters = new URLSearchParams(url[type].slice(1));\n const serializedValue = value !== undefined ? serialize(value) : undefined;\n\n if (serializedValue === undefined || serializedValue === serializedDefaultValue) {\n parameters.delete(key);\n } else {\n parameters.set(key, serializedValue);\n }\n\n url[type] = parameters.toString();\n\n window.history.replaceState(window.history.state, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n });\n\n return disposable(() => {\n cancelUrlListener();\n cancelSubscription();\n });\n}\n\nfunction defaultDeserializer(value: string): any {\n if (value === undefined) {\n return undefined;\n }\n\n try {\n return JSON.parse(value, (_k, v) => {\n if (typeof v === 'object' && v !== null && '__set' in v) {\n return new Set(v.__set);\n }\n if (typeof v === 'object' && v !== null && '__map' in v) {\n return new Map(v.__map);\n }\n return v;\n });\n } catch {\n return undefined;\n }\n}\n\nfunction defaultSerializer(value: any): string {\n return JSON.stringify(value, (_k, v) => {\n if (v instanceof Set) {\n return { __set: Array.from(v) };\n }\n if (v instanceof Map) {\n return { __map: Array.from(v) };\n }\n return v;\n });\n}\n\nexport function createUrlStore<T>(options: UrlStoreOptionsWithDefaults<T>): Store<T>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>): Store<T | undefined>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>) {\n const store = createStore(options.defaultValue, options);\n connectUrl(store, options);\n return store;\n}\n"],"names":["createStore","store","disposable"],"mappings":";;;;AAoBA,MAAM,QAAA,GAAWA,kBAAY,MAAO,OA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ACR,CAAA;AACH;AAMO,SAAS,eAAkB,OAA6B,EAAA;AAC7D,EAAA,MAAMD,OAAQ,GAAAD,iBAAA,CAAY,OAAQ,CAAA,YAAA,EAAc,OAAO,CAAA;AACvD,EAAA,UAAA,CAAWC,SAAO,OAAO,CAAA;AACzB,EAAO,OAAAA,OAAA;AACT;;;;;;"}
1
+ {"version":3,"file":"urlStore.cjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import disposable from '@lib/disposable';\nimport { type DisposableCancel, type Duration } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\nimport { debounce } from '@lib/debounce';\n\nexport interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {\n key: string;\n type?: 'search' | 'hash';\n serialize?: (value: T) => string;\n deserialize?: (value: string) => T;\n defaultValue?: T;\n onCommit?: (value: T | undefined) => void;\n debounce?: Duration;\n}\n\nexport interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {\n defaultValue: T;\n}\n\nexport type UrlStoreOptionsRequired<T> = UrlStoreOptions<T> &\n Required<Pick<UrlStoreOptions<T>, 'type' | 'serialize' | 'deserialize' | 'defaultValue'>>;\n\nconst urlStore = createStore(() => (typeof window !== 'undefined' ? window.location.href : ''));\n\nurlStore.addEffect(() => {\n const originalPushState = window.history.pushState;\n const originalReplaceState = window.history.replaceState;\n\n const update = () => {\n urlStore.set(window.location.href);\n };\n\n window.history.pushState = (...args) => {\n originalPushState.apply(window.history, args);\n update();\n };\n\n window.history.replaceState = (...args) => {\n originalReplaceState.apply(window.history, args);\n update();\n };\n\n window.addEventListener('popstate', update);\n\n return () => {\n window.history.pushState = originalPushState;\n window.history.replaceState = originalReplaceState;\n window.removeEventListener('popstate', update);\n };\n});\n\nexport function updateUrlStore(): void {\n urlStore.set(window.location.href);\n}\n\nexport function connectUrl<T>(\n store: Store<T>,\n options: UrlStoreOptionsWithDefaults<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T | undefined>,\n options: UrlStoreOptions<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T>,\n {\n key,\n type = 'search',\n serialize = defaultSerializer,\n deserialize = defaultDeserializer,\n defaultValue = undefined as T,\n onCommit,\n debounce: debounceTime = 500,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = serialize(defaultValue);\n let isDirty = false;\n\n const commit = debounce(() => {\n if (isDirty) {\n const value = store.get();\n const url = new URL(window.location.href);\n const parameters = new URLSearchParams(url[type].slice(1));\n const serializedValue = value !== undefined ? serialize(value) : undefined;\n\n if (serializedValue === undefined || serializedValue === serializedDefaultValue) {\n parameters.delete(key);\n } else {\n parameters.set(key, serializedValue);\n }\n\n url[type] = parameters.toString();\n\n window.history.replaceState(window.history.state, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n isDirty = false;\n }\n }, debounceTime);\n\n const cancelUrlListener = urlStore.subscribe((_url) => {\n if (isDirty) {\n return;\n }\n\n const url = new URL(_url);\n const parameters = new URLSearchParams(url[type].slice(1));\n const urlValue = parameters.get(key);\n\n store.set(urlValue !== null ? deserialize(urlValue) : defaultValue);\n });\n\n const cancelSubscription = store.subscribe(\n () => {\n isDirty = true;\n commit();\n },\n { runNow: false },\n );\n\n return disposable(() => {\n cancelUrlListener();\n cancelSubscription();\n commit.flush();\n });\n}\n\nfunction defaultDeserializer(value: string): any {\n if (value === undefined) {\n return undefined;\n }\n\n try {\n return JSON.parse(value, (_k, v) => {\n if (typeof v === 'object' && v !== null && '__set' in v) {\n return new Set(v.__set);\n }\n if (typeof v === 'object' && v !== null && '__map' in v) {\n return new Map(v.__map);\n }\n return v;\n });\n } catch {\n return undefined;\n }\n}\n\nfunction defaultSerializer(value: any): string {\n return JSON.stringify(value, (_k, v) => {\n if (v instanceof Set) {\n return { __set: Array.from(v) };\n }\n if (v instanceof Map) {\n return { __map: Array.from(v) };\n }\n return v;\n });\n}\n\nexport function createUrlStore<T>(options: UrlStoreOptionsWithDefaults<T>): Store<T>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>): Store<T | undefined>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>) {\n const store = createStore(options.defaultValue, options);\n connectUrl(store, options);\n return store;\n}\n"],"names":["createStore","store","debounce","disposable"],"mappings":";;;;AAsBA,MAAM,QAAA,GAAWA,kBAAY,MAAO,OAAO,WAAW,WAAc,GAAA,MAAA,CAAO,QAAS,CAAA,IAAA,GAAO,EAAG,CAAA;AAE9F,QAAA,CAAS,UAAU,MAAM;AACvB,EAAM,MAAA,iBAAA,GAAoB,OAAO,OAAQ,CAAA,SAAA;AACzC,EAAM,MAAA,oBAAA,GAAuB,OAAO,OAAQ,CAAA,YAAA;AAE5C,EAAA,MAAM,SAAS,MAAM;AACnB,IAAS,QAAA,CAAA,GAAA,CAAI,MAAO,CAAA,QAAA,CAAS,IAAI,CAAA;AAAA,GACnC;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,SAAY,GAAA,CAAA,GAAI,IAAS,KAAA;AACtC,IAAkB,iBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;AAC5C,IAAO,MAAA,EAAA;AAAA,GACT;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,YAAe,GAAA,CAAA,GAAI,IAAS,KAAA;AACzC,IAAqB,oBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;AAC/C,IAAO,MAAA,EAAA;AAAA,GACT;AAEA,EAAO,MAAA,CAAA,gBAAA,CAAiB,YAAY,MAAM,CAAA;AAE1C,EAAA,OAAO,MAAM;AACX,IAAA,MAAA,CAAO,QAAQ,SAAY,GAAA,iBAAA;AAC3B,IAAA,MAAA,CAAO,QAAQ,YAAe,GAAA,oBAAA;AAC9B,IAAO,MAAA,CAAA,mBAAA,CAAoB,YAAY,MAAM,CAAA;AAAA,GAC/C;AACF,CAAC,CAAA;AAEM,SAAS,cAAuB,GAAA;A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AAA;AAExB,MAAA,IAAI,OAAO,CAAM,KAAA,QAAA,IAAY,CAAM,KAAA,IAAA,IAAQ,WAAW,CAAG,EAAA;AACvD,QAAO,OAAA,IAAI,GAAI,CAAA,CAAA,CAAE,KAAK,CAAA;AAAA;AAExB,MAAO,OAAA,CAAA;AAAA,KACR,CAAA;AAAA,GACK,CAAA,MAAA;AACN,IAAO,OAAA,SAAA;AAAA;AAEX;AAEA,SAAS,kBAAkB,KAAoB,EAAA;AAC7C,EAAA,OAAO,IAAK,CAAA,SAAA,CAAU,KAAO,EAAA,CAAC,IAAI,CAAM,KAAA;AACtC,IAAA,IAAI,aAAa,GAAK,EAAA;AACpB,MAAA,OAAO,EAAE,KAAA,EAAO,KAAM,CAAA,IAAA,CAAK,CAAC,CAAE,EAAA;AAAA;AAEhC,IAAA,IAAI,aAAa,GAAK,EAAA;AACpB,MAAA,OAAO,EAAE,KAAA,EAAO,KAAM,CAAA,IAAA,CAAK,CAAC,CAAE,EAAA;AAAA;AAEhC,IAAO,OAAA,CAAA;AAAA,GACR,CAAA;AACH;AAMO,SAAS,eAAkB,OAA6B,EAAA;AAC7D,EAAA,MAAMF,OAAQ,GAAAD,iBAAA,CAAY,OAAQ,CAAA,YAAA,EAAc,OAAO,CAAA;AACvD,EAAA,UAAA,CAAWC,SAAO,OAAO,CAAA;AACzB,EAAO,OAAAA,OAAA;AACT;;;;;;"}
@@ -1,4 +1,4 @@
1
- import { S as Store, e as autobind } from '../store.mjs';
1
+ import { S as Store, f as autobind } from '../store.mjs';
2
2
  import { m as mutativeMethods } from '../mutativeMethods.mjs';
3
3
 
4
4
  Object.assign(Store.prototype, mutativeMethods);
@@ -1,4 +1,4 @@
1
- import { S as Store, e as autobind } from '../store.mjs';
1
+ import { S as Store, f as autobind } from '../store.mjs';
2
2
  import { p as patchMethods } from '../index2.mjs';
3
3
 
4
4
  Object.assign(Store.prototype, patchMethods);
@@ -1,7 +1,7 @@
1
1
  import { u as useStore } from '../storeMethods.mjs';
2
2
  export { L as LoadingBoundary, S as ScopeProvider, c as cacheMethods, a as scopeMethods, s as storeMethods, e as useCache, b as useLoadingBoundary, f as useProp, d as useScope } from '../storeMethods.mjs';
3
3
  import { useState, useEffect, createElement, useCallback, Fragment as Fragment$1, useRef, useMemo, createContext, useContext, startTransition } from 'react';
4
- import { c as calcDuration, q as queue, h as debounce, e as autobind, b as createStore, t as throttle } from '../store.mjs';
4
+ import { c as calcDuration, q as queue, d as debounce, f as autobind, b as createStore, t as throttle } from '../store.mjs';
5
5
  import { s as simpleHash } from '../hash.mjs';
6
6
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
7
7
  import { c as connectUrl } from '../urlStore.mjs';
@@ -1,5 +1,5 @@
1
1
  import { C as Cache, S as Scope } from '../scope.mjs';
2
- import { S as Store, e as autobind } from '../store.mjs';
2
+ import { S as Store, f as autobind } from '../store.mjs';
3
3
  import { s as storeMethods, c as cacheMethods, a as scopeMethods } from '../storeMethods.mjs';
4
4
 
5
5
  Object.assign(Store.prototype, storeMethods);
package/dist/es/scope.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { e as autobind, S as Store, b as createStore, f as calculatedValue, P as PromiseWithState, g as makeSelector, c as calcDuration } from './store.mjs';
1
+ import { f as autobind, S as Store, b as createStore, g as calculatedValue, P as PromiseWithState, h as makeSelector, c as calcDuration } from './store.mjs';
2
2
  import { d as deepEqual } from './propAccess.mjs';
3
3
  import { s as simpleHash } from './hash.mjs';
4
4
 
package/dist/es/store.mjs CHANGED
@@ -814,5 +814,5 @@ const createStore = /* @__PURE__ */ Object.assign(create, {
814
814
  }
815
815
  });
816
816
 
817
- export { PromiseWithState as P, Store as S, arrayMethods as a, createStore as b, calcDuration as c, disposable as d, autobind as e, calculatedValue as f, makeSelector as g, debounce as h, isPromise as i, mapMethods as m, queue as q, recordMethods as r, setMethods as s, throttle as t };
817
+ export { PromiseWithState as P, Store as S, arrayMethods as a, createStore as b, calcDuration as c, debounce as d, disposable as e, autobind as f, calculatedValue as g, makeSelector as h, isPromise as i, mapMethods as m, queue as q, recordMethods as r, setMethods as s, throttle as t };
818
818
  //# sourceMappingURL=store.mjs.map
@@ -1,4 +1,4 @@
1
- import { g as makeSelector, b as createStore } from './store.mjs';
1
+ import { h as makeSelector, b as createStore } from './store.mjs';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
  import require$$0, { useRef, useCallback, useLayoutEffect, useDebugValue, useContext, createContext, useMemo, useEffect } from 'react';
4
4
  import { d as deepEqual, i as isPlainObject } from './propAccess.mjs';
@@ -1,4 +1,4 @@
1
- import { b as createStore, d as disposable } from './store.mjs';
1
+ import { d as debounce, b as createStore, e as disposable } from './store.mjs';
2
2
 
3
3
  const urlStore = createStore(() => typeof window !== "undefined" ? window.location.href : "");
4
4
  urlStore.addEffect(() => {
@@ -31,32 +31,49 @@ function connectUrl(store, {
31
31
  serialize = defaultSerializer,
32
32
  deserialize = defaultDeserializer,
33
33
  defaultValue = undefined,
34
- onCommit
34
+ onCommit,
35
+ debounce: debounceTime = 500
35
36
  }) {
36
37
  const serializedDefaultValue = serialize(defaultValue);
38
+ let isDirty = false;
39
+ const commit = debounce(() => {
40
+ if (isDirty) {
41
+ const value = store.get();
42
+ const url = new URL(window.location.href);
43
+ const parameters = new URLSearchParams(url[type].slice(1));
44
+ const serializedValue = value !== undefined ? serialize(value) : undefined;
45
+ if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
46
+ parameters.delete(key);
47
+ } else {
48
+ parameters.set(key, serializedValue);
49
+ }
50
+ url[type] = parameters.toString();
51
+ window.history.replaceState(window.history.state, "", url.toString());
52
+ window.dispatchEvent(new PopStateEvent("popstate"));
53
+ onCommit?.(value);
54
+ isDirty = false;
55
+ }
56
+ }, debounceTime);
37
57
  const cancelUrlListener = urlStore.subscribe((_url) => {
58
+ if (isDirty) {
59
+ return;
60
+ }
38
61
  const url = new URL(_url);
39
62
  const parameters = new URLSearchParams(url[type].slice(1));
40
63
  const urlValue = parameters.get(key);
41
64
  store.set(urlValue !== null ? deserialize(urlValue) : defaultValue);
42
65
  });
43
- const cancelSubscription = store.subscribe((value) => {
44
- const url = new URL(window.location.href);
45
- const parameters = new URLSearchParams(url[type].slice(1));
46
- const serializedValue = value !== undefined ? serialize(value) : undefined;
47
- if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
48
- parameters.delete(key);
49
- } else {
50
- parameters.set(key, serializedValue);
51
- }
52
- url[type] = parameters.toString();
53
- window.history.replaceState(window.history.state, "", url.toString());
54
- window.dispatchEvent(new PopStateEvent("popstate"));
55
- onCommit?.(value);
56
- });
66
+ const cancelSubscription = store.subscribe(
67
+ () => {
68
+ isDirty = true;
69
+ commit();
70
+ },
71
+ { runNow: false }
72
+ );
57
73
  return disposable(() => {
58
74
  cancelUrlListener();
59
75
  cancelSubscription();
76
+ commit.flush();
60
77
  });
61
78
  }
62
79
  function defaultDeserializer(value) {
@@ -1 +1 @@
1
- {"version":3,"file":"urlStore.mjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import disposable from '@lib/disposable';\nimport { type DisposableCancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {\n key: string;\n type?: 'search' | 'hash';\n serialize?: (value: T) => string;\n deserialize?: (value: string) => T;\n defaultValue?: T;\n onCommit?: (value: T | undefined) => void;\n}\n\nexport interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {\n defaultValue: T;\n}\n\nexport type UrlStoreOptionsRequired<T> = UrlStoreOptions<T> &\n Required<Pick<UrlStoreOptions<T>, 'type' | 'serialize' | 'deserialize' | 'defaultValue'>>;\n\nconst urlStore = createStore(() => (typeof window !== 'undefined' ? window.location.href : ''));\n\nurlStore.addEffect(() => {\n const originalPushState = window.history.pushState;\n const originalReplaceState = window.history.replaceState;\n\n const update = () => {\n urlStore.set(window.location.href);\n };\n\n window.history.pushState = (...args) => {\n originalPushState.apply(window.history, args);\n update();\n };\n\n window.history.replaceState = (...args) => {\n originalReplaceState.apply(window.history, args);\n update();\n };\n\n window.addEventListener('popstate', update);\n\n return () => {\n window.history.pushState = originalPushState;\n window.history.replaceState = originalReplaceState;\n window.removeEventListener('popstate', update);\n };\n});\n\nexport function updateUrlStore(): void {\n urlStore.set(window.location.href);\n}\n\nexport function connectUrl<T>(\n store: Store<T>,\n options: UrlStoreOptionsWithDefaults<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T | undefined>,\n options: UrlStoreOptions<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T>,\n {\n key,\n type = 'search',\n serialize = defaultSerializer,\n deserialize = defaultDeserializer,\n defaultValue = undefined as T,\n onCommit,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = serialize(defaultValue);\n\n const cancelUrlListener = urlStore.subscribe((_url) => {\n const url = new URL(_url);\n const parameters = new URLSearchParams(url[type].slice(1));\n const urlValue = parameters.get(key);\n\n store.set(urlValue !== null ? deserialize(urlValue) : defaultValue);\n });\n\n const cancelSubscription = store.subscribe((value) => {\n const url = new URL(window.location.href);\n const parameters = new URLSearchParams(url[type].slice(1));\n const serializedValue = value !== undefined ? serialize(value) : undefined;\n\n if (serializedValue === undefined || serializedValue === serializedDefaultValue) {\n parameters.delete(key);\n } else {\n parameters.set(key, serializedValue);\n }\n\n url[type] = parameters.toString();\n\n window.history.replaceState(window.history.state, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n });\n\n return disposable(() => {\n cancelUrlListener();\n cancelSubscription();\n });\n}\n\nfunction defaultDeserializer(value: string): any {\n if (value === undefined) {\n return undefined;\n }\n\n try {\n return JSON.parse(value, (_k, v) => {\n if (typeof v === 'object' && v !== null && '__set' in v) {\n return new Set(v.__set);\n }\n if (typeof v === 'object' && v !== null && '__map' in v) {\n return new Map(v.__map);\n }\n return v;\n });\n } catch {\n return undefined;\n }\n}\n\nfunction defaultSerializer(value: any): string {\n return JSON.stringify(value, (_k, v) => {\n if (v instanceof Set) {\n return { __set: Array.from(v) };\n }\n if (v instanceof Map) {\n return { __map: Array.from(v) };\n }\n return v;\n });\n}\n\nexport function createUrlStore<T>(options: UrlStoreOptionsWithDefaults<T>): Store<T>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>): Store<T | undefined>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>) {\n const store = createStore(options.defaultValue, options);\n connectUrl(store, options);\n return store;\n}\n"],"names":[],"mappings":";;AAoBA,MAAM,QAAA,GAAW,YAAY,MAAO,OAAO,WAAW,WAAc,GAAA,MAAA,CAAO,QAAS,CAAA,IAAA,GAAO,EAAG,CAAA;AAE9F,QAAA,CAAS,UAAU,MAAM;AACvB,EAAM,MAAA,iBAAA,GAAoB,OAAO,OAAQ,CAAA,SAAA;AACzC,EAAM,MAAA,oBAAA,GAAuB,OAAO,OAAQ,CAAA,YAAA;AAE5C,EAAA,MAAM,SAAS,MAAM;AACnB,IAAS,QAAA,CAAA,GAAA,CAAI,MAAO,CAAA,QAAA,CAAS,IAAI,CAAA;AAAA,GACnC;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,SAAY,GAAA,CAAA,GAAI,IAAS,KAAA;AACtC,IAAkB,iBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;AAC5C,IAAO,MAAA,EAAA;AAAA,GACT;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,YAAe,GAAA,CAAA,GAAI,IAAS,KAAA;AACzC,IAAqB,oBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;AAC/C,IAAO,MAAA,EAAA;AAAA,GACT;AAEA,EAAO,MAAA,CAAA,gBAAA,CAAiB,YAAY,MAAM,CAAA;AAE1C,EAAA,OAAO,MAAM;AACX,IAAA,MAAA,CAAO,QAAQ,SAAY,GAAA,iBAAA;AAC3B,IAAA,MAAA,CAAO,QAAQ,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B,EAAA;AAC7C,EAAA,OAAO,IAAK,CAAA,SAAA,CAAU,KAAO,EAAA,CAAC,IAAI,CAAM,KAAA;AACtC,IAAA,IAAI,aAAa,GAAK,EAAA;AACpB,MAAA,OAAO,EAAE,KAAA,EAAO,KAAM,CAAA,IAAA,CAAK,CAAC,CAAE,EAAA;AAAA;AAEhC,IAAA,IAAI,aAAa,GAAK,EAAA;AACpB,MAAA,OAAO,EAAE,KAAA,EAAO,KAAM,CAAA,IAAA,CAAK,CAAC,CAAE,EAAA;AAAA;AAEhC,IAAO,OAAA,CAAA;AAAA,GACR,CAAA;AACH;AAMO,SAAS,eAAkB,OAA6B,EAAA;AAC7D,EAAA,MAAM,KAAQ,GAAA,WAAA,CAAY,OAAQ,CAAA,YAAA,EAAc,OAAO,CAAA;AACvD,EAAA,UAAA,CAAW,OAAO,OAAO,CAAA;AACzB,EAAO,OAAA,KAAA;AACT;;;;"}
1
+ {"version":3,"file":"urlStore.mjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import disposable from '@lib/disposable';\nimport { type DisposableCancel, type Duration } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\nimport { debounce } from '@lib/debounce';\n\nexport interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {\n key: string;\n type?: 'search' | 'hash';\n serialize?: (value: T) => string;\n deserialize?: (value: string) => T;\n defaultValue?: T;\n onCommit?: (value: T | undefined) => void;\n debounce?: Duration;\n}\n\nexport interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {\n defaultValue: T;\n}\n\nexport type UrlStoreOptionsRequired<T> = UrlStoreOptions<T> &\n Required<Pick<UrlStoreOptions<T>, 'type' | 'serialize' | 'deserialize' | 'defaultValue'>>;\n\nconst urlStore = createStore(() => (typeof window !== 'undefined' ? window.location.href : ''));\n\nurlStore.addEffect(() => {\n const originalPushState = window.history.pushState;\n const originalReplaceState = window.history.replaceState;\n\n const update = () => {\n urlStore.set(window.location.href);\n };\n\n window.history.pushState = (...args) => {\n originalPushState.apply(window.history, args);\n update();\n };\n\n window.history.replaceState = (...args) => {\n originalReplaceState.apply(window.history, args);\n update();\n };\n\n window.addEventListener('popstate', update);\n\n return () => {\n window.history.pushState = originalPushState;\n window.history.replaceState = originalReplaceState;\n window.removeEventListener('popstate', update);\n };\n});\n\nexport function updateUrlStore(): void {\n urlStore.set(window.location.href);\n}\n\nexport function connectUrl<T>(\n store: Store<T>,\n options: UrlStoreOptionsWithDefaults<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T | undefined>,\n options: UrlStoreOptions<T>,\n): DisposableCancel;\n\nexport function connectUrl<T>(\n store: Store<T>,\n {\n key,\n type = 'search',\n serialize = defaultSerializer,\n deserialize = defaultDeserializer,\n defaultValue = undefined as T,\n onCommit,\n debounce: debounceTime = 500,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = serialize(defaultValue);\n let isDirty = false;\n\n const commit = debounce(() => {\n if (isDirty) {\n const value = store.get();\n const url = new URL(window.location.href);\n const parameters = new URLSearchParams(url[type].slice(1));\n const serializedValue = value !== undefined ? serialize(value) : undefined;\n\n if (serializedValue === undefined || serializedValue === serializedDefaultValue) {\n parameters.delete(key);\n } else {\n parameters.set(key, serializedValue);\n }\n\n url[type] = parameters.toString();\n\n window.history.replaceState(window.history.state, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n isDirty = false;\n }\n }, debounceTime);\n\n const cancelUrlListener = urlStore.subscribe((_url) => {\n if (isDirty) {\n return;\n }\n\n const url = new URL(_url);\n const parameters = new URLSearchParams(url[type].slice(1));\n const urlValue = parameters.get(key);\n\n store.set(urlValue !== null ? deserialize(urlValue) : defaultValue);\n });\n\n const cancelSubscription = store.subscribe(\n () => {\n isDirty = true;\n commit();\n },\n { runNow: false },\n );\n\n return disposable(() => {\n cancelUrlListener();\n cancelSubscription();\n commit.flush();\n });\n}\n\nfunction defaultDeserializer(value: string): any {\n if (value === undefined) {\n return undefined;\n }\n\n try {\n return JSON.parse(value, (_k, v) => {\n if (typeof v === 'object' && v !== null && '__set' in v) {\n return new Set(v.__set);\n }\n if (typeof v === 'object' && v !== null && '__map' in v) {\n return new Map(v.__map);\n }\n return v;\n });\n } catch {\n return undefined;\n }\n}\n\nfunction defaultSerializer(value: any): string {\n return JSON.stringify(value, (_k, v) => {\n if (v instanceof Set) {\n return { __set: Array.from(v) };\n }\n if (v instanceof Map) {\n return { __map: Array.from(v) };\n }\n return v;\n });\n}\n\nexport function createUrlStore<T>(options: UrlStoreOptionsWithDefaults<T>): Store<T>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>): Store<T | undefined>;\n\nexport function createUrlStore<T>(options: UrlStoreOptions<T>) {\n const store = createStore(options.defaultValue, options);\n connectUrl(store, options);\n return store;\n}\n"],"names":[],"mappings":";;AAsBA,MAAM,QAAA,GAAW,YAAY,MAAO,OAAO,WAAW,WAAc,GAAA,MAAA,CAAO,QAAS,CAAA,IAAA,GAAO,EAAG,CAAA;AAE9F,QAAA,CAAS,UAAU,MAAM;AACvB,EAAM,MAAA,iBAAA,GAAoB,OAAO,OAAQ,CAAA,SAAA;AACzC,EAAM,MAAA,oBAAA,GAAuB,OAAO,OAAQ,CAAA,YAAA;AAE5C,EAAA,MAAM,SAAS,MAAM;AACnB,IAAS,QAAA,CAAA,GAAA,CAAI,MAAO,CAAA,QAAA,CAAS,IAAI,CAAA;AAAA,GACnC;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,SAAY,GAAA,CAAA,GAAI,IAAS,KAAA;AACtC,IAAkB,iBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;AAC5C,IAAO,MAAA,EAAA;AAAA,GACT;AAEA,EAAO,MAAA,CAAA,OAAA,CAAQ,YAAe,GAAA,CAAA,GAAI,IAAS,KAAA;AACzC,IAAqB,oBAAA,CAAA,KAAA,CAAM,MAAO,CAAA,OAAA,EAAS,IAAI,CAAA;A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 import { type DisposableCancel, type Duration } from './commonTypes';
2
2
  import { type Store, type StoreOptions } from './store';
3
3
  export interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {
4
4
  key: string;
@@ -7,6 +7,7 @@ export interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {
7
7
  deserialize?: (value: string) => T;
8
8
  defaultValue?: T;
9
9
  onCommit?: (value: T | undefined) => void;
10
+ debounce?: Duration;
10
11
  }
11
12
  export interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {
12
13
  defaultValue: T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cross-state",
3
- "version": "0.44.0",
3
+ "version": "0.45.0",
4
4
  "description": "(React) state library",
5
5
  "license": "ISC",
6
6
  "repository": "schummar/cross-state",