@solid-primitives/storage
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -351,2 +351,4 @@ import { Accessor, Setter, Signal } from 'solid-js'; | ||
* storageSync - synchronize localStorage | ||
* This does only work for { storage: localStorage }. | ||
* If you wish to use e.g. cookieStorage, you may use a different sync method | ||
*/ | ||
@@ -361,5 +363,12 @@ declare const storageSync: PersistenceSyncAPI; | ||
*/ | ||
declare const wsSync: (ws: WebSocket) => PersistenceSyncAPI; | ||
declare const wsSync: (ws: WebSocket, warnOnError?: boolean) => PersistenceSyncAPI; | ||
/** | ||
* multiplex arbitrary sync APIs | ||
* | ||
* ```ts | ||
* makePersisted(createSignal(0), { sync: multiplexSync(messageSync(bc), wsSync(ws)) }) | ||
* ``` | ||
*/ | ||
declare const multiplexSync: (...syncAPIs: PersistenceSyncAPI[]) => PersistenceSyncAPI; | ||
export { AnyStorageProps, AsyncStorage, AsyncStorageObject, AsyncStorageSetter, AsyncStorageWithOptions, CookieOptions, PersistenceBaseOptions, PersistenceOptions, PersistenceSyncAPI, PersistenceSyncCallback, PersistenceSyncData, StorageDeserializer, StorageObject, StorageProps, StorageSerializer, StorageSetter, StorageSignalProps, StorageWithOptions, StringStorageProps, addClearMethod, addWithOptionsMethod, cookieStorage, createAsyncStorage, createCookieStorage, createLocalStorage, createSessionStorage, createStorage, createStorageSignal, makePersisted, messageSync, multiplexStorage, multiplexSync, storageSync, wsSync }; |
import { createHydratableSignal } from '@solid-primitives/utils'; | ||
import { createSignal, onMount, onCleanup, createEffect, createUniqueId, untrack } from 'solid-js'; | ||
import { isServer, getRequestEvent } from 'solid-js/web'; | ||
import { isServer, getRequestEvent, isDev } from 'solid-js/web'; | ||
import { reconcile } from 'solid-js/store'; | ||
@@ -402,3 +402,6 @@ | ||
if (typeof storage[key] === "function") { | ||
wrapped[key] = (...args) => storage[key]([...args, options]); | ||
wrapped[key] = (...args) => { | ||
args[storage[key].length - 1] = options; | ||
storage[key](...args); | ||
}; | ||
} | ||
@@ -539,3 +542,19 @@ return wrapped; | ||
const init = storage.getItem(name, storageOptions); | ||
const set = typeof signal[0] === "function" ? (data) => signal[1](() => deserialize(data)) : (data) => signal[1](reconcile(deserialize(data))); | ||
const set = typeof signal[0] === "function" ? (data) => { | ||
try { | ||
const value = deserialize(data); | ||
signal[1](() => value); | ||
} catch (e) { | ||
if (isDev) | ||
console.warn(e); | ||
} | ||
} : (data) => { | ||
try { | ||
const value = deserialize(data); | ||
signal[1](reconcile(value)); | ||
} catch (e) { | ||
if (isDev) | ||
console.warn(e); | ||
} | ||
}; | ||
let unchanged = true; | ||
@@ -582,13 +601,20 @@ if (init instanceof Promise) | ||
var messageSync = (channel = window) => [ | ||
(subscriber) => channel.addEventListener("message", (ev) => subscriber(JSON.parse(ev.data))), | ||
(key, newValue) => postMessage( | ||
JSON.stringify({ key, newValue, timeStamp: +/* @__PURE__ */ new Date(), url: location.href }), | ||
(subscriber) => channel.addEventListener("message", (ev) => { | ||
subscriber(ev.data); | ||
}), | ||
(key, newValue) => channel.postMessage( | ||
{ key, newValue, timeStamp: +/* @__PURE__ */ new Date(), url: location.href }, | ||
location.origin | ||
) | ||
]; | ||
var wsSync = (ws) => [ | ||
var wsSync = (ws, warnOnError = isDev) => [ | ||
(subscriber) => ws.addEventListener("message", (ev) => { | ||
try { | ||
subscriber(JSON.parse(ev.data)); | ||
const data = JSON.parse(ev.data); | ||
if (["key", "newValue", "timeStamp"].every((item) => Object.hasOwn(data, item))) { | ||
subscriber(data); | ||
} | ||
} catch (e) { | ||
if (warnOnError) | ||
console.warn(e); | ||
} | ||
@@ -595,0 +621,0 @@ }), |
{ | ||
"name": "@solid-primitives/storage", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Primitive that provides reactive wrappers for storage access", | ||
@@ -5,0 +5,0 @@ "author": "Alex Lohr <alex.lohr@logmein.com>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
92522
7
1630