Skip to content

Commit 2582159

Browse files
David Biereggeratk
authored andcommitted
Fix documentation of makePersisted
1 parent 34f71be commit 2582159

File tree

1 file changed

+114
-23
lines changed

1 file changed

+114
-23
lines changed

packages/storage/src/persisted.ts

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createUniqueId, untrack} from "solid-js";
22
import {reconcile} from "solid-js/store";
3-
import type {Accessor, Setter} from "solid-js";
3+
import type {Accessor, Setter, Signal} from "solid-js";
44
import type {Store, SetStoreFunction} from "solid-js/store";
55
import type {StorageWithOptions, AsyncStorage, AsyncStorageWithOptions} from "./types.js";
66

@@ -19,41 +19,132 @@ export type PersistenceOptions<T, O extends Record<string, any> = {}> = Persiste
1919
storageOptions: O;
2020
});
2121

22-
/*
22+
/**
2323
* Persists a signal, store or similar API
24-
* ```ts
25-
* const [getter, setter] = makePersisted(createSignal("data"), options);
26-
* const options = {
27-
* storage: cookieStorage, // can be any synchronous or asynchronous storage
28-
* storageOptions: { ... }, // for storages with options, otherwise not needed
29-
* name: "solid-data", // optional
30-
* autoRemove: true, // optional, removes key from storage when value is set to null or undefined
31-
* serialize: (value: string) => value, // optional
32-
* deserialize: (data: string) => data, // optional
33-
* };
34-
* ```
35-
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the item from the storage.
24+
* ```ts
25+
* const [getter, setter] = makePersisted(createSignal("data"), options);
26+
* const options = {
27+
* storage: cookieStorage, // can be any synchronous or asynchronous storage
28+
* storageOptions: { ... }, // for storages with options, otherwise not needed
29+
* name: "solid-data", // optional
30+
* serialize: (value: string) => value, // optional
31+
* deserialize: (data: string) => data, // optional
32+
* };
33+
* ```
34+
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial
35+
* value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the
36+
* item from the storage.
37+
*
38+
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
39+
* @param {PersistenceOptions<T, O>} options - The options for persistence.
40+
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
3641
*/
3742
export function makePersisted<T>(
3843
signal: [Accessor<T>, Setter<T>],
3944
options?: PersistenceOptions<T>,
4045
): [Accessor<T>, Setter<T>];
46+
47+
48+
/**
49+
* Persists a signal, store or similar API
50+
* ```ts
51+
* const [getter, setter] = makePersisted(createSignal("data"), options);
52+
* const options = {
53+
* storage: cookieStorage, // can be any synchronous or asynchronous storage
54+
* storageOptions: { ... }, // for storages with options, otherwise not needed
55+
* name: "solid-data", // optional
56+
* serialize: (value: string) => value, // optional
57+
* deserialize: (data: string) => data, // optional
58+
* };
59+
* ```
60+
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial
61+
* value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the
62+
* item from the storage.
63+
*
64+
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
65+
* @param {PersistenceOptions<T, O>} options - The options for persistence.
66+
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
67+
*/
4168
export function makePersisted<T, O extends Record<string, any>>(
42-
signal: [Accessor<T>, Setter<T>],
69+
signal: Signal<T>,
4370
options: PersistenceOptions<T, O>,
44-
): [Accessor<T>, Setter<T>];
71+
): Signal<T>;
72+
73+
/**
74+
* Persists a signal, store or similar API
75+
* ```ts
76+
* const [getter, setter] = makePersisted(createSignal("data"), options);
77+
* const options = {
78+
* storage: cookieStorage, // can be any synchronous or asynchronous storage
79+
* storageOptions: { ... }, // for storages with options, otherwise not needed
80+
* name: "solid-data", // optional
81+
* serialize: (value: string) => value, // optional
82+
* deserialize: (data: string) => data, // optional
83+
* };
84+
* ```
85+
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial
86+
* value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the
87+
* item from the storage.
88+
*
89+
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
90+
* @param {PersistenceOptions<T, O>} options - The options for persistence.
91+
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
92+
*/
4593
export function makePersisted<T>(
46-
signal: [Store<T>, SetStoreFunction<T>],
94+
signal: [get: Store<T>, set: SetStoreFunction<T>],
4795
options?: PersistenceOptions<T>,
48-
): [Store<T>, SetStoreFunction<T>];
96+
): [get: Store<T>, set: SetStoreFunction<T>];
97+
98+
/**
99+
* Persists a signal, store or similar API
100+
* ```ts
101+
* const [getter, setter] = makePersisted(createSignal("data"), options);
102+
* const options = {
103+
* storage: cookieStorage, // can be any synchronous or asynchronous storage
104+
* storageOptions: { ... }, // for storages with options, otherwise not needed
105+
* name: "solid-data", // optional
106+
* serialize: (value: string) => value, // optional
107+
* deserialize: (data: string) => data, // optional
108+
* };
109+
* ```
110+
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial
111+
* value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the
112+
* item from the storage.
113+
*
114+
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
115+
* @param {PersistenceOptions<T, O>} options - The options for persistence.
116+
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
117+
*/
49118
export function makePersisted<T, O extends Record<string, any>>(
50-
signal: [Store<T>, SetStoreFunction<T>],
119+
signal: [get: Store<T>, set: SetStoreFunction<T>],
51120
options: PersistenceOptions<T, O>,
52-
): [Store<T>, SetStoreFunction<T>];
121+
): [get: Store<T>, set: SetStoreFunction<T>];
122+
123+
124+
/**
125+
* Persists a signal, store or similar API
126+
* ```ts
127+
* const [getter, setter] = makePersisted(createSignal("data"), options);
128+
* const options = {
129+
* storage: cookieStorage, // can be any synchronous or asynchronous storage
130+
* storageOptions: { ... }, // for storages with options, otherwise not needed
131+
* name: "solid-data", // optional
132+
* serialize: (value: string) => value, // optional
133+
* deserialize: (data: string) => data, // optional
134+
* };
135+
* ```
136+
* Can be used with `createSignal` or `createStore`. The initial value from the storage will overwrite the initial
137+
* value of the signal or store unless overwritten. Overwriting a signal with `null` or `undefined` will remove the
138+
* item from the storage.
139+
*
140+
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
141+
* @param {PersistenceOptions<T, O>} options - The options for persistence.
142+
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
143+
*/
53144
export function makePersisted<T, O extends Record<string, any> = {}>(
54-
signal: [Accessor<T>, Setter<T>] | [Store<T>, SetStoreFunction<T>],
145+
signal: Signal<T> | [get: Store<T>, set: SetStoreFunction<T>],
55146
options: PersistenceOptions<T, O> = {},
56-
): [Accessor<T>, Setter<T>] | [Store<T>, SetStoreFunction<T>] {
147+
): Signal<T> | [get: Store<T>, set: SetStoreFunction<T>] {
57148
const storage = options.storage || globalThis.localStorage;
58149
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
59150
if (!storage) {
@@ -81,7 +172,7 @@ export function makePersisted<T, O extends Record<string, any> = {}>(
81172
? (value?: T | ((prev: T) => T)) => {
82173
const output = (signal[1] as Setter<T>)(value as any);
83174

84-
if (value != null)
175+
if (value)
85176
storage.setItem(name, serialize(output), storageOptions);
86177
else
87178
storage.removeItem(name, storageOptions);

0 commit comments

Comments
 (0)