Skip to content

Commit

Permalink
fix(docs): Avoid ghost value creation when using `app/utils/remember.…
Browse files Browse the repository at this point in the history
…ts` (#6756)
  • Loading branch information
edzis authored Jul 3, 2023
1 parent fbc6e23 commit 6dd3a20
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
- EddyVinck
- edgesoft
- edmundhung
- edzis
- efkann
- eldarshamukhamedov
- eltociear
Expand Down
6 changes: 3 additions & 3 deletions docs/other-api/dev-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ Luckily, there's a trick to get around this: use `global` as a cache for keeping
Here's a nifty utility adapted from [Jon Jensen's code][jenseng-code] for [his Remix Conf 2023 talk][jenseng-talk]:

```ts filename=app/utils/remember.ts
export function remember<T>(key: string, value: T) {
export function remember<T>(key: string, valueFactory: () => T) {
const g = global as any;
g.__singletons ??= {};
g.__singletons[key] ??= value;
g.__singletons[key] ??= valueFactory();
return g.__singletons[key];
}
```
Expand All @@ -234,7 +234,7 @@ import { PrismaClient } from "@prisma/client";
import { remember } from "~/utils/remember";

// hard-code a unique key so we can look up the client when this module gets re-imported
export const db = remember("db", new PrismaClient());
export const db = remember("db", () => new PrismaClient());
```

### How to set up MSW
Expand Down

0 comments on commit 6dd3a20

Please sign in to comment.