-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reversed getCacheKey and loadData params
- Loading branch information
Showing
22 changed files
with
126 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { exampleCache } from "./cache"; | ||
import { userProfileCache } from "./cache"; | ||
|
||
// REMOVE_BEFORE | ||
async function load(userId: string) { | ||
const userData = await exampleCache.fetchAsync(userId); | ||
const userProfile = await userProfileCache.fetchAsync(userId); | ||
|
||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
import { createCache } from "suspense"; | ||
|
||
export const exampleCache = createCache<[userId: string], JSON>( | ||
// Create unique key for params | ||
(userId: string) => userId, | ||
|
||
// Load data for params | ||
export const userProfileCache = createCache<[userId: string], JSON>( | ||
async (userId: string) => { | ||
const response = await fetch(`https://example.com/user?id=${userId}`); | ||
return await response.json(); | ||
const json = await response.json(); | ||
return json; | ||
} | ||
); |
16 changes: 16 additions & 0 deletions
16
packages/suspense-website/src/examples/createCache/cacheWithKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createCache } from "suspense"; | ||
|
||
class ApiClient { | ||
async loadData(id: string) { | ||
return JSON.parse(""); | ||
} | ||
} | ||
|
||
// REMOVE_BEFORE | ||
createCache<[client: ApiClient, id: string], JSON>( | ||
// In this example, data is loaded by a "client" object | ||
async (client: ApiClient, id: string) => client.loadData(id), | ||
|
||
// The id parameter is sufficiently unique to be the key | ||
(client: ApiClient, id: string) => id | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { exampleCache } from "./cache"; | ||
import { userProfileCache } from "./cache"; | ||
|
||
// REMOVE_BEFORE | ||
const userId = "123"; | ||
|
||
exampleCache.evict(userId); | ||
userProfileCache.evict(userId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
packages/suspense-website/src/examples/createCache/suspense.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { exampleCache } from "./cache"; | ||
import { userProfileCache } from "./cache"; | ||
|
||
// REMOVE_BEFORE | ||
function UserProfile({ userId }: { userId: string }) { | ||
const userData = exampleCache.fetchSuspense(userId); | ||
const userProfile = userProfileCache.fetchSuspense(userId); | ||
|
||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { exampleCache } from "./cache"; | ||
import { userProfileCache } from "./cache"; | ||
|
||
const userId = "fake"; | ||
// REMOVE_BEFORE | ||
// Returns a cached value if one has already been saved in the cache | ||
const userDataOrUndefined = exampleCache.getValueIfCached(userId); | ||
const userProfileOrUndefined = userProfileCache.getValueIfCached(userId); | ||
|
||
// Returns a cached value or throws if none has been loaded | ||
const userData = exampleCache.getValue(userId); | ||
const userProfile = userProfileCache.getValue(userId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/suspense-website/src/examples/createStreamingCache/cacheWithKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createStreamingCache, StreamingProgressNotifier } from "suspense"; | ||
|
||
class ApiClient { | ||
async streamData( | ||
id: string, | ||
onData: (data: string[]) => void, | ||
onComplete: () => void | ||
) { | ||
return JSON.parse(""); | ||
} | ||
} | ||
|
||
// REMOVE_BEFORE | ||
export const exampleStreamingCache = createStreamingCache< | ||
[client: ApiClient, id: string], | ||
string | ||
>( | ||
// In this example, data is streamed by a "client" object | ||
async ( | ||
notifier: StreamingProgressNotifier<string>, | ||
client: ApiClient, | ||
id: string | ||
) => | ||
client.streamData( | ||
id, | ||
(values: string[]) => notifier.update(values), | ||
() => notifier.resolve() | ||
), | ||
|
||
// The id parameter is sufficiently unique to be the key | ||
(client: ApiClient, id: string) => id | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c5e2342
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
suspense – ./
suspense-git-main-bvaughn.vercel.app
suspense-bvaughn.vercel.app
suspense-npm.vercel.app