Skip to content

Commit

Permalink
Use undefined for empty value in get
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Oct 4, 2024
1 parent 1ef9f6a commit 831105d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion examples/api/src/views/Store.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
await store.delete(key);
}
const v = await store.get(key);
cache[key] = v;
if (v === undefined) {
delete cache[key];
cache = cache;
} else {
cache[key] = v;
}
} catch (error) {
onMessage(error);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/store/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions plugins/store/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class LazyStore implements IStore {
return (await this.store).set(key, value)
}

async get<T>(key: string): Promise<T | null> {
async get<T>(key: string): Promise<T | undefined> {
return (await this.store).get<T>(key)
}

Expand Down Expand Up @@ -203,11 +203,12 @@ export class Store extends Resource implements IStore {
})
}

async get<T>(key: string): Promise<T | null> {
return await invoke('plugin:store|get', {
async get<T>(key: string): Promise<T | undefined> {
const [value, exists] = await invoke<[T, boolean]>('plugin:store|get', {
rid: this.rid,
key
})
return exists ? value : undefined
}

async has(key: string): Promise<boolean> {
Expand Down Expand Up @@ -294,12 +295,12 @@ interface IStore {
set(key: string, value: unknown): Promise<void>

/**
* Returns the value for the given `key` or `null` if the key does not exist.
* Returns the value for the given `key` or `undefined` if the key does not exist.
*
* @param key
* @returns
*/
get<T>(key: string): Promise<T | null>
get<T>(key: string): Promise<T | undefined>

/**
* Returns `true` if the given `key` exists in the store.
Expand Down
6 changes: 4 additions & 2 deletions plugins/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ async fn get<R: Runtime>(
app: AppHandle<R>,
rid: ResourceId,
key: String,
) -> Result<Option<JsonValue>> {
) -> Result<(Option<JsonValue>, bool)> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.get(key))
let value = store.get(key);
let exists = value.is_some();
Ok((value, exists))
}

#[tauri::command]
Expand Down

0 comments on commit 831105d

Please sign in to comment.