Skip to content

Commit

Permalink
Merge branch 'main' into chore/add-details-in-cr-action
Browse files Browse the repository at this point in the history
  • Loading branch information
sukvvon authored Dec 19, 2024
2 parents 25e35d8 + 7e463d1 commit 5fa540f
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/extensions/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ a third-party library to help such use cases.
### Install

```
npm i jotai-cache
npm install jotai-cache
```

## atomWithCache
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/effect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords: effect, atom effect, side effect, side-effect, sideeffect
## install

```
npm i jotai-effect
npm install jotai-effect
```

## atomEffect
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/immer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords: immer
You have to install `immer` and `jotai-immer` to use this feature.

```
npm i immer jotai-immer
npm install immer jotai-immer
```

## atomWithImmer
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/location.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To deal with `window.location`, we have some functions to create atoms.
You have to install `jotai-location` to use this feature.

```
npm i jotai-location
npm install jotai-location
```

## atomWithLocation
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/optics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords: optics
You have to install `optics-ts` and `jotai-optics` to use this feature.

```
npm i optics-ts jotai-optics
npm install optics-ts jotai-optics
```

## focusAtom
Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jotai-tanstack-query currently supports TanStack Query v5.
In addition to `jotai`, you have to install `jotai-tanstack-query` and `@tanstack/query-core` to use the extension.

```bash
npm i jotai-tanstack-query @tanstack/query-core
npm install jotai-tanstack-query @tanstack/query-core
```

### Incremental Adoption
Expand Down Expand Up @@ -338,7 +338,7 @@ See [a working example](https://codesandbox.io/s/4gfp6z) to learn more.
In order to use the Devtools, you need to install it additionally.

```bash
npm i @tanstack/react-query-devtools
npm install @tanstack/react-query-devtools
```

All you have to do is put the `<ReactQueryDevtools />` within `<QueryClientProvider />`.
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/redux.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and sync with atoms in Jotai.
You have to install `redux` and `jotai-redux` to use this feature.

```
npm i redux jotai-redux
npm install redux jotai-redux
```

## atomWithStore
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/relay.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can use Jotai with [Relay](https://relay.dev).
You have to install `jotai-relay` and `relay-runtime`.

```
npm i jotai-relay relay-runtime
npm install jotai-relay relay-runtime
```

### Usage
Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/scope.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and the use of those atoms is scoped within the subtree.
### Install

```
npm i jotai-scope
npm install jotai-scope
```

### Counter Example
Expand Down Expand Up @@ -105,7 +105,7 @@ See [Motivation](https://github.com/saasquatch/bunshi/tree/v1.1.1#motivation) fo
### Install

```
npm i bunshi
npm install bunshi
```

### Counter Example
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/trpc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You can use Jotai with [tRPC](https://trpc.io).
You have to install `jotai-trpc`, `@trpc/client` and `@trpc/server` to use the extension.

```
npm i jotai-trpc @trpc/client @trpc/server
npm install jotai-trpc @trpc/client @trpc/server
```

### Usage
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/urql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ From the [Overview docs](https://formidable.com/open-source/urql/docs/):
You have to install `jotai-urql`, `@urql/core` and `wonka` to use the extension.

```
npm i jotai-urql @urql/core wonka
npm install jotai-urql @urql/core wonka
```

### Exported functions
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/valtio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This only uses the vanilla api of valtio.
You have to install `valtio` and `jotai-valtio` to use this feature.

```
npm i valtio jotai-valtio
npm install valtio jotai-valtio
```

## atomWithProxy
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/xstate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ a better and safer abstraction for state management.
You have to install `xstate` and `jotai-xstate` to use this feature.

```
npm i xstate jotai-xstate
npm install xstate jotai-xstate
```

## atomWithMachine
Expand Down
2 changes: 1 addition & 1 deletion docs/extensions/zustand.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This only uses the vanilla api of zustand.
You have to install `zustand` and `jotai-zustand` to use this feature.

```
npm i zustand jotai-zustand
npm install zustand jotai-zustand
```

## atomWithStore
Expand Down
2 changes: 1 addition & 1 deletion docs/third-party/derive.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Building data graphs with these dual-natured (sometimes async, sometimes sync) a
You have to install `jotai-derive` to use this feature.

```
npm i jotai-derive
npm install jotai-derive
```

## derive
Expand Down
2 changes: 1 addition & 1 deletion docs/third-party/history.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords: history,undo,redo,jotai
## install

```
npm i jotai-history
npm install jotai-history
```

## withHistory
Expand Down
2 changes: 1 addition & 1 deletion docs/tools/devtools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords: devtools,debug,snapshot
Install `jotai-devtools` to your project to get started.

```sh
npm i jotai-devtools
npm install jotai-devtools
```

### Notes
Expand Down
19 changes: 8 additions & 11 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const registerBatchAtom = (
if (!batch.D.has(atom)) {
batch.D.set(atom, new Set())
addBatchFuncMedium(batch, () => {
atomState.m?.l.forEach((listener) => listener())
atomState.m?.l.forEach((listener) => addBatchFuncMedium(batch, listener))
})
}
}
Expand All @@ -220,12 +220,6 @@ const addBatchAtomDependent = (
const getBatchAtomDependents = (batch: Batch, atom: AnyAtom) =>
batch.D.get(atom)

const copySetAndClear = <T>(origSet: Set<T>): Set<T> => {
const newSet = new Set(origSet)
origSet.clear()
return newSet
}

const flushBatch = (batch: Batch) => {
let error: AnyError
let hasError = false
Expand All @@ -239,11 +233,14 @@ const flushBatch = (batch: Batch) => {
}
}
}
while (batch.M.size || batch.L.size) {
while (batch.H.size || batch.M.size || batch.L.size) {
batch.D.clear()
copySetAndClear(batch.H).forEach(call)
copySetAndClear(batch.M).forEach(call)
copySetAndClear(batch.L).forEach(call)
batch.H.forEach(call)
batch.H.clear()
batch.M.forEach(call)
batch.M.clear()
batch.L.forEach(call)
batch.L.clear()
}
if (hasError) {
throw error
Expand Down
22 changes: 22 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,25 @@ it('mounted atom should be recomputed eagerly', () => {
store.set(a, 1)
expect(result).toEqual(['bRead', 'aCallback', 'bCallback'])
})

it('should process all atom listeners even if some of them throw errors', () => {
const store = createStore()
const a = atom(0)
const listenerA = vi.fn()
const listenerB = vi.fn(() => {
throw new Error('error')
})
const listenerC = vi.fn()

store.sub(a, listenerA)
store.sub(a, listenerB)
store.sub(a, listenerC)
try {
store.set(a, 1)
} catch {
// expect empty
}
expect(listenerA).toHaveBeenCalledTimes(1)
expect(listenerB).toHaveBeenCalledTimes(1)
expect(listenerC).toHaveBeenCalledTimes(1)
})
16 changes: 13 additions & 3 deletions tests/vanilla/storedev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {

describe('[DEV-ONLY] dev-only methods rev4', () => {
it('should get atom value', () => {
const store = createStore() as any
const store = createStore() as INTERNAL_DevStoreRev4 & INTERNAL_PrdStore
if (!('dev4_get_internal_weak_map' in store)) {
throw new Error('dev methods are not available')
}
Expand All @@ -19,7 +19,7 @@ describe('[DEV-ONLY] dev-only methods rev4', () => {
})

it('should restore atoms and its dependencies correctly', () => {
const store = createStore() as any
const store = createStore() as INTERNAL_DevStoreRev4 & INTERNAL_PrdStore
if (!('dev4_restore_atoms' in store)) {
throw new Error('dev methods are not available')
}
Expand All @@ -32,7 +32,7 @@ describe('[DEV-ONLY] dev-only methods rev4', () => {
})

it('should restore atoms and call store listeners correctly', () => {
const store = createStore() as any
const store = createStore() as INTERNAL_DevStoreRev4 & INTERNAL_PrdStore
if (!('dev4_restore_atoms' in store)) {
throw new Error('dev methods are not available')
}
Expand Down Expand Up @@ -96,4 +96,14 @@ describe('[DEV-ONLY] dev-only methods rev4', () => {
const result = store.dev4_get_mounted_atoms()
expect(Array.from(result)).toStrictEqual([])
})

it('should restore atoms with custom write function', () => {
const store = createStore() as INTERNAL_DevStoreRev4 & INTERNAL_PrdStore
if (!('dev4_restore_atoms' in store)) {
throw new Error('dev methods are not available')
}
const countAtom = atom(0, () => {})
store.dev4_restore_atoms([[countAtom, 1]])
expect(store.get(countAtom)).toBe(1)
})
})
2 changes: 1 addition & 1 deletion website/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const gettingStartedTabs = {
<h2>Installation</h2>
<p>First add Jotai as a dependency to your React project.</p>
<Code language="bash">{`# npm
npm i jotai
npm install jotai
# yarn
yarn add jotai
Expand Down

0 comments on commit 5fa540f

Please sign in to comment.