cross-state 0.31.2 → 0.31.3

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
- {"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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AAU,CAAC;AAElD,yCAAW;AAAA,EAAK,CACjB;AAED,SAAO,MAAM;AACO;AACC;EAAA;AAEvB;AAEA,SAAS,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,QACK;AACC,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 updateUrlStore() {\n urlStore.set(window.location.href);\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;AAUM,SAAS,WACdC,QACA;AAAA,EACE;AAAA,EACA,OAAO;AAAA,EACP,YA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AAA,MACxB;AACO,aAAA;AAAA,IAAA,CACR;AAAA,EAAA,QACK;AACC,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 +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 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,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;AAClC,UAAA,a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;AACT;"}
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 updateUrlStore() {\n urlStore.set(window.location.href);\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,IA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BAAoB,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,QACK;AACC,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;"}
@@ -12,6 +12,7 @@ export interface UrlStoreOptionsWithDefaults<T> extends UrlStoreOptions<T> {
12
12
  defaultValue: T;
13
13
  }
14
14
  export type UrlStoreOptionsRequired<T> = UrlStoreOptions<T> & Required<Pick<UrlStoreOptions<T>, 'type' | 'serialize' | 'deserialize' | 'defaultValue'>>;
15
+ export declare function updateUrlStore(): void;
15
16
  export declare function connectUrl<T>(store: Store<T>, options: UrlStoreOptionsWithDefaults<T>): Cancel;
16
17
  export declare function connectUrl<T>(store: Store<T | undefined>, options: UrlStoreOptions<T>): Cancel;
17
18
  export declare function createUrlStore<T>(options: UrlStoreOptionsWithDefaults<T>): Store<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cross-state",
3
- "version": "0.31.2",
3
+ "version": "0.31.3",
4
4
  "description": "(React) state library",
5
5
  "license": "ISC",
6
6
  "repository": "schummar/cross-state",