Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: tauri submodule #621

Merged
merged 11 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-baboons-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/storage": minor
---

move tauri storage to submodule
2 changes: 1 addition & 1 deletion packages/share/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@solid-primitives/share",
"version": "2.0.6",
"description": "Primitives to help with sharing content on social media and beyond.",
"author": "David Di Biase <dave.dibiase@gmail.com",
"author": "David Di Biase <dave.dibiase@gmail.com>",
"contributors": [
"Omer Ma<mapinxue@qq.com>",
"Tom Pichaud <dev.tompichaud@icloud.com>"
Expand Down
6 changes: 6 additions & 0 deletions packages/storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @solid-primitives/storage

## 3.5.0

### Minor Changes

- 72db58b: type fixes for makePersisted

## 3.4.0

### Minor Changes
Expand Down
2 changes: 2 additions & 0 deletions packages/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ fn main() {
Once these preparations are finished, `tauriStorage(name?: string)` can be used as another storage option. To fallback to localStorage if the app does not run within tauri, you can check for `window.__TAURI_INTERNALS__`:

```ts
import { tauriStorage } from "@solid-primitives/storage/tauri";

const storage = window.__TAURI_INTERNALS__ ? tauriStorage() : localStorage;
```

Expand Down
2 changes: 1 addition & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solid-primitives/storage",
"version": "3.4.0",
"version": "3.5.0",
"description": "Primitive that provides reactive wrappers for storage access",
"author": "Alex Lohr <alex.lohr@logmein.com>",
"contributors": [
Expand Down
4 changes: 0 additions & 4 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import {
createSessionStorage,
} from "./storage.js";
import { type CookieOptions, cookieStorage, createCookieStorage } from "./cookies.js";
import { tauriStorage } from "./tauri.js";
import { addClearMethod, addWithOptionsMethod, multiplexStorage } from "./tools.js";
import {
type PersistenceOptions,
type PersistenceSyncAPI,
type PersistenceSyncData,
type PersistenceBaseOptions,
type PersistenceSyncCallback,
makePersisted,
multiplexSync,
Expand All @@ -45,11 +43,9 @@ export {
createSessionStorage,
type CookieOptions,
cookieStorage,
tauriStorage,
addClearMethod,
addWithOptionsMethod,
multiplexStorage,
type PersistenceBaseOptions,
type PersistenceOptions,
type PersistenceSyncCallback,
type PersistenceSyncData,
Expand Down
140 changes: 24 additions & 116 deletions packages/storage/src/persisted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Accessor, Setter, Signal } from "solid-js";
import type { Setter, Signal } from "solid-js";
import { createUniqueId, untrack } from "solid-js";
import { isServer, isDev } from "solid-js/web";
import type { SetStoreFunction, Store } from "solid-js/store";
Expand All @@ -20,121 +20,20 @@ export type PersistenceSyncAPI = [
update: (key: string, value: string | null | undefined) => void,
];

export type PersistenceBaseOptions<T> = {
export type PersistenceOptions<T, O extends Record<string, any> | undefined> = {
name?: string;
serialize?: (data: T) => string;
deserialize?: (data: string) => T;
sync?: PersistenceSyncAPI;
};

export type PersistenceOptions<T, O extends Record<string, any>> = PersistenceBaseOptions<T> &
(
| {
storage: StorageWithOptions<O> | AsyncStorageWithOptions<O>;
storageOptions: O;
}
| { storage?: Storage | AsyncStorage }
);

/**
* Persists a signal, store or similar API
* ```ts
* const [getter, setter] = makePersisted(createSignal("data"), options);
* const options = {
* storage: cookieStorage, // can be any synchronous or asynchronous storage
* storageOptions: { ... }, // for storages with options, otherwise not needed
* name: "solid-data", // optional
* serialize: (value: string) => value, // optional
* deserialize: (data: string) => data, // optional
* };
* ```
* 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.
*
* @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 {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
*/
export function makePersisted<T>(
signal: [Accessor<T>, Setter<T>],
options?: PersistenceOptions<T, {}>,
): [Accessor<T>, Setter<T>];

/**
* Persists a signal, store or similar API
* ```ts
* const [getter, setter] = makePersisted(createSignal("data"), options);
* const options = {
* storage: cookieStorage, // can be any synchronous or asynchronous storage
* storageOptions: { ... }, // for storages with options, otherwise not needed
* name: "solid-data", // optional
* serialize: (value: string) => value, // optional
* deserialize: (data: string) => data, // optional
* };
* ```
* 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.
*
* @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 {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
*/
export function makePersisted<T, O extends Record<string, any>>(
signal: Signal<T>,
options: PersistenceOptions<T, O>,
): Signal<T>;

/**
* Persists a signal, store or similar API
* ```ts
* const [getter, setter] = makePersisted(createSignal("data"), options);
* const options = {
* storage: cookieStorage, // can be any synchronous or asynchronous storage
* storageOptions: { ... }, // for storages with options, otherwise not needed
* name: "solid-data", // optional
* serialize: (value: string) => value, // optional
* deserialize: (data: string) => data, // optional
* };
* ```
* 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.
*
* @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 {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
*/
export function makePersisted<T>(
signal: [get: Store<T>, set: SetStoreFunction<T>],
options?: PersistenceOptions<T, {}>,
): [get: Store<T>, set: SetStoreFunction<T>];
} & (undefined extends O
? { storage?: Storage | AsyncStorage }
: {
storage: StorageWithOptions<O> | AsyncStorageWithOptions<O>;
storageOptions?: O;
});

/**
* Persists a signal, store or similar API
* ```ts
* const [getter, setter] = makePersisted(createSignal("data"), options);
* const options = {
* storage: cookieStorage, // can be any synchronous or asynchronous storage
* storageOptions: { ... }, // for storages with options, otherwise not needed
* name: "solid-data", // optional
* serialize: (value: string) => value, // optional
* deserialize: (data: string) => data, // optional
* };
* ```
* 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.
*
* @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 {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
*/
export function makePersisted<T, O extends Record<string, any>>(
signal: [get: Store<T>, set: SetStoreFunction<T>],
options: PersistenceOptions<T, O>,
): [get: Store<T>, set: SetStoreFunction<T>];
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;

/**
* Persists a signal, store or similar API
Expand All @@ -156,10 +55,19 @@ export function makePersisted<T, O extends Record<string, any>>(
* @param {PersistenceOptions<T, O>} options - The options for persistence.
* @returns {Signal<T> | [get: Store<T>, set: SetStoreFunction<T>]} - The persisted signal or store.
*/
export function makePersisted<T, O extends Record<string, any> = {}>(
signal: Signal<T> | [get: Store<T>, set: SetStoreFunction<T>],
options: PersistenceOptions<T, O> = {},
): Signal<T> | [get: Store<T>, set: SetStoreFunction<T>] {
export function makePersisted<S extends Signal<any> | [Store<any>, SetStoreFunction<any>]>(
signal: S,
options?: PersistenceOptions<SignalType<S>, undefined>,
): 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;
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 {
const storage = options.storage || globalThis.localStorage;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!storage) {
Expand Down Expand Up @@ -229,7 +137,7 @@ export function makePersisted<T, O extends Record<string, any> = {}>(
storage.setItem(name, value, storageOptions);
unchanged = false;
},
] as typeof signal;
] as S;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ const custom_entries: Record<string, preset.EntryOptions | preset.EntryOptions[]
"controlled-props": {
entry: "src/index.tsx",
},
/*filesystem: [
{
entry: "src/index.ts",
},
{
entry: "src/tauri.ts",
name: "tauri",
},
],*/
storage: [
{
entry: "src/index.ts",
},
{
entry: "src/tauri.ts",
name: "tauri",
},
],
utils: [
{
entry: "src/index.ts",
Expand Down
Loading