cross-state 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,63 +1,22 @@
1
- [![npm badge](https://badgen.net/npm/v/schummar-state)](https://www.npmjs.com/package/schummar-state)
2
- [![bundlephobia badge](https://badgen.net/bundlephobia/minzip/schummar-state)](https://bundlephobia.com/result?p=schummar-state)
1
+ ![npm badge](https://img.shields.io/npm/v/cross-state)
2
+ [![bundlejs badge](https://deno.bundlejs.com/?badge&q=cross-state)](https://bundlejs.com/?q=cross-state)
3
3
 
4
- Lighweight React hooks based state library.
5
- Heavily inspired by [pullstate](https://github.com/lostpebble/pullstate)
4
+ State library for frontend and backend. With React bindings.
6
5
 
7
6
  # Getting started
8
7
 
9
8
  ### Install
10
9
 
11
10
  ```
12
- npm install schummar-state
11
+ npm install cross-state
13
12
  ```
14
13
 
15
14
  ### Create a store
16
15
 
17
16
  ```ts
18
- import { Store } from 'cross-state';
17
+ import { createStore } from 'cross-state';
19
18
 
20
- export const store = new Store({
19
+ export const store = createStore({
21
20
  counter: 0,
22
21
  });
23
22
  ```
24
-
25
- You can easily use multiple stores in parallel.
26
-
27
- ### Use store in a component
28
-
29
- ```tsx
30
- import store from './store';
31
-
32
- export function App() {
33
- const counter = store.useState((state) => state.counter);
34
-
35
- return (
36
- <div>
37
- <div>Counter: {counter}</div>
38
- </div>
39
- );
40
- }
41
- ```
42
-
43
- ### Update a store
44
-
45
- ```tsx
46
- import store from './store';
47
-
48
- export function App() {
49
- const counter = store.useState((state) => state.counter);
50
-
51
- const increment = () =>
52
- store.update((state) => {
53
- state.counter++;
54
- });
55
-
56
- return (
57
- <div>
58
- <div>Counter: {counter}</div>
59
- <button onClick={increment}>Increment</button>
60
- </div>
61
- );
62
- }
63
- ```
@@ -48,6 +48,7 @@ function connectUrl(store2, {
48
48
  }
49
49
  url[type] = parameters.toString();
50
50
  window.history.replaceState(null, "", url.toString());
51
+ window.dispatchEvent(new PopStateEvent("popstate"));
51
52
  onCommit == null ? void 0 : onCommit(value);
52
53
  });
53
54
  return () => {
@@ -1 +1 @@
1
- {"version":3,"file":"urlStore.cjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import { type Cancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions {\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 connectUrl<T>(store: Store<T>, options: UrlStoreOptionsWithDefaults<T>): Cancel;\n\nexport function connectUrl<T>(store: Store<T | undefined>, options: UrlStoreOptions<T>): Cancel;\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): Cancel {\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(null, '', url.toString());\n\n onCommit?.(value);\n });\n\n return () => {\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"],"mappings":";;AAmBA,MAAM,WAAWA,MAAAA,YAAY,MAAO,OAAO,WAAW,cAAc,OAAO,SAAS,OAAO,EAAG;AAE9F,SAAS,UAAU,MAAM;AACjB,QAAA,oBAAoB,OAAO,QAAQ;AACnC,QAAA,uBAAuB,OAAO,QAAQ;AAE5C,QAAM,SAAS,MAAM;AACV,aAAA,IAAI,OAAO,SAAS,IAAI;AAAA,EAAA;AAG5B,SAAA,QAAQ,YAAY,IAAI,SAAS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S,oBAAoB,OAAoB;AAC/C,MAAI,UAAU,QAAW;AAChB,WAAA;AAAA,EACT;AAEI,MAAA;AACF,WAAO,KAAK,MAAM,OAAO,CAAC,IAAI,MAAM;AAClC,UAAI,OAAO,MAAM,YAAY,MAAM,QAAQ,WAAW,GAAG;AAChD,eAAA,IAAI,IAAI,EAAE,KAAK;AAAA,MACxB;AACA,UAAI,OAAO,MAAM,YAAY,MAAM,QAAQ,WAAW,GAAG;AAChD,eAAA,IAAI,IAAI,EAAE,KAAK;AAAA,MACxB;AACO,aAAA;AAAA,IAAA,CACR;AAAA,EAAA,QACD;AACO,WAAA;AAAA,EACT;AACF;AAEA,SAAS,kBAAkB,OAAoB;AAC7C,SAAO,KAAK,UAAU,OAAO,CAAC,IAAI,MAAM;AACtC,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACA,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACO,WAAA;AAAA,EAAA,CACR;AACH;AAMO,SAAS,eAAkB,SAA6B;AAC7D,QAAMA,UAAQD,MAAA,YAAY,QAAQ,cAAc,OAAO;AACvD,aAAWC,SAAO,OAAO;AAClB,SAAAA;AACT;;;"}
1
+ {"version":3,"file":"urlStore.cjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import { type Cancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions {\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 connectUrl<T>(store: Store<T>, options: UrlStoreOptionsWithDefaults<T>): Cancel;\n\nexport function connectUrl<T>(store: Store<T | undefined>, options: UrlStoreOptions<T>): Cancel;\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): Cancel {\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(null, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n });\n\n return () => {\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"],"mappings":";;AAmBA,MAAM,WAAWA,MAAAA,YAAY,MAAO,OAAO,WAAW,cAAc,OAAO,SAAS,OAAO,EAAG;AAE9F,SAAS,UAAU,MAAM;AACjB,QAAA,oBAAoB,OAAO,QAAQ;AACnC,QAAA,uBAAuB,OAAO,QAAQ;AAE5C,QAAM,SAAS,MAAM;AACV,aAAA,IAAI,OAAO,SAAS,IAAI;AAAA,EAAA;AAG5B,SAAA,QAAQ,YAAY,IAAI,SAAS;AACpB,sBAAA,MAAM,OAAO,SAAS,IAAI;AACrC;EAAA;AAGF,SAAA,QAAQ,eAAe,IAAI,SAAS;AACpB,yBAAA,MAAM,OAAO,SAAS,IAAI;AACxC;EAAA;AAGF,SAAA,iBAAiB,YAAY,MAAM;AAE1C,SAAO,MAAM;AACX,WAAO,QAAQ,YAAY;AAC3B,WAAO,QAAQ,eAAe;AACvB,WAAA,oBAAoB,YAAY,MAAM;AAAA,EAAA;AAEjD,CAAC;AAMM,SAAS,WACdC,QACA;AAAA,EACE;AAAA,EACA,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,eAAe;AAAA,EACf;AACF,GACQ;AACF,QAAA,yBAAyB,UAAU,YAAY;AAErD,QAAM,oBAAoB,SAAS,UAAU,CAAC,SA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U,OAAO,CAAC,IAAI,MAAM;AACtC,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACA,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACO,WAAA;AAAA,EAAA,CACR;AACH;AAMO,SAAS,eAAkB,SAA6B;AAC7D,QAAMA,UAAQD,MAAA,YAAY,QAAQ,cAAc,OAAO;AACvD,aAAWC,SAAO,OAAO;AAClB,SAAAA;AACT;;;"}
@@ -47,6 +47,7 @@ function connectUrl(store, {
47
47
  }
48
48
  url[type] = parameters.toString();
49
49
  window.history.replaceState(null, "", url.toString());
50
+ window.dispatchEvent(new PopStateEvent("popstate"));
50
51
  onCommit == null ? void 0 : onCommit(value);
51
52
  });
52
53
  return () => {
@@ -1 +1 @@
1
- {"version":3,"file":"urlStore.mjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import { type Cancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions {\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 connectUrl<T>(store: Store<T>, options: UrlStoreOptionsWithDefaults<T>): Cancel;\n\nexport function connectUrl<T>(store: Store<T | undefined>, options: UrlStoreOptions<T>): Cancel;\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): Cancel {\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(null, '', url.toString());\n\n onCommit?.(value);\n });\n\n return () => {\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":";AAmBA,MAAM,WAAW,YAAY,MAAO,OAAO,WAAW,cAAc,OAAO,SAAS,OAAO,EAAG;AAE9F,SAAS,UAAU,MAAM;AACjB,QAAA,oBAAoB,OAAO,QAAQ;AACnC,QAAA,uBAAuB,OAAO,QAAQ;AAE5C,QAAM,SAAS,MAAM;AACV,aAAA,IAAI,OAAO,SAAS,IAAI;AAAA,EAAA;AAG5B,SAAA,QAAQ,YAAY,IAAI,SAAS;AACpB,sBAAA,MAAM,OAAO,SAAS,IAAI;AACrC;EAAA;AAGF,SAAA,QAAQ,eAAe,IAAI,SAAS;AACpB,yBAAA,MAAM,OAAO,SAAS,IAAI;AACxC;EAAA;AAGF,SAAA,iBAAiB,YAAY,MAAM;AAE1C,SAAO,MAAM;AACX,WAAO,QAAQ,YAAY;AAC3B,WAAO,QAAQ,eAAe;AACvB,WAAA,oBAAoB,YAAY,MAAM;AAAA,EAAA;AAEjD,CAAC;AAMM,SAAS,WACd,OACA;AAAA,EACE;AAAA,EACA,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,eAAe;AAAA,EACf;AACF,GACQ;AACF,QAAA,yBAAyB,UAAU,YAAY;AAErD,QAAM,oBAAoB,SAAS,UAAU,CAAC,SAAS;AAC/C,UAAA,MAAM,IAAI,IAAI,IAAI;AAClB,UAAA,aAAa,IAAI,gBAAgB,IAAI,IAAI,EAAE,MAAM,CAAC,CAAC;AACnD,UAAA,WAAW,WAAW,IAAI,GAAG;AAEnC,UAAM,IAAI,aAAa,OAAO,YAAY,QAAQ,IAAI,YAAY;AAAA,EAAA,CACnE;AAED,QAAM,qBAAqB,MAAM,UAAU,CAAC,UAAU;AACpD,UAAM,MAAM,IAAI,IAAI,OAAO,SAAS,IAAI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urlStore.mjs","sources":["../../src/core/urlStore.ts"],"sourcesContent":["import { type Cancel } from './commonTypes';\nimport { createStore, type Store, type StoreOptions } from './store';\n\nexport interface UrlStoreOptions<T> extends StoreOptions {\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 connectUrl<T>(store: Store<T>, options: UrlStoreOptionsWithDefaults<T>): Cancel;\n\nexport function connectUrl<T>(store: Store<T | undefined>, options: UrlStoreOptions<T>): Cancel;\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): Cancel {\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(null, '', url.toString());\n window.dispatchEvent(new PopStateEvent('popstate'));\n\n onCommit?.(value);\n });\n\n return () => {\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":";AAmBA,MAAM,WAAW,YAAY,MAAO,OAAO,WAAW,cAAc,OAAO,SAAS,OAAO,EAAG;AAE9F,SAAS,UAAU,MAAM;AACjB,QAAA,oBAAoB,OAAO,QAAQ;AACnC,QAAA,uBAAuB,OAAO,QAAQ;AAE5C,QAAM,SAAS,MAAM;AACV,aAAA,IAAI,OAAO,SAAS,IAAI;AAAA,EAAA;AAG5B,SAAA,QAAQ,YAAY,IAAI,SAAS;AACpB,sBAAA,MAAM,OAAO,SAAS,IAAI;AACrC;EAAA;AAGF,SAAA,QAAQ,eAAe,IAAI,SAAS;AACpB,yBAAA,M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,MAAA;AACF,WAAO,KAAK,MAAM,OAAO,CAAC,IAAI,MAAM;AAClC,UAAI,OAAO,MAAM,YAAY,MAAM,QAAQ,WAAW,GAAG;AAChD,eAAA,IAAI,IAAI,EAAE,KAAK;AAAA,MACxB;AACA,UAAI,OAAO,MAAM,YAAY,MAAM,QAAQ,WAAW,GAAG;AAChD,eAAA,IAAI,IAAI,EAAE,KAAK;AAAA,MACxB;AACO,aAAA;AAAA,IAAA,CACR;AAAA,EAAA,QACD;AACO,WAAA;AAAA,EACT;AACF;AAEA,SAAS,kBAAkB,OAAoB;AAC7C,SAAO,KAAK,UAAU,OAAO,CAAC,IAAI,MAAM;AACtC,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACA,QAAI,aAAa,KAAK;AACpB,aAAO,EAAE,OAAO,MAAM,KAAK,CAAC,EAAE;AAAA,IAChC;AACO,WAAA;AAAA,EAAA,CACR;AACH;AAMO,SAAS,eAAkB,SAA6B;AAC7D,QAAM,QAAQ,YAAY,QAAQ,cAAc,OAAO;AACvD,aAAW,OAAO,OAAO;AAClB,SAAA;AACT;"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "cross-state",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "(React) state library",
5
5
  "license": "ISC",
6
- "repository": "schummar/schummar-state",
6
+ "repository": "schummar/cross-state",
7
7
  "author": {
8
8
  "name": "Marco Schumacher",
9
9
  "email": "marco@schumacher.dev"