Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 19, 2024
1 parent ff6b1fa commit ef5b609
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: ci

on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install
- name: Run tests
run: |
npm run test
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# zli
# asyncforge

`asyncforge` allows you to remove singletons from your codebase with the
use of [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage).

It provides helpers to build Next.js-style helpers to access "singletons".

## Install

```sh
npm i asyncforge
```

## Usage

```js
import { start, forge, memo } from 'asyncforge'

const a = forge((config) => {
return {
value: config.foo
}
})

const b = memo()

start({ foo: 'bar' })
b.set(123)

// simulate an event loop turn
setImmediate(() => {
console.log('-- first event loop turn --')
console.log('a', a())
console.log('b', b())

b.set(456)
setImmediate(() => {
console.log('-- third event loop turn --')
console.log('a', a())
console.log('b', b())
})
})

start({ foo: 'baz' })
b.set(321)

// simulate an event loop turn
setImmediate(() => {
console.log('-- second event loop turn --')
console.log('a', a())
console.log('b', b())
})
```

## License

MIT
36 changes: 36 additions & 0 deletions example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { start, forge, memo } from './asyncforge.js'

const a = forge((config) => {
return {
value: config.foo
}
})

const b = memo()

start({ foo: 'bar' })
b.set(123)

// simulate an event loop turn
setImmediate(() => {
console.log('-- first event loop turn --')
console.log('a', a())
console.log('b', b())

b.set(456)
setImmediate(() => {
console.log('-- third event loop turn --')
console.log('a', a())
console.log('b', b())
})
})

start({ foo: 'baz' })
b.set(321)

// simulate an event loop turn
setImmediate(() => {
console.log('-- second event loop turn --')
console.log('a', a())
console.log('b', b())
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "standard && borp --coverage"
},
"keywords": [],
"keywords": ["async", "local", "storage", "asynclocalstorage", "memo", "forge", "singleton"],
"author": "Matteo Collina <[email protected]>",
"license": "MIT",
"devDependencies": {
Expand Down

0 comments on commit ef5b609

Please sign in to comment.