diff --git a/app/utils/cache.server.ts b/app/utils/cache.server.ts index 0a374911..0d48dd94 100644 --- a/app/utils/cache.server.ts +++ b/app/utils/cache.server.ts @@ -13,12 +13,12 @@ import { LRUCache } from 'lru-cache' import { z } from 'zod' import { updatePrimaryCacheValue } from '#app/routes/admin+/cache_.sqlite.tsx' import { getInstanceInfo, getInstanceInfoSync } from './litefs.server.ts' -import { singleton } from './singleton.server.ts' +import { remember } from '@epic-web/remember' import { cachifiedTimingReporter, type Timings } from './timing.server.ts' const CACHE_DATABASE_PATH = process.env.CACHE_DATABASE_PATH -const cacheDb = singleton('cacheDb', createDatabase) +const cacheDb = remember('cacheDb', createDatabase) function createDatabase(tryAgain = true): Database.Database { const db = new Database(CACHE_DATABASE_PATH) @@ -47,7 +47,7 @@ function createDatabase(tryAgain = true): Database.Database { return db } -const lru = singleton( +const lru = remember( 'lru-cache', () => new LRUCache>({ max: 5000 }), ) diff --git a/app/utils/client-hints.tsx b/app/utils/client-hints.tsx index 09e2de25..f3e1628e 100644 --- a/app/utils/client-hints.tsx +++ b/app/utils/client-hints.tsx @@ -6,7 +6,7 @@ import { useRevalidator } from '@remix-run/react' import * as React from 'react' import { useRequestInfo } from './request-info.ts' -export const clientHints = { +const clientHints = { theme: { cookieName: 'CH-prefers-color-scheme', getValueCode: `window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'`, diff --git a/app/utils/db.server.ts b/app/utils/db.server.ts index a0aead94..e614e93e 100644 --- a/app/utils/db.server.ts +++ b/app/utils/db.server.ts @@ -1,8 +1,8 @@ import { PrismaClient } from '@prisma/client' import chalk from 'chalk' -import { singleton } from './singleton.server.ts' +import { remember } from '@epic-web/remember' -const prisma = singleton('prisma', () => { +export const prisma = remember('prisma', () => { // NOTE: if you change anything in this function you'll need to restart // the dev server to see your changes. @@ -34,5 +34,3 @@ const prisma = singleton('prisma', () => { client.$connect() return client }) - -export { prisma } diff --git a/app/utils/singleton.server.ts b/app/utils/singleton.server.ts deleted file mode 100644 index 5b519131..00000000 --- a/app/utils/singleton.server.ts +++ /dev/null @@ -1,10 +0,0 @@ -// since the dev server re-requires the bundle, do some shenanigans to make -// certain things persist across that 😆 -// Borrowed/modified from https://github.com/jenseng/abuse-the-platform/blob/2993a7e846c95ace693ce61626fa072174c8d9c7/app/utils/singleton.ts - -export function singleton(name: string, value: () => Value): Value { - const yolo = global as any - yolo.__singletons ??= {} - yolo.__singletons[name] ??= value() - return yolo.__singletons[name] -} diff --git a/package-lock.json b/package-lock.json index 1b08f739..835da7ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "dependencies": { "@conform-to/react": "^0.8.0", "@conform-to/zod": "^0.8.0", + "@epic-web/remember": "^1.0.0", "@epic-web/totp": "^1.0.6", "@paralleldrive/cuid2": "^2.2.2", "@prisma/client": "^5.1.1", @@ -2208,6 +2209,11 @@ "integrity": "sha512-gJB6HLm5rYwSLI6PQa+X1t5CFGrv1J1TWG+sOyMCeKz2ojaj6Fnl/rZEspogG+cvqbt4AE/2eIyD2QfLKTBNlQ==", "dev": true }, + "node_modules/@epic-web/remember": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@epic-web/remember/-/remember-1.0.0.tgz", + "integrity": "sha512-GXri76ZFt6khvBYP60jbO1+cEJFa4udP1j1G5SyZNVzcuRzhWBu3z1z9rCeFCBYSnDHzXOd58XO94jnRIrrIXw==" + }, "node_modules/@epic-web/totp": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@epic-web/totp/-/totp-1.0.6.tgz", diff --git a/package.json b/package.json index 4c582030..09811dee 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "@conform-to/react": "^0.8.0", "@conform-to/zod": "^0.8.0", + "@epic-web/remember": "^1.0.0", "@epic-web/totp": "^1.0.6", "@paralleldrive/cuid2": "^2.2.2", "@prisma/client": "^5.1.1",