Skip to content

Commit

Permalink
feat: snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Mar 30, 2024
1 parent 35bd927 commit 8333fed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "turbo-query",
"version": "2.0.0",
"version": "2.1.0",
"license": "MIT",
"description": "Lightweight, isomorphic and framework agnostic asynchronous data management for modern UIs",
"author": {
Expand Down
24 changes: 19 additions & 5 deletions src/turbo-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ export interface TurboCache<T = unknown> {
/**
* Gets an item from the cache.
*/
get(key: string): T
readonly get: (key: string) => T

/**
* Sets an item to the cache.
*/
set(key: string, value: T): void
readonly set: (key: string, value: T) => void

/**
* Removes a key-value pair from the cache.
*/
delete(key: string): void
readonly delete: (key: string) => void

/**
* Returns the current cached keys.
*/
keys(): IterableIterator<string>
readonly keys: () => IterableIterator<string>
}

/**
Expand Down Expand Up @@ -200,6 +200,12 @@ export interface TurboQuery {
* If the item is not in the cache, it will return `undefined`.
*/
readonly expiration: (key: string) => Date | undefined

/**
* Returns the current snapshot of the given key.
* If the item is not in the items cache, it will return `undefined`.
*/
readonly snapshot: <T = unknown>(key: string) => T | undefined
}

/**
Expand Down Expand Up @@ -348,6 +354,14 @@ export function createTurboQuery(instanceOptions?: TurboQueryConfiguration): Tur
events.dispatchEvent(new CustomEvent(`mutated:${key}`, { detail: item }))
}

/**
* Returns the current snapshot of the given key.
* If the item is not in the items cache, it will return `undefined`.
*/
function snapshot<T = unknown>(key: string): T | undefined {
return itemsCache.get(key)?.item as T
}

/**
* Determines if the given key is currently resolving.
*/
Expand Down Expand Up @@ -549,5 +563,5 @@ export function createTurboQuery(instanceOptions?: TurboQueryConfiguration): Tur
return await refetch(key)
}

return { query, subscribe, mutate, configure, abort, forget, keys, expiration, hydrate }
return { query, subscribe, mutate, configure, abort, forget, keys, expiration, hydrate, snapshot }
}

0 comments on commit 8333fed

Please sign in to comment.