cross-state 0.49.4 → 0.50.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/dist/cjs/urlStore.cjs
CHANGED
|
@@ -33,18 +33,19 @@ function connectUrl(store$1, {
|
|
|
33
33
|
serialize = defaultSerializer,
|
|
34
34
|
deserialize = defaultDeserializer,
|
|
35
35
|
defaultValue = undefined,
|
|
36
|
+
writeDefaultValue,
|
|
36
37
|
onCommit,
|
|
37
38
|
debounce: debounceTime = 500
|
|
38
39
|
}) {
|
|
39
40
|
const serializedDefaultValue = defaultValue !== undefined ? serialize(defaultValue) : undefined;
|
|
40
|
-
let isDirty = false;
|
|
41
|
+
let isDirty = writeDefaultValue ?? false;
|
|
41
42
|
const commit = store.debounce(() => {
|
|
42
43
|
if (isDirty) {
|
|
43
44
|
const value = store$1.get();
|
|
44
45
|
const url = new URL(window.location.href);
|
|
45
46
|
const parameters = new URLSearchParams(url[type].slice(1));
|
|
46
47
|
const serializedValue = value !== undefined ? serialize(value) : undefined;
|
|
47
|
-
if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
|
|
48
|
+
if (serializedValue === undefined || !writeDefaultValue && serializedValue === serializedDefaultValue) {
|
|
48
49
|
parameters.delete(key);
|
|
49
50
|
} else {
|
|
50
51
|
parameters.set(key, serializedValue);
|
|
@@ -70,7 +71,7 @@ function connectUrl(store$1, {
|
|
|
70
71
|
isDirty = true;
|
|
71
72
|
commit();
|
|
72
73
|
},
|
|
73
|
-
{ runNow: false }
|
|
74
|
+
{ runNow: writeDefaultValue ?? false }
|
|
74
75
|
);
|
|
75
76
|
return store.disposable(() => {
|
|
76
77
|
cancelUrlListener();
|
|
@@ -1 +1 @@
|
|
|
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 = defaultValue !== undefined ? serialize(defaultValue) : undefined;\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
|
|
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 writeDefaultValue?: boolean;\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 writeDefaultValue,\n onCommit,\n debounce: debounceTime = 500,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = defaultValue !== undefined ? serialize(defaultValue) : undefined;\n let isDirty = writeDefaultValue ?? 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 (\n serializedValue === undefined ||\n (!writeDefaultValue && serializedValue === serializedDefaultValue)\n ) {\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: writeDefaultValue ?? 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":";;;;AAuBA,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;AACrC,EAAS,QAAA,CAAA,GAAA,CAAI,MAAO,CAAA,QAAA,CAAS,IAAI,CAAA;AACnC;AAYO,SAA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AE,KAAK,CAAA;AAAA;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;;;;;;"}
|
package/dist/es/urlStore.mjs
CHANGED
|
@@ -31,18 +31,19 @@ function connectUrl(store, {
|
|
|
31
31
|
serialize = defaultSerializer,
|
|
32
32
|
deserialize = defaultDeserializer,
|
|
33
33
|
defaultValue = undefined,
|
|
34
|
+
writeDefaultValue,
|
|
34
35
|
onCommit,
|
|
35
36
|
debounce: debounceTime = 500
|
|
36
37
|
}) {
|
|
37
38
|
const serializedDefaultValue = defaultValue !== undefined ? serialize(defaultValue) : undefined;
|
|
38
|
-
let isDirty = false;
|
|
39
|
+
let isDirty = writeDefaultValue ?? false;
|
|
39
40
|
const commit = debounce(() => {
|
|
40
41
|
if (isDirty) {
|
|
41
42
|
const value = store.get();
|
|
42
43
|
const url = new URL(window.location.href);
|
|
43
44
|
const parameters = new URLSearchParams(url[type].slice(1));
|
|
44
45
|
const serializedValue = value !== undefined ? serialize(value) : undefined;
|
|
45
|
-
if (serializedValue === undefined || serializedValue === serializedDefaultValue) {
|
|
46
|
+
if (serializedValue === undefined || !writeDefaultValue && serializedValue === serializedDefaultValue) {
|
|
46
47
|
parameters.delete(key);
|
|
47
48
|
} else {
|
|
48
49
|
parameters.set(key, serializedValue);
|
|
@@ -68,7 +69,7 @@ function connectUrl(store, {
|
|
|
68
69
|
isDirty = true;
|
|
69
70
|
commit();
|
|
70
71
|
},
|
|
71
|
-
{ runNow: false }
|
|
72
|
+
{ runNow: writeDefaultValue ?? false }
|
|
72
73
|
);
|
|
73
74
|
return disposable(() => {
|
|
74
75
|
cancelUrlListener();
|
package/dist/es/urlStore.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 = defaultValue !== undefined ? serialize(defaultValue) : undefined;\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
|
|
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 writeDefaultValue?: boolean;\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 writeDefaultValue,\n onCommit,\n debounce: debounceTime = 500,\n }: UrlStoreOptions<T>,\n): DisposableCancel {\n const serializedDefaultValue = defaultValue !== undefined ? serialize(defaultValue) : undefined;\n let isDirty = writeDefaultValue ?? 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 (\n serializedValue === undefined ||\n (!writeDefaultValue && serializedValue === serializedDefaultValue)\n ) {\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: writeDefaultValue ?? 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":";;AAuBA,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,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,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;;;;"}
|
|
@@ -6,6 +6,7 @@ export interface UrlStoreOptions<T> extends StoreOptions<T | undefined> {
|
|
|
6
6
|
serialize?: (value: T) => string;
|
|
7
7
|
deserialize?: (value: string) => T;
|
|
8
8
|
defaultValue?: T;
|
|
9
|
+
writeDefaultValue?: boolean;
|
|
9
10
|
onCommit?: (value: T | undefined) => void;
|
|
10
11
|
debounce?: Duration;
|
|
11
12
|
}
|