Skip to content

Commit

Permalink
storage: address PR comments
Browse files Browse the repository at this point in the history
storage: type/lint fixes
  • Loading branch information
atk committed Aug 31, 2024
1 parent b3f3848 commit 620df2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions packages/storage/src/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function serializeCookieOptions(options?: CookieOptions) {
if (!options) return "";
const result = Object.entries(options)
.map(([key, value]) => {
const serializedKey = cookiePropertyMap[key as keyof CookiePropertyTypes];
const serializedKey: string | undefined = cookiePropertyMap[key as keyof CookiePropertyTypes];
if (!serializedKey) return undefined;

if (value instanceof Date) return `${serializedKey}=${value.toUTCString()}`;
Expand All @@ -52,11 +52,11 @@ function deserializeCookieOptions(cookie: string, key: string) {
}

const getRequestHeaders = isServer
? () => getRequestEvent()?.request?.headers || new Headers()
? () => getRequestEvent()?.request.headers || new Headers()
: () => new Headers();
const getResponseHeaders = isServer
? () =>
(getRequestEvent() as RequestEvent & { response: Response })?.response?.headers ||
(getRequestEvent() as (RequestEvent & { response: Response }) | undefined)?.response.headers ||
new Headers()
: () => new Headers();

Expand Down Expand Up @@ -103,9 +103,9 @@ export const cookieStorage: SyncStorageWithOptions<CookieOptions> = addWithOptio
const responseHeaders = getResponseHeaders();
const currentCookies =
responseHeaders
?.get("Set-Cookie")
.get("Set-Cookie")
?.split(", ")
?.filter(cookie => cookie && !cookie.startsWith(`${key}=`)) ?? [];
.filter(cookie => cookie && !cookie.startsWith(`${key}=`)) ?? [];
responseHeaders.set(
"Set-Cookie",
[...currentCookies, `${key}=${value}${serializeCookieOptions(options)}`].join(", "),
Expand Down
20 changes: 10 additions & 10 deletions packages/storage/src/persisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export type PersistenceOptions<T, O extends Record<string, any> | undefined> = {
export type SignalType<S extends Signal<any> | [Store<any>, SetStoreFunction<any>]> =
S extends Signal<infer T> ? T : S extends [Store<infer T>, SetStoreFunction<infer T>] ? T : never;

export type EnhancedSignalOrStore<T> =
export type PersistedState<T> =
| [get: Accessor<T>, set: Setter<T>, init: Promise<string> | string | null]
| [get: Store<T>, set: SetStoreFunction<T>, init: Promise<string> | string | null];

Expand All @@ -95,27 +95,27 @@ export type EnhancedSignalOrStore<T> =
*
* @param {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} signal - The signal or store to be persisted.
* @param {PersistenceOptions<T, O>} options - The options for persistence.
* @returns {EnhancedSignalOrStore<T>} - The persisted signal or store.
* @returns {PersistedState<T>} - The persisted signal or store.
*/
export function makePersisted<S extends Signal<any> | [Store<any>, SetStoreFunction<any>]>(
signal: S,
options?: PersistenceOptions<SignalType<S>, undefined>,
): [S[0], S[1], init: Promise<string> | string | null];
): PersistedState<SignalType<S>>;
export function makePersisted<
S extends Signal<any> | [Store<any>, SetStoreFunction<any>],
O extends Record<string, any>,
>(
signal: S,
options: PersistenceOptions<SignalType<S>, O>,
): [S[0], S[1], init: Promise<string> | string | null];
): PersistedState<SignalType<S>>;
export function makePersisted<
S extends Signal<any> | [Store<any>, SetStoreFunction<any>],
O extends Record<string, any> | undefined,
T = SignalType<S>,
>(
signal: S,
options: PersistenceOptions<T, O> = {} as PersistenceOptions<T, O>,
): [S[0], S[1], init: Promise<string> | string | null] {
): PersistedState<SignalType<S>> {
const storage = options.storage || (globalThis.localStorage as Storage | undefined);
const name = options.name || `storage-${createUniqueId()}`;
if (!storage) {
Expand Down Expand Up @@ -145,7 +145,7 @@ export function makePersisted<
};
let unchanged = true;

if (init instanceof Promise) init.then(data => unchanged && data && set(data as string));
if (init instanceof Promise) init.then(data => unchanged && data && set(data));
else if (init) set(init);

if (typeof options.sync?.[0] === "function") {
Expand All @@ -169,22 +169,22 @@ export function makePersisted<
? (value?: T | ((prev: T) => T)) => {
const output = (signal[1] as Setter<T>)(value as any);
const serialized: string | null | undefined =
value != null ? (serialize(output) as string) : (value as null | undefined);
value != null ? (serialize(output)) : (value as null | undefined);
options.sync?.[1](name, serialized);
if (value != null) storage.setItem(name, serialized as string, storageOptions);
if (serialized != null) storage.setItem(name, serialized, storageOptions);
else storage.removeItem(name, storageOptions);
unchanged = false;
return output;
}
: (...args: any[]) => {
(signal[1] as any)(...args);
const value = serialize(untrack(() => signal[0] as any));
const value = serialize(untrack(() => signal[0]));
options.sync?.[1](name, value);
storage.setItem(name, value, storageOptions);
unchanged = false;
},
init,
] as [S[0], S[1], init: Promise<string> | string | null];
] as PersistedState<SignalType<S>>;
}

/**
Expand Down

0 comments on commit 620df2f

Please sign in to comment.