An open API service indexing awesome lists of open source software.

https://github.com/epicweb-dev/remember

Simple, type-safe, "singleton" implementation.
https://github.com/epicweb-dev/remember

Last synced: about 2 months ago
JSON representation

Simple, type-safe, "singleton" implementation.

Awesome Lists containing this project

README

          


🤔 @epic-web/remember



Simple, type-safe, "singleton" implementation.


For when your "hot module replacement" involves re-evaluating a module, but
you don't want to actually re-evaluate a portion of it.


```
npm install @epic-web/remember
```






[![Build Status][build-badge]][build]
[![GPL 3.0 License][license-badge]][license]
[![Code of Conduct][coc-badge]][coc]

## The problem

You're using a framework like Remix with
[`--manual` mode](https://remix.run/docs/en/guides/manual-mode) and
re-evaluating your modules on every change. But you have some state that you
don't want to lose between changes. For example:

- Database connections
- In-memory caches

## This solution

This was copy/paste/modified/tested from
[@jenseng's `abuse-the-platform` demo](https://github.com/jenseng/abuse-the-platform/blob/2993a7e846c95ace693ce61626fa072174c8d9c7/app/utils/singleton.ts)
(ISC). It's basically a type-safe singleton implementation that you can use to
keep state between module re-evaluations.

## Usage

```tsx
import { remember } from '@epic-web/remember'

export const prisma = remember('prisma', () => new PrismaClient())
```

Keep in mind that any changes you make within that callback will not be
reflected when the module is re-evaluated (that's the whole point). So if you
need to change the callback, then you'll need to restart your server.

#### Forget a value

It might be required to explicitly forget a value if it gets outdated, a memorized
connection gets lost or memorized instance closes/errors/etc.

```tsx
import { remember, forget } from '@epic-web/remember'

export const server = remember('server', () =>
http.createServer().listen('8080')
.on('close', () => forget('server')))
```

## License

MIT

## Credit

The original code was written by [@jenseng](https://github.com/jenseng) and then
I modified it and published it to fit my needs.

[build-badge]: https://img.shields.io/github/actions/workflow/status/epicweb-dev/remember/release.yml?branch=main&logo=github&style=flat-square
[build]: https://github.com/epicweb-dev/remember/actions?query=workflow%3Arelease
[license-badge]: https://img.shields.io/badge/license-GPL%203.0%20License-blue.svg?style=flat-square
[license]: https://github.com/epicweb-dev/remember/blob/main/LICENSE
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://kentcdodds.com/conduct