From c472fe907bdd59afc9751476609e2a9bb0681acd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 10 Aug 2024 13:20:25 +0000 Subject: [PATCH] chore: run prettier --- docs/README.md | 1 - .../docs/blog/what-is-state-manager.md | 2 +- docs/src/content/docs/compat/core-v1.md | 16 +- docs/src/content/docs/compat/core-v2.md | 52 +-- docs/src/content/docs/compat/react-v1.md | 2 - docs/src/content/docs/compat/react-v2.md | 10 +- .../content/docs/getting-started/learning.md | 4 +- .../src/content/docs/getting-started/setup.md | 12 +- .../content/docs/getting-started/testing.md | 14 +- docs/src/content/docs/handbook.md | 17 +- docs/src/content/docs/index.md | 17 +- docs/src/content/docs/package/async.md | 258 +++++--------- docs/src/content/docs/package/core.md | 55 +-- docs/src/content/docs/package/effects.md | 4 +- .../src/content/docs/package/eslint-plugin.md | 8 +- docs/src/content/docs/package/form.md | 3 +- docs/src/content/docs/package/hooks.md | 13 +- docs/src/content/docs/package/jsx.md | 12 +- docs/src/content/docs/package/lens.md | 43 +-- .../content/docs/package/npm-cookie-baker.md | 7 +- docs/src/content/docs/package/npm-svelte.md | 10 +- .../docs/package/persist-web-storage.md | 38 +- docs/src/content/docs/package/primitives.md | 18 +- docs/src/content/docs/package/undo.md | 6 +- docs/src/content/docs/package/url.md | 13 +- docs/src/content/docs/package/web.md | 47 ++- .../content/docs/recipes/custom-operator.md | 3 +- docs/sync-readme-to-pages.ts | 30 +- examples/model/README.md | 2 +- examples/model/search.ts | 7 +- examples/nextjs/src/features/api.ts | 19 +- examples/nextjs/src/pages/api/hello.ts | 5 +- .../src/model.ts | 20 +- examples/react-notifications/src/model.ts | 10 +- examples/react-tree/src/model.ts | 25 +- examples/reatom-jsx/src/model.ts | 10 +- examples/reatom-jsx/vite.config.js | 4 +- packages/all-settled/CHANGELOG.md | 67 ++-- packages/all-settled/src/index.ts | 4 +- packages/async/CHANGELOG.md | 195 ++++------ packages/async/README.md | 246 +++++-------- packages/async/src/handleEffect.ts | 4 +- packages/async/src/index.story.test.ts | 12 +- packages/async/src/index.test.ts | 35 +- packages/async/src/index.ts | 159 +++------ packages/async/src/reatomResource.test.ts | 2 +- packages/async/src/reatomResource.ts | 34 +- packages/async/src/withCache.test.ts | 31 +- packages/async/src/withCache.ts | 154 +++----- packages/async/src/withStatusesAtom.test.ts | 10 +- packages/async/src/withStatusesAtom.ts | 23 +- packages/cjs-import-check/src/hello.ts | 4 +- packages/core-v1/CHANGELOG.md | 135 ++++--- packages/core-v1/README.md | 16 +- packages/core-v1/src/createStore.ts | 43 +-- packages/core-v1/src/declareAction.ts | 50 +-- packages/core-v1/src/declareAtom.ts | 71 +--- packages/core-v1/src/index.test.ts | 156 ++------ packages/core-v1/src/kernel.ts | 5 +- packages/core-v1/src/shared.ts | 15 +- packages/core-v2/CHANGELOG.md | 65 ++-- packages/core-v2/README.md | 52 +-- .../core-v2/primitives/createBooleanAtom.ts | 5 +- .../core-v2/primitives/createEnumAtom.test.ts | 12 +- packages/core-v2/primitives/createEnumAtom.ts | 16 +- .../core-v2/primitives/createNumberAtom.ts | 5 +- .../core-v2/primitives/createPrimitiveAtom.ts | 32 +- packages/core-v2/primitives/createSetAtom.ts | 8 +- .../core-v2/primitives/createStringAtom.ts | 15 +- packages/core-v2/src/createAtom.ts | 39 +- packages/core-v2/src/createStore.ts | 15 +- packages/core-v2/src/index.test.ts | 101 ++---- packages/core-v2/src/shared.ts | 5 +- packages/core-v2/src/types.ts | 65 +--- packages/core-v2/test_utils/index.ts | 8 +- packages/core/CHANGELOG.md | 137 +++---- packages/core/README.md | 55 +-- packages/core/history-v2.md | 86 ++--- packages/core/src/atom.test.ts | 85 +---- packages/devtools/CHANGELOG.md | 21 +- packages/effects/CHANGELOG.md | 135 +++---- packages/effects/README.md | 4 +- packages/eslint-plugin/.eslintrc.js | 6 +- packages/eslint-plugin/CHANGELOG.md | 46 +-- packages/eslint-plugin/README.md | 8 +- .../src/rules/async-rule.test.ts | 3 +- .../eslint-plugin/src/rules/async-rule.ts | 11 +- .../src/rules/unit-naming-rule.ts | 47 +-- packages/eslint-plugin/src/shared.ts | 10 +- packages/esm-import-check/src/hello.ts | 4 +- packages/form-web/CHANGELOG.md | 73 ++-- packages/form-web/src/index.ts | 172 ++------- packages/form/CHANGELOG.md | 78 ++-- packages/form/README.md | 3 +- packages/form/src/reatomField.ts | 35 +- packages/form/src/reatomForm.ts | 19 +- packages/form/src/utils.ts | 3 +- packages/form/vite.config.ts | 2 +- packages/framework/src/index.test.ts | 11 +- packages/hooks/CHANGELOG.md | 93 +++-- packages/hooks/README.md | 13 +- packages/hooks/src/index.ts | 72 +--- packages/jsx/CHANGELOG.md | 60 ++-- packages/jsx/README.md | 12 +- packages/jsx/src/index.ts | 59 +-- packages/jsx/src/jsx.d.ts | 249 +++---------- packages/lens/CHANGELOG.md | 132 +++---- packages/lens/README.md | 43 +-- packages/lens/src/delay.ts | 81 ++--- packages/lens/src/effect.ts | 76 ++-- packages/lens/src/filter.ts | 28 +- packages/lens/src/index.test.ts | 10 +- packages/lens/src/index.ts | 94 ++--- packages/lens/src/match.test.ts | 28 +- packages/lens/src/match.ts | 33 +- packages/lens/src/parseAtoms.ts | 11 +- packages/lens/src/sample.ts | 10 +- packages/lens/src/select.test.ts | 13 +- packages/lens/src/select.ts | 15 +- packages/lens/src/withReset.ts | 3 +- packages/logger/CHANGELOG.md | 114 +++--- packages/logger/src/graphView.ts | 16 +- packages/logger/src/index.test.ts | 9 +- packages/logger/src/index.ts | 32 +- packages/npm-cookie-baker/CHANGELOG.md | 67 ++-- packages/npm-cookie-baker/README.md | 7 +- packages/npm-cookie-baker/src/index.ts | 46 +-- packages/npm-history/CHANGELOG.md | 70 ++-- packages/npm-history/src/index.test.ts | 18 +- packages/npm-history/src/index.ts | 12 +- packages/npm-history/vite.config.ts | 2 +- packages/npm-lit/CHANGELOG.md | 67 ++-- packages/npm-lit/src/index.ts | 7 +- packages/npm-react/CHANGELOG.md | 139 +++----- packages/npm-solid-js/CHANGELOG.md | 11 +- packages/npm-solid-js/src/index.ts | 42 +-- packages/npm-svelte/CHANGELOG.md | 67 ++-- packages/npm-svelte/README.md | 10 +- packages/npm-svelte/src/index.ts | 12 +- packages/npm-vue/CHANGELOG.md | 13 +- packages/npm-vue/src/index.ts | 23 +- packages/npm-zod/CHANGELOG.md | 18 +- packages/npm-zod/src/index.test.ts | 38 +- packages/npm-zod/src/index.ts | 247 +++++++------ packages/persist-web-storage/CHANGELOG.md | 107 +++--- packages/persist-web-storage/README.md | 38 +- .../src/reatomPersistBroadcastChannel.ts | 12 +- .../src/reatomPersistCookie.ts | 21 +- .../src/reatomPersistIndexedDb.ts | 10 +- .../src/reatomPersistWebStorage.ts | 20 +- packages/persist/CHANGELOG.md | 84 ++--- packages/primitives/CHANGELOG.md | 87 ++--- packages/primitives/README.md | 18 +- packages/primitives/src/reatomArray.test.ts | 93 ++--- packages/primitives/src/reatomArray.ts | 85 ++--- packages/primitives/src/reatomBoolean.ts | 5 +- packages/primitives/src/reatomEnum.test.ts | 7 +- packages/primitives/src/reatomEnum.ts | 38 +- .../primitives/src/reatomLinkedList.test.ts | 335 +++++++++--------- packages/primitives/src/reatomLinkedList.ts | 109 ++---- packages/primitives/src/reatomList.ts | 187 +++++----- packages/primitives/src/reatomMap.ts | 12 +- packages/primitives/src/reatomNumber.ts | 10 +- packages/primitives/src/reatomRecord.ts | 121 +++---- packages/primitives/src/reatomSet.test.ts | 50 +-- packages/primitives/src/reatomSet.ts | 43 +-- packages/primitives/src/withAssign.ts | 7 +- packages/primitives/src/withComputed.ts | 4 +- packages/primitives/src/withReducers.ts | 7 +- packages/react-v1/CHANGELOG.md | 126 ++++--- packages/react-v1/README.md | 2 - packages/react-v1/src/index.ts | 38 +- packages/react-v2/CHANGELOG.md | 8 +- packages/react-v2/README.md | 10 +- .../react-dom-batched-updates/index.d.ts | 2 +- packages/react-v2/src/index.ts | 18 +- packages/testing/CHANGELOG.md | 84 ++--- packages/testing/src/index.ts | 17 +- packages/timer/CHANGELOG.md | 79 ++--- packages/timer/src/index.test.ts | 6 +- packages/timer/src/index.ts | 172 ++++----- packages/undo/CHANGELOG.md | 82 ++--- packages/undo/README.md | 6 +- packages/undo/src/index.test.ts | 14 +- packages/undo/src/index.ts | 58 +-- packages/url/CHANGELOG.md | 53 +-- packages/url/README.md | 13 +- packages/url/src/index.test.ts | 7 +- packages/url/src/index.ts | 78 +--- packages/utils/CHANGELOG.md | 98 +++-- packages/utils/src/index.test.ts | 8 +- packages/web-fetch/README.md | 10 +- packages/web-fetch/src/index.test.ts | 25 +- packages/web-fetch/src/index.ts | 51 +-- packages/web/CHANGELOG.md | 11 +- packages/web/README.md | 47 ++- packages/web/src/event.ts | 25 +- packages/web/src/fetch.ts | 29 +- tools/actualize-issue-templates.ts | 33 +- tools/new-package.ts | 47 +-- 200 files changed, 3029 insertions(+), 5745 deletions(-) diff --git a/docs/README.md b/docs/README.md index bea634937..2d7df38e4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,6 @@ ![basics](https://user-images.githubusercontent.com/4677417/186188965-73453154-fdec-4d6b-9c34-cb35c248ae5b.png) - ## πŸš€ Project Structure Inside of your Astro project, you'll see the following folders and files: diff --git a/docs/src/content/docs/blog/what-is-state-manager.md b/docs/src/content/docs/blog/what-is-state-manager.md index 1821fc13a..66d993004 100644 --- a/docs/src/content/docs/blog/what-is-state-manager.md +++ b/docs/src/content/docs/blog/what-is-state-manager.md @@ -74,7 +74,7 @@ The last condition is very important - computational resources are limited, so w You can talk about the state as data related by some meaning, although often we are talking about some specific cache. For example, traffic light data contains information about three light bulbs, their colors and which one is on. Semantics follows from the subject area and represents the meaning of the data: only one light bulb can be turned on at a time, and the order of their switching is strictly regulated, this information is described not by the data structure, but by the code, therefore less explicit, although no less important. -The traffic light example deduced an important distinguishing feature of state as a phenomenon, is the need for data consistency: we cannot turn on one light without turning off the other, otherwise we would get erroneous data with their unpredictable impact on the user. The property of a state to be always consistent, i.e. to contain non-contradictory data, is called [atomicity](https://en.wikipedia.org/wiki/Atomicity_(database_systems)) in database theory. +The traffic light example deduced an important distinguishing feature of state as a phenomenon, is the need for data consistency: we cannot turn on one light without turning off the other, otherwise we would get erroneous data with their unpredictable impact on the user. The property of a state to be always consistent, i.e. to contain non-contradictory data, is called [atomicity]() in database theory. > [Here is the test of atomicity for a few state managers](https://github.com/artalar/state-management-specification/blob/master/src/index.test.js). Btw, React.js throws all your app away from screen if uncaught error occurs in render function, there is no way to get inconsistent state during render. diff --git a/docs/src/content/docs/compat/core-v1.md b/docs/src/content/docs/compat/core-v1.md index 79837c164..b2d13d51f 100644 --- a/docs/src/content/docs/compat/core-v1.md +++ b/docs/src/content/docs/compat/core-v1.md @@ -17,12 +17,8 @@ before import { declareAction, declareAtom, map, combine } from '@reatom/core-v1' const add = declareAction() -const n1Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) -const n2Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) +const n1Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) +const n2Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) const sumAtom = map(combine([n1Atom, n2Atom]), ([n1, n2]) => n1 + n2) const rootAtom = combine({ sumAtom }) ``` @@ -34,12 +30,8 @@ import { declareAction, declareAtom, combine, v3toV1 } from '@reatom/core-v1' import { atom } from '@reatom/core' const add = declareAction() -const n1Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) -const n2Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) +const n1Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) +const n2Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) const sumAtom = atom((ctx) => ctx.spy(n1Atom.v3atom) + ctx.spy(n2Atom.v3atom)) const rootAtom = combine({ sumAtom: v3toV1(sumAtom) }) ``` diff --git a/docs/src/content/docs/compat/core-v2.md b/docs/src/content/docs/compat/core-v2.md index 04ea0449e..86e9e83ed 100644 --- a/docs/src/content/docs/compat/core-v2.md +++ b/docs/src/content/docs/compat/core-v2.md @@ -206,13 +206,10 @@ import { createAtom } from '@reatom/core-v2' type TimerCtx = { intervalId?: number | NodeJS.Timer | any } /** Timer update interval */ -export const intervalAtom = createAtom( - { setSeconds: (seconds: number) => seconds }, - ({ onAction }, state = 1000) => { - onAction(`setSeconds`, (seconds) => (state = seconds * 1000)) - return state - }, -) +export const intervalAtom = createAtom({ setSeconds: (seconds: number) => seconds }, ({ onAction }, state = 1000) => { + onAction(`setSeconds`, (seconds) => (state = seconds * 1000)) + return state +}) export const timerAtom = createAtom( { @@ -239,10 +236,7 @@ export const timerAtom = createAtom( if (remains <= interval) { clearInterval(ctx.intervalId) - ctx.intervalId = setTimeout( - () => dispatch(create(`_update`, 0)), - remains, - ) + ctx.intervalId = setTimeout(() => dispatch(create(`_update`, 0)), remains) } dispatch(create(`_update`, remains)) @@ -318,10 +312,7 @@ But a better way is use the `createEnumAtom`. ```ts import { createEnumAtom } from '@reatom/core-v2/primitives' -const githubRepoSortFilterAtom = createEnumAtom( - ['full_name', 'created', 'updated', 'pushed'], - { format: 'snake_case' }, -) +const githubRepoSortFilterAtom = createEnumAtom(['full_name', 'created', 'updated', 'pushed'], { format: 'snake_case' }) console.log(sortFilterAtom.getState()) // -> 'full_name' @@ -462,11 +453,7 @@ const formAtom = createAtom( // you should't call `track.get` async // (scheduled callback calls async after all atoms) // (use `email` and `password` variables instead) - track.create( - '_fetch', - track.get('emailAtom'), - track.get('passwordAtom'), - ), + track.create('_fetch', track.get('emailAtom'), track.get('passwordAtom')), ), ) }) @@ -540,21 +527,18 @@ const counterAtom = createAtom({ inc: () => {} }, ({ onAction }, state = 0) => { Important note. Feel free to mutate **variable**, not a value. Reducer functions should not mutate any input values. ```ts -const counterAtom = createAtom( - { inc: () => {} }, - ({ onAction }, state = { count: 0 }) => { - // WRONG - onAction('inc', () => { - state.count++ - }) - // Right - onAction('inc', () => { - state = { count: state.count + 1 } - }) +const counterAtom = createAtom({ inc: () => {} }, ({ onAction }, state = { count: 0 }) => { + // WRONG + onAction('inc', () => { + state.count++ + }) + // Right + onAction('inc', () => { + state = { count: state.count + 1 } + }) - return state - }, -) + return state +}) ``` ### How to handle one action in a few atoms? diff --git a/docs/src/content/docs/compat/react-v1.md b/docs/src/content/docs/compat/react-v1.md index 8589e6cd0..da2e2f155 100644 --- a/docs/src/content/docs/compat/react-v1.md +++ b/docs/src/content/docs/compat/react-v1.md @@ -7,8 +7,6 @@ description: Reatom for react-v1 - - This is compatible package which allow you to use `@reatom/core-v1` with react. All docs is [here](/package/npm-react/). ## Setup batching for old React diff --git a/docs/src/content/docs/compat/react-v2.md b/docs/src/content/docs/compat/react-v2.md index 884c29557..c2d6d9385 100644 --- a/docs/src/content/docs/compat/react-v2.md +++ b/docs/src/content/docs/compat/react-v2.md @@ -62,10 +62,7 @@ const [data] = useAtom(dataAtom) #### Depended value by selector ```ts -const [propAtom] = useMemo( - () => createAtom({ dataAtom }, ({ get }) => get('dataAtom')[props.id]), - [props.id], -) +const [propAtom] = useMemo(() => createAtom({ dataAtom }, ({ get }) => get('dataAtom')[props.id]), [props.id]) const [propValue] = useAtom(propAtom) ``` @@ -82,10 +79,7 @@ const handleUpdateData = useAction(dataAtom.update) #### Prepare payload for dispatch ```ts -const handleUpdateData = useAction( - (value) => dataAtom.update({ id: props.id, value }), - [props.id], -) +const handleUpdateData = useAction((value) => dataAtom.update({ id: props.id, value }), [props.id]) ``` #### Conditional dispatch diff --git a/docs/src/content/docs/getting-started/learning.md b/docs/src/content/docs/getting-started/learning.md index 554dba34f..051f1fc98 100644 --- a/docs/src/content/docs/getting-started/learning.md +++ b/docs/src/content/docs/getting-started/learning.md @@ -206,9 +206,7 @@ export const todoAtom = atom(null) export const isLoadingAtom = atom(false) export const fetchTodo = action(async (ctx) => { - const response = await ctx.schedule(() => - fetch('https://jsonplaceholder.typicode.com/todos/1'), - ) + const response = await ctx.schedule(() => fetch('https://jsonplaceholder.typicode.com/todos/1')) return await response.json() }) diff --git a/docs/src/content/docs/getting-started/setup.md b/docs/src/content/docs/getting-started/setup.md index bdd980a0c..bb18e386f 100644 --- a/docs/src/content/docs/getting-started/setup.md +++ b/docs/src/content/docs/getting-started/setup.md @@ -14,9 +14,10 @@ The base template project includes Vite, TypeScript, React and Reatom ecosystem. You can check in out [here](https://github.com/artalar/reatom-react-ts) You can also try it online: - - [codesandbox](https://codesandbox.io/p/sandbox/github/artalar/reatom-react-ts/tree/main) - - [stackblitz](https://githubblitz.com/artalar/reatom-react-ts) - - [gitpod](https://gitpod.io/#https://github.com/artalar/reatom-react-ts) + +- [codesandbox](https://codesandbox.io/p/sandbox/github/artalar/reatom-react-ts/tree/main) +- [stackblitz](https://githubblitz.com/artalar/reatom-react-ts) +- [gitpod](https://gitpod.io/#https://github.com/artalar/reatom-react-ts) To setup it in your machine you can use the [degit](https://github.com/Rich-Harris/degit) package. @@ -90,10 +91,7 @@ const nameAtom = atom('Joe') const Greeting = () => { const t = useTranslation() const [name, setName] = useAtom(nameAtom) - const [greeting] = useAtom( - (ctx) => `${t('common:GREETING')} ${ctx.spy(nameAtom)}!`, - [t], - ) + const [greeting] = useAtom((ctx) => `${t('common:GREETING')} ${ctx.spy(nameAtom)}!`, [t]) return ( <> diff --git a/docs/src/content/docs/getting-started/testing.md b/docs/src/content/docs/getting-started/testing.md index 23c258c07..8f5182813 100644 --- a/docs/src/content/docs/getting-started/testing.md +++ b/docs/src/content/docs/getting-started/testing.md @@ -57,18 +57,18 @@ In the next example, we have an async API. import { action, atom } from '@reatom/core' export const todoAtom = atom(null) -export const isLoadingAtom = atom(false); +export const isLoadingAtom = atom(false) export const fetchTodo = action(async (ctx) => { const response = await ctx.schedule(() => fetch('https://jsonplaceholder.typicode.com/todos/1')) - return await response.json(); + return await response.json() }) export const loadTodo = action(async (ctx) => { try { isLoadingAtom(ctx, true) const data = await ctx.schedule((ctx) => fetchTodo(ctx)) - todoAtom(ctx, data); + todoAtom(ctx, data) } catch (e) { console.error(e) } finally { @@ -80,13 +80,13 @@ export const loadTodo = action(async (ctx) => { Let's test it without calling the real api ```js -import { expect, test } from 'vitest'; -import { createTestCtx } from '@reatom/testing'; -import { loadTodo, fetchTodo, todoAtom } from './main'; +import { expect, test } from 'vitest' +import { createTestCtx } from '@reatom/testing' +import { loadTodo, fetchTodo, todoAtom } from './main' test('Test loadData atom', async () => { const ctx = createTestCtx() - const track = ctx.subscribeTrack(todoAtom) + const track = ctx.subscribeTrack(todoAtom) // Mock action with call ctx.mockAction(fetchTodo, (ctx) => Promise.resolve([{ id: 'foo' }])) diff --git a/docs/src/content/docs/handbook.md b/docs/src/content/docs/handbook.md index 20c023753..e944adfbf 100644 --- a/docs/src/content/docs/handbook.md +++ b/docs/src/content/docs/handbook.md @@ -48,7 +48,6 @@ Most likely you will need [@reatom/npm-react](/package/npm-react/) adapter packa > The "npm-" prefix in adapter packages prevents naming collisions with ecosystem packages, as the NPM global namespace is widely used, and many common words are already taken. - ```sh npm i @reatom/framework @reatom/testing @reatom/eslint-plugin @reatom/npm-react ``` @@ -110,8 +109,9 @@ Reactive programming can address these issues by accurately describing dependent Let's refactor the code using Reatom. We use the `atom` function to wrap our changeable data: - - If you pass a primitive value to the atom, it allows the state to change. - - If you pass a `computer` function to the atom, it creates a read-only atom that automatically recomputes when dependent atoms change, but only if the computed atom has a subscription. + +- If you pass a primitive value to the atom, it allows the state to change. +- If you pass a `computer` function to the atom, it creates a read-only atom that automatically recomputes when dependent atoms change, but only if the computed atom has a subscription. ```ts export const nameAtom = atom(localStorage.getItem('name') ?? '') @@ -215,9 +215,7 @@ export const submit = action(async (ctx, event) => { const body = new FormData() body.append('name', name) - const response = await ctx.schedule(() => - fetch('/api/submit', { method: 'POST', body: body }), - ) + const response = await ctx.schedule(() => fetch('/api/submit', { method: 'POST', body: body })) if (!response.ok) { alert(`Oups, the API is doesn't exist, this is just a test.`) } @@ -261,10 +259,7 @@ export const pageAtom = atom(1, 'pageAtom').pipe( export const issuesReaction = reatomResource(async (ctx) => { const page = ctx.spy(pageAtom) return await ctx.schedule(() => - request( - `https://api.github.com/search/issues?q=reatom&page=${page}&per_page=10`, - ctx.controller, - ), + request(`https://api.github.com/search/issues?q=reatom&page=${page}&per_page=10`, ctx.controller), ) }, 'issuesReaction').pipe(withDataAtom({ items: [] })) @@ -314,7 +309,7 @@ To view persisted actions data and explore many more features, try [reatom/logge Additionally, you can inspect all atom and action patches by using: ```javascript -ctx.subscribe(logs => console.log(logs)); +ctx.subscribe((logs) => console.log(logs)) ``` ## Lifecycle diff --git a/docs/src/content/docs/index.md b/docs/src/content/docs/index.md index 088e0072e..53646aaef 100644 --- a/docs/src/content/docs/index.md +++ b/docs/src/content/docs/index.md @@ -28,7 +28,7 @@ description: Reatom - tiny and powerful reactive system with immutable nature - **smallest bundle** size: [2 KB](https://bundlejs.com/?q=%40reatom%2Fcore) gzipped With the power of base primitives, the whole ecosystem with A LOT of enterprise-level helpers takes only [~15KB](https://bundlejs.com/?q=%40reatom%2Fframework%2C%40reatom%2Fnpm-react%2C%40reatom%2Fpersist-web-storage%2C%40reatom%2Fundo%2C%40reatom%2Fform-web&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22react%22%2C%22use-sync-external-store%22%5D%7D%7D). Insane! - **the best TypeScript** experience - [Type inference](/recipes/typescript/) is one of the main priorities for Reatom. + [Type inference](/recipes/typescript/) is one of the main priorities for Reatom. [The core package](/core) includes most of these features and, due to its minimal overhead, can be used in any project, from small libraries to large applications. @@ -176,9 +176,7 @@ const fetchIssues = reatomAsync(async (ctx, query: string) => { withRetry({ onReject(ctx, error: any, retries) { // return delay in ms or -1 to prevent retries - return error?.message.includes('rate limit') - ? 100 * Math.min(500, retries ** 2) - : -1 + return error?.message.includes('rate limit') ? 100 * Math.min(500, retries ** 2) : -1 }, }), ) @@ -205,11 +203,7 @@ export const Search = () => { return (
- setSearch(e.currentTarget.value)} - placeholder="Search" - /> + setSearch(e.currentTarget.value)} placeholder="Search" /> {isLoading && 'Loading...'}
    {issues.map(({ title }, i) => ( @@ -221,7 +215,7 @@ export const Search = () => { } ``` -The logic definition consists of only about 15 lines of code and is entirely independent from the the view part (React in our case). It makes it easy to test. +The logic definition consists of only about 15 lines of code and is entirely independent from the the view part (React in our case). It makes it easy to test. Imagine the line count in other libraries! The most impressive part is that the overhead is [less than 4KB (gzip)](https://bundlejs.com/?q=%28import%29%40reatom%2Fframework%2C%28import%29%40reatom%2Fnpm-react&treeshake=%5B%7B%0A++atom%2CcreateCtx%2ConUpdate%2CreatomAsync%2Csleep%2CwithAbort%2CwithDataAtom%2CwithRetry%2C%7D%5D%2C%5B%7B+useAtom+%7D%5D&share=MYewdgzgLgBBCmBDATsAFgQSiAtjAvDItjgBQBE5ANDOQiulruQJQDcAUKJLAGbxR0ASQgQArvAgEYyJCQwQAnmGClESlTFLAoADxoBHCckUAuOFGQBLMAHMWBAHwwA3hxhEA7oiuwIAG3h4AAdSACYAVgAGdhgAejiYABN4ACMQMRV4dxhuaFcYX3gcKQBfaURvXyJgqwA6fkE0EXFJUiN4ExodXTruSxB-QOR2HNkoMWQwQqhiiE5SmnJG4VEJCFY62uD4UhzPXzQAEWJEJjIAbQBdFip9w4x05ChSFwtkYnhbM1p-dSgALQ2AEHMDkGClW73KBoABKAhMrxyHnA8IAVvAdNo9DROsgQMhzIgwIoaONrJIHG4PDT4olxpNpik-opCtMSjACTAAQBGGDYGDBWQAN3gYFg5KskmRNIZUxgeIJAH46jhJBBELZ4HUbMB-GIUhAKB9ZjB-FYcL5WDLaUqYDyolEYAAqGAAWWIaFVNlI0SiZIRUqkztdYRYNpp5l5nFpixykLuowSMkyMBWzTWkk503gopMcCQqEwJBgYmCSU%2BHHAAFVy59SPQi%2BcaOmWutRlxZJ8AMJ6UijMQIc6kVuZiB1CtQM4kFhAA&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22react%22%2C%22use-sync-external-store%22%5D%7D%7D). Amazing, right? On top of that, you’re not limited to network cache. Reatom is powerful and expressive enough to manage any state. @@ -261,7 +255,7 @@ While this can be more predictable, it is certainly not optimal. Effector's hot connections make it unfriendly for factory creation, which prevents the use of [atomization](/recipes/atomization/) patterns necessary for efficient immutability handling. Additionally, Effector's [bundle size is 2-3 times more significant](https://bundlejs.com/?q=effector&treeshake=%5B%7BcraeteStore%2CcreateEvent%2Ccombine%7D%5D) with [worse performance](https://github.com/artalar/reactive-computed-bench). -[Zustand](https://github.com/pmndrs/zustand), [nanostores](https://github.com/nanostores/nanostores), [xstate](https://xstate.js.org), and [many other](https://gist.github.com/artalar/e5e8a7274dfdfbe9d36c9e5ec22fc650) state managers do not offer the same exceptional combination of type inference, features, bundle size, and performance that Reatom provides. +[Zustand](https://github.com/pmndrs/zustand), [nanostores](https://github.com/nanostores/nanostores), [xstate](https://xstate.js.org), and [many other](https://gist.github.com/artalar/e5e8a7274dfdfbe9d36c9e5ec22fc650) state managers do not offer the same exceptional combination of type inference, features, bundle size, and performance that Reatom provides. ### Why immutability? @@ -299,6 +293,7 @@ Also, remember to check out our [atomization guide](/recipes/atomization). ### Limitations No software is perfect, and Reatom is no exception. Here are some limitations you should be aware of: + - **Immutable Data**: While immutable data structures are great, they can impact performance. In critical situations, think carefully about your data structures. The good news is you [don't have to use normalization](/recipes/atomization). - **Laziness**: Laziness is less obvious sometimes and might lead to missed updates. However, debugging a missing update is straightforward and often easier than dealing with hot observables' memory leaks and performance issues. We also have [hooks](/package/hooks) for hot linking. - **Error Handling**: Currently, you can't subscribe to errors from any dependency, but we're working on it. In [reatomAsync](/package/async), passed effects are wrapped in an error handler, allowing you to manage errors, but you need to wrap them explicitly. diff --git a/docs/src/content/docs/package/async.md b/docs/src/content/docs/package/async.md index 204a9868b..ba4f0bd8f 100644 --- a/docs/src/content/docs/package/async.md +++ b/docs/src/content/docs/package/async.md @@ -61,23 +61,20 @@ import { reatomAsync } from '@reatom/async' const listAtom = atom([]) const errorAtom = atom(null) -export const fetchList = reatomAsync( - (ctx, page: number) => request(`/api/list?page=${page}`, ctx.controller), - { - name: 'fetchList', - onFulfill(ctx, result) { - listAtom(ctx, result) - }, - onReject(ctx, error) { - errorAtom(ctx, error) - }, - onEffect(ctx, params, promise) { - // clear outdated data on request start - listAtom(ctx, []) - errorAtom(ctx, null) - }, +export const fetchList = reatomAsync((ctx, page: number) => request(`/api/list?page=${page}`, ctx.controller), { + name: 'fetchList', + onFulfill(ctx, result) { + listAtom(ctx, result) }, -) + onReject(ctx, error) { + errorAtom(ctx, error) + }, + onEffect(ctx, params, promise) { + // clear outdated data on request start + listAtom(ctx, []) + errorAtom(ctx, null) + }, +}) ``` ### Qualified usage @@ -97,10 +94,7 @@ type Element = { export const listAtom = atom(new Array(), 'listAtom') export const errorAtom = atom(null, 'errorAtom') // if number of pending requests are equal or more than 1 - there is a loading state -export const isLoadingAtom = atom( - (ctx) => ctx.spy(fetchList.pendingAtom) > 0, - 'isLoadingAtom', -) +export const isLoadingAtom = atom((ctx) => ctx.spy(fetchList.pendingAtom) > 0, 'isLoadingAtom') // store abort controller of last request to prevent race conditions const abortControllerAtom = atom(new AbortController()) const ABORT = 'ABORT' @@ -121,14 +115,11 @@ fetchList.onReject.onCall((ctx, thing) => { } }) -export const updateElement = reatomAsync( - (ctx, id: string, slice: Partial) => { - const { signal } = ctx.controller - const data = JSON.stringify(slice) - return request(`/api/list/${id}`, { method: 'POST', data, signal }) - }, - 'updateElement', -) +export const updateElement = reatomAsync((ctx, id: string, slice: Partial) => { + const { signal } = ctx.controller + const data = JSON.stringify(slice) + return request(`/api/list/${id}`, { method: 'POST', data, signal }) +}, 'updateElement') // refresh backend data on successful update updateElement.onFulfill.onCall((ctx) => fetchList(ctx, 1)) ``` @@ -151,20 +142,16 @@ type Element = { } export const fetchList = reatomAsync( - (ctx, page: number) => - request>(`/api/list?page=${page}`, ctx.controller), + (ctx, page: number) => request>(`/api/list?page=${page}`, ctx.controller), 'fetchList', // add extra handlers with full type inference ).pipe(withDataAtom([]), withErrorAtom(), withAbort(), withStatusesAtom()) -export const updateElement = reatomAsync( - (ctx, id: string, slice: Partial) => { - const { signal } = ctx.controller - const data = JSON.stringify(slice) - return request(`/api/list/${id}`, { method: 'POST', data, signal }) - }, - 'updateElement', -) +export const updateElement = reatomAsync((ctx, id: string, slice: Partial) => { + const { signal } = ctx.controller + const data = JSON.stringify(slice) + return request(`/api/list/${id}`, { method: 'POST', data, signal }) +}, 'updateElement') updateElement.onFulfill.onCall((ctx) => fetchList(ctx, 1)) ``` @@ -187,19 +174,15 @@ type Feature = { /*...*/ } -export const fetchFeature = reatomAsync( - (ctx) => request('/api/feature', ctx.controller), - 'fetchFeature', -).pipe(withDataAtom(null)) +export const fetchFeature = reatomAsync((ctx) => request('/api/feature', ctx.controller), 'fetchFeature').pipe( + withDataAtom(null), +) // use subscription to `fetchFeature.dataAtom` to get the actual data // mutate data manually in the feature form -export const changeFeature = action( - (ctx, property: keyof Feature, value: any) => { - fetchUser.dataAtom(ctx, (user) => ({ ...user, [property]: value })) - }, - 'changeFeature', -) +export const changeFeature = action((ctx, property: keyof Feature, value: any) => { + fetchUser.dataAtom(ctx, (user) => ({ ...user, [property]: value })) +}, 'changeFeature') // save new feature data to backend on form submit export const syncFeature = reatomAsync((ctx) => { @@ -223,10 +206,7 @@ Here how you can fetch data declaratively and lazy only when needed. This is a s import { reatomAsync, withDataAtom } from '@reatom/async' import { onConnect } from '@reatom/hooks' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) onConnect(fetchList.dataAtom, fetchList) ``` @@ -249,10 +229,7 @@ import { reatomAsync, withDataAtom } from '@reatom/async' const PAGE_SIZE = 10 export const fetchFeed = reatomAsync(async (ctx, page: number) => { - const data = await request( - `api/feed?page=${page}&limit?${page}`, - ctx.controller, - ) + const data = await request(`api/feed?page=${page}&limit?${page}`, ctx.controller) return { data, page } }, 'fetchFeed').pipe( withDataAtom([], (ctx, { data, page }, state) => { @@ -270,10 +247,7 @@ You could describe optimistic async logic easily with `onEffect` handler, which ```ts import { reatomAsync, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) export const updateList = reatomAsync( (ctx, newList) => { @@ -306,10 +280,7 @@ export type Element = { // ... } -export const fetchList = reatomAsync( - (ctx) => request>('api/list', ctx.controller), - 'fetchList', -) +export const fetchList = reatomAsync((ctx) => request>('api/list', ctx.controller), 'fetchList') export const listAtom = reatomArray(new Array(), 'listAtom') fetchList.onFulfill.onCall(listAtom) ``` @@ -320,9 +291,7 @@ Here the interface of `onFulfill` update hook and `listAtom` update is the same import { reatomMap } from '@reatom/primitives' // .... export const mapAtom = reatomMap(new Map(), 'mapAtom') -fetchList.onFulfill.onCall((ctx, payload) => - mapAtom(ctx, new Map(payload.map((el) => [el.id, el]))), -) +fetchList.onFulfill.onCall((ctx, payload) => mapAtom(ctx, new Map(payload.map((el) => [el.id, el])))) ``` ## withErrorAtom @@ -334,16 +303,10 @@ You could update the error atom manually as a usual atom: `fetchList.errorAtom(c ```ts import { reatomAsync, withErrorAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withErrorAtom( // optional mapper - (ctx, error) => - error instanceof Response - ? error.status - : error?.message || 'unknown error', + (ctx, error) => (error instanceof Response ? error.status : error?.message || 'unknown error'), ), ) ``` @@ -355,10 +318,7 @@ Adds property `statusesAtom` with additional statuses, which updates by the effe ```ts import { reatomAsync, withStatusesAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withStatusesAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withStatusesAtom()) // ... @@ -388,9 +348,7 @@ onDisconnect(fetchList.dataAtom, (ctx) => { You could import special types of statuses of each effect state and use it for typesafe conditional logic. ```ts -export type AsyncStatusesPending = - | AsyncStatusesFirstPending - | AsyncStatusesAnotherPending +export type AsyncStatusesPending = AsyncStatusesFirstPending | AsyncStatusesAnotherPending export type AsyncStatuses = | AsyncStatusesNeverPending @@ -425,10 +383,10 @@ You could rule the cache behavior by set of optional parameters. ```ts import { reatomAsync, withDataAtom, withCache } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom(), withCache()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withDataAtom(), + withCache(), +) // fetch data await fetchList(ctx, { query: 'foo', page: 1 }) // call the effect @@ -450,10 +408,10 @@ You can invalidate the cache by `reset` action on `cacheAtom`. It will clear the ```ts import { reatomAsync, withCache, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), +) export const updateList = reatomAction(async () => { /* */ @@ -466,19 +424,17 @@ You can use `withRetry` to retry the effect after cache invalidation or use buil ```ts import { reatomAsync, withCache, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), +) export const updateList = reatomAction(async () => { /* */ }, 'updateList') updateList.onFulfill.onCall(fetchList.cacheAtom.invalidate) -export const listLoadingAtom = atom( - (ctx) => ctx.spy(fetchList.pendingAtom) + ctx.spy(updateList.pendingAtom) > 0, -) +export const listLoadingAtom = atom((ctx) => ctx.spy(fetchList.pendingAtom) + ctx.spy(updateList.pendingAtom) > 0) ``` Use `listLoadingAtom` to show a loader in a UI during the whole process of data updating and invalidation. @@ -519,10 +475,9 @@ You could persist the cache for a chosen time and sync it across a tabs by `with import { reatomAsync, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache({ withPersist: withLocalStorage })) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache({ withPersist: withLocalStorage }), +) ``` `withCache` applies `withPersist` to `cacheAtom` with options for optimal serialization. You could redefine the options by an inline decorator function. It is recommended to set the key explicitly, by default the async action name used. @@ -531,13 +486,9 @@ export const fetchList = reatomAsync( import { reatomAsync, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withCache({ - withPersist: (options) => - withLocalStorage({ ...options, key: 'LIST_CACHE' }), + withPersist: (options) => withLocalStorage({ ...options, key: 'LIST_CACHE' }), }), ) ``` @@ -548,10 +499,10 @@ If you want to use persisted cache as an init state of `dataAtom` - just put `wi import { reatomAsync, withDataAtom, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([]), withCache({ withPersist })) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withDataAtom([]), + withCache({ withPersist }), +) ``` ## withRetry @@ -561,10 +512,11 @@ Adds `retry` action and `paramsAtom` to store last params of the effect call. ```ts import { reatomAsync, withCache, withDataAtom, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom(), withRetry()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), + withRetry(), +) export const updateList = reatomAction(async () => { /* */ @@ -578,10 +530,9 @@ If you will try to call `retry` before first effect call, it will throw an error ```ts import { reatomAsync, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx, page) => request(`api/list?page=${page}`, ctx.controller), - 'fetchList', -).pipe(withRetry({ fallbackParams: [1] })) +export const fetchList = reatomAsync((ctx, page) => request(`api/list?page=${page}`, ctx.controller), 'fetchList').pipe( + withRetry({ fallbackParams: [1] }), +) // will call fetch(`api/list?page=1`) fetchList.retry(ctx) @@ -615,17 +566,13 @@ Progressive retry: `100 * Math.min(200, retries ** 3)`. Will retry after 100ms, ```ts import { atom, reatomAsync, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withRetry({ onReject: (ctx, error, retries) => 100 * Math.min(200, retries ** 3), }), withAssign((target, name) => ({ loadingAtom: atom( - (ctx) => - ctx.spy(target.pendingAtom) > 0 || ctx.spy(target.retriesAtom) > 0, + (ctx) => ctx.spy(target.pendingAtom) > 0 || ctx.spy(target.retriesAtom) > 0, `${name}.loadingAtom`, ), })), @@ -635,18 +582,9 @@ export const fetchList = reatomAsync( Note that `retriesAtom` will drop to `0` when any promise resolves successfully or when you return `undefined` or a negative number. So, it is good practice to avoid calling multiple async actions in parallel. If you are using `withRetry`, it is recommended to always use it with [withAbort](#withabort) (with the default 'last-in-win' strategy). ```ts -import { - atom, - reatomAsync, - withAbort, - withErrorAtom, - withRetry, -} from '@reatom/async' +import { atom, reatomAsync, withAbort, withErrorAtom, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withAbort(), withRetry({ onReject: (ctx, error, retries) => { @@ -660,8 +598,7 @@ export const fetchList = reatomAsync( withErrorAtom(), ) export const isFetchListLoading = atom( - (ctx) => - ctx.spy(fetchList.pendingAtom) > 0 || ctx.spy(fetchList.retriesAtom) > 0, + (ctx) => ctx.spy(fetchList.pendingAtom) > 0 || ctx.spy(fetchList.retriesAtom) > 0, 'isFetchListLoading', ) ``` @@ -671,13 +608,7 @@ export const isFetchListLoading = atom( Do you need to implement a **pooling** pattern to stay your data fresh? Lets use `onConnect` from [@reatom/hooks](/package/hooks) to control the neediness of this. ```ts -import { - reatomAsync, - withDataAtom, - withRetry, - onConnect, - sleep, -} from '@reatom/framework' +import { reatomAsync, withDataAtom, withRetry, onConnect, sleep } from '@reatom/framework' export const fetchList = reatomAsync( (ctx, search: string) => request(`/api/list?q=${search}`, ctx.controller), @@ -715,10 +646,11 @@ To be clear, you don't need to use `retry`, if you have no need to manage parame ```ts import { reatomAsync, withAbort, withDataAtom, sleep } from '@reatom/framework' -export const fetchList = reatomAsync( - (ctx) => request('/api/list', ctx.controller), - 'fetchList', -).pipe(withAbort(), withDataAtom([]), withRetry()) +export const fetchList = reatomAsync((ctx) => request('/api/list', ctx.controller), 'fetchList').pipe( + withAbort(), + withDataAtom([]), + withRetry(), +) onConnect(fetchList.dataAtom, async (ctx) => { while (true) { await fetchList(ctx).catch(() => {}) @@ -788,12 +720,8 @@ onConnect(fetchList.dataAtom, (ctx) => { return () => fetchList.abort(ctx) }) // trigger -pageAtom.onChange((ctx, page) => - fetchSuggestion(ctx, page, ctx.get(searchAtom)), -) -searchAtom.onChange((ctx, search) => - fetchSuggestion(ctx, ctx.get(pageAtom), search), -) +pageAtom.onChange((ctx, page) => fetchSuggestion(ctx, page, ctx.get(searchAtom))) +searchAtom.onChange((ctx, search) => fetchSuggestion(ctx, ctx.get(pageAtom), search)) ``` There are a lot of boilerplates. `reatomResource` is a fabric method that encapsulates all this logic and allows you to use `ctx.spy` just like in the regular `atom`. It is much simpler, more intuitive, and works automatically for both caching and cancelling previous requests. @@ -804,9 +732,7 @@ import { reatomResource, withDataAtom } from '@reatom/async' const listResource = reatomResource(async (ctx) => { const page = ctx.spy(pageAtom) const search = ctx.spy(searchAtom) - return await ctx.schedule(() => - request(`/api/list?page=${page}&q=${search}`, ctx.controller), - ) + return await ctx.schedule(() => request(`/api/list?page=${page}&q=${search}`, ctx.controller)) }, 'listResource').pipe(withDataAtom([])) ``` @@ -823,9 +749,7 @@ import { reatomResource } from '@reatom/async' const aResource = reatomResource(async (ctx) => { const page = ctx.spy(pageAtom) - return await ctx.schedule(() => - request(`/api/a?page=${page}`, ctx.controller), - ) + return await ctx.schedule(() => request(`/api/a?page=${page}`, ctx.controller)) }, 'aResource') const bResource = reatomResource(async (ctx) => { const a = await ctx.spy(aResource.promiseAtom) @@ -884,18 +808,14 @@ describe('optimistic update', () => { const getData = reatomAsync.from(api.getData).pipe( // add `dataAtom` and map the effect payload into it // try to prevent new reference stream if nothing really changed - withDataAtom([], (ctx, payload, state) => - isDeepEqual(payload, state) ? state : payload, - ), + withDataAtom([], (ctx, payload, state) => (isDeepEqual(payload, state) ? state : payload)), ) const putData = reatomAsync.from(api.putData) putData.onCall((ctx, promise, params) => { const [id, value] = params const oldList = ctx.get(getData.dataAtom) // optimistic update - const newList = getData.dataAtom(ctx, (state) => - state.map((item) => (item.id === id ? { ...item, value } : item)), - ) + const newList = getData.dataAtom(ctx, (state) => state.map((item) => (item.id === id ? { ...item, value } : item))) // rollback on error promise.catch((error) => { if (ctx.get(getData.dataAtom) === newList) { @@ -1000,9 +920,7 @@ describe('concurrent pooling', () => { assert.is(ctx.get(progressAtom), 100) - const expectedProgress = [ - 0, 10, /* start again */ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, - ] + const expectedProgress = [0, 10, /* start again */ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] // assert.equal(track.inputs(), expectedProgress) }) diff --git a/docs/src/content/docs/package/core.md b/docs/src/content/docs/package/core.md index 3f588f778..fd4c0aa08 100644 --- a/docs/src/content/docs/package/core.md +++ b/docs/src/content/docs/package/core.md @@ -90,9 +90,7 @@ const fetchGoods = action(async (ctx, search: string) => { // schedule side-effects // which will be called after successful execution of all computations - const goods = await ctx.schedule(() => - fetch(`/api/goods?search=${search}`).then((r) => r.json()), - ) + const goods = await ctx.schedule(() => fetch(`/api/goods?search=${search}`).then((r) => r.json())) // use `batch` to prevent glitches and extra effects. batch(ctx, () => { @@ -207,10 +205,7 @@ You could create a computed derived atom by passing a function to `atom`. The fi > **Note to TypeScript users**: It is impossible to describe the reducer type with an optional generic state argument, which is returned from the function. If you use the second `state` argument, you should define its type; do not rely on the return type. ```ts -const isCountEvenAtom = atom( - (ctx) => ctx.spy(countAtom) % 2 === 0, - 'isCountEven', -) +const isCountEvenAtom = atom((ctx) => ctx.spy(countAtom) % 2 === 0, 'isCountEven') // isCountEvenAtom: Atom ``` @@ -276,9 +271,7 @@ export const currencyAtom = atom((ctx, state?: string) => { Pipe is a general chain helper, it applies an operator to the atom to map it to another thing. Classic operator interface is `(options?: any) => (anAtom: T) => aNewThing`. The main reason is a readable and type-safe way to apply decorators. ```ts -const countAtom = atom(0).pipe( - withInit(() => localStorage.getItem('COUNT') ?? 0), -) +const countAtom = atom(0).pipe(withInit(() => localStorage.getItem('COUNT') ?? 0)) // equals to const countAtom = withInit(() => localStorage.getItem('COUNT') ?? 0)(atom(0)) ``` @@ -380,7 +373,6 @@ doSome.onCall((ctx, payload, params) => { }) ``` - ## createCtx API A context creation function accepts a few optional parameters that you probably won't want to change in regular use. However, it might be useful for testing and some rare production needs. @@ -485,10 +477,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { const user = await ctx.schedule(() => api.getUser(id)) firstNameAtom(ctx, user.firstName) @@ -511,10 +500,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { const user = await ctx.schedule(() => api.getUser(id)) batch(ctx, () => { @@ -539,10 +525,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) @@ -571,10 +554,7 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) @@ -599,21 +579,15 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) }, 'saveUser') -export const resolveFetchUser = action( - (ctx, firstName: string, lastName: string) => { - saveUser(ctx, firstName, firstName) - isUserLoadingAtom(ctx, false) - }, - 'resolveFetchUser', -) +export const resolveFetchUser = action((ctx, firstName: string, lastName: string) => { + saveUser(ctx, firstName, firstName) + isUserLoadingAtom(ctx, false) +}, 'resolveFetchUser') export const fetchUser = action(async (ctx, id: string) => { isUserLoadingAtom(ctx, true) try { @@ -633,10 +607,7 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { isUserLoadingAtom(ctx, true) try { diff --git a/docs/src/content/docs/package/effects.md b/docs/src/content/docs/package/effects.md index f6ab46526..0e7851336 100644 --- a/docs/src/content/docs/package/effects.md +++ b/docs/src/content/docs/package/effects.md @@ -274,9 +274,7 @@ const someRequest = reatomRequest<{ data: Data } | { error: string }>() ```ts // type-safe destructuring -const { data } = await take(ctx, someRequest, (ctx, payload, skip) => - 'error' in payload ? skip : payload, -) +const { data } = await take(ctx, someRequest, (ctx, payload, skip) => ('error' in payload ? skip : payload)) ``` ### takeNested diff --git a/docs/src/content/docs/package/eslint-plugin.md b/docs/src/content/docs/package/eslint-plugin.md index e6cdd3632..1a4b67ceb 100644 --- a/docs/src/content/docs/package/eslint-plugin.md +++ b/docs/src/content/docs/package/eslint-plugin.md @@ -93,13 +93,7 @@ const someNamespace = { When creating atoms dynamically with factories, you can also specify the "namespace" of the name before the `.` symbol: ```ts -const reatomFood = (config: { - name: string - calories: number - fat: number - carbs: number - protein: number -}) => { +const reatomFood = (config: { name: string; calories: number; fat: number; carbs: number; protein: number }) => { const { name } = config.name const calories = atom(config.calories, `${name}.calories`) const fat = atom(config.fat, `${name}.fat`) diff --git a/docs/src/content/docs/package/form.md b/docs/src/content/docs/package/form.md index bd79eba2a..36e3aa88d 100644 --- a/docs/src/content/docs/package/form.md +++ b/docs/src/content/docs/package/form.md @@ -42,8 +42,7 @@ export const passwordField = loginForm.reatomField( { initState: '', validate(ctx, { state }) { - if (state.length < 6) - throw new Error('The password should have at least six characters.') + if (state.length < 6) throw new Error('The password should have at least six characters.') }, }, 'passwordField', diff --git a/docs/src/content/docs/package/hooks.md b/docs/src/content/docs/package/hooks.md index cb124c3fd..4aac93aa5 100644 --- a/docs/src/content/docs/package/hooks.md +++ b/docs/src/content/docs/package/hooks.md @@ -49,10 +49,10 @@ For example, in React you should manage abort strategy by yourself by `useEffect import { reatomAsync, withAbort, withDataAtom } from '@reatom/async' import { useAtom, useAction } from '@reatom/npm-react' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withAbort(), withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withAbort(), + withDataAtom([]), +) export const List = () => { const [list] = useAtom(fetchList.dataAtom) @@ -74,10 +74,7 @@ With Reatom, you can simplify it and make it more readable. import { reatomAsync, onConnect, withDataAtom } from '@reatom/framework' import { useAtom } from '@reatom/npm-react' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) onConnect(fetchList.dataAtom, fetchList) export const List = () => { diff --git a/docs/src/content/docs/package/jsx.md b/docs/src/content/docs/package/jsx.md index 68f539bd6..a2d891bd6 100644 --- a/docs/src/content/docs/package/jsx.md +++ b/docs/src/content/docs/package/jsx.md @@ -61,7 +61,7 @@ You can use different JSX pragmas in different files. For example, if you have a ```tsx // @jsxRuntime classic // @jsx h -import { h } from '@reatom/jsx'; +import { h } from '@reatom/jsx' ``` ## Example @@ -74,9 +74,7 @@ Define a component: import { atom, action } from '@reatom/core' export const inputAtom = atom('') -const onInput = action((ctx, event) => - inputAtom(ctx, event.currentTarget.value), -) +const onInput = action((ctx, event) => inputAtom(ctx, event.currentTarget.value)) export const Input = () => ``` @@ -194,11 +192,7 @@ In Reatom, there is no concept of "rerender" like React. Instead, we have a spec ```tsx
    - ctx.spy(valid) - ? { disabled: true, readonly: true } - : { disabled: false, readonly: false }, - )} + $spread={atom((ctx) => (ctx.spy(valid) ? { disabled: true, readonly: true } : { disabled: false, readonly: false }))} /> ``` diff --git a/docs/src/content/docs/package/lens.md b/docs/src/content/docs/package/lens.md index 697438a72..2c9bf4d3c 100644 --- a/docs/src/content/docs/package/lens.md +++ b/docs/src/content/docs/package/lens.md @@ -20,9 +20,7 @@ import { mapState } from '@reatom/lens' // this is a typical code which have a problem with extra updates // in case when an element of the list changes not `myProp` -export const filteredListAtom = atom((ctx) => - ctx.spy(listAtom).map((obj) => obj.myProp), -) +export const filteredListAtom = atom((ctx) => ctx.spy(listAtom).map((obj) => obj.myProp)) // `mapState` could help to solve this problem, as it pass previous state as a second argument export const bAtom = listAtom.pipe( mapState((ctx, list, prevState) => { @@ -44,9 +42,7 @@ import { isShallowEqual } from '@reatom/utils' export const listMemoAtom = filteredListAtom.pipe(filter()) // equals to -export const listMemoAtom = filteredListAtom.pipe( - filter((ctx, next, prev) => !isShallowEqual(next, prev)), -) +export const listMemoAtom = filteredListAtom.pipe(filter((ctx, next, prev) => !isShallowEqual(next, prev))) ``` This operator could filter actions too! @@ -54,9 +50,7 @@ This operator could filter actions too! ```ts import { filter } from '@reatom/lens' -export const linkClicked = onDocumentClick.pipe( - filter((ctx, event) => event.target.tagName === 'A'), -) +export const linkClicked = onDocumentClick.pipe(filter((ctx, event) => event.target.tagName === 'A')) ``` ## `mapPayload` @@ -66,9 +60,7 @@ Map payload of each action call. Resulted action is not callable. ```ts import { mapPayload } from '@reatom/lens' -export const changeFullname = changeName.pipe( - mapPayload((ctx, { firstName, lastName }) => `${firstName} ${lastName}`), -) +export const changeFullname = changeName.pipe(mapPayload((ctx, { firstName, lastName }) => `${firstName} ${lastName}`)) ``` You could pass initial state by first argument to create an atom. @@ -78,9 +70,7 @@ import { action } from '@reatom/core' import { mapPayload } from '@reatom/lens' export const onInput = action('onInput') -export const inputAtom = onInput.pipe( - mapPayload('', (ctx, event) => event.currentTarget.value, 'inputAtom'), -) +export const inputAtom = onInput.pipe(mapPayload('', (ctx, event) => event.currentTarget.value, 'inputAtom')) ``` ## `mapPayloadAwaited` @@ -92,9 +82,7 @@ import { mapPayloadAwaited } from '@reatom/lens' export const newData = fetchData.pipe(mapPayloadAwaited()) // OR pick needed value -export const newData = fetchData.pipe( - mapPayloadAwaited((ctx, response) => response.data), -) +export const newData = fetchData.pipe(mapPayloadAwaited((ctx, response) => response.data)) ``` You could pass initial state by first argument to create an atom. @@ -118,9 +106,7 @@ import { atom } from '@reatom/core' import { mapInput } from '@reatom/lens' export const inputAtom = atom('', 'inputAtom') -export const changeInput = inputAtom.pipe( - mapInput((ctx, event) => event.currentTarget.value, 'changeInput'), -) +export const changeInput = inputAtom.pipe(mapInput((ctx, event) => event.currentTarget.value, 'changeInput')) ``` ## `debounce` @@ -227,8 +213,7 @@ const getField = (id: number, name: string, value: string): Field => { export const listAtom = atom>([], 'listAtom').pipe( withLocalStorage({ toSnapshot: (state) => parseAtoms(state), - fromSnapshot: (snapshot: any) => - getField(snapshot.id, snapshot.name, snapshot.value), + fromSnapshot: (snapshot: any) => getField(snapshot.id, snapshot.name, snapshot.value), }), ) ``` @@ -348,11 +333,7 @@ export const ListSum = reatomComponent(({ ctx }) => { // correct optimal way, the component will rerender only on `length` change const length = select(ctx, (selectCtx) => selectCtx.spy(listAtom).length) // you could call `select` many times - const sum = select(ctx, (selectCtx) => - selectCtx - .spy(listAtom) - .reduce((acc, el) => acc + selectCtx.spy(el.value), 0), - ) + const sum = select(ctx, (selectCtx) => selectCtx.spy(listAtom).reduce((acc, el) => acc + selectCtx.spy(el.value), 0)) return (
    @@ -367,9 +348,5 @@ Under the hood `select` creates additional atom, so you can perform all regular Note for a rare cases. A created atom is momorized for the each select by the passed function sources from "toString()" method, so every computed callback in different selects of the same atom should contains differen code. This will throw an error, as the select called multiple times in the same atom with the same string represantation of the passed callback: ```ts -const sumAtom = atom((ctx) => - ctx - .spy(listAtom) - .reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0), -) +const sumAtom = atom((ctx) => ctx.spy(listAtom).reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0)) ``` diff --git a/docs/src/content/docs/package/npm-cookie-baker.md b/docs/src/content/docs/package/npm-cookie-baker.md index af7ec5081..f3144ac79 100644 --- a/docs/src/content/docs/package/npm-cookie-baker.md +++ b/docs/src/content/docs/package/npm-cookie-baker.md @@ -46,14 +46,11 @@ npm install @cookie-baker/node ``` ## Example -[codesandbox](https://codesandbox.io/s/reatom-cookie-baker-ec6h63-ec6h63?file=/src/App.tsx) +[codesandbox](https://codesandbox.io/s/reatom-cookie-baker-ec6h63-ec6h63?file=/src/App.tsx) ```ts -import { - Cookie as CookieClient, - createRealTimeCookie, -} from '@cookie-baker/browser' +import { Cookie as CookieClient, createRealTimeCookie } from '@cookie-baker/browser' import { reatomCookie } from '@reatom/npm-cookie-baker' type CookieModel = { diff --git a/docs/src/content/docs/package/npm-svelte.md b/docs/src/content/docs/package/npm-svelte.md index fcfa3e836..d0cd7a6b6 100644 --- a/docs/src/content/docs/package/npm-svelte.md +++ b/docs/src/content/docs/package/npm-svelte.md @@ -76,14 +76,8 @@ Of course, you could describe atoms as a [separate module](/guides/architecture) import { atom, action } from '@reatom/core' export const countAtom = atom(0, 'countAtom') -export const timesAtom = atom( - (ctx) => (ctx.spy(countAtom) === 1 ? 'time' : 'times'), - 'timesAtom', -) -export const increment = action( - (ctx) => countAtom(ctx, (s) => ++s), - 'increment', -) +export const timesAtom = atom((ctx) => (ctx.spy(countAtom) === 1 ? 'time' : 'times'), 'timesAtom') +export const increment = action((ctx) => countAtom(ctx, (s) => ++s), 'increment') ``` ### Data fetching and Svelte diff --git a/docs/src/content/docs/package/persist-web-storage.md b/docs/src/content/docs/package/persist-web-storage.md index 0ab4b430a..7a02e7320 100644 --- a/docs/src/content/docs/package/persist-web-storage.md +++ b/docs/src/content/docs/package/persist-web-storage.md @@ -47,9 +47,7 @@ The main goal of this adapter is to synchronize atom between tabs without pollut import { atom } from '@reatom/framework' import { withBroadcastChannel } from '@reatom/persist-web-storage' -export const isAuthedAtom = atom('', 'isAuthedAtom').pipe( - withBroadcastChannel('isAuthedAtom'), -) +export const isAuthedAtom = atom('', 'isAuthedAtom').pipe(withBroadcastChannel('isAuthedAtom')) ``` You can also give an instance of `BroadcastChannel` @@ -68,10 +66,7 @@ myChannel.onmessage((event) => { const withMyBroadcastChannel = reatomPersistBroadcastChannel(myChannel) - -export const isAuthedAtom = atom('', 'isAuthedAtom').pipe( - withMyBroadcastChannel('isAuthedAtom'), -) +export const isAuthedAtom = atom('', 'isAuthedAtom').pipe(withMyBroadcastChannel('isAuthedAtom')) ``` ### withIndexedDb @@ -84,10 +79,9 @@ The main goal of this adapter is to persist atom's state to IndexedDB. `withInde import { reatomResource, withCache } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withCache({ withPersist: withIndexedDb })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withCache({ withPersist: withIndexedDb }), +) ``` You can also specify a custom database name and a custom `BroadcastChannel' that will be used to synchronize the data in real time. @@ -96,10 +90,9 @@ You can also specify a custom database name and a custom `BroadcastChannel' that import { reatomResource, withCache } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel }), +) ``` If you want to avoid flickering, all you have to do is add a small delay after the atom is connected. Subscribe / spy the data atom, wait the ready atom and then use the actual data. @@ -110,10 +103,9 @@ If you want to avoid flickering, all you have to do is add a small delay after t import { reatomResource, withCache, onConnect, sleep } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel }), +) const isListReadyAtom = atom(false, 'isListReadyAtom') onConnect(listResource, async (ctx) => { await ctx.schedule(() => new Promise((r) => requestIdleCallback(r))) @@ -124,7 +116,7 @@ onConnect(listResource, async (ctx) => { ### withCookie -Synchronizes atom state to the `document.cookie` with a given name. +Synchronizes atom state to the `document.cookie` with a given name. When using `withCookie`, the first argument it takes is an options object that allows you to configure various aspects of cookie behavior. @@ -141,9 +133,9 @@ interface CookieAttributes { sameSite?: 'strict' | 'lax' | 'none' } -export const tokenAtom = atom("", "tokenAtom").pipe( +export const tokenAtom = atom('', 'tokenAtom').pipe( withCookie({ maxAge: 3600, // 1 hour - })("token"), -); + })('token'), +) ``` diff --git a/docs/src/content/docs/package/primitives.md b/docs/src/content/docs/package/primitives.md index 653d8c484..b442bced6 100644 --- a/docs/src/content/docs/package/primitives.md +++ b/docs/src/content/docs/package/primitives.md @@ -173,20 +173,11 @@ export const pageAtom = atom(0, 'pageAtom').pipe( An operator that makes it easier to attach properties such as computed atoms, reducer actions etc. It is just a better code organization pattern to have `thingAtom`, `thingAtom.doSome`, instead of `thingAtom` and `doSomeThing`. ```ts -import { - atom, - withAssign, - action, - reatomResource, - withRetry, -} from '@reatom/framework' +import { atom, withAssign, action, reatomResource, withRetry } from '@reatom/framework' const pageAtom = atom(1).pipe( withAssign((pageAtom, name) => ({ - prev: action( - (ctx) => pageAtom(ctx, (prev) => Math.max(1, prev - 1)), - `${name}.prev`, - ), + prev: action((ctx) => pageAtom(ctx, (prev) => Math.max(1, prev - 1)), `${name}.prev`), next: action((ctx) => pageAtom(ctx, (prev) => prev + 1), `${name}.next`), })), ) @@ -199,10 +190,7 @@ const list = reatomResource(async (ctx) => { onReject: (ctx, error, retries) => 100 * Math.min(200, retries ** 3), }), withAssign((list, name) => ({ - loadingAtom: atom( - (ctx) => ctx.spy(list.pendingAtom) > 0 || ctx.spy(list.retriesAtom) > 0, - `${name}.loadingAtom`, - ), + loadingAtom: atom((ctx) => ctx.spy(list.pendingAtom) > 0 || ctx.spy(list.retriesAtom) > 0, `${name}.loadingAtom`), })), ) ``` diff --git a/docs/src/content/docs/package/undo.md b/docs/src/content/docs/package/undo.md index 93c5022aa..a756ca7b7 100644 --- a/docs/src/content/docs/package/undo.md +++ b/docs/src/content/docs/package/undo.md @@ -31,7 +31,7 @@ All methods reuse `WithUndo` interface which includes the following atoms and ac All methods accepts the optional properties: - `length` is the max amount of state records, **30** by default -- `shouldUpdate` function allows you to ignore some updates, by default it skips state updates which is equal to the last history record +- `shouldUpdate` function allows you to ignore some updates, by default it skips state updates which is equal to the last history record - `shouldReplace` function allows you define what to do with the new update, replace the last history record (`true`) or add a new record (`false` - by default) - `withPersist` - `WithPersist` instance from one of the adapter of [@reatom/persist](/package/persist). It will persist data from `historyAtom` and `persistAtom`, the target atom init state may be derived from the history, if it preserve. @@ -55,9 +55,7 @@ This option helps you store only important updates by replacing the last state w For example, we want to save in the history all user input, but the each record of the history should include only words, not a letters. ```ts -const inputAtom = atom('').pipe( - withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') }), -) +const inputAtom = atom('').pipe(withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') })) for (const letter of 'This is a test') { inputAtom(ctx, (s) => s + letter) diff --git a/docs/src/content/docs/package/url.md b/docs/src/content/docs/package/url.md index a1a686638..f474600f8 100644 --- a/docs/src/content/docs/package/url.md +++ b/docs/src/content/docs/package/url.md @@ -37,9 +37,7 @@ In the code below `filterAtom` is a mutable atom which changes will be synced wi import { reatomNumber } from '@reatom/primitives' import { withSearchParamsPersist } from '@reatom/url' -export const pageAtom = reatomNumber(0, 'pageAtom').pipe( - withSearchParamsPersist('page', (page = '1') => Number(page)), -) +export const pageAtom = reatomNumber(0, 'pageAtom').pipe(withSearchParamsPersist('page', (page = '1') => Number(page))) ``` Now you have handy `increment` and `decrement` actions in `pageAtom` and synchronization with "page" search parameter. @@ -79,11 +77,7 @@ export interface SearchParamsAtom extends Atom> { set: Action<[key: string, value: string, replace?: boolean], void> del: Action<[key: string, replace?: boolean], void> /** create AtomMut which will synced with the specified query parameter */ - lens( - key: string, - parse?: (value?: string) => T, - serialize?: (value: T) => undefined | string, - ): AtomMut + lens(key: string, parse?: (value?: string) => T, serialize?: (value: T) => undefined | string): AtomMut /** create AtomMut which will synced with the specified query parameter */ lens( key: string, @@ -144,8 +138,7 @@ export const RouterSync = () => { setupRef.current = true urlAtom.settingsAtom(ctx, { init: () => new URL(location.href), - sync: (_ctx, url, replace) => - navigate(url.pathname + url.search, { replace }), + sync: (_ctx, url, replace) => navigate(url.pathname + url.search, { replace }), }) // trigger `onChange` hooks. urlAtom(ctx, new URL(location.href)) diff --git a/docs/src/content/docs/package/web.md b/docs/src/content/docs/package/web.md index ef2565265..1e44a64ed 100644 --- a/docs/src/content/docs/package/web.md +++ b/docs/src/content/docs/package/web.md @@ -23,7 +23,7 @@ You can pass a callback as the last argument. In this case, the method will retu Please note that this API handles the abort context from the [onConnect](/package/hooks/#onconnect) effect and other Reatom APIs. It enables you to describe complex logic in a concise and clear manner with memory safety underneath. -### onEvent WebSocket example +### onEvent WebSocket example Here is a usage example, which was derived from [this observable example](https://github.com/domfarolino/observable/blob/c232b2e585b71a61034fd23ba4337570b537ef27/README.md?plain=1#L86): @@ -45,9 +45,7 @@ const reatomStock = (ticker) => { }) onEvent(ctx, socket, 'close', () => ctx.controller.abort()) onEvent(ctx, socket, 'error', () => ctx.controller.abort()) - onCtxAbort(ctx, () => - socket.send(JSON.stringify({ ticker, type: 'unsub' })), - ) + onCtxAbort(ctx, () => socket.send(JSON.stringify({ ticker, type: 'unsub' }))) }) return stockAtom @@ -59,6 +57,7 @@ ctx.subscribe(googStockAtom, updateView) ``` ## onEvent checkpoint example + Make sure to listen to event before you actually need it. As in [take](https://reatom.dev/package/effects/#take-checkpoints) you should use checkpoints to handle all events without skipping it. @@ -70,34 +69,34 @@ import { api } from '~/api' const heroElement = document.getElementById('#hero') -const loadPageContent = reatomAsync(async (ctx)=>{ - // Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/animate - const animation = heroElement.animate(heroAnimation) +const loadPageContent = reatomAsync(async (ctx) => { + // Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/animate + const animation = heroElement.animate(heroAnimation) - const content = await api.fetchContent() + const content = await api.fetchContent() - // ❌ Bug: - // If person's connection is not fast enough animation can finish before we load content. - // And we will be showing last frame of animation forever... - await onEvent(ctx, animation, 'finish') + // ❌ Bug: + // If person's connection is not fast enough animation can finish before we load content. + // And we will be showing last frame of animation forever... + await onEvent(ctx, animation, 'finish') - pageContent(ctx, content) + pageContent(ctx, content) }) ``` And that's how we fix this behaviour using checkpoint: ```ts -const loadPageContent = reatomAsync(async (ctx)=>{ - const animation = heroElement.animate(heroAnimation) - // βœ… We make a checkpoint before loading... - const animationFinishedCheckpoint = onEvent(ctx, animation, 'finish') - - const content = await api.fetchContent() - - // ...and we will catch that event even if content loading takes ages - await animationFinishedCheckpoint - - pageContent(ctx, content) +const loadPageContent = reatomAsync(async (ctx) => { + const animation = heroElement.animate(heroAnimation) + // βœ… We make a checkpoint before loading... + const animationFinishedCheckpoint = onEvent(ctx, animation, 'finish') + + const content = await api.fetchContent() + + // ...and we will catch that event even if content loading takes ages + await animationFinishedCheckpoint + + pageContent(ctx, content) }) ``` diff --git a/docs/src/content/docs/recipes/custom-operator.md b/docs/src/content/docs/recipes/custom-operator.md index 7bfedeb4b..90214d238 100644 --- a/docs/src/content/docs/recipes/custom-operator.md +++ b/docs/src/content/docs/recipes/custom-operator.md @@ -34,6 +34,7 @@ declare function withStateHistory( stateHistoryAtom: Atom> } ``` + We use `T extends Atom` instead of the simpler `(length: string): (anAtom: Atom) => Atom & {...}` to preserve all additional properties added by previous operators. > [Real `historyAtom`](/package/undo) @@ -48,7 +49,7 @@ import { action, atom, Atom } from '@reatom/core' // operator accepts an options by a first argument // and returns function witch accepts target atom export const delay = - (ms: number) => + (ms: number) => (anAtom: Atom) => { // to improve debugability compute name of the new atom const name = `${anAtom.__reatom.name}.delay` diff --git a/docs/sync-readme-to-pages.ts b/docs/sync-readme-to-pages.ts index 7d1749996..a33bc4048 100644 --- a/docs/sync-readme-to-pages.ts +++ b/docs/sync-readme-to-pages.ts @@ -12,12 +12,7 @@ const packagesPath = path.join(root, 'packages') const packages = await fs.readdir(path.join(process.cwd(), '..', 'packages')) const getStoryTests = async (packageName: string) => { - const storyTestsPath = path.join( - packagesPath, - packageName, - 'src', - 'index.story.test.ts', - ) + const storyTestsPath = path.join(packagesPath, packageName, 'src', 'index.story.test.ts') const isExist = await fs.access(storyTestsPath).then( () => true, () => false, @@ -46,9 +41,7 @@ for (const packageName of packages) { 'src', 'content', 'docs', - packageName.endsWith('-v1') || packageName.endsWith('-v2') - ? 'compat' - : 'package', + packageName.endsWith('-v1') || packageName.endsWith('-v2') ? 'compat' : 'package', `${packageName}.md`, ) let content = await fs.readFile(readmePath, 'utf8') @@ -59,10 +52,7 @@ for (const packageName of packages) { if (packageJSON.name === '@reatom/form-web') continue if (!content.trim()) { - content = await fs.readFile( - path.join(packagesPath, packageName, 'src', 'index.test.ts'), - 'utf8', - ) + content = await fs.readFile(path.join(packagesPath, packageName, 'src', 'index.test.ts'), 'utf8') content = ` @@ -72,10 +62,7 @@ There is no docs yet, but you could check tests instead: content + '\n```\n' } else { - content = content.replaceAll( - '\n[Main docs starts here](https://www.reatom.dev).', - '', - ) + content = content.replaceAll('\n[Main docs starts here](https://www.reatom.dev).', '') content = content.replaceAll('https://www.reatom.dev', '') content = content.replaceAll('../../docs/public', '') } @@ -109,14 +96,7 @@ ${GEN_NOTE} } const rootReadmePath = path.join(root, 'README.md') -const rootPagePath = path.join( - root, - 'docs', - 'src', - 'content', - 'docs', - 'index.md', -) +const rootPagePath = path.join(root, 'docs', 'src', 'content', 'docs', 'index.md') let readme = await fs.readFile(rootReadmePath, 'utf8') readme = `--- diff --git a/examples/model/README.md b/examples/model/README.md index 7cd55c01c..78ae29271 100644 --- a/examples/model/README.md +++ b/examples/model/README.md @@ -1 +1 @@ -This is where we store shareable models for different frameworks to show Reatom agnoscizm. +This is where we store shareable models for different frameworks to show Reatom agnoscizm. diff --git a/examples/model/search.ts b/examples/model/search.ts index 632a3996b..4f9ff6a3d 100644 --- a/examples/model/search.ts +++ b/examples/model/search.ts @@ -90,10 +90,9 @@ export const api = { perPage?: number signal: AbortSignal }): Promise { - const response = await fetch( - `https://api.github.com/search/issues?q=${query}&page=${page}&per_page=${perPage}`, - { signal }, - ) + const response = await fetch(`https://api.github.com/search/issues?q=${query}&page=${page}&per_page=${perPage}`, { + signal, + }) if (response.status !== 200) { const error = new Error(`HTTP Error: ${response.statusText}`) diff --git a/examples/nextjs/src/features/api.ts b/examples/nextjs/src/features/api.ts index d2492eb37..709509542 100644 --- a/examples/nextjs/src/features/api.ts +++ b/examples/nextjs/src/features/api.ts @@ -1,13 +1,7 @@ import { Ctx } from '@reatom/framework' -export async function fetchIssuesApi( - query: string, - { signal }: { signal: AbortSignal }, -) { - const response = await fetch( - `https://api.github.com/search/issues?q=${query}&page=${1}&per_page=10`, - { signal }, - ) +export async function fetchIssuesApi(query: string, { signal }: { signal: AbortSignal }) { + const response = await fetch(`https://api.github.com/search/issues?q=${query}&page=${1}&per_page=10`, { signal }) if (!response.ok) throw new Error(await response.text()) @@ -20,11 +14,6 @@ export async function fetchIssuesApi( })) } -export const isRateLimitError = (thing: unknown) => - thing instanceof Error && thing.message.includes('rate limit') +export const isRateLimitError = (thing: unknown) => thing instanceof Error && thing.message.includes('rate limit') export const getErrorMessage = (ctx: Ctx, thing: unknown) => - isRateLimitError(thing) - ? undefined - : thing instanceof Error - ? thing.message - : String(thing) + isRateLimitError(thing) ? undefined : thing instanceof Error ? thing.message : String(thing) diff --git a/examples/nextjs/src/pages/api/hello.ts b/examples/nextjs/src/pages/api/hello.ts index f8bcc7e5c..9bafa6e25 100644 --- a/examples/nextjs/src/pages/api/hello.ts +++ b/examples/nextjs/src/pages/api/hello.ts @@ -5,9 +5,6 @@ type Data = { name: string } -export default function handler( - req: NextApiRequest, - res: NextApiResponse -) { +export default function handler(req: NextApiRequest, res: NextApiResponse) { res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/react-atomization-persistance/src/model.ts b/examples/react-atomization-persistance/src/model.ts index 8c3b83190..2c4ddffae 100644 --- a/examples/react-atomization-persistance/src/model.ts +++ b/examples/react-atomization-persistance/src/model.ts @@ -1,13 +1,4 @@ -import { - Action, - action, - atom, - AtomMut, - ParseAtoms, - parseAtoms, - withInit, - random, -} from '@reatom/framework' +import { Action, action, atom, AtomMut, ParseAtoms, parseAtoms, withInit, random } from '@reatom/framework' export type Field = { id: number @@ -34,19 +25,14 @@ const reatomField = (id: number, name: string, value: string): Field => { id, name, value: atom(value, valueName), - remove: action( - (ctx) => listAtom(ctx, (state) => state.filter((el) => el !== field)), - `${valueName}.remove`, - ), + remove: action((ctx) => listAtom(ctx, (state) => state.filter((el) => el !== field)), `${valueName}.remove`), } field.value.onChange(toLS) return field } -export const listAtom = atom(new Array(), 'listAtom').pipe( - withInit(fromLS), -) +export const listAtom = atom(new Array(), 'listAtom').pipe(withInit(fromLS)) listAtom.onChange(toLS) export const newFieldAtom = atom('', 'newFieldAtom') diff --git a/examples/react-notifications/src/model.ts b/examples/react-notifications/src/model.ts index 08b413c09..22381ce70 100644 --- a/examples/react-notifications/src/model.ts +++ b/examples/react-notifications/src/model.ts @@ -29,14 +29,8 @@ const reatomNotification = (message: string, kind: string): Notification => { resetProgress: false, delayMultiplier: 1, }) - const remains = atom( - (ctx) => (1 - ctx.spy(timer.progressAtom)) * 100, - `${name}._remains`, - ) - const readed = atom( - (ctx) => ctx.spy(remains) === 0 || ctx.spy(timer) === 0, - `${name}.readed`, - ) + const remains = atom((ctx) => (1 - ctx.spy(timer.progressAtom)) * 100, `${name}._remains`) + const readed = atom((ctx) => ctx.spy(remains) === 0 || ctx.spy(timer) === 0, `${name}.readed`) const remove = action(timer.stopTimer, `${name}.remove`) timer.endTimer.onCall(async (ctx) => { await sleep(HIDE_DELAY) diff --git a/examples/react-tree/src/model.ts b/examples/react-tree/src/model.ts index b1b86f42e..2119a58c4 100644 --- a/examples/react-tree/src/model.ts +++ b/examples/react-tree/src/model.ts @@ -1,11 +1,4 @@ -import { - type Atom, - action, - atom, - type Action, - type AtomMut, - random, -} from '@reatom/framework' +import { type Atom, action, atom, type Action, type AtomMut, random } from '@reatom/framework' export interface Tree { id: string @@ -21,25 +14,15 @@ export const reatomTree = (id: string, parent?: Tree): Tree => { id += random(1000, 9999) const name = `tree#${id}` - const childrenAtom: Tree['childrenAtom'] = atom( - new Array(), - `${name}.childrenAtom`, - ) + const childrenAtom: Tree['childrenAtom'] = atom(new Array(), `${name}.childrenAtom`) const indeterminateChildrenAtom = atom( - (ctx) => - ctx.spy(childrenAtom).some((child) => ctx.spy(child.indeterminateAtom)), + (ctx) => ctx.spy(childrenAtom).some((child) => ctx.spy(child.indeterminateAtom)), `${name}._indeterminateChildrenAtom`, ) const checkedCountAtom = atom( - (ctx) => - ctx - .spy(childrenAtom) - .reduce( - (acc, child) => (ctx.spy(child.checkedAtom) ? acc + 1 : acc), - 0, - ), + (ctx) => ctx.spy(childrenAtom).reduce((acc, child) => (ctx.spy(child.checkedAtom) ? acc + 1 : acc), 0), `${name}._checkedCountAtom`, ) diff --git a/examples/reatom-jsx/src/model.ts b/examples/reatom-jsx/src/model.ts index 039fdcfd4..3265015c3 100644 --- a/examples/reatom-jsx/src/model.ts +++ b/examples/reatom-jsx/src/model.ts @@ -1,12 +1,4 @@ -import { - atom, - action, - AtomMut, - LLNode, - reatomLinkedList, - LL_NEXT, - LL_PREV, -} from '@reatom/framework' +import { atom, action, AtomMut, LLNode, reatomLinkedList, LL_NEXT, LL_PREV } from '@reatom/framework' export type ListElement = LLNode> diff --git a/examples/reatom-jsx/vite.config.js b/examples/reatom-jsx/vite.config.js index 49e642c62..1f7f28398 100644 --- a/examples/reatom-jsx/vite.config.js +++ b/examples/reatom-jsx/vite.config.js @@ -1,4 +1,4 @@ -import { defineConfig } from 'vite'; +import { defineConfig } from 'vite' // https://vitejs.dev/config/ export default defineConfig({ @@ -7,4 +7,4 @@ export default defineConfig({ jsxFragment: 'hf', jsxInject: `import { h, hf } from "@reatom/jsx";`, }, -}); +}) diff --git a/packages/all-settled/CHANGELOG.md b/packages/all-settled/CHANGELOG.md index 1f731ebbf..1211e1454 100644 --- a/packages/all-settled/CHANGELOG.md +++ b/packages/all-settled/CHANGELOG.md @@ -2,50 +2,47 @@ ## [3.1.4](https://github.com/artalar/reatom/compare/all-settled-v3.1.3...all-settled-v3.1.4) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.3](https://github.com/artalar/reatom/compare/all-settled-v3.1.2...all-settled-v3.1.3) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.2](https://github.com/artalar/reatom/compare/all-settled-v3.1.1...all-settled-v3.1.2) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/all-settled/src/index.ts b/packages/all-settled/src/index.ts index 096c48fd5..14d5a5d78 100644 --- a/packages/all-settled/src/index.ts +++ b/packages/all-settled/src/index.ts @@ -1,5 +1,7 @@ import { takeNested } from '@reatom/effects' -console.warn("This package is outdated and will be removed soon, `all-settled` is `takeNested` now and part of the [effects](https://www.reatom.dev/package/effects) package") +console.warn( + 'This package is outdated and will be removed soon, `all-settled` is `takeNested` now and part of the [effects](https://www.reatom.dev/package/effects) package', +) export const allSettled = takeNested diff --git a/packages/async/CHANGELOG.md b/packages/async/CHANGELOG.md index 0844a7136..faddcbe73 100644 --- a/packages/async/CHANGELOG.md +++ b/packages/async/CHANGELOG.md @@ -2,298 +2,259 @@ ## [3.15.2](https://github.com/artalar/reatom/compare/async-v3.15.1...async-v3.15.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.15.1](https://github.com/artalar/reatom/compare/async-v3.15.0...async-v3.15.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.15.0](https://github.com/artalar/reatom/compare/async-v3.14.2...async-v3.15.0) (2024-05-25) - ### Features -* **async:** add to cache setWithParams and deleteWithParams [#796](https://github.com/artalar/reatom/issues/796) ([b53fe98](https://github.com/artalar/reatom/commit/b53fe98b37e178735240b274336c29de6e3be1c5)) +- **async:** add to cache setWithParams and deleteWithParams [#796](https://github.com/artalar/reatom/issues/796) ([b53fe98](https://github.com/artalar/reatom/commit/b53fe98b37e178735240b274336c29de6e3be1c5)) ## [3.14.2](https://github.com/artalar/reatom/compare/async-v3.14.1...async-v3.14.2) (2024-05-03) - ### Bug Fixes -* **async:** add AsyncStatusesAbortedSettle ([bd03669](https://github.com/artalar/reatom/commit/bd03669bfa1814e1ce4cd28387c8de70a5d0b4d7)) +- **async:** add AsyncStatusesAbortedSettle ([bd03669](https://github.com/artalar/reatom/commit/bd03669bfa1814e1ce4cd28387c8de70a5d0b4d7)) ## [3.14.1](https://github.com/artalar/reatom/compare/async-v3.14.0...async-v3.14.1) (2024-04-28) - ### Bug Fixes -* **async:** add AsyncStatusesFirstAborted and AsyncStatusesAbortedPending ([f653bb2](https://github.com/artalar/reatom/commit/f653bb2cd2a07eef9c062aa9477de91b06716e69)) +- **async:** add AsyncStatusesFirstAborted and AsyncStatusesAbortedPending ([f653bb2](https://github.com/artalar/reatom/commit/f653bb2cd2a07eef9c062aa9477de91b06716e69)) ## [3.14.0](https://github.com/artalar/reatom/compare/async-v3.13.6...async-v3.14.0) (2024-04-12) - ### Features -* **primitives:** add withAssign, deprecate withReducers ([3ac66fc](https://github.com/artalar/reatom/commit/3ac66fc76fffa4ef05e9782d93c982020188196f)) - +- **primitives:** add withAssign, deprecate withReducers ([3ac66fc](https://github.com/artalar/reatom/commit/3ac66fc76fffa4ef05e9782d93c982020188196f)) ### Bug Fixes -* **async:** change type of dataAtom in withDataAtom operator without initState ([#785](https://github.com/artalar/reatom/issues/785)) ([ecf44a6](https://github.com/artalar/reatom/commit/ecf44a6039e3217cb2d45c465564a67bd21f095c)) +- **async:** change type of dataAtom in withDataAtom operator without initState ([#785](https://github.com/artalar/reatom/issues/785)) ([ecf44a6](https://github.com/artalar/reatom/commit/ecf44a6039e3217cb2d45c465564a67bd21f095c)) ## [3.13.6](https://github.com/artalar/reatom/compare/async-v3.13.5...async-v3.13.6) (2024-03-19) - ### Bug Fixes -* **async:** drop retriesAtom if onReject returns undefined ([77e71b3](https://github.com/artalar/reatom/commit/77e71b3cafe79732a5611d4983778ed90d95f69e)) +- **async:** drop retriesAtom if onReject returns undefined ([77e71b3](https://github.com/artalar/reatom/commit/77e71b3cafe79732a5611d4983778ed90d95f69e)) ## [3.13.5](https://github.com/artalar/reatom/compare/async-v3.13.4...async-v3.13.5) (2024-03-06) - ### Bug Fixes -* **async:** withAbort + reatomResource ([47d81f7](https://github.com/artalar/reatom/commit/47d81f7aed563720692c4ecbf627d701e4cafe29)) +- **async:** withAbort + reatomResource ([47d81f7](https://github.com/artalar/reatom/commit/47d81f7aed563720692c4ecbf627d701e4cafe29)) ## [3.13.4](https://github.com/artalar/reatom/compare/async-v3.13.3...async-v3.13.4) (2024-01-21) - ### Bug Fixes -* **async:** prevent ERR_UNHANDLED_REJECTION if the onReject has any handlers ([c66ad56](https://github.com/artalar/reatom/commit/c66ad56e9b4cd536fda93e9cf0cc4a4b862abf91)) +- **async:** prevent ERR_UNHANDLED_REJECTION if the onReject has any handlers ([c66ad56](https://github.com/artalar/reatom/commit/c66ad56e9b4cd536fda93e9cf0cc4a4b862abf91)) ## [3.13.3](https://github.com/artalar/reatom/compare/async-v3.13.2...async-v3.13.3) (2023-11-25) - ### Bug Fixes -* **async:** prevent unhandled error for abort ([8c64522](https://github.com/artalar/reatom/commit/8c64522bed4d13b35a9f7f354ef6b086b5fbda98)) +- **async:** prevent unhandled error for abort ([8c64522](https://github.com/artalar/reatom/commit/8c64522bed4d13b35a9f7f354ef6b086b5fbda98)) ## [3.13.2](https://github.com/artalar/reatom/compare/async-v3.13.1...async-v3.13.2) (2023-11-23) - ### Bug Fixes -* **async:** reatomResource: do not drop the cache of an error ([75f54e6](https://github.com/artalar/reatom/commit/75f54e6fe8ab5a46d20846656d908c0779551e81)) +- **async:** reatomResource: do not drop the cache of an error ([75f54e6](https://github.com/artalar/reatom/commit/75f54e6fe8ab5a46d20846656d908c0779551e81)) ## [3.13.1](https://github.com/artalar/reatom/compare/async-v3.13.0...async-v3.13.1) (2023-11-20) - ### Bug Fixes -* **async:** allow optional resetTrigger for withErrorAtom ([185f99d](https://github.com/artalar/reatom/commit/185f99db81541ad72c18b5360517a89a08386807)) -* **async:** more mem safety for abortCauseContext ([aa5e29f](https://github.com/artalar/reatom/commit/aa5e29f17d250a20a10476cb2054897feda43628)) -* **async:** use abortCauseContext ([6133f27](https://github.com/artalar/reatom/commit/6133f275d4cb3529fd744558324e38a621cb66a4)) +- **async:** allow optional resetTrigger for withErrorAtom ([185f99d](https://github.com/artalar/reatom/commit/185f99db81541ad72c18b5360517a89a08386807)) +- **async:** more mem safety for abortCauseContext ([aa5e29f](https://github.com/artalar/reatom/commit/aa5e29f17d250a20a10476cb2054897feda43628)) +- **async:** use abortCauseContext ([6133f27](https://github.com/artalar/reatom/commit/6133f275d4cb3529fd744558324e38a621cb66a4)) ## [3.13.0](https://github.com/artalar/reatom/compare/async-v3.12.1...async-v3.13.0) (2023-11-12) - ### Features -* **async:** add initState to errorAtom ([6852b72](https://github.com/artalar/reatom/commit/6852b7291ccc4fb6acfb9ecfb48d3c2326fa78ee)) - +- **async:** add initState to errorAtom ([6852b72](https://github.com/artalar/reatom/commit/6852b7291ccc4fb6acfb9ecfb48d3c2326fa78ee)) ### Bug Fixes -* **async:** activate resource on pending connection ([81f19b9](https://github.com/artalar/reatom/commit/81f19b996c0a2535b2c9056dd4197f447620604f)) -* **async:** dataAtom should be computed first ([8aab299](https://github.com/artalar/reatom/commit/8aab299916f35afc0cb89983c58eeb4ae5b237cb)) -* **async:** withErrorAtom should be computed first ([0c458da](https://github.com/artalar/reatom/commit/0c458da9f1eaf6a3226ca2e57f8ba5360dc7ce9e)) -* **async:** withRetry races ([0abe299](https://github.com/artalar/reatom/commit/0abe29976c319525544efd5fd659e004b76d6edf)) +- **async:** activate resource on pending connection ([81f19b9](https://github.com/artalar/reatom/commit/81f19b996c0a2535b2c9056dd4197f447620604f)) +- **async:** dataAtom should be computed first ([8aab299](https://github.com/artalar/reatom/commit/8aab299916f35afc0cb89983c58eeb4ae5b237cb)) +- **async:** withErrorAtom should be computed first ([0c458da](https://github.com/artalar/reatom/commit/0c458da9f1eaf6a3226ca2e57f8ba5360dc7ce9e)) +- **async:** withRetry races ([0abe299](https://github.com/artalar/reatom/commit/0abe29976c319525544efd5fd659e004b76d6edf)) ## [3.12.1](https://github.com/artalar/reatom/compare/async-v3.12.0...async-v3.12.1) (2023-11-07) - ### Bug Fixes -* **async:** errorAtom: AtomMut ([a1349a8](https://github.com/artalar/reatom/commit/a1349a8118366a75d4cb9bf40d6e94274a2a87f2)) +- **async:** errorAtom: AtomMut ([a1349a8](https://github.com/artalar/reatom/commit/a1349a8118366a75d4cb9bf40d6e94274a2a87f2)) ## [3.12.0](https://github.com/artalar/reatom/compare/async-v3.11.0...async-v3.12.0) (2023-11-05) - ### Features -* **async:** add shouldPending and swrPendingAtom ([103e30c](https://github.com/artalar/reatom/commit/103e30c7aa8bd7879da347d2653389a39221d952)) - +- **async:** add shouldPending and swrPendingAtom ([103e30c](https://github.com/artalar/reatom/commit/103e30c7aa8bd7879da347d2653389a39221d952)) ### Bug Fixes -* **async:** reatomResource small mem leak ([408d916](https://github.com/artalar/reatom/commit/408d9167712cf53dc4381caee3c08f353752d990)) +- **async:** reatomResource small mem leak ([408d916](https://github.com/artalar/reatom/commit/408d9167712cf53dc4381caee3c08f353752d990)) ## [3.11.0](https://github.com/artalar/reatom/compare/async-v3.10.1...async-v3.11.0) (2023-10-31) - ### Features -* **async:** add reset action to statusesAtom ([7803168](https://github.com/artalar/reatom/commit/78031689792d7737641fc822324dbe482c4a8fec)) - +- **async:** add reset action to statusesAtom ([7803168](https://github.com/artalar/reatom/commit/78031689792d7737641fc822324dbe482c4a8fec)) ### Bug Fixes -* **async:** improve withStatusesAtom update order ([316bac5](https://github.com/artalar/reatom/commit/316bac55d18cdd24c743e3e6e6eb5645b2ebf5c6)) -* **async:** withAbort + withRetry ([d5190ab](https://github.com/artalar/reatom/commit/d5190ab87d57e3925652e07c0741c2938b80cd39)) +- **async:** improve withStatusesAtom update order ([316bac5](https://github.com/artalar/reatom/commit/316bac55d18cdd24c743e3e6e6eb5645b2ebf5c6)) +- **async:** withAbort + withRetry ([d5190ab](https://github.com/artalar/reatom/commit/d5190ab87d57e3925652e07c0741c2938b80cd39)) ## [3.10.1](https://github.com/artalar/reatom/compare/async-v3.10.0...async-v3.10.1) (2023-10-23) - ### Bug Fixes -* **async:** allow empty deps for reatomResource ([47c9a2e](https://github.com/artalar/reatom/commit/47c9a2e4350522ca39af3c5c4cf848a1ad5f967a)) +- **async:** allow empty deps for reatomResource ([47c9a2e](https://github.com/artalar/reatom/commit/47c9a2e4350522ca39af3c5c4cf848a1ad5f967a)) ## [3.10.0](https://github.com/artalar/reatom/compare/async-v3.9.4...async-v3.10.0) (2023-10-20) - ### Features -* **async:** deprecate reatomAsyncReaction in favor of reatomReactiveAsync ([678adf2](https://github.com/artalar/reatom/commit/678adf2b337b6a895f94e5997739274822332c4f)) - +- **async:** deprecate reatomAsyncReaction in favor of reatomReactiveAsync ([678adf2](https://github.com/artalar/reatom/commit/678adf2b337b6a895f94e5997739274822332c4f)) ### Bug Fixes -* **async:** do not cache aborted promise ([1b6fbe8](https://github.com/artalar/reatom/commit/1b6fbe83ae10a0b170996a16c272a1783d77cb39)) +- **async:** do not cache aborted promise ([1b6fbe8](https://github.com/artalar/reatom/commit/1b6fbe83ae10a0b170996a16c272a1783d77cb39)) ## [3.9.4](https://github.com/artalar/reatom/compare/async-v3.9.3...async-v3.9.4) (2023-10-09) - ### Bug Fixes -* **async:** reatomAsyncReaction direct call ([e88b96c](https://github.com/artalar/reatom/commit/e88b96cadfe40ff25432d6bb5d69cbbc20ce67f8)) -* **async:** retry abort ([7722626](https://github.com/artalar/reatom/commit/7722626effd408f81a6a1864ce12faa43a617581)) +- **async:** reatomAsyncReaction direct call ([e88b96c](https://github.com/artalar/reatom/commit/e88b96cadfe40ff25432d6bb5d69cbbc20ce67f8)) +- **async:** retry abort ([7722626](https://github.com/artalar/reatom/commit/7722626effd408f81a6a1864ce12faa43a617581)) ## [3.9.3](https://github.com/artalar/reatom/compare/async-v3.9.2...async-v3.9.3) (2023-09-26) - ### Bug Fixes -* **async:** abort should not stale for reatomAsyncReaction ([2a7e272](https://github.com/artalar/reatom/commit/2a7e2720bb5e8a681128eac161535e350fc04c59)) -* **async:** prevent unhandled exception for reatomAsyncReaction ([4b940ae](https://github.com/artalar/reatom/commit/4b940aee188ccfcb66a137834f5b877b24321e00)) +- **async:** abort should not stale for reatomAsyncReaction ([2a7e272](https://github.com/artalar/reatom/commit/2a7e2720bb5e8a681128eac161535e350fc04c59)) +- **async:** prevent unhandled exception for reatomAsyncReaction ([4b940ae](https://github.com/artalar/reatom/commit/4b940aee188ccfcb66a137834f5b877b24321e00)) ## [3.9.2](https://github.com/artalar/reatom/compare/async-v3.9.1...async-v3.9.2) (2023-09-13) - ### Bug Fixes -* **async:** reatomAsyncReaction withCache ([3bfee9d](https://github.com/artalar/reatom/commit/3bfee9de70e5c645979e81abb6edacde3e8ecac7)) +- **async:** reatomAsyncReaction withCache ([3bfee9d](https://github.com/artalar/reatom/commit/3bfee9de70e5c645979e81abb6edacde3e8ecac7)) ## [3.9.1](https://github.com/artalar/reatom/compare/async-v3.9.0...async-v3.9.1) (2023-09-13) - ### Bug Fixes -* **async:** promiseAtomConnection ([cc3b707](https://github.com/artalar/reatom/commit/cc3b707312ba504e8853facf5dce3dbbfafd4bbe)) +- **async:** promiseAtomConnection ([cc3b707](https://github.com/artalar/reatom/commit/cc3b707312ba504e8853facf5dce3dbbfafd4bbe)) ## [3.9.0](https://github.com/artalar/reatom/compare/async-v3.8.3...async-v3.9.0) (2023-09-13) - ### Features -* **async:** add reatomAsyncReaction ([e44e641](https://github.com/artalar/reatom/commit/e44e6417b5795c380e8c2e5dd1e576e7a6462bc0)) - +- **async:** add reatomAsyncReaction ([e44e641](https://github.com/artalar/reatom/commit/e44e6417b5795c380e8c2e5dd1e576e7a6462bc0)) ### Bug Fixes -* **async:** reatomAsyncReaction AsyncCtxSpy ([c0d014d](https://github.com/artalar/reatom/commit/c0d014d987cf1b3081133b45a146c30e107fb063)) +- **async:** reatomAsyncReaction AsyncCtxSpy ([c0d014d](https://github.com/artalar/reatom/commit/c0d014d987cf1b3081133b45a146c30e107fb063)) ## [3.8.3](https://github.com/artalar/reatom/compare/async-v3.8.2...async-v3.8.3) (2023-08-08) - ### Bug Fixes -* **async:** add mapFulfill to dataAtom for correct cache init ([994c0ff](https://github.com/artalar/reatom/commit/994c0ff483c0c8aafea276bc4bc6006bc7b5a1b9)) +- **async:** add mapFulfill to dataAtom for correct cache init ([994c0ff](https://github.com/artalar/reatom/commit/994c0ff483c0c8aafea276bc4bc6006bc7b5a1b9)) ## [3.8.2](https://github.com/artalar/reatom/compare/async-v3.8.1...async-v3.8.2) (2023-07-22) - ### Bug Fixes -* **async:** withStatusesAtom missing ctx ([9c12a7c](https://github.com/artalar/reatom/commit/9c12a7c74dcb2ac5394251e9e0a7fd3c3896d42a)) +- **async:** withStatusesAtom missing ctx ([9c12a7c](https://github.com/artalar/reatom/commit/9c12a7c74dcb2ac5394251e9e0a7fd3c3896d42a)) ## [3.8.1](https://github.com/artalar/reatom/compare/async-v3.8.0...async-v3.8.1) (2023-07-12) - ### Bug Fixes -* **async:** errorAtom AtomMut ([774ebc0](https://github.com/artalar/reatom/commit/774ebc0c649cac50073c0bdeae47a27780577c5c)) +- **async:** errorAtom AtomMut ([774ebc0](https://github.com/artalar/reatom/commit/774ebc0c649cac50073c0bdeae47a27780577c5c)) ## [3.8.0](https://github.com/artalar/reatom/compare/async-v3.7.0...async-v3.8.0) (2023-07-07) - ### Features -* **async:** add "ignoreAbort" and refactor withCache ([f404abf](https://github.com/artalar/reatom/commit/f404abfa36e91db9d109094eee672098ad7c6536)) - +- **async:** add "ignoreAbort" and refactor withCache ([f404abf](https://github.com/artalar/reatom/commit/f404abfa36e91db9d109094eee672098ad7c6536)) ### Bug Fixes -* **async:** shallow equal memo for withStatusesAtom ([8042e18](https://github.com/artalar/reatom/commit/8042e18dc0d38844628c73ba6a7a2bb2beaf4256)) -* **async:** withStatusesAtom and SWR ([b8d2798](https://github.com/artalar/reatom/commit/b8d2798a29b37cbb5f3a441adf4ca332a449ace9)) +- **async:** shallow equal memo for withStatusesAtom ([8042e18](https://github.com/artalar/reatom/commit/8042e18dc0d38844628c73ba6a7a2bb2beaf4256)) +- **async:** withStatusesAtom and SWR ([b8d2798](https://github.com/artalar/reatom/commit/b8d2798a29b37cbb5f3a441adf4ca332a449ace9)) ## [3.7.0](https://github.com/artalar/reatom/compare/async-v3.6.1...async-v3.7.0) (2023-06-12) - ### Features -* **async:** refactor withCache, add shouldFulfill option for SWR ([eb54d34](https://github.com/artalar/reatom/commit/eb54d34598ad48dd51ee21cfd2e3c0964bfdc7ae)) +- **async:** refactor withCache, add shouldFulfill option for SWR ([eb54d34](https://github.com/artalar/reatom/commit/eb54d34598ad48dd51ee21cfd2e3c0964bfdc7ae)) ## [3.6.1](https://github.com/artalar/reatom/compare/async-v3.6.0...async-v3.6.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** withCache and withAbort same params ([0466036](https://github.com/artalar/reatom/commit/0466036a2f6cf1dbc3a2f8ce70a5a586825fba85)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** withCache and withAbort same params ([0466036](https://github.com/artalar/reatom/commit/0466036a2f6cf1dbc3a2f8ce70a5a586825fba85)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.6.0](https://github.com/artalar/reatom/compare/async-v3.5.2...async-v3.6.0) (2023-05-10) - ### Features -* **async:** add withPersist for withCache ([dab74e9](https://github.com/artalar/reatom/commit/dab74e964d8092387e9bcfb24f3724cb088ec38c)) +- **async:** add withPersist for withCache ([dab74e9](https://github.com/artalar/reatom/commit/dab74e964d8092387e9bcfb24f3724cb088ec38c)) ## [3.5.2](https://github.com/artalar/reatom/compare/async-v3.5.1...async-v3.5.2) (2023-04-18) - ### Bug Fixes -* **async:** withCache for reject ([034d5ec](https://github.com/artalar/reatom/commit/034d5ec5cc4e7581083707275a6c3dd83f1507e0)) +- **async:** withCache for reject ([034d5ec](https://github.com/artalar/reatom/commit/034d5ec5cc4e7581083707275a6c3dd83f1507e0)) ## [3.5.1](https://github.com/artalar/reatom/compare/async-v3.5.0...async-v3.5.1) (2023-04-10) - ### Bug Fixes -* **async:** [#489](https://github.com/artalar/reatom/issues/489) error in console ([#534](https://github.com/artalar/reatom/issues/534)) ([2f75da5](https://github.com/artalar/reatom/commit/2f75da59325062c05168199a0c247da79fd3fc38)) +- **async:** [#489](https://github.com/artalar/reatom/issues/489) error in console ([#534](https://github.com/artalar/reatom/issues/534)) ([2f75da5](https://github.com/artalar/reatom/commit/2f75da59325062c05168199a0c247da79fd3fc38)) diff --git a/packages/async/README.md b/packages/async/README.md index 4fd4c1690..c50db70b3 100644 --- a/packages/async/README.md +++ b/packages/async/README.md @@ -52,23 +52,20 @@ import { reatomAsync } from '@reatom/async' const listAtom = atom([]) const errorAtom = atom(null) -export const fetchList = reatomAsync( - (ctx, page: number) => request(`/api/list?page=${page}`, ctx.controller), - { - name: 'fetchList', - onFulfill(ctx, result) { - listAtom(ctx, result) - }, - onReject(ctx, error) { - errorAtom(ctx, error) - }, - onEffect(ctx, params, promise) { - // clear outdated data on request start - listAtom(ctx, []) - errorAtom(ctx, null) - }, +export const fetchList = reatomAsync((ctx, page: number) => request(`/api/list?page=${page}`, ctx.controller), { + name: 'fetchList', + onFulfill(ctx, result) { + listAtom(ctx, result) }, -) + onReject(ctx, error) { + errorAtom(ctx, error) + }, + onEffect(ctx, params, promise) { + // clear outdated data on request start + listAtom(ctx, []) + errorAtom(ctx, null) + }, +}) ``` ### Qualified usage @@ -88,10 +85,7 @@ type Element = { export const listAtom = atom(new Array(), 'listAtom') export const errorAtom = atom(null, 'errorAtom') // if number of pending requests are equal or more than 1 - there is a loading state -export const isLoadingAtom = atom( - (ctx) => ctx.spy(fetchList.pendingAtom) > 0, - 'isLoadingAtom', -) +export const isLoadingAtom = atom((ctx) => ctx.spy(fetchList.pendingAtom) > 0, 'isLoadingAtom') // store abort controller of last request to prevent race conditions const abortControllerAtom = atom(new AbortController()) const ABORT = 'ABORT' @@ -112,14 +106,11 @@ fetchList.onReject.onCall((ctx, thing) => { } }) -export const updateElement = reatomAsync( - (ctx, id: string, slice: Partial) => { - const { signal } = ctx.controller - const data = JSON.stringify(slice) - return request(`/api/list/${id}`, { method: 'POST', data, signal }) - }, - 'updateElement', -) +export const updateElement = reatomAsync((ctx, id: string, slice: Partial) => { + const { signal } = ctx.controller + const data = JSON.stringify(slice) + return request(`/api/list/${id}`, { method: 'POST', data, signal }) +}, 'updateElement') // refresh backend data on successful update updateElement.onFulfill.onCall((ctx) => fetchList(ctx, 1)) ``` @@ -142,20 +133,16 @@ type Element = { } export const fetchList = reatomAsync( - (ctx, page: number) => - request>(`/api/list?page=${page}`, ctx.controller), + (ctx, page: number) => request>(`/api/list?page=${page}`, ctx.controller), 'fetchList', // add extra handlers with full type inference ).pipe(withDataAtom([]), withErrorAtom(), withAbort(), withStatusesAtom()) -export const updateElement = reatomAsync( - (ctx, id: string, slice: Partial) => { - const { signal } = ctx.controller - const data = JSON.stringify(slice) - return request(`/api/list/${id}`, { method: 'POST', data, signal }) - }, - 'updateElement', -) +export const updateElement = reatomAsync((ctx, id: string, slice: Partial) => { + const { signal } = ctx.controller + const data = JSON.stringify(slice) + return request(`/api/list/${id}`, { method: 'POST', data, signal }) +}, 'updateElement') updateElement.onFulfill.onCall((ctx) => fetchList(ctx, 1)) ``` @@ -178,19 +165,15 @@ type Feature = { /*...*/ } -export const fetchFeature = reatomAsync( - (ctx) => request('/api/feature', ctx.controller), - 'fetchFeature', -).pipe(withDataAtom(null)) +export const fetchFeature = reatomAsync((ctx) => request('/api/feature', ctx.controller), 'fetchFeature').pipe( + withDataAtom(null), +) // use subscription to `fetchFeature.dataAtom` to get the actual data // mutate data manually in the feature form -export const changeFeature = action( - (ctx, property: keyof Feature, value: any) => { - fetchUser.dataAtom(ctx, (user) => ({ ...user, [property]: value })) - }, - 'changeFeature', -) +export const changeFeature = action((ctx, property: keyof Feature, value: any) => { + fetchUser.dataAtom(ctx, (user) => ({ ...user, [property]: value })) +}, 'changeFeature') // save new feature data to backend on form submit export const syncFeature = reatomAsync((ctx) => { @@ -214,10 +197,7 @@ Here how you can fetch data declaratively and lazy only when needed. This is a s import { reatomAsync, withDataAtom } from '@reatom/async' import { onConnect } from '@reatom/hooks' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) onConnect(fetchList.dataAtom, fetchList) ``` @@ -240,10 +220,7 @@ import { reatomAsync, withDataAtom } from '@reatom/async' const PAGE_SIZE = 10 export const fetchFeed = reatomAsync(async (ctx, page: number) => { - const data = await request( - `api/feed?page=${page}&limit?${page}`, - ctx.controller, - ) + const data = await request(`api/feed?page=${page}&limit?${page}`, ctx.controller) return { data, page } }, 'fetchFeed').pipe( withDataAtom([], (ctx, { data, page }, state) => { @@ -261,10 +238,7 @@ You could describe optimistic async logic easily with `onEffect` handler, which ```ts import { reatomAsync, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) export const updateList = reatomAsync( (ctx, newList) => { @@ -297,10 +271,7 @@ export type Element = { // ... } -export const fetchList = reatomAsync( - (ctx) => request>('api/list', ctx.controller), - 'fetchList', -) +export const fetchList = reatomAsync((ctx) => request>('api/list', ctx.controller), 'fetchList') export const listAtom = reatomArray(new Array(), 'listAtom') fetchList.onFulfill.onCall(listAtom) ``` @@ -311,9 +282,7 @@ Here the interface of `onFulfill` update hook and `listAtom` update is the same import { reatomMap } from '@reatom/primitives' // .... export const mapAtom = reatomMap(new Map(), 'mapAtom') -fetchList.onFulfill.onCall((ctx, payload) => - mapAtom(ctx, new Map(payload.map((el) => [el.id, el]))), -) +fetchList.onFulfill.onCall((ctx, payload) => mapAtom(ctx, new Map(payload.map((el) => [el.id, el])))) ``` ## withErrorAtom @@ -325,16 +294,10 @@ You could update the error atom manually as a usual atom: `fetchList.errorAtom(c ```ts import { reatomAsync, withErrorAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withErrorAtom( // optional mapper - (ctx, error) => - error instanceof Response - ? error.status - : error?.message || 'unknown error', + (ctx, error) => (error instanceof Response ? error.status : error?.message || 'unknown error'), ), ) ``` @@ -346,10 +309,7 @@ Adds property `statusesAtom` with additional statuses, which updates by the effe ```ts import { reatomAsync, withStatusesAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withStatusesAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withStatusesAtom()) // ... @@ -379,9 +339,7 @@ onDisconnect(fetchList.dataAtom, (ctx) => { You could import special types of statuses of each effect state and use it for typesafe conditional logic. ```ts -export type AsyncStatusesPending = - | AsyncStatusesFirstPending - | AsyncStatusesAnotherPending +export type AsyncStatusesPending = AsyncStatusesFirstPending | AsyncStatusesAnotherPending export type AsyncStatuses = | AsyncStatusesNeverPending @@ -416,10 +374,10 @@ You could rule the cache behavior by set of optional parameters. ```ts import { reatomAsync, withDataAtom, withCache } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom(), withCache()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withDataAtom(), + withCache(), +) // fetch data await fetchList(ctx, { query: 'foo', page: 1 }) // call the effect @@ -441,10 +399,10 @@ You can invalidate the cache by `reset` action on `cacheAtom`. It will clear the ```ts import { reatomAsync, withCache, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), +) export const updateList = reatomAction(async () => { /* */ @@ -457,19 +415,17 @@ You can use `withRetry` to retry the effect after cache invalidation or use buil ```ts import { reatomAsync, withCache, withDataAtom } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), +) export const updateList = reatomAction(async () => { /* */ }, 'updateList') updateList.onFulfill.onCall(fetchList.cacheAtom.invalidate) -export const listLoadingAtom = atom( - (ctx) => ctx.spy(fetchList.pendingAtom) + ctx.spy(updateList.pendingAtom) > 0, -) +export const listLoadingAtom = atom((ctx) => ctx.spy(fetchList.pendingAtom) + ctx.spy(updateList.pendingAtom) > 0) ``` Use `listLoadingAtom` to show a loader in a UI during the whole process of data updating and invalidation. @@ -510,10 +466,9 @@ You could persist the cache for a chosen time and sync it across a tabs by `with import { reatomAsync, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache({ withPersist: withLocalStorage })) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache({ withPersist: withLocalStorage }), +) ``` `withCache` applies `withPersist` to `cacheAtom` with options for optimal serialization. You could redefine the options by an inline decorator function. It is recommended to set the key explicitly, by default the async action name used. @@ -522,13 +477,9 @@ export const fetchList = reatomAsync( import { reatomAsync, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withCache({ - withPersist: (options) => - withLocalStorage({ ...options, key: 'LIST_CACHE' }), + withPersist: (options) => withLocalStorage({ ...options, key: 'LIST_CACHE' }), }), ) ``` @@ -539,10 +490,10 @@ If you want to use persisted cache as an init state of `dataAtom` - just put `wi import { reatomAsync, withDataAtom, withCache } from '@reatom/async' import { withLocalStorage } from '@reatom/persist-web-storage' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([]), withCache({ withPersist })) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withDataAtom([]), + withCache({ withPersist }), +) ``` ## withRetry @@ -552,10 +503,11 @@ Adds `retry` action and `paramsAtom` to store last params of the effect call. ```ts import { reatomAsync, withCache, withDataAtom, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withCache(), withDataAtom(), withRetry()) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withCache(), + withDataAtom(), + withRetry(), +) export const updateList = reatomAction(async () => { /* */ @@ -569,10 +521,9 @@ If you will try to call `retry` before first effect call, it will throw an error ```ts import { reatomAsync, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx, page) => request(`api/list?page=${page}`, ctx.controller), - 'fetchList', -).pipe(withRetry({ fallbackParams: [1] })) +export const fetchList = reatomAsync((ctx, page) => request(`api/list?page=${page}`, ctx.controller), 'fetchList').pipe( + withRetry({ fallbackParams: [1] }), +) // will call fetch(`api/list?page=1`) fetchList.retry(ctx) @@ -606,17 +557,13 @@ Progressive retry: `100 * Math.min(200, retries ** 3)`. Will retry after 100ms, ```ts import { atom, reatomAsync, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withRetry({ onReject: (ctx, error, retries) => 100 * Math.min(200, retries ** 3), }), withAssign((target, name) => ({ loadingAtom: atom( - (ctx) => - ctx.spy(target.pendingAtom) > 0 || ctx.spy(target.retriesAtom) > 0, + (ctx) => ctx.spy(target.pendingAtom) > 0 || ctx.spy(target.retriesAtom) > 0, `${name}.loadingAtom`, ), })), @@ -626,18 +573,9 @@ export const fetchList = reatomAsync( Note that `retriesAtom` will drop to `0` when any promise resolves successfully or when you return `undefined` or a negative number. So, it is good practice to avoid calling multiple async actions in parallel. If you are using `withRetry`, it is recommended to always use it with [withAbort](#withabort) (with the default 'last-in-win' strategy). ```ts -import { - atom, - reatomAsync, - withAbort, - withErrorAtom, - withRetry, -} from '@reatom/async' +import { atom, reatomAsync, withAbort, withErrorAtom, withRetry } from '@reatom/async' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe( +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( withAbort(), withRetry({ onReject: (ctx, error, retries) => { @@ -651,8 +589,7 @@ export const fetchList = reatomAsync( withErrorAtom(), ) export const isFetchListLoading = atom( - (ctx) => - ctx.spy(fetchList.pendingAtom) > 0 || ctx.spy(fetchList.retriesAtom) > 0, + (ctx) => ctx.spy(fetchList.pendingAtom) > 0 || ctx.spy(fetchList.retriesAtom) > 0, 'isFetchListLoading', ) ``` @@ -662,13 +599,7 @@ export const isFetchListLoading = atom( Do you need to implement a **pooling** pattern to stay your data fresh? Lets use `onConnect` from [@reatom/hooks](https://www.reatom.dev/package/hooks) to control the neediness of this. ```ts -import { - reatomAsync, - withDataAtom, - withRetry, - onConnect, - sleep, -} from '@reatom/framework' +import { reatomAsync, withDataAtom, withRetry, onConnect, sleep } from '@reatom/framework' export const fetchList = reatomAsync( (ctx, search: string) => request(`/api/list?q=${search}`, ctx.controller), @@ -706,10 +637,11 @@ To be clear, you don't need to use `retry`, if you have no need to manage parame ```ts import { reatomAsync, withAbort, withDataAtom, sleep } from '@reatom/framework' -export const fetchList = reatomAsync( - (ctx) => request('/api/list', ctx.controller), - 'fetchList', -).pipe(withAbort(), withDataAtom([]), withRetry()) +export const fetchList = reatomAsync((ctx) => request('/api/list', ctx.controller), 'fetchList').pipe( + withAbort(), + withDataAtom([]), + withRetry(), +) onConnect(fetchList.dataAtom, async (ctx) => { while (true) { await fetchList(ctx).catch(() => {}) @@ -779,12 +711,8 @@ onConnect(fetchList.dataAtom, (ctx) => { return () => fetchList.abort(ctx) }) // trigger -pageAtom.onChange((ctx, page) => - fetchSuggestion(ctx, page, ctx.get(searchAtom)), -) -searchAtom.onChange((ctx, search) => - fetchSuggestion(ctx, ctx.get(pageAtom), search), -) +pageAtom.onChange((ctx, page) => fetchSuggestion(ctx, page, ctx.get(searchAtom))) +searchAtom.onChange((ctx, search) => fetchSuggestion(ctx, ctx.get(pageAtom), search)) ``` There are a lot of boilerplates. `reatomResource` is a fabric method that encapsulates all this logic and allows you to use `ctx.spy` just like in the regular `atom`. It is much simpler, more intuitive, and works automatically for both caching and cancelling previous requests. @@ -795,9 +723,7 @@ import { reatomResource, withDataAtom } from '@reatom/async' const listResource = reatomResource(async (ctx) => { const page = ctx.spy(pageAtom) const search = ctx.spy(searchAtom) - return await ctx.schedule(() => - request(`/api/list?page=${page}&q=${search}`, ctx.controller), - ) + return await ctx.schedule(() => request(`/api/list?page=${page}&q=${search}`, ctx.controller)) }, 'listResource').pipe(withDataAtom([])) ``` @@ -814,9 +740,7 @@ import { reatomResource } from '@reatom/async' const aResource = reatomResource(async (ctx) => { const page = ctx.spy(pageAtom) - return await ctx.schedule(() => - request(`/api/a?page=${page}`, ctx.controller), - ) + return await ctx.schedule(() => request(`/api/a?page=${page}`, ctx.controller)) }, 'aResource') const bResource = reatomResource(async (ctx) => { const a = await ctx.spy(aResource.promiseAtom) diff --git a/packages/async/src/handleEffect.ts b/packages/async/src/handleEffect.ts index 19706039e..b4b66df62 100644 --- a/packages/async/src/handleEffect.ts +++ b/packages/async/src/handleEffect.ts @@ -24,9 +24,7 @@ export const handleEffect = ( new Promise((res, rej) => { throwIfAborted(ctx.controller) effect(...params).then(res, rej) - ctx.controller.signal.addEventListener('abort', () => - rej(toAbortError(ctx.controller.signal.reason)), - ) + ctx.controller.signal.addEventListener('abort', () => rej(toAbortError(ctx.controller.signal.reason))) }), ) diff --git a/packages/async/src/index.story.test.ts b/packages/async/src/index.story.test.ts index 32729d94c..ac330715a 100644 --- a/packages/async/src/index.story.test.ts +++ b/packages/async/src/index.story.test.ts @@ -36,18 +36,14 @@ describe('optimistic update', () => { const getData = reatomAsync.from(api.getData).pipe( // add `dataAtom` and map the effect payload into it // try to prevent new reference stream if nothing really changed - withDataAtom([], (ctx, payload, state) => - isDeepEqual(payload, state) ? state : payload, - ), + withDataAtom([], (ctx, payload, state) => (isDeepEqual(payload, state) ? state : payload)), ) const putData = reatomAsync.from(api.putData) putData.onCall((ctx, promise, params) => { const [id, value] = params const oldList = ctx.get(getData.dataAtom) // optimistic update - const newList = getData.dataAtom(ctx, (state) => - state.map((item) => (item.id === id ? { ...item, value } : item)), - ) + const newList = getData.dataAtom(ctx, (state) => state.map((item) => (item.id === id ? { ...item, value } : item))) // rollback on error promise.catch((error) => { if (ctx.get(getData.dataAtom) === newList) { @@ -153,9 +149,7 @@ describe('concurrent pooling', () => { assert.is(ctx.get(progressAtom), 100) - const expectedProgress = [ - 0, 10, /* start again */ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, - ] + const expectedProgress = [0, 10, /* start again */ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] // assert.equal(track.inputs(), expectedProgress) ;`πŸ‘` //? diff --git a/packages/async/src/index.test.ts b/packages/async/src/index.test.ts index d413e56f9..23f3321c9 100644 --- a/packages/async/src/index.test.ts +++ b/packages/async/src/index.test.ts @@ -7,13 +7,7 @@ import { onConnect } from '@reatom/hooks' import { createTestCtx, mockFn } from '@reatom/testing' import { noop, random, sleep } from '@reatom/utils' -import { - reatomAsync, - withAbort, - withDataAtom, - withRetry, - withErrorAtom, -} from './' +import { reatomAsync, withAbort, withDataAtom, withRetry, withErrorAtom } from './' import './index.story.test' import './withCache.test' @@ -123,9 +117,7 @@ test('withAbort', async () => { const ctx = createTestCtx() - const valueTrack = ctx.subscribeTrack( - a1.pipe(mapPayloadAwaited((ctx, v) => v)), - ) + const valueTrack = ctx.subscribeTrack(a1.pipe(mapPayloadAwaited((ctx, v) => v))) const errorTrack = ctx.subscribeTrack(a1.onReject) const abortTrack = ctx.subscribeTrack(a1.onAbort) @@ -165,9 +157,7 @@ test('withAbort user abort', async () => { const ctx = createTestCtx() - const valueSubscriber = ctx.subscribeTrack( - a1.pipe(mapPayloadAwaited((ctx, v) => v)), - ) + const valueSubscriber = ctx.subscribeTrack(a1.pipe(mapPayloadAwaited((ctx, v) => v))) valueSubscriber.calls.length = 0 const errorSubscriber = ctx.subscribeTrack(a1.onReject) errorSubscriber.calls.length = 0 @@ -198,15 +188,13 @@ test('withAbort and real fetch', async () => { const handleError = mockFn((e) => { throw e }) - const fetchData = reatomAsync((ctx) => - fetch('https://www.google.ru/404', ctx.controller).catch(handleError), - ).pipe(withAbort()) + const fetchData = reatomAsync((ctx) => fetch('https://www.google.ru/404', ctx.controller).catch(handleError)).pipe( + withAbort(), + ) const ctx = createTestCtx() - const cb = ctx.subscribeTrack( - fetchData.pipe(mapPayloadAwaited((ctx, resp) => resp.status)), - ) + const cb = ctx.subscribeTrack(fetchData.pipe(mapPayloadAwaited((ctx, resp) => resp.status))) assert.is(cb.calls.length, 1) assert.is(handleError.calls.length, 0) @@ -261,9 +249,7 @@ test('hooks', async () => { }) test('onConnect', async () => { - const fetchData = reatomAsync(async (ctx, payload: number) => payload).pipe( - withDataAtom(0), - ) + const fetchData = reatomAsync(async (ctx, payload: number) => payload).pipe(withDataAtom(0)) const ctx = createTestCtx() onConnect(fetchData.dataAtom, (ctx) => fetchData(ctx, 123)) const track = ctx.subscribeTrack(fetchData.dataAtom) @@ -277,10 +263,7 @@ test('withErrorAtom resetTrigger', async () => { const effect = reatomAsync(async () => { if (1) throw 42 return 42 - }).pipe( - withDataAtom(), - withErrorAtom(undefined, { resetTrigger: 'dataAtom' }), - ) + }).pipe(withDataAtom(), withErrorAtom(undefined, { resetTrigger: 'dataAtom' })) const ctx = createTestCtx() await effect(ctx).catch(noop) diff --git a/packages/async/src/index.ts b/packages/async/src/index.ts index be0ac7ed5..5920c1d95 100644 --- a/packages/async/src/index.ts +++ b/packages/async/src/index.ts @@ -42,15 +42,9 @@ export type { AsyncStatuses, AsyncStatusesAtom, } from './withStatusesAtom' -export { - reatomAsyncReaction, - AsyncReaction, - ResourceAtom, - reatomResource, -} from './reatomResource' - -export interface AsyncAction - extends Action> { +export { reatomAsyncReaction, AsyncReaction, ResourceAtom, reatomResource } from './reatomResource' + +export interface AsyncAction extends Action> { onFulfill: Action<[Resp], Resp> onReject: Action<[unknown], unknown> onSettle: Action<[], void> @@ -90,10 +84,7 @@ const getCache = (ctx: Ctx, anAtom: Atom): AtomCache => { return anAtom.__reatom.patch ?? ctx.get((read) => read(anAtom.__reatom))! } -export const reatomAsync = < - Params extends [AsyncCtx, ...any[]] = [AsyncCtx, ...any[]], - Resp = any, ->( +export const reatomAsync = ( effect: Fn>, options: string | AsyncOptions, Resp> = {}, ): AsyncAction, Resp> => { @@ -103,9 +94,7 @@ export const reatomAsync = < onFulfill: onFulfillHook, onReject: onRejectHook, onSettle: onSettleHook, - } = typeof options === 'string' - ? ({ name: options } as AsyncOptions, Resp>) - : options + } = typeof options === 'string' ? ({ name: options } as AsyncOptions, Resp>) : options const pendingAtom = atom(0, `${name}.pendingAtom`) @@ -114,43 +103,36 @@ export const reatomAsync = < // do not put this function inside `action` to not broke effect mocking (...params: Params) => params[0].get((read, actualize) => { - const { state } = actualize!( - params[0], - theAsync.__reatom, - (ctx: AsyncCtx, patch: AtomCache) => { - // userspace controller - abortCauseContext.set( - ctx.cause, - (ctx.controller = new AbortController()), - ) - - const unabort = onCtxAbort(params[0], (error) => { - // prevent unhandled error for abort - payload?.catch(noop) - ctx.controller.abort(error) - }) - if (unabort) { - ctx.controller.signal.addEventListener('abort', unabort) + const { state } = actualize!(params[0], theAsync.__reatom, (ctx: AsyncCtx, patch: AtomCache) => { + // userspace controller + abortCauseContext.set(ctx.cause, (ctx.controller = new AbortController())) + + const unabort = onCtxAbort(params[0], (error) => { + // prevent unhandled error for abort + payload?.catch(noop) + ctx.controller.abort(error) + }) + if (unabort) { + ctx.controller.signal.addEventListener('abort', unabort) + } + + params[0] = withAbortableSchedule(ctx) + + var payload = theAsync._handleCache + ? // @ts-expect-error + theAsync._handleCache(...params) + : handleEffect(theAsync, params) + + // prevent ERR_UNHANDLED_REJECTION if the onReject has any handlers + __thenReatomed(ctx, payload, undefined, () => { + // One hook is for "onSettle" + if (onReject.__reatom.updateHooks!.size > 1) { + payload.catch(noop) } + }) - params[0] = withAbortableSchedule(ctx) - - var payload = theAsync._handleCache - ? // @ts-expect-error - theAsync._handleCache(...params) - : handleEffect(theAsync, params) - - // prevent ERR_UNHANDLED_REJECTION if the onReject has any handlers - __thenReatomed(ctx, payload, undefined, () => { - // One hook is for "onSettle" - if (onReject.__reatom.updateHooks!.size > 1) { - payload.catch(noop) - } - }) - - patch.state = [...patch.state, { params: params.slice(1), payload }] - }, - ) + patch.state = [...patch.state, { params: params.slice(1), payload }] + }) return state[state.length - 1]!.payload }), action( @@ -168,9 +150,7 @@ export const reatomAsync = < onReject.onCall((ctx) => onSettle(ctx)) if (onEffectHook) { - theAsync.onCall((ctx, promise, params) => - onEffectHook(ctx, params as any, promise), - ) + theAsync.onCall((ctx, promise, params) => onEffectHook(ctx, params as any, promise)) } if (onFulfillHook) onFulfill.onCall(onFulfillHook) if (onRejectHook) onReject.onCall(onRejectHook) @@ -232,10 +212,7 @@ export const withDataAtom: { }, >( initState: AsyncResp, - map?: Fn< - [ctx: Ctx, payload: AsyncResp, state: AsyncResp], - AsyncResp - >, + map?: Fn<[ctx: Ctx, payload: AsyncResp, state: AsyncResp], AsyncResp>, ): Fn<[T], T & { dataAtom: AsyncDataAtom> }> < T extends AsyncAction & { @@ -284,37 +261,24 @@ export const withErrorAtom = Err = Error, EmptyError = undefined, >( - parseError: Fn<[Ctx, unknown], Err> = (ctx, e) => - (e instanceof Error ? e : new Error(String(e))) as Err, + parseError: Fn<[Ctx, unknown], Err> = (ctx, e) => (e instanceof Error ? e : new Error(String(e))) as Err, { initState, resetTrigger = 'onEffect', }: { initState?: EmptyError - resetTrigger?: - | null - | 'onEffect' - | 'onFulfill' - | ('dataAtom' extends keyof T ? 'dataAtom' : null) + resetTrigger?: null | 'onEffect' | 'onFulfill' | ('dataAtom' extends keyof T ? 'dataAtom' : null) } = {}, - ): Fn< - [T], - T & { errorAtom: AtomMut & { reset: Action } } - > => + ): Fn<[T], T & { errorAtom: AtomMut & { reset: Action } }> => (anAsync) => { if (!anAsync.errorAtom) { const errorAtomName = `${anAsync.__reatom.name}.errorAtom` - const resetTriggerAtom = - resetTrigger && - ({ ...anAsync, onEffect: anAsync }[resetTrigger] as Atom) - const errorAtom = (anAsync.errorAtom = assign( - atom(initState as EmptyError | Err, errorAtomName), - { - reset: action((ctx) => { - errorAtom(ctx, initState as EmptyError) - }, `${errorAtomName}.reset`), - }, - )) + const resetTriggerAtom = resetTrigger && ({ ...anAsync, onEffect: anAsync }[resetTrigger] as Atom) + const errorAtom = (anAsync.errorAtom = assign(atom(initState as EmptyError | Err, errorAtomName), { + reset: action((ctx) => { + errorAtom(ctx, initState as EmptyError) + }, `${errorAtomName}.reset`), + })) errorAtom.__reatom.computer = (ctx, state) => { if (resetTriggerAtom) { ctx.spy(resetTriggerAtom, (ctx) => { @@ -349,9 +313,7 @@ export const withAbort = onAbort?: Action<[Error], Error> abortControllerAtom?: Atom }, - >({ - strategy = 'last-in-win', - }: { strategy?: 'none' | 'last-in-win' | 'first-in-win' } = {}): Fn< + >({ strategy = 'last-in-win' }: { strategy?: 'none' | 'last-in-win' | 'first-in-win' } = {}): Fn< [T], T & { abort: Action<[reason?: string], void> @@ -377,16 +339,12 @@ export const withAbort = const controller = state ctx.schedule(() => { - controller.abort( - toAbortError('concurrent request (last-in-win)'), - ) + controller.abort(toAbortError('concurrent request (last-in-win)')) }) } if (strategy === 'first-in-win' && state && !state.settled) { - promise.controller.abort( - toAbortError('concurrent request (first-in-win)'), - ) + promise.controller.abort(toAbortError('concurrent request (first-in-win)')) return } @@ -399,8 +357,7 @@ export const withAbort = state.signal.addEventListener('abort', handleAbort) - const removeAbortHandler = () => - state.signal.removeEventListener('abort', handleAbort) + const removeAbortHandler = () => state.signal.removeEventListener('abort', handleAbort) __thenReatomed(ctx, promise, removeAbortHandler, removeAbortHandler) }) @@ -454,13 +411,8 @@ export const withRetry = > => (anAsync) => { if (!anAsync.paramsAtom) { - const paramsAtom = (anAsync.paramsAtom = atom( - fallbackParams as Params, - `${anAsync.__reatom.name}._paramsAtom`, - )) - anAsync.onCall((ctx, payload, params) => - paramsAtom(ctx, params as Params), - ) + const paramsAtom = (anAsync.paramsAtom = atom(fallbackParams as Params, `${anAsync.__reatom.name}._paramsAtom`)) + anAsync.onCall((ctx, payload, params) => paramsAtom(ctx, params as Params)) anAsync.retry = action((ctx, after = 0): ActionPayload => { throwReatomError(after < 0, 'wrong timeout') @@ -485,10 +437,7 @@ export const withRetry = return anAsync(ctx, ...params!) as ActionPayload }, `${anAsync.__reatom.name}.retry`) - const retriesAtom = (anAsync.retriesAtom = atom( - 0, - `${anAsync.__reatom.name}.retriesAtom`, - )) + const retriesAtom = (anAsync.retriesAtom = atom(0, `${anAsync.__reatom.name}.retriesAtom`)) if (onReject) { anAsync.onCall((ctx, payload) => { @@ -536,10 +485,8 @@ export const withRetry = /** @deprecated use `withRetry` instead */ export const withRetryAction = withRetry -export interface MappedAsyncAction< - Params extends any[] = unknown[], - Payload = unknown, -> extends AsyncAction { +export interface MappedAsyncAction + extends AsyncAction { /** * Function that unsubscribes from source atom * @experimental diff --git a/packages/async/src/reatomResource.test.ts b/packages/async/src/reatomResource.test.ts index 579e3d93d..1ecd52125 100644 --- a/packages/async/src/reatomResource.test.ts +++ b/packages/async/src/reatomResource.test.ts @@ -141,7 +141,7 @@ test('withErrorAtom withRetry', async () => { throw new Error('test error') } await ctx.schedule(() => sleep()) - console.log('ctx.controller.signal.aborted', ctx.controller.signal.aborted); + console.log('ctx.controller.signal.aborted', ctx.controller.signal.aborted) return params }, 'someResource').pipe( withDataAtom(0), diff --git a/packages/async/src/reatomResource.ts b/packages/async/src/reatomResource.ts index eb52c80c0..47ef97b1a 100644 --- a/packages/async/src/reatomResource.ts +++ b/packages/async/src/reatomResource.ts @@ -1,21 +1,5 @@ -import { - Atom, - AtomCache, - Ctx, - Fn, - __count, - atom, - throwReatomError, - Unsubscribe, - AtomProto, -} from '@reatom/core' -import { - CauseContext, - __thenReatomed, - abortCauseContext, - onCtxAbort, - withAbortableSchedule, -} from '@reatom/effects' +import { Atom, AtomCache, Ctx, Fn, __count, atom, throwReatomError, Unsubscribe, AtomProto } from '@reatom/core' +import { CauseContext, __thenReatomed, abortCauseContext, onCtxAbort, withAbortableSchedule } from '@reatom/effects' import { merge, noop, toAbortError } from '@reatom/utils' import { reatomAsync, AsyncAction, ControlledPromise, AsyncCtx } from '.' @@ -70,9 +54,7 @@ export const reatomResource = ( if (unabort) controller.signal.addEventListener('abort', unabort) abortCauseContext.set(ctx.cause, (ctx.controller = controller)) - const computedPromise = asyncComputed( - withAbortableSchedule(ctx) as AsyncCtxSpy, - ) + const computedPromise = asyncComputed(withAbortableSchedule(ctx) as AsyncCtxSpy) computedPromise.catch(noop) promises.set(ctx.cause, computedPromise) @@ -97,10 +79,7 @@ export const reatomResource = ( const fulfillCalls = ctx.get(theAsync.onFulfill) if (cached) controller.abort(toAbortError('cached')) if (cached && fulfillCallsBefore !== fulfillCalls) { - promise = Object.assign( - Promise.resolve(fulfillCalls[fulfillCalls.length - 1]!.payload), - { controller }, - ) + promise = Object.assign(Promise.resolve(fulfillCalls[fulfillCalls.length - 1]!.payload), { controller }) } __thenReatomed( @@ -134,10 +113,7 @@ export const reatomResource = ( actualize!(ctx, promiseAtom.__reatom, noop) const state = ctx.get(theAsync) const payload = state[state.length - 1]?.payload - throwReatomError( - !payload, - 'unexpectedly failed invalidation. Please, report the issue', - ) + throwReatomError(!payload, 'unexpectedly failed invalidation. Please, report the issue') return payload! }), theAsync, diff --git a/packages/async/src/withCache.test.ts b/packages/async/src/withCache.test.ts index 03be22624..b5804a85e 100644 --- a/packages/async/src/withCache.test.ts +++ b/packages/async/src/withCache.test.ts @@ -11,9 +11,7 @@ import { reatomAsync, withAbort, withDataAtom, withCache, AsyncCtx } from './' const test = suite('withCache') test('withCache', async () => { - const fetchData = reatomAsync( - async (ctx, { a, b }: { a: number; b: number }) => a, - ).pipe(withDataAtom(0), withCache()) + const fetchData = reatomAsync(async (ctx, { a, b }: { a: number; b: number }) => a).pipe(withDataAtom(0), withCache()) const ctx = createTestCtx() await fetchData(ctx, { a: 400, b: 0 }) @@ -57,10 +55,7 @@ test('withCache dataAtom mapper', async () => { test('withCache swr true (default)', async () => { let i = 0 - const fetchData = reatomAsync((ctx) => Promise.resolve(++i)).pipe( - withDataAtom(0), - withCache(), - ) + const fetchData = reatomAsync((ctx) => Promise.resolve(++i)).pipe(withDataAtom(0), withCache()) const ctx = createTestCtx() const track = ctx.subscribeTrack(fetchData.dataAtom) @@ -139,11 +134,7 @@ test('withCache withAbort vary params', async () => { return n }) - const fetchData = reatomAsync(effect).pipe( - withDataAtom(0), - withCache(), - withAbort(), - ) + const fetchData = reatomAsync(effect).pipe(withDataAtom(0), withCache(), withAbort()) const ctx = createTestCtx() const track = ctx.subscribeTrack(fetchData.dataAtom) @@ -201,11 +192,7 @@ test('withCache withAbort same params', async () => { test('withCache and action mocking', async () => { const effect = mockFn(async (ctx: any, n: number) => n) - const fetchData = reatomAsync(effect).pipe( - withDataAtom(0), - withCache(), - withAbort(), - ) + const fetchData = reatomAsync(effect).pipe(withDataAtom(0), withCache(), withAbort()) const ctx = createTestCtx() ctx.mockAction(fetchData, async (ctx, n) => n * 10) @@ -257,10 +244,7 @@ test('do not cache aborted promise', async () => { ctx.controller.signal.throwIfAborted() return 1 }) - const fetchData = reatomAsync(effect).pipe( - withDataAtom(0), - withCache({ ignoreAbort: false }), - ) + const fetchData = reatomAsync(effect).pipe(withDataAtom(0), withCache({ ignoreAbort: false })) onConnect(fetchData.dataAtom, fetchData) const ctx = createTestCtx() @@ -281,10 +265,7 @@ test('do not cache aborted promise', async () => { test('should be able to manage cache manually', async () => { const effect = mockFn(async (ctx: any, n: number) => n) - const fetchData = reatomAsync(effect).pipe( - withDataAtom(0), - withCache({ swr: false }), - ) + const fetchData = reatomAsync(effect).pipe(withDataAtom(0), withCache({ swr: false })) const ctx = createTestCtx() fetchData(ctx, 1) diff --git a/packages/async/src/withCache.ts b/packages/async/src/withCache.ts index 7debe0cd5..d35facfa8 100644 --- a/packages/async/src/withCache.ts +++ b/packages/async/src/withCache.ts @@ -1,32 +1,12 @@ -import { - Action, - action, - ActionParams, - atom, - Atom, - AtomState, - Ctx, - Fn, -} from '@reatom/core' +import { Action, action, ActionParams, atom, Atom, AtomState, Ctx, Fn } from '@reatom/core' import { MapAtom, reatomMap, withAssign } from '@reatom/primitives' import { isDeepEqual, MAX_SAFE_TIMEOUT, setTimeout } from '@reatom/utils' import { type WithPersistOptions } from '@reatom/persist' -import { - AsyncAction, - AsyncCtx, - AsyncDataAtom, - AsyncResp, - ControlledPromise, -} from '.' +import { AsyncAction, AsyncCtx, AsyncDataAtom, AsyncResp, ControlledPromise } from '.' import { handleEffect } from './handleEffect' import { onConnect } from '@reatom/hooks' -import { - __thenReatomed, - abortCauseContext, - getTopController, - spawn, -} from '@reatom/effects' +import { __thenReatomed, abortCauseContext, getTopController, spawn } from '@reatom/effects' export interface CacheRecord { clearTimeoutId: ReturnType @@ -43,8 +23,7 @@ export interface CacheRecord { version: number } -export interface CacheAtom - extends MapAtom> { +export interface CacheAtom extends MapAtom> { /** Clear all records and call the effect with the last params. */ invalidate: Action<[], null | ControlledPromise> setWithParams: Action<[params: Params, value: T]> @@ -52,9 +31,7 @@ export interface CacheAtom options: WithCacheOptions } -type CacheMapRecord = - | undefined - | CacheRecord, ActionParams> +type CacheMapRecord = undefined | CacheRecord, ActionParams> export type WithCacheOptions = { /** Define if the effect should be prevented from abort. @@ -114,12 +91,8 @@ export type WithCacheOptions = { /** Persist adapter, which will used with predefined optimal parameters */ withPersist?: ( - options: WithPersistOptions< - AtomState, ActionParams>> - >, - ) => ( - anAsync: CacheAtom, ActionParams>, - ) => CacheAtom, ActionParams> + options: WithPersistOptions, ActionParams>>>, + ) => (anAsync: CacheAtom, ActionParams>) => CacheAtom, ActionParams> } & ( | { /** Convert params to stable string and use as a map key. @@ -133,20 +106,12 @@ export type WithCacheOptions = { * Alternative to `paramsToKey`. * @default `isDeepEqual` from @reatom/utils */ - isEqual?: ( - ctx: Ctx, - prev: ActionParams, - next: ActionParams, - ) => boolean + isEqual?: (ctx: Ctx, prev: ActionParams, next: ActionParams) => boolean } ) type Find = Fn< - [ - ctx: Ctx, - params: ActionParams, - state?: AtomState, ActionParams>>, - ], + [ctx: Ctx, params: ActionParams, state?: AtomState, ActionParams>>], { cached?: CacheMapRecord; key: unknown } > @@ -194,8 +159,7 @@ export const withCache = // @ts-expect-error valid and correct JS shouldReject = false, } = swrOptions - if (staleTime !== Infinity) - staleTime = Math.min(MAX_SAFE_TIMEOUT, staleTime) + if (staleTime !== Infinity) staleTime = Math.min(MAX_SAFE_TIMEOUT, staleTime) const find: Find = paramsToKey ? (ctx, params, state = ctx.get(cacheAtom)) => { @@ -211,10 +175,7 @@ export const withCache = const findLatestWithValue = (ctx: Ctx, state = ctx.get(cacheAtom)) => { for (const cached of state.values()) { - if ( - cached.version > 0 && - (!latestCached || cached.lastUpdate > latestCached.lastUpdate) - ) { + if (cached.version > 0 && (!latestCached || cached.lastUpdate > latestCached.lastUpdate)) { var latestCached: undefined | ThisCacheRecord = cached } } @@ -239,9 +200,7 @@ export const withCache = staleTime === Infinity ? NOOP_TIMEOUT_ID : setTimeout(() => { - if ( - cacheAtom.get(ctx, key)?.clearTimeoutId === clearTimeoutId - ) { + if (cacheAtom.get(ctx, key)?.clearTimeoutId === clearTimeoutId) { cacheAtom.delete(ctx, key) } }, time) @@ -252,29 +211,24 @@ export const withCache = return clearTimeoutId } - const cacheAtom = (anAsync.cacheAtom = reatomMap( - new Map(), - `${anAsync.__reatom.name}._cacheAtom`, - ).pipe( + const cacheAtom = (anAsync.cacheAtom = reatomMap(new Map(), `${anAsync.__reatom.name}._cacheAtom`).pipe( withAssign((target, name) => ({ - setWithParams: action( - (ctx, params: ThisParams, value: AsyncResp) => { - const { cached, key } = find(ctx, params) - - cacheAtom.set(ctx, key, { - clearTimeoutId: planCleanup(ctx, key), - promise: undefined, - value, - version: cached ? cached.version + 1 : 1, - controller: new AbortController(), - lastUpdate: Date.now(), - params, - }) - - // TODO ? - // cached?.controller.abort() - }, - ), + setWithParams: action((ctx, params: ThisParams, value: AsyncResp) => { + const { cached, key } = find(ctx, params) + + cacheAtom.set(ctx, key, { + clearTimeoutId: planCleanup(ctx, key), + promise: undefined, + value, + version: cached ? cached.version + 1 : 1, + controller: new AbortController(), + lastUpdate: Date.now(), + params, + }) + + // TODO ? + // cached?.controller.abort() + }), deleteWithParams: action((ctx, params: ThisParams) => { const { cached, key } = find(ctx, params) if (cached) cacheAtom.delete(ctx, key) @@ -313,11 +267,7 @@ export const withCache = withPersist({ key: cacheAtom.__reatom.name!, // @ts-expect-error snapshot unknown type - fromSnapshot: ( - ctx, - snapshot: Array<[unknown, ThisCacheRecord]>, - state = new Map(), - ) => { + fromSnapshot: (ctx, snapshot: Array<[unknown, ThisCacheRecord]>, state = new Map()) => { if ( snapshot.length <= state?.size && snapshot.every(([, { params, value }]) => { @@ -335,11 +285,7 @@ export const withCache = if (restStaleTime <= 0) { newState.delete(key) } else { - rec.clearTimeoutId = planCleanup( - ctx, - key, - staleTime - (Date.now() - rec.lastUpdate), - ) + rec.clearTimeoutId = planCleanup(ctx, key, staleTime - (Date.now() - rec.lastUpdate)) } } @@ -357,27 +303,17 @@ export const withCache = return newState }, time: Math.min(staleTime, MAX_SAFE_TIMEOUT), - toSnapshot: (ctx, cache) => - [...cache].filter(([, rec]) => !rec.promise), + toSnapshot: (ctx, cache) => [...cache].filter(([, rec]) => !rec.promise), }), ) } - const swrPendingAtom = (anAsync.swrPendingAtom = atom( - 0, - `${anAsync.__reatom.name}.swrPendingAtom`, - )) - - const handlePromise = ( - ctx: Ctx, - key: unknown, - cached: ThisCacheRecord, - swr: boolean, - ) => { + const swrPendingAtom = (anAsync.swrPendingAtom = atom(0, `${anAsync.__reatom.name}.swrPendingAtom`)) + + const handlePromise = (ctx: Ctx, key: unknown, cached: ThisCacheRecord, swr: boolean) => { cached.clearTimeoutId = planCleanup(ctx, key) // the case: the whole cache was cleared and a new fetching was started - const isSame = () => - cacheAtom.get(ctx, key)?.clearTimeoutId === cached.clearTimeoutId + const isSame = () => cacheAtom.get(ctx, key)?.clearTimeoutId === cached.clearTimeoutId // @ts-expect-error could be reassigned by the testing package const { unstable_fn } = anAsync.__reatom @@ -386,9 +322,7 @@ export const withCache = return async (...a: Parameters) => { try { - const value = await (ignoreAbort - ? spawn(a[0], unstable_fn, a.slice(1)) - : unstable_fn(...a)) + const value = await (ignoreAbort ? spawn(a[0], unstable_fn, a.slice(1)) : unstable_fn(...a)) res(value) ctx.get(() => { @@ -431,10 +365,7 @@ export const withCache = const controller = getTopController(ctx.cause.cause!)! abortCauseContext.set(ctx.cause, (ctx.controller = controller)) - const paramsKey = params.slice( - 1, - 1 + (paramsLength ?? params.length), - ) as ThisParams + const paramsKey = params.slice(1, 1 + (paramsLength ?? params.length)) as ThisParams let { cached = { @@ -465,10 +396,7 @@ export const withCache = const cache = cacheAtom.set(ctx, key, cached) if (cache.size > length) deleteOldest(cache) - if ( - (cached.version === 0 && !cached.promise) || - (cached.promise && prevController.signal.aborted) - ) { + if ((cached.version === 0 && !cached.promise) || (cached.promise && prevController.signal.aborted)) { return handleEffect(anAsync, params, { effect: handlePromise(ctx, key, cached, false), }) @@ -514,9 +442,7 @@ export const withCache = // TODO handle it in dataAtom too to not couple to the order of operations if (withPersist && 'dataAtom' in anAsync) { - onConnect(anAsync.dataAtom!, (ctx) => - ctx.subscribe(cacheAtom, () => {}), - ) + onConnect(anAsync.dataAtom!, (ctx) => ctx.subscribe(cacheAtom, () => {})) } } diff --git a/packages/async/src/withStatusesAtom.test.ts b/packages/async/src/withStatusesAtom.test.ts index ad530ebc0..710274479 100644 --- a/packages/async/src/withStatusesAtom.test.ts +++ b/packages/async/src/withStatusesAtom.test.ts @@ -192,10 +192,7 @@ test('reset during pending', async () => { }) test('do not reject on abort', async () => { - const fetchData = reatomAsync(async () => sleep()).pipe( - withAbort(), - withStatusesAtom(), - ) + const fetchData = reatomAsync(async () => sleep()).pipe(withAbort(), withStatusesAtom()) const ctx = createTestCtx() assert.is(ctx.get(fetchData.statusesAtom), asyncStatusesInitState) @@ -237,10 +234,7 @@ test('do not reject on resource abort', async () => { }) test('restore isFulfilled after abort', async () => { - const fetchData = reatomAsync(async (ctx) => {}).pipe( - withAbort(), - withStatusesAtom(), - ) + const fetchData = reatomAsync(async (ctx) => {}).pipe(withAbort(), withStatusesAtom()) const ctx = createTestCtx() await fetchData(ctx) diff --git a/packages/async/src/withStatusesAtom.ts b/packages/async/src/withStatusesAtom.ts index c84cdc214..ad91c20f0 100644 --- a/packages/async/src/withStatusesAtom.ts +++ b/packages/async/src/withStatusesAtom.ts @@ -70,9 +70,7 @@ export interface AsyncStatusesAbortedReject { isEverSettled: true } -export type AsyncStatusesAbortedSettle = - | AsyncStatusesAbortedFulfill - | AsyncStatusesAbortedReject +export type AsyncStatusesAbortedSettle = AsyncStatusesAbortedFulfill | AsyncStatusesAbortedReject export interface AsyncStatusesFulfilled { isPending: false @@ -107,10 +105,7 @@ export interface AsyncStatusesAnotherPending { isEverSettled: true } -export type AsyncStatusesPending = - | AsyncStatusesFirstPending - | AsyncStatusesAbortedPending - | AsyncStatusesAnotherPending +export type AsyncStatusesPending = AsyncStatusesFirstPending | AsyncStatusesAbortedPending | AsyncStatusesAnotherPending export type AsyncStatuses = | AsyncStatusesNeverPending @@ -162,10 +157,7 @@ export const withStatusesAtom = `${anAsync.__reatom.name}.statusesAtom._lastSettledStatusAtom`, ) - const statusesAtom = atom( - asyncStatusesInitState, - `${anAsync.__reatom.name}.statusesAtom`, - ) + const statusesAtom = atom(asyncStatusesInitState, `${anAsync.__reatom.name}.statusesAtom`) // @ts-expect-error computer dump types statusesAtom.__reatom.computer = (ctx, state: AsyncStatuses) => { @@ -192,10 +184,7 @@ export const withStatusesAtom = anAsync.statusesAtom = Object.assign(statusesAtom, { reset: action((ctx) => { relatedPromisesAtom(ctx, new Set()) - return statusesAtom( - ctx, - asyncStatusesInitState, - ) as AsyncStatusesNeverPending + return statusesAtom(ctx, asyncStatusesInitState) as AsyncStatusesNeverPending }), }) @@ -258,9 +247,7 @@ export const withStatusesAtom = isFirstPending: false, isEverPending: true, isEverSettled: false, - } satisfies - | AsyncStatusesAbortedPending - | AsyncStatusesFirstAborted + } satisfies AsyncStatusesAbortedPending | AsyncStatusesFirstAborted } } diff --git a/packages/cjs-import-check/src/hello.ts b/packages/cjs-import-check/src/hello.ts index a793626f3..5f711508a 100644 --- a/packages/cjs-import-check/src/hello.ts +++ b/packages/cjs-import-check/src/hello.ts @@ -1,4 +1,4 @@ -const {atom, createCtx} = require('@reatom/framework') +const { atom, createCtx } = require('@reatom/framework') const me = atom('check βœ…', 'me') const ctx = createCtx() -console.log('CommonJS import:', ctx.get(me)) \ No newline at end of file +console.log('CommonJS import:', ctx.get(me)) diff --git a/packages/core-v1/CHANGELOG.md b/packages/core-v1/CHANGELOG.md index a2bdc1031..91f66fb67 100644 --- a/packages/core-v1/CHANGELOG.md +++ b/packages/core-v1/CHANGELOG.md @@ -2,95 +2,90 @@ ## [3.1.9](https://github.com/artalar/reatom/compare/core-v1-v3.1.8...core-v1-v3.1.9) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.8](https://github.com/artalar/reatom/compare/core-v1-v3.1.7...core-v1-v3.1.8) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.7](https://github.com/artalar/reatom/compare/core-v1-v3.1.6...core-v1-v3.1.7) (2023-07-12) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** stale unconnected atom ([60f677c](https://github.com/artalar/reatom/commit/60f677cd4b18bf88a2195da2b67c0a1de8f1b585)) -* **core-v1:** wrong action check ([3466ad6](https://github.com/artalar/reatom/commit/3466ad674cc09c2f38998a4d0ad313a7dd5092bb)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** stale unconnected atom ([60f677c](https://github.com/artalar/reatom/commit/60f677cd4b18bf88a2195da2b67c0a1de8f1b585)) +- **core-v1:** wrong action check ([3466ad6](https://github.com/artalar/reatom/commit/3466ad674cc09c2f38998a4d0ad313a7dd5092bb)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.1.5](https://github.com/artalar/reatom/compare/core-v1-v3.1.4...core-v1-v3.1.5) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** stale unconnected atom ([60f677c](https://github.com/artalar/reatom/commit/60f677cd4b18bf88a2195da2b67c0a1de8f1b585)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** stale unconnected atom ([60f677c](https://github.com/artalar/reatom/commit/60f677cd4b18bf88a2195da2b67c0a1de8f1b585)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.1.4](https://github.com/artalar/reatom/compare/core-v1-v3.1.3...core-v1-v3.1.4) (2023-04-10) - ### Bug Fixes -* **core-v1:** wrong action check ([3466ad6](https://github.com/artalar/reatom/commit/3466ad674cc09c2f38998a4d0ad313a7dd5092bb)) +- **core-v1:** wrong action check ([3466ad6](https://github.com/artalar/reatom/commit/3466ad674cc09c2f38998a4d0ad313a7dd5092bb)) diff --git a/packages/core-v1/README.md b/packages/core-v1/README.md index cacc15071..52555e38c 100644 --- a/packages/core-v1/README.md +++ b/packages/core-v1/README.md @@ -8,12 +8,8 @@ before import { declareAction, declareAtom, map, combine } from '@reatom/core-v1' const add = declareAction() -const n1Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) -const n2Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) +const n1Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) +const n2Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) const sumAtom = map(combine([n1Atom, n2Atom]), ([n1, n2]) => n1 + n2) const rootAtom = combine({ sumAtom }) ``` @@ -25,12 +21,8 @@ import { declareAction, declareAtom, combine, v3toV1 } from '@reatom/core-v1' import { atom } from '@reatom/core' const add = declareAction() -const n1Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) -const n2Atom = declareAtom(0, (on) => [ - on(add, (state, value) => state + value), -]) +const n1Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) +const n2Atom = declareAtom(0, (on) => [on(add, (state, value) => state + value)]) const sumAtom = atom((ctx) => ctx.spy(n1Atom.v3atom) + ctx.spy(n2Atom.v3atom)) const rootAtom = combine({ sumAtom: v3toV1(sumAtom) }) ``` diff --git a/packages/core-v1/src/createStore.ts b/packages/core-v1/src/createStore.ts index 6dd49dec7..2339eeb06 100644 --- a/packages/core-v1/src/createStore.ts +++ b/packages/core-v1/src/createStore.ts @@ -1,22 +1,12 @@ import * as v3 from '@reatom/core' import { State, BaseAction } from './kernel' -import { - throwError, - safetyFunc, - assign, - getIsAtom, - getIsAction, - TREE, -} from './shared' +import { throwError, safetyFunc, assign, getIsAtom, getIsAction, TREE } from './shared' import { Action, actions, PayloadActionCreator } from './declareAction' import { Atom, init, replace } from './declareAtom' type ActionsSubscriber = (action: Action, stateDiff: State) => any type SubscribeFunction = { - ( - target: Atom | PayloadActionCreator, - listener: (state: T) => any, - ): () => void + (target: Atom | PayloadActionCreator, listener: (state: T) => any): () => void (listener: ActionsSubscriber): () => void } type GetStateFunction = { @@ -39,11 +29,7 @@ export function createStore(atom: Atom, initState?: State): Store // TODO: try to use ES6 Map's instead of plain object // for prevent using `delete` operator // (need perf tests) -export function createStore( - atom?: Atom | State, - initState?: State, - v3ctx = v3.createCtx(), -): Store { +export function createStore(atom?: Atom | State, initState?: State, v3ctx = v3.createCtx()): Store { const activeAtoms = new Map() // @ts-expect-error v3ctx.cause[TREE] = activeAtoms @@ -52,8 +38,7 @@ export function createStore( let snapshot: State = {} if (atom !== undefined) { - if (typeof atom === 'object' && initState === undefined) - assign(snapshot, atom) + if (typeof atom === 'object' && initState === undefined) assign(snapshot, atom) else { if (!getIsAtom(atom)) throwError('Invalid atom') @@ -99,10 +84,7 @@ export function createStore( } function subscribe(subscriber: ActionsSubscriber): () => void - function subscribe( - target: Atom | PayloadActionCreator, - subscriber: (state: T) => any, - ): () => void + function subscribe(target: Atom | PayloadActionCreator, subscriber: (state: T) => any): () => void function subscribe( target: Atom | PayloadActionCreator | ActionsSubscriber, subscriber?: (state: T) => any, @@ -112,8 +94,7 @@ export function createStore( let skipFirst = true if (subscriber === undefined) { - if (getIsAtom(listener) || getIsAction(listener)) - throwError('Invalid listener') + if (getIsAtom(listener) || getIsAction(listener)) throwError('Invalid listener') ensureCanMutateNextDispatchListeners() nextDispatchListeners.push(listener) @@ -125,11 +106,9 @@ export function createStore( } } - if (!getIsAtom(target) && !getIsAction(target)) - throwError('Invalid subscription target') + if (!getIsAtom(target) && !getIsAction(target)) throwError('Invalid subscription target') - const { v3atom, v3action }: { v3atom?: v3.Atom; v3action?: v3.Action } = - target as any + const { v3atom, v3action }: { v3atom?: v3.Atom; v3action?: v3.Action } = target as any if (v3action) { return v3ctx.subscribe(v3action, (calls) => { @@ -166,11 +145,7 @@ export function createStore( } const { type, payload, reactions, v3action } = action - if ( - typeof action !== 'object' || - action === null || - typeof type !== 'string' - ) { + if (typeof action !== 'object' || action === null || typeof type !== 'string') { throwError('Invalid action') } diff --git a/packages/core-v1/src/declareAction.ts b/packages/core-v1/src/declareAction.ts index 70cf4d5ca..f060998e6 100644 --- a/packages/core-v1/src/declareAction.ts +++ b/packages/core-v1/src/declareAction.ts @@ -1,23 +1,12 @@ import * as v3 from '@reatom/core' import { Leaf, Tree, BaseAction } from './kernel' -import { - TREE, - nameToId, - Unit, - getName, - getStoreByCtx, - __onConnect, - __onDisconnect, -} from './shared' +import { TREE, nameToId, Unit, getName, getStoreByCtx, __onConnect, __onDisconnect } from './shared' import { Store } from './createStore' export type ActionType = Leaf export type Reaction = (payload: T, store: Store) => any -export type Action< - Payload, - Type extends ActionType = string, -> = BaseAction & { +export type Action = BaseAction & { type: Type reactions?: Reaction[] } @@ -27,13 +16,10 @@ export type BaseActionCreator = { v3action: v3.Action } & Unit -export type ActionCreator = - BaseActionCreator & (() => Action) +export type ActionCreator = BaseActionCreator & (() => Action) -export type PayloadActionCreator< - Payload, - Type extends string = string, -> = BaseActionCreator & ((payload: Payload) => Action) +export type PayloadActionCreator = BaseActionCreator & + ((payload: Payload) => Action) export const actions = new Map() @@ -57,10 +43,7 @@ export function declareAction( ...reactions: Reaction[] ): PayloadActionCreator -export function declareAction< - Payload = undefined, - Type extends ActionType = string, ->( +export function declareAction( name: string | [Type] | Reaction = 'action', ...reactions: Reaction[] ): ActionCreator | PayloadActionCreator { @@ -75,20 +58,13 @@ export function declareAction< const ACTree = new Tree(id, true) - const v3action = v3.action( - (ctx, payload, r: Array> = reactions) => { - r.forEach((cb) => ctx.schedule(() => cb(payload, getStoreByCtx(ctx)!))) - - return payload - }, - getName(id), - ) - ;(v3action.__reatom.connectHooks = new Set()).add((ctx) => - __onConnect(ctx, v3action), - ) - ;(v3action.__reatom.disconnectHooks = new Set()).add((ctx) => - __onDisconnect(ctx, v3action), - ) + const v3action = v3.action((ctx, payload, r: Array> = reactions) => { + r.forEach((cb) => ctx.schedule(() => cb(payload, getStoreByCtx(ctx)!))) + + return payload + }, getName(id)) + ;(v3action.__reatom.connectHooks = new Set()).add((ctx) => __onConnect(ctx, v3action)) + ;(v3action.__reatom.disconnectHooks = new Set()).add((ctx) => __onDisconnect(ctx, v3action)) const actionCreator = function actionCreator(payload?: Payload) { return { diff --git a/packages/core-v1/src/declareAtom.ts b/packages/core-v1/src/declareAtom.ts index 94023bade..c5ba7e61f 100644 --- a/packages/core-v1/src/declareAtom.ts +++ b/packages/core-v1/src/declareAtom.ts @@ -24,8 +24,7 @@ const DEPS_SHAPE = Symbol('@@Reatom/DEPS_SHAPE') // action for set initialState of each atom to global state export const init = declareAction(['@@Reatom/init']) export const initAction = init() -export const replace = - v3.action>('@@Reatom/replace') +export const replace = v3.action>('@@Reatom/replace') type AtomName = TreeId | [string] type AtomsMap = { [key: string]: Atom } @@ -38,10 +37,7 @@ type Reducer = (state: TState, value: TValue) => TState interface DependencyMatcherOn { (dependency: Atom, reducer: Reducer): void (dependency: PayloadActionCreator, reducer: Reducer): void - ( - dependency: Atom | PayloadActionCreator, - reducer: Reducer, - ): void + (dependency: Atom | PayloadActionCreator, reducer: Reducer): void } type DependencyMatcher = (on: DependencyMatcherOn) => any @@ -52,10 +48,7 @@ export interface Atom extends Unit { [DEPS_SHAPE]?: AtomsMap | TupleOfAtoms } -export function declareAtom( - initialState: TState, - dependencyMatcher: DependencyMatcher, -): Atom +export function declareAtom(initialState: TState, dependencyMatcher: DependencyMatcher): Atom export function declareAtom( name: AtomName, initialState: TState, @@ -76,8 +69,7 @@ export function declareAtom( name = getName(id) - if (initialState === undefined) - throwError(`Atom "${name}". Initial state can't be undefined`) + if (initialState === undefined) throwError(`Atom "${name}". Initial state can't be undefined`) const deps: Array<{ dep: v3.Atom; reducer: Reducer }> = [] const tree = new Tree(id) @@ -102,9 +94,7 @@ export function declareAtom( if (state === undefined) { const idx = 1 + deps.findIndex((el) => el.reducer === reducer) - throwError( - `Invalid state. Reducer number ${idx} in "${name}" atom returns undefined`, - ) + throwError(`Invalid state. Reducer number ${idx} in "${name}" atom returns undefined`) } } }) @@ -112,20 +102,12 @@ export function declareAtom( return state }, id as string) - ;(v3atom.__reatom.connectHooks = new Set()).add((ctx) => - __onConnect(ctx, v3atom), - ) - ;(v3atom.__reatom.disconnectHooks = new Set()).add((ctx) => - __onDisconnect(ctx, v3atom), - ) + ;(v3atom.__reatom.connectHooks = new Set()).add((ctx) => __onConnect(ctx, v3atom)) + ;(v3atom.__reatom.disconnectHooks = new Set()).add((ctx) => __onDisconnect(ctx, v3atom)) const initialPhase = true - function on( - dep: Unit | PayloadActionCreator, - reducer: Reducer, - ) { - if (!initialPhase) - throwError("Can't define dependencies after atom initialization") + function on(dep: Unit | PayloadActionCreator, reducer: Reducer) { + if (!initialPhase) throwError("Can't define dependencies after atom initialization") safetyFunc(reducer, 'reducer') @@ -151,18 +133,12 @@ export function declareAtom( dependencyMatcher(on) - const atom = function atom( - state: State = {}, - action: Action = initAction, - ) { + const atom = function atom(state: State = {}, action: Action = initAction) { const ctx = v3.createCtx() ctx.subscribe((logs) => { if (logs.length > 0) state = assign({}, state) - logs.forEach( - (patch) => - patch.proto.isAction || (state[patch.proto.name!] = patch.state), - ) + logs.forEach((patch) => patch.proto.isAction || (state[patch.proto.name!] = patch.state)) }) ctx.get(() => { @@ -236,10 +212,7 @@ export function combine( name: AtomName, shape: T, ): Atom<{ [key in keyof T]: T[key] extends Atom ? S : never }> -export function combine( - name: AtomName | T, - shape?: T, -) { +export function combine(name: AtomName | T, shape?: T) { if (arguments.length === 1) shape = name as T const isArray = Array.isArray(shape) @@ -249,19 +222,13 @@ export function combine( if (arguments.length === 1) name = isArray - ? Symbol( - `[${keys - .map((k) => getName(getTree((shape as TupleOfAtoms)[k as any]!).id)) - .join()}]`, - ) + ? Symbol(`[${keys.map((k) => getName(getTree((shape as TupleOfAtoms)[k as any]!).id)).join()}]`) : Symbol(`{${keys.map(getName).join()}}`) const atom = declareAtom(name as AtomName, isArray ? [] : {}, (on) => keys.forEach((key) => on((shape as any)[key], (state, payload) => { - const newState: any = isArray - ? (state as any[]).slice(0) - : assign({}, state) + const newState: any = isArray ? (state as any[]).slice(0) : assign({}, state) newState[key] = payload return newState }), @@ -271,9 +238,7 @@ export function combine( return atom } -export function getDepsShape( - thing: Atom, -): AtomsMap | TupleOfAtoms | undefined { +export function getDepsShape(thing: Atom): AtomsMap | TupleOfAtoms | undefined { return thing[DEPS_SHAPE] } @@ -284,11 +249,7 @@ export function v3toV1(anAtom: v3.Atom): Atom { const name = (anAtom.__reatom.name ??= nameToId('atom') as string) // @ts-expect-error - const v1atom: Atom = (anAtom.v1atom = declareAtom( - [name], - null as any, - (on) => [], - )) + const v1atom: Atom = (anAtom.v1atom = declareAtom([name], null as any, (on) => [])) v1atom.v3atom = anAtom return v1atom diff --git a/packages/core-v1/src/index.test.ts b/packages/core-v1/src/index.test.ts index f817b6c67..b8d93bf66 100644 --- a/packages/core-v1/src/index.test.ts +++ b/packages/core-v1/src/index.test.ts @@ -73,12 +73,8 @@ test('main api, declareAtom, basics', () => { test('main api, declareAtom, strict uid', () => { const addUnderscore = declareAction() - const atom1 = declareAtom(['name1'], '1', (on) => [ - on(addUnderscore, (state) => `_${state}`), - ]) - const atom2 = declareAtom(['name2'], '2', (on) => [ - on(addUnderscore, (state) => `_${state}`), - ]) + const atom1 = declareAtom(['name1'], '1', (on) => [on(addUnderscore, (state) => `_${state}`)]) + const atom2 = declareAtom(['name2'], '2', (on) => [on(addUnderscore, (state) => `_${state}`)]) const atomRoot = combine([atom1, atom2]) let state = atomRoot() @@ -106,21 +102,17 @@ test('main api, declareAtom, throw error if atom produced undefined value', () = const action = declareAction() assert.throws( - () => - declareAtom(['myAtom'], {}, (on) => on(action, () => undefined as any))( - {}, - action(), - ), + () => declareAtom(['myAtom'], {}, (on) => on(action, () => undefined as any))({}, action()), '[reatom] Invalid state. Reducer number 1 in "myAtom" atom returns undefined', ) assert.throws( () => - declareAtom(['test'], 0, (on) => [ - on(declareAction(), () => 0), - on(action, () => undefined as any), - ])({}, action()), + declareAtom(['test'], 0, (on) => [on(declareAction(), () => 0), on(action, () => undefined as any)])( + {}, + action(), + ), '[reatom] Invalid state. Reducer number 2 in "test" atom returns undefined', ) @@ -150,13 +142,9 @@ test('main api, createStore', () => { const increment = declareAction('increment') const toggle = declareAction() - const count = declareAtom('count', 0, (on) => [ - on(increment, (state) => state + 1), - ]) + const count = declareAtom('count', 0, (on) => [on(increment, (state) => state + 1)]) const countDoubled = map('count/map', count, (state) => state * 2) - const toggled = declareAtom('toggled', false, (on) => - on(toggle, (state) => !state), - ) + const toggled = declareAtom('toggled', false, (on) => on(toggle, (state) => !state)) const root = combine('combine', { count, @@ -179,10 +167,7 @@ test('main api, createStore', () => { assert.is(store.getState(countDoubled), 0) assert.is(store.getState(count), 0) - assert.ok( - store.getState(root) !== - (store.dispatch(increment()), store.getState(root)), - ) + assert.ok(store.getState(root) !== (store.dispatch(increment()), store.getState(root))) assert.equal(store.getState(root), { count: 1, countDoubled: 2, @@ -234,10 +219,7 @@ test('main api, createStore', () => { assert.is(subscriberToggled.calls.length, 1) assert.is(subscriberToggled.calls[0]!.i[0], true) - assert.ok( - store.getState(root) === - (store.dispatch(declareAction()()), store.getState(root)), - ) + assert.ok(store.getState(root) === (store.dispatch(declareAction()()), store.getState(root))) assert.is(storeSubscriber.calls.length, 3) assert.is(subscriberToggled.calls.length, 1) }) @@ -252,10 +234,7 @@ test('main api, createStore lazy selectors', () => { const count1 = declareAtom(0, (on) => on(increment, (state) => state + 1)) const count2SetMap = mockFn((state, payload) => payload) - const count2 = declareAtom(0, (on) => [ - on(increment, (state) => state + 1), - on(set, count2SetMap), - ]) + const count2 = declareAtom(0, (on) => [on(increment, (state) => state + 1), on(set, count2SetMap)]) const root = combine({ count1 }) @@ -344,9 +323,7 @@ test('main api, createStore lazy resubscribes', () => { const storeSubscriber = mockFn() const increment = declareAction() - const count = declareAtom('count', 0, (on) => - on(increment, (state) => state + 1), - ) + const count = declareAtom('count', 0, (on) => on(increment, (state) => state + 1)) const countDoubled = map(['countDoubled'], count, (payload) => payload * 2) const root = combine({ count }) @@ -377,9 +354,7 @@ test('main api, createStore lazy resubscribes', () => { test('main api, createStore lazy derived resubscribes', () => { const increment = declareAction() - const count = declareAtom(['count'], 0, (on) => - on(increment, (state) => state + 1), - ) + const count = declareAtom(['count'], 0, (on) => on(increment, (state) => state + 1)) const root = combine(['root'], { count }) const store = createStore() @@ -397,9 +372,7 @@ test('main api, createStore lazy derived resubscribes', () => { test('main api, createStore with undefined atom', () => { const increment = declareAction() - const countStatic = declareAtom(['countStatic'], 0, (on) => - on(increment, (state) => state + 1), - ) + const countStatic = declareAtom(['countStatic'], 0, (on) => on(increment, (state) => state + 1)) const store = createStore({ countStatic: 10 }) store.dispatch(increment()) @@ -420,12 +393,8 @@ test('main api, createStore with undefined atom and state', () => { test('main api, createStore preloaded state', () => { const increment = declareAction() - const staticCount = declareAtom(['staticCount'], 0, (on) => - on(increment, (state) => state + 1), - ) - const dynamicCount = declareAtom(['dynamicCount'], 0, (on) => - on(increment, (state) => state + 1), - ) + const staticCount = declareAtom(['staticCount'], 0, (on) => on(increment, (state) => state + 1)) + const dynamicCount = declareAtom(['dynamicCount'], 0, (on) => on(increment, (state) => state + 1)) const root = combine(['staticRoot'], { staticCount }) const storeWithoutPreloadedState = createStore(root) @@ -523,10 +492,7 @@ test('atom id as symbol', () => { assert.is(typeof getTree(atomMap).id, 'symbol') assert.is(getTree(atomMap).id.toString(), 'Symbol(my atom [map])') assert.is(typeof getTree(atomCombine).id, 'symbol') - assert.is( - getTree(atomCombine).id.toString(), - 'Symbol([my atom,my atom [map]])', - ) + assert.is(getTree(atomCombine).id.toString(), 'Symbol([my atom,my atom [map]])') assert.is( getTree( map( @@ -557,9 +523,7 @@ test('IoC example', () => { test('createStore replace state', () => { const increment = declareAction() - const countAtom = declareAtom(0, (on) => [ - on(increment, (state) => state + 1), - ]) + const countAtom = declareAtom(0, (on) => [on(increment, (state) => state + 1)]) const listener = mockFn() const store = createStore(countAtom) @@ -605,9 +569,7 @@ test('declareAction reactions', async () => { await delay() if (incrementCallId === lastCallId) store.dispatch(setValue(payload)) }) - const valueAtom = declareAtom(0, (on) => [ - on(setValue, (state, payload) => payload), - ]) + const valueAtom = declareAtom(0, (on) => [on(setValue, (state, payload) => payload)]) const store = createStore(valueAtom) const valueSubscriber = mockFn() store.subscribe(valueAtom, valueSubscriber) @@ -641,9 +603,7 @@ test('declareAction reactions', async () => { test('derived state, map + combine', () => { const increment = declareAction() - const count = declareAtom('@count', 0, (on) => - on(increment, (state) => state + 1), - ) + const count = declareAtom('@count', 0, (on) => on(increment, (state) => state + 1)) const countDoubled = map(count, (state) => state * 2) const root = combine({ count, countDoubled }) @@ -668,9 +628,7 @@ test('derived state, map + combine', () => { }) test('derived state, combine array', () => { const increment = declareAction() - const count = declareAtom('@count', 0, (on) => - on(increment, (state) => state + 1), - ) + const count = declareAtom('@count', 0, (on) => on(increment, (state) => state + 1)) const countDoubled = map(count, (state) => state * 2) const root = combine([count, countDoubled]) @@ -691,11 +649,7 @@ test('derived state, should checks atoms with equal ids', () => { assert.not.throws(() => combine([aAtom, cAtom, bAtom])) assert.throws( - () => - combine([ - map(['aAtom'], aAtom, (v) => v), - map(['aAtom'], aAtom, (v) => v), - ]), + () => combine([map(['aAtom'], aAtom, (v) => v), map(['aAtom'], aAtom, (v) => v)]), '[reatom] One of dependencies has the equal id', ) }) @@ -730,10 +684,7 @@ test('subscriber should not be called if returns previous state from atom reduce test('subscriber should not be called if returns snapshot state from atom reducer', () => { const action = declareAction() - const rootAtom = declareAtom(0, (on) => [ - on(action, (state) => state + 1), - on(action, (state) => state - 1), - ]) + const rootAtom = declareAtom(0, (on) => [on(action, (state) => state + 1), on(action, (state) => state - 1)]) const subReducerMock = mockFn((state) => state) const subAtom = map(rootAtom, subReducerMock) @@ -772,9 +723,7 @@ test('state of initial atom with %s should not be cleared after unsubscribing', ] as [string, string | symbol | [string]][] ).forEach((_, name) => { const action = declareAction() - const atom = declareAtom(name.toString(), 0, (on) => [ - on(action, (state) => state + 1), - ]) + const atom = declareAtom(name.toString(), 0, (on) => [on(action, (state) => state + 1)]) const store = createStore(atom) store.dispatch(action()) @@ -807,14 +756,10 @@ function getInitialStoreState(rootAtom, state): Record { test('getInitialStoreState init root atom with combine', () => { const setTitle = declareAction() - const titleAtom = declareAtom('title', (on) => [ - on(setTitle, (_, payload) => payload), - ]) + const titleAtom = declareAtom('title', (on) => [on(setTitle, (_, payload) => payload)]) const setMode = declareAction() - const modeAtom = declareAtom('desktop', (on) => [ - on(setMode, (_, payload) => payload), - ]) + const modeAtom = declareAtom('desktop', (on) => [on(setMode, (_, payload) => payload)]) const appAtom = combine(['app_store'], { title: titleAtom, @@ -841,13 +786,9 @@ test('subscription', () => { const store = createStore() const addItem = declareAction('addItem') - const aAtom = declareAtom(['a'], [], (on) => [ - on(addItem, (state, item) => [...state, item]), - ]) + const aAtom = declareAtom(['a'], [], (on) => [on(addItem, (state, item) => [...state, item])]) - const rootAtom = declareAtom(['root'], [], (on) => - on(aAtom, (state, payload) => payload), - ) + const rootAtom = declareAtom(['root'], [], (on) => on(aAtom, (state, payload) => payload)) assert.equal(store.getState(), {}) @@ -876,9 +817,7 @@ test('direct and wia combine subscription', () => { const store = createStore() const addItem = declareAction('addItem') - const aAtom = declareAtom(['a'], [], (on) => [ - on(addItem, (state, item) => [...state, item]), - ]) + const aAtom = declareAtom(['a'], [], (on) => [on(addItem, (state, item) => [...state, item])]) const rootAtom = combine({ a: aAtom }) @@ -912,9 +851,7 @@ test('direct and wia combine subscription', () => { test('dynamic initialState, unsubscribed atom should recalculate on each `getState`', async () => { const sleep = (ms = 50) => new Promise((r) => setTimeout(r, ms)) - const dateAtom = declareAtom(Date.now(), (on) => [ - on(declareAction([initAction.type]), () => Date.now()), - ]) + const dateAtom = declareAtom(Date.now(), (on) => [on(declareAction([initAction.type]), () => Date.now())]) const store = createStore() const date1 = store.getState(dateAtom) @@ -925,9 +862,7 @@ test('dynamic initialState, unsubscribed atom should recalculate on each `getSta test('dynamic initialState, reducer of `initAction.type` should calling on each mount', async () => { const sleep = (ms = 50) => new Promise((r) => setTimeout(r, ms)) - const dateAtom = declareAtom(Date.now(), (on) => [ - on(declareAction([initAction.type]), () => Date.now()), - ]) + const dateAtom = declareAtom(Date.now(), (on) => [on(declareAction([initAction.type]), () => Date.now())]) const store = createStore() const un = store.subscribe(dateAtom, () => {}) @@ -966,13 +901,8 @@ test('unsubscribe from atom should not cancel the subscription from the action', test(`v3`, () => { const store = createStore() const increment = declareAction(['increment']) - const counter = declareAtom(['counter'], 0, (on) => [ - on(increment, (state) => state + 1), - ]) - const counterDoubled = v3.atom( - (ctx) => ctx.spy(counter.v3atom) * 2, - 'counterDoubled', - ) + const counter = declareAtom(['counter'], 0, (on) => [on(increment, (state) => state + 1)]) + const counterDoubled = v3.atom((ctx) => ctx.spy(counter.v3atom) * 2, 'counterDoubled') const cbV1 = mockFn() const cbV3 = mockFn() @@ -994,19 +924,11 @@ test(`v3`, () => { test(`v3 computed`, () => { const store = createStore() const increment = declareAction() - const counterV1 = declareAtom('counterV1', 0, (on) => [ - on(increment, (state) => state + 1), - ]) - const counterDoubledV3 = v3.atom( - (ctx) => ctx.spy(counterV1.v3atom) + 1, - 'counterDoubledV3', - ) + const counterV1 = declareAtom('counterV1', 0, (on) => [on(increment, (state) => state + 1)]) + const counterDoubledV3 = v3.atom((ctx) => ctx.spy(counterV1.v3atom) + 1, 'counterDoubledV3') const counterDoubledV1 = v3toV1(counterDoubledV3) const counterTripleV1 = map('counterTripleV1', counterDoubledV1, (v) => v + 1) - const counterQuadV3 = v3.atom( - (ctx) => ctx.spy(counterTripleV1.v3atom) + 1, - 'counterQuadV3', - ) + const counterQuadV3 = v3.atom((ctx) => ctx.spy(counterTripleV1.v3atom) + 1, 'counterQuadV3') const cb = mockFn() @@ -1024,9 +946,7 @@ test('stale unconnected atom', () => { const createEntityAtom = (name: string, initState: T) => { const set = declareAction(`${name}.set`) - const entityAtom = declareAtom([name], initState, (on) => [ - on(set, (_, n) => n), - ]) + const entityAtom = declareAtom([name], initState, (on) => [on(set, (_, n) => n)]) return Object.assign(entityAtom, { set }) } diff --git a/packages/core-v1/src/kernel.ts b/packages/core-v1/src/kernel.ts index defafcf7f..15dcd6a15 100644 --- a/packages/core-v1/src/kernel.ts +++ b/packages/core-v1/src/kernel.ts @@ -64,10 +64,7 @@ export class Tree { } _getFns(key: Leaf) { - return ( - this.fnsMap.get(key) || - (this.fnsMap.set(key, new SetCounted()).get(key) as SetCounted) - ) + return this.fnsMap.get(key) || (this.fnsMap.set(key, new SetCounted()).get(key) as SetCounted) } addFn(fn: Fn, key: Leaf) { diff --git a/packages/core-v1/src/shared.ts b/packages/core-v1/src/shared.ts index b0cc5ff1a..b3fee7a0c 100644 --- a/packages/core-v1/src/shared.ts +++ b/packages/core-v1/src/shared.ts @@ -18,11 +18,7 @@ export type NonUndefined = Exclude * type MyAtomType = InferType * type MyActionType = InferType */ -export type InferType = T extends - | Atom - | PayloadActionCreator - ? R - : never +export type InferType = T extends Atom | PayloadActionCreator ? R : never export function noop() {} @@ -36,9 +32,7 @@ export function getTree(thing: Unit): Tree { } export function getName(treeId: TreeId): string { - return typeof treeId === 'symbol' - ? treeId.description || treeId.toString().replace(/Symbol\((.*)\)/, '$1') - : treeId + return typeof treeId === 'symbol' ? treeId.description || treeId.toString().replace(/Symbol\((.*)\)/, '$1') : treeId } export function getIsAtom(thing: any): thing is Atom { @@ -77,10 +71,7 @@ export function safetyStr(str: string, name: string): string { if (typeof str !== 'string' || str.length === 0) throwError(`Invalid ${name}`) return str } -export function safetyFunc( - func: T | undefined, - name: string, -): T { +export function safetyFunc(func: T | undefined, name: string): T { if (typeof func !== 'function') throwError(`Invalid ${name}`) return func as T } diff --git a/packages/core-v2/CHANGELOG.md b/packages/core-v2/CHANGELOG.md index 5183d3764..72dd07e54 100644 --- a/packages/core-v2/CHANGELOG.md +++ b/packages/core-v2/CHANGELOG.md @@ -2,49 +2,46 @@ ## [3.1.4](https://github.com/artalar/reatom/compare/core-v2-v3.1.3...core-v2-v3.1.4) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.3](https://github.com/artalar/reatom/compare/core-v2-v3.1.2...core-v2-v3.1.3) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.2](https://github.com/artalar/reatom/compare/core-v2-v3.1.1...core-v2-v3.1.2) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/core-v2/README.md b/packages/core-v2/README.md index 07707104b..0da1007d6 100644 --- a/packages/core-v2/README.md +++ b/packages/core-v2/README.md @@ -197,13 +197,10 @@ import { createAtom } from '@reatom/core-v2' type TimerCtx = { intervalId?: number | NodeJS.Timer | any } /** Timer update interval */ -export const intervalAtom = createAtom( - { setSeconds: (seconds: number) => seconds }, - ({ onAction }, state = 1000) => { - onAction(`setSeconds`, (seconds) => (state = seconds * 1000)) - return state - }, -) +export const intervalAtom = createAtom({ setSeconds: (seconds: number) => seconds }, ({ onAction }, state = 1000) => { + onAction(`setSeconds`, (seconds) => (state = seconds * 1000)) + return state +}) export const timerAtom = createAtom( { @@ -230,10 +227,7 @@ export const timerAtom = createAtom( if (remains <= interval) { clearInterval(ctx.intervalId) - ctx.intervalId = setTimeout( - () => dispatch(create(`_update`, 0)), - remains, - ) + ctx.intervalId = setTimeout(() => dispatch(create(`_update`, 0)), remains) } dispatch(create(`_update`, remains)) @@ -309,10 +303,7 @@ But a better way is use the `createEnumAtom`. ```ts import { createEnumAtom } from '@reatom/core-v2/primitives' -const githubRepoSortFilterAtom = createEnumAtom( - ['full_name', 'created', 'updated', 'pushed'], - { format: 'snake_case' }, -) +const githubRepoSortFilterAtom = createEnumAtom(['full_name', 'created', 'updated', 'pushed'], { format: 'snake_case' }) console.log(sortFilterAtom.getState()) // -> 'full_name' @@ -453,11 +444,7 @@ const formAtom = createAtom( // you should't call `track.get` async // (scheduled callback calls async after all atoms) // (use `email` and `password` variables instead) - track.create( - '_fetch', - track.get('emailAtom'), - track.get('passwordAtom'), - ), + track.create('_fetch', track.get('emailAtom'), track.get('passwordAtom')), ), ) }) @@ -531,21 +518,18 @@ const counterAtom = createAtom({ inc: () => {} }, ({ onAction }, state = 0) => { Important note. Feel free to mutate **variable**, not a value. Reducer functions should not mutate any input values. ```ts -const counterAtom = createAtom( - { inc: () => {} }, - ({ onAction }, state = { count: 0 }) => { - // WRONG - onAction('inc', () => { - state.count++ - }) - // Right - onAction('inc', () => { - state = { count: state.count + 1 } - }) +const counterAtom = createAtom({ inc: () => {} }, ({ onAction }, state = { count: 0 }) => { + // WRONG + onAction('inc', () => { + state.count++ + }) + // Right + onAction('inc', () => { + state = { count: state.count + 1 } + }) - return state - }, -) + return state +}) ``` ### How to handle one action in a few atoms? diff --git a/packages/core-v2/primitives/createBooleanAtom.ts b/packages/core-v2/primitives/createBooleanAtom.ts index 26292fe76..702bd37bc 100644 --- a/packages/core-v2/primitives/createBooleanAtom.ts +++ b/packages/core-v2/primitives/createBooleanAtom.ts @@ -13,10 +13,7 @@ export type BooleanAtomCreator = PrimitiveAtomCreator< > let count = 0 -export function createBooleanAtom( - initState = false, - options: AtomOptions = `boolean${++count}`, -): BooleanAtomCreator { +export function createBooleanAtom(initState = false, options: AtomOptions = `boolean${++count}`): BooleanAtomCreator { return createPrimitiveAtom< boolean, { diff --git a/packages/core-v2/primitives/createEnumAtom.test.ts b/packages/core-v2/primitives/createEnumAtom.test.ts index 236d28c96..0ac064b45 100644 --- a/packages/core-v2/primitives/createEnumAtom.test.ts +++ b/packages/core-v2/primitives/createEnumAtom.test.ts @@ -11,12 +11,7 @@ test(`enum object`, async () => { }) test(`camelCase`, async () => { - const sortFilterAtom = createEnumAtom([ - 'fullName', - 'created', - 'updated', - 'pushed', - ]) + const sortFilterAtom = createEnumAtom(['fullName', 'created', 'updated', 'pushed']) sortFilterAtom.setUpdated.dispatch() @@ -25,10 +20,7 @@ test(`camelCase`, async () => { }) test(`snake_case`, async () => { - const sortFilterAtom = createEnumAtom( - ['full_name', 'created', 'updated', 'pushed'], - { format: 'snake_case' }, - ) + const sortFilterAtom = createEnumAtom(['full_name', 'created', 'updated', 'pushed'], { format: 'snake_case' }) sortFilterAtom.enum diff --git a/packages/core-v2/primitives/createEnumAtom.ts b/packages/core-v2/primitives/createEnumAtom.ts index d01bb58b0..e57ef022d 100644 --- a/packages/core-v2/primitives/createEnumAtom.ts +++ b/packages/core-v2/primitives/createEnumAtom.ts @@ -1,10 +1,7 @@ import { Atom, AtomOptions, Rec } from '@reatom/core-v2' import { createPrimitiveAtom, PrimitiveAtom, PrimitiveAtomCreator } from '.' -export type EnumAtom< - T extends string, - Format extends 'camelCase' | 'snake_case' = 'camelCase', -> = PrimitiveAtomCreator< +export type EnumAtom = PrimitiveAtomCreator< T, { [K in T as Format extends 'camelCase' @@ -18,10 +15,7 @@ export type EnumAtom< } let count = 0 -export function createEnumAtom< - T extends string, - Format extends 'camelCase' | 'snake_case' = 'camelCase', ->( +export function createEnumAtom( variants: ReadonlyArray, options: | Atom['id'] @@ -40,11 +34,7 @@ export function createEnumAtom< const actionCreatorName = variant.replace( /^./, - (firstLetter) => - 'set' + - (format === 'camelCase' - ? firstLetter.toUpperCase() - : `_${firstLetter}`), + (firstLetter) => 'set' + (format === 'camelCase' ? firstLetter.toUpperCase() : `_${firstLetter}`), ) acc[actionCreatorName] = () => variant return acc diff --git a/packages/core-v2/primitives/createNumberAtom.ts b/packages/core-v2/primitives/createNumberAtom.ts index 56aa09763..8f5b89269 100644 --- a/packages/core-v2/primitives/createNumberAtom.ts +++ b/packages/core-v2/primitives/createNumberAtom.ts @@ -14,10 +14,7 @@ export type NumberAtom = PrimitiveAtomCreator< > let count = 0 -export function createNumberAtom( - initState = 0, - options: AtomOptions = `number${++count}`, -): NumberAtom { +export function createNumberAtom(initState = 0, options: AtomOptions = `number${++count}`): NumberAtom { return createPrimitiveAtom( initState, { diff --git a/packages/core-v2/primitives/createPrimitiveAtom.ts b/packages/core-v2/primitives/createPrimitiveAtom.ts index 3743a5c33..8855bd65a 100644 --- a/packages/core-v2/primitives/createPrimitiveAtom.ts +++ b/packages/core-v2/primitives/createPrimitiveAtom.ts @@ -1,12 +1,4 @@ -import { - AtomBinded, - AtomOptions, - AtomSelfBinded, - createAtom, - Fn, - isString, - Rec, -} from '@reatom/core-v2' +import { AtomBinded, AtomOptions, AtomSelfBinded, createAtom, Fn, isString, Rec } from '@reatom/core-v2' export type PrimitiveAtom< State, @@ -16,10 +8,7 @@ export type PrimitiveAtom< }, > = AtomSelfBinded -export type PrimitiveAtomCreator< - State, - ActionsPayloads extends Rec, -> = PrimitiveAtom< +export type PrimitiveAtomCreator> = PrimitiveAtom< State, { [K in keyof ActionsPayloads]: Fn } > @@ -30,19 +19,14 @@ export function createPrimitiveAtom( actions?: null | undefined, options?: AtomOptions, ): PrimitiveAtom -export function createPrimitiveAtom< - State, - ActionsMappers extends Rec>, ->( +export function createPrimitiveAtom>>( initState: State, actions: ActionsMappers, options?: AtomOptions, ): PrimitiveAtom< State, { - [K in keyof ActionsMappers]: ActionsMappers[K] extends Fn< - [any, ...infer Payload] - > + [K in keyof ActionsMappers]: ActionsMappers[K] extends Fn<[any, ...infer Payload]> ? (...payload: Payload) => Payload : never } @@ -54,8 +38,7 @@ export function createPrimitiveAtom( ): AtomBinded { actions ??= { set: (state: State, payload: State): State => payload, - change: (state: State, payload: (state: State) => State): State => - payload(state), + change: (state: State, payload: (state: State) => State): State => payload(state), } let { decorators, ...restOptions } = isString(options) @@ -63,10 +46,7 @@ export function createPrimitiveAtom( : options const atom = createAtom( - Object.keys(actions).reduce( - (acc, key) => ((acc[key] = (...payload) => payload), acc), - {} as Rec, - ), + Object.keys(actions).reduce((acc, key) => ((acc[key] = (...payload) => payload), acc), {} as Rec), (track, state = initState) => { for (const name in actions) { diff --git a/packages/core-v2/primitives/createSetAtom.ts b/packages/core-v2/primitives/createSetAtom.ts index 5a1006b4d..b7ba2af72 100644 --- a/packages/core-v2/primitives/createSetAtom.ts +++ b/packages/core-v2/primitives/createSetAtom.ts @@ -12,10 +12,7 @@ export type SetAtom = PrimitiveAtomCreator< > let count = 0 -export function createSetAtom( - initState = new Set(), - options: AtomOptions> = `set${++count}`, -) { +export function createSetAtom(initState = new Set(), options: AtomOptions> = `set${++count}`) { type State = Set return createPrimitiveAtom( @@ -28,8 +25,7 @@ export function createSetAtom( return newState }, clear: (): State => new Set(), - change: (state, cb: (stateCopy: State) => State): State => - cb(new Set(state)), + change: (state, cb: (stateCopy: State) => State): State => cb(new Set(state)), }, options, ) diff --git a/packages/core-v2/primitives/createStringAtom.ts b/packages/core-v2/primitives/createStringAtom.ts index 03b5317d0..d16bc73d5 100644 --- a/packages/core-v2/primitives/createStringAtom.ts +++ b/packages/core-v2/primitives/createStringAtom.ts @@ -7,18 +7,9 @@ export type StringAtom = PrimitiveAtomCreator< > let count = 0 -export function createStringAtom( - initState?: string, - options?: AtomOptions, -): StringAtom -export function createStringAtom( - initState: T, - options?: AtomOptions, -): StringAtom -export function createStringAtom( - initState = ``, - options: AtomOptions = `string${++count}`, -): StringAtom { +export function createStringAtom(initState?: string, options?: AtomOptions): StringAtom +export function createStringAtom(initState: T, options?: AtomOptions): StringAtom +export function createStringAtom(initState = ``, options: AtomOptions = `string${++count}`): StringAtom { // @ts-ignore return createPrimitiveAtom(initState, null, options) } diff --git a/packages/core-v2/src/createAtom.ts b/packages/core-v2/src/createAtom.ts index d971e9349..47466ef9f 100644 --- a/packages/core-v2/src/createAtom.ts +++ b/packages/core-v2/src/createAtom.ts @@ -49,18 +49,12 @@ export type AtomOptions = v3atom?: v3.Atom } -export type AtomDecorator = Fn< - [cacheReducer: CacheReducer], - CacheReducer -> +export type AtomDecorator = Fn<[cacheReducer: CacheReducer], CacheReducer> type PayloadMapper = Fn let atomsCount = 0 -export function createAtom< - State, - Deps extends Rec, ->( +export function createAtom>( dependencies: Deps, reducer: TrackReducer, options: AtomOptions = {}, @@ -70,9 +64,7 @@ export function createAtom< v3atom, id = v3atom?.__reatom.name ?? `atom${++atomsCount}`, store = defaultStore, - } = isString(options) - ? ({ id: options } as Exclude, string>) - : options + } = isString(options) ? ({ id: options } as Exclude, string>) : options const trackedTypes: Array = [] const types: Array = [] const actionCreators: Rec = {} @@ -81,10 +73,7 @@ export function createAtom< throwReatomError(!isFunction(reducer) || !isString(id), 'atom arguments') Object.entries(dependencies).forEach(([name, dep]) => { - throwReatomError( - !isFunction(dep), - `Invalid atom dependencies (type ${typeof dep}) at ${name}`, - ) + throwReatomError(!isFunction(dep), `Invalid atom dependencies (type ${typeof dep}) at ${name}`) if (isAtom(dep)) { dep.types.forEach((type) => pushUnique(types, type)) @@ -104,8 +93,7 @@ export function createAtom< v3action: actionCreator.v3action, }) actionCreator.type = type - actionCreator.dispatch = (...a: any[]) => - store.dispatch(actionCreator(...a)) + actionCreator.dispatch = (...a: any[]) => store.dispatch(actionCreator(...a)) actionCreator.v3action = v3.action(type) actionCreators[name] = actionCreator @@ -167,10 +155,7 @@ export function createAtom< const ctxs = new WeakMap>() -function createDynamicallyTrackedCacheReducer< - State, - Deps extends Rec, ->( +function createDynamicallyTrackedCacheReducer>( reducer: TrackReducer, dependencies: Deps, trackedTypes: Array, @@ -184,19 +169,15 @@ function createDynamicallyTrackedCacheReducer< return (v3ctx: v3.CtxSpy, state?: any): any => { const rootCause = getRootCause(v3ctx.cause) if (!ctxs.has(rootCause)) ctxs.set(rootCause, new WeakMap()) - if (!ctxs.get(rootCause)!.has(reducer)) - ctxs.get(rootCause)!.set(reducer, {}) + if (!ctxs.get(rootCause)!.has(reducer)) ctxs.get(rootCause)!.set(reducer, {}) const ctx = ctxs.get(rootCause)!.get(reducer)! - const get: Track[`get`] = (name) => - v3ctx.spy((dependencies[name as string] as Atom).v3atom) + const get: Track[`get`] = (name) => v3ctx.spy((dependencies[name as string] as Atom).v3atom) - const getUnlistedState: Track[`getUnlistedState`] = (targetAtom) => - v3ctx.get(targetAtom.v3atom) + const getUnlistedState: Track[`getUnlistedState`] = (targetAtom) => v3ctx.get(targetAtom.v3atom) const onAction: Track[`onAction`] = (name, reaction) => { - const ac = - externalActions[name as string] ?? actionCreators[name as string] + const ac = externalActions[name as string] ?? actionCreators[name as string] throwReatomError(ac === undefined, `Unknown action`) diff --git a/packages/core-v2/src/createStore.ts b/packages/core-v2/src/createStore.ts index e3f243434..daeb197cf 100644 --- a/packages/core-v2/src/createStore.ts +++ b/packages/core-v2/src/createStore.ts @@ -17,26 +17,19 @@ export function createStore({ const dispatch: Store['dispatch'] = (action) => { const actions = Array.isArray(action) ? action : [action] - throwReatomError( - actions.length == 0 || !actions.every(isAction), - `dispatch arguments`, - ) + throwReatomError(actions.length == 0 || !actions.every(isAction), `dispatch arguments`) v3ctx.get(() => { actions.forEach((action) => action.v3action(v3ctx, action.payload)) - actions.forEach(({ targets }) => - targets?.forEach((target) => v3ctx.get(target.v3atom)), - ) + actions.forEach(({ targets }) => targets?.forEach((target) => v3ctx.get(target.v3atom))) }) } - const getCache: Store['getCache'] = (atom) => - v3ctx.get((read) => read(atom.v3atom.__reatom)) + const getCache: Store['getCache'] = (atom) => v3ctx.get((read) => read(atom.v3atom.__reatom)) const getState: Store['getState'] = (atom) => v3ctx.get(atom.v3atom) - const subscribe: Store['subscribe'] = (atom, cb) => - v3ctx.subscribe(atom.v3atom, (state) => cb(state, [])) + const subscribe: Store['subscribe'] = (atom, cb) => v3ctx.subscribe(atom.v3atom, (state) => cb(state, [])) const store: Store = { dispatch, diff --git a/packages/core-v2/src/index.test.ts b/packages/core-v2/src/index.test.ts index 79aea2ba6..05ce29547 100644 --- a/packages/core-v2/src/index.test.ts +++ b/packages/core-v2/src/index.test.ts @@ -3,16 +3,7 @@ import * as assert from 'uvu/assert' import { atom } from '@reatom/core' -import { - Atom, - createAtom, - createStore, - Fn, - getState, - Rec, - callSafety, - defaultStore, -} from '../' +import { Atom, createAtom, createStore, Fn, getState, Rec, callSafety, defaultStore } from '../' import { createNumberAtom, createPrimitiveAtom } from '../primitives' import { mockFn, parseCauses, sleep } from '../test_utils' @@ -55,8 +46,7 @@ test(`displayName`, () => { const displayNameAtom = createAtom( { isFirstNameShortAtom, fullNameAtom, firstNameAtom }, - ({ get }) => - get(`isFirstNameShortAtom`) ? get(`fullNameAtom`) : get(`firstNameAtom`), + ({ get }) => (get(`isFirstNameShortAtom`) ? get(`fullNameAtom`) : get(`firstNameAtom`)), `displayName`, ) @@ -117,13 +107,10 @@ test(`combine`, () => { }) test(`atom external action subscribe`, () => { - const a1 = createAtom( - { add: (value: number) => value }, - (track, state = 0) => { - track.onAction('add', (value) => (state += value)) - return state - }, - ) + const a1 = createAtom({ add: (value: number) => value }, (track, state = 0) => { + track.onAction('add', (value) => (state += value)) + return state + }) const a2 = createAtom({ add: a1.add }, (track, state = 0) => { track.onAction('add', (value) => (state += value)) // @ts-expect-error @@ -178,10 +165,7 @@ test(`atom filter`, () => { assert.equal(bCache4.state, 1) assert.not.equal(bCache3.state, bCache4.state) - const bCache5 = bAtom( - createTransaction([a1Atom.change((s) => s + 2)]), - bCache4, - ) + const bCache5 = bAtom(createTransaction([a1Atom.change((s) => s + 2)]), bCache4) assert.equal(track.calls.length, 3) assert.not.equal(bCache4, bCache5) assert.equal(bCache5.state, 3) @@ -190,10 +174,7 @@ test(`atom filter`, () => { }) test(`in atom action effect`, async () => { - function createResource( - fetcher: (params: I) => Promise, - id: string, - ) { + function createResource(fetcher: (params: I) => Promise, id: string) { const resourceAtom = createAtom( { request: (payload: I) => payload, @@ -204,11 +185,7 @@ test(`in atom action effect`, async () => { schedule((dispatch) => fetcher(payload) .then((data) => dispatch(create('response', data))) - .catch((e) => - dispatch( - create('response', e instanceof Error ? e : new Error(e)), - ), - ), + .catch((e) => dispatch(create('response', e instanceof Error ? e : new Error(e)))), ) }) @@ -316,16 +293,10 @@ test(`Manage dynamic dependencies`, () => { const a = createPrimitiveAtom(0) const b = createAtom( { add: (atom: Atom) => atom }, - ( - { onAction, getUnlistedState }, - state = new Array(), - ) => { + ({ onAction, getUnlistedState }, state = new Array()) => { reducerCalls++ - onAction( - `add`, - (atom) => (state = [...state, [atom, getUnlistedState(atom)]]), - ) + onAction(`add`, (atom) => (state = [...state, [atom, getUnlistedState(atom)]])) return state }, @@ -457,16 +428,8 @@ test(`subscription call cause`, () => { }, `counter`, ) - const counterIsEvenAtom = createAtom( - { counterAtom }, - ({ get }) => get(`counterAtom`) % 2 === 0, - `counterIsEven`, - ) - const counterIsHugeAtom = createAtom( - { counterAtom }, - ({ get }) => get(`counterAtom`) > 10_000, - `counterIsHuge`, - ) + const counterIsEvenAtom = createAtom({ counterAtom }, ({ get }) => get(`counterAtom`) % 2 === 0, `counterIsEven`) + const counterIsHugeAtom = createAtom({ counterAtom }, ({ get }) => get(`counterAtom`) > 10_000, `counterIsHuge`) const titleAtom = createAtom( { counterIsEvenAtom, counterIsHugeAtom }, ({ onChange }, title = 'counter') => { @@ -483,16 +446,10 @@ test(`subscription call cause`, () => { store.subscribe(titleAtom, cb) store.dispatch(counterAtom.inc()) - assert.equal(parseCauses(cb.lastInput(1)), [ - 'DISPATCH: inc_counter', - 'counterIsEven atom', - ]) + assert.equal(parseCauses(cb.lastInput(1)), ['DISPATCH: inc_counter', 'counterIsEven atom']) store.dispatch(counterAtom.add(100_000)) - assert.equal(parseCauses(cb.lastInput(1)), [ - 'DISPATCH: add_counter', - 'counterIsHuge atom', - ]) + assert.equal(parseCauses(cb.lastInput(1)), ['DISPATCH: add_counter', 'counterIsHuge atom']) ;`πŸ‘` //? }) @@ -503,8 +460,7 @@ test(`createTemplateCache`, () => { const snapshot = { [atomWithSnapshot.id]: 42 } const store = createStore({ - createTemplateCache: (atom) => - Object.assign(createTemplateCache(atom), { state: snapshot[atom.id] }), + createTemplateCache: (atom) => Object.assign(createTemplateCache(atom), { state: snapshot[atom.id] }), }) assert.is(store.getState(atomWithoutSnapshot), 0) @@ -547,23 +503,20 @@ test(`onPatch / onError`, () => { }) test('State updates order', async () => { - const a = createAtom( - { setB: () => null, _setC: () => null }, - ({ onAction, schedule, create }, state = 'a') => { - onAction('setB', () => { - state = 'b' - schedule((dispatch) => { - dispatch(create('_setC')) - }) + const a = createAtom({ setB: () => null, _setC: () => null }, ({ onAction, schedule, create }, state = 'a') => { + onAction('setB', () => { + state = 'b' + schedule((dispatch) => { + dispatch(create('_setC')) }) + }) - onAction('_setC', () => { - state = 'c' - }) + onAction('_setC', () => { + state = 'c' + }) - return state - }, - ) + return state + }) const store = createStore() const listener = mockFn() store.subscribe(a, listener) diff --git a/packages/core-v2/src/shared.ts b/packages/core-v2/src/shared.ts index 96c5ba972..8f97fd4a4 100644 --- a/packages/core-v2/src/shared.ts +++ b/packages/core-v2/src/shared.ts @@ -35,9 +35,6 @@ export function isAction(thing: any): thing is Action { return isObject(thing) && isString(thing.type) && 'payload' in thing } -export function getState( - atom: Atom, - store = defaultStore, -): State { +export function getState(atom: Atom, store = defaultStore): State { return store.getState(atom) } diff --git a/packages/core-v2/src/types.ts b/packages/core-v2/src/types.ts index fa87a48fb..c3682b2f3 100644 --- a/packages/core-v2/src/types.ts +++ b/packages/core-v2/src/types.ts @@ -2,9 +2,7 @@ import * as v3 from '@reatom/core' /* UTILITY */ -export type Fn = ( - ...a: Args -) => Return +export type Fn = (...a: Args) => Return export type Rec = Record @@ -106,20 +104,14 @@ export type AtomBindings = { export type AtomBinded = Atom & AtomBindings -export type TrackReducer< - State = any, - Deps extends Rec = Rec, -> = { +export type TrackReducer = Rec> = { (track: Track): State // TODO: how to infer type from default value of optional argument? // jsdoc? // (track: Track, state?: undefined | State): State } -export type CacheReducer = Fn< - [transaction: Transaction, cache: CacheTemplate], - Cache -> +export type CacheReducer = Fn<[transaction: Transaction, cache: CacheTemplate], Cache> export type AtomListener = Fn<[state: State, ctx: Causes]> @@ -127,25 +119,18 @@ export type AtomEffect = Fn< [dispatch: Store['dispatch'], ctx: Ctx, causes: Causes] > -export type DepsPayloadMappers< - Deps extends Rec = Rec, -> = OmitValues +export type DepsPayloadMappers = Rec> = + OmitValues -export type DepsActionCreators< - Deps extends Rec = Rec, -> = { +export type DepsActionCreators = Rec> = { [K in keyof Deps]: Deps[K] extends ActionCreator ? Deps[K] : never } -export type DepsAtoms< - Deps extends Rec = Rec, -> = { +export type DepsAtoms = Rec> = { [K in keyof Deps]: Deps[K] extends Atom ? Deps[K] : never } -export type Track< - Deps extends Rec = Rec, -> = { +export type Track = Rec> = { /** Create action */ create: >( name: Name, @@ -153,9 +138,7 @@ export type Track< ) => Action[Name]>> /** Subscribe to atom state changes and receive it */ - get>( - name: Name, - ): AtomState[Name]> + get>(name: Name): AtomState[Name]> /** Get atom state without subscribing to it! */ getUnlistedState(atom: Atom): State @@ -179,12 +162,7 @@ export type Track< /** Subscribe to atom state changes and react to it */ onChange>( name: Name, - reaction: Fn< - [ - newState: AtomState[Name]>, - oldState: undefined | AtomState[Name]>, - ] - >, + reaction: Fn<[newState: AtomState[Name]>, oldState: undefined | AtomState[Name]>]>, ): void /** React only at first reducer call */ @@ -224,10 +202,7 @@ type CustomAction = Merge< } > -export type ActionCreator< - Args extends any[] = any[], - Data extends ActionData = { payload: Args[0] }, -> = { +export type ActionCreator = { (...a: Args): CustomAction type: Action['type'] @@ -257,9 +232,7 @@ export type Cause = Atom['id'] | Action['type'] | string // export type Causes = [TransactionResult, ...(Array | Causes)] export type Causes = Array -export type TransactionEffect = Fn< - [dispatch: Store['dispatch'], causes: Causes] -> +export type TransactionEffect = Fn<[dispatch: Store['dispatch'], causes: Causes]> export type Store = { /** @@ -316,12 +289,10 @@ export type AtomState> = T extends Atom ? State : never -export type ActionPayload = - T extends ActionCreator - ? Payload - : T extends Action - ? Payload - : never +export type ActionPayload = T extends ActionCreator + ? Payload + : T extends Action + ? Payload + : never -export type ActionCreatorData = - T extends ActionCreator ? Data : never +export type ActionCreatorData = T extends ActionCreator ? Data : never diff --git a/packages/core-v2/test_utils/index.ts b/packages/core-v2/test_utils/index.ts index bfa1c9faa..3122bb44e 100644 --- a/packages/core-v2/test_utils/index.ts +++ b/packages/core-v2/test_utils/index.ts @@ -2,9 +2,7 @@ import type { Causes } from '../src' export const sleep = (ms = 0) => new Promise((r) => setTimeout(r, ms)) -export function mockFn( - fn: (...input: I) => O = (...i: any) => void 0 as any, -) { +export function mockFn(fn: (...input: I) => O = (...i: any) => void 0 as any) { const _fn = Object.assign( function (...i: I) { // @ts-ignore @@ -29,8 +27,6 @@ export function mockFn( export function parseCauses(causes: Causes): Array { return causes.map((cause) => - typeof cause === 'string' - ? cause - : `DISPATCH: ${cause.actions.map(({ type }) => type).join(`, `)}`, + typeof cause === 'string' ? cause : `DISPATCH: ${cause.actions.map(({ type }) => type).join(`, `)}`, ) } diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 56c712ee1..241bb38a6 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -2,176 +2,155 @@ ## [3.9.0](https://github.com/artalar/reatom/compare/core-v3.8.3...core-v3.9.0) (2024-07-16) - ### Features -* **core:** add restrictMultipleContexts option and warning ([99e0e3c](https://github.com/artalar/reatom/commit/99e0e3c723a529effc43f0e2a4908806064d30af)) +- **core:** add restrictMultipleContexts option and warning ([99e0e3c](https://github.com/artalar/reatom/commit/99e0e3c723a529effc43f0e2a4908806064d30af)) ## [3.8.3](https://github.com/artalar/reatom/compare/core-v3.8.2...core-v3.8.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.8.2](https://github.com/artalar/reatom/compare/core-v3.8.1...core-v3.8.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.8.1](https://github.com/artalar/reatom/compare/core-v3.8.0...core-v3.8.1) (2024-06-04) - ### Bug Fixes -* **docs:** fixed links to Reatom handbook ([#855](https://github.com/artalar/reatom/issues/855)) ([c9e6a56](https://github.com/artalar/reatom/commit/c9e6a56201c9a496664cd9409fe0fa5dff67606e)) +- **docs:** fixed links to Reatom handbook ([#855](https://github.com/artalar/reatom/issues/855)) ([c9e6a56](https://github.com/artalar/reatom/commit/c9e6a56201c9a496664cd9409fe0fa5dff67606e)) ## [3.8.0](https://github.com/artalar/reatom/compare/core-v3.7.0...core-v3.8.0) (2024-04-12) - ### Features -* **core:** add batch method ([2e082b6](https://github.com/artalar/reatom/commit/2e082b6296d933ca24046f60ad31b11098027af2)) - +- **core:** add batch method ([2e082b6](https://github.com/artalar/reatom/commit/2e082b6296d933ca24046f60ad31b11098027af2)) ### Bug Fixes -* **core:** remove extra computation ([6e3d86d](https://github.com/artalar/reatom/commit/6e3d86dc9d8de9dd78a9c10ce4cb3a899e407093)) +- **core:** remove extra computation ([6e3d86d](https://github.com/artalar/reatom/commit/6e3d86dc9d8de9dd78a9c10ce4cb3a899e407093)) ## [3.7.0](https://github.com/artalar/reatom/compare/core-v3.6.6...core-v3.7.0) (2024-02-16) - ### Features -* **core:** add experimental_PLUGINS ([334759c](https://github.com/artalar/reatom/commit/334759c1e20487545a1276f18c14812a1a080fbe)) - +- **core:** add experimental_PLUGINS ([334759c](https://github.com/artalar/reatom/commit/334759c1e20487545a1276f18c14812a1a080fbe)) ### Bug Fixes -* **core:** actualization of unsusbscribed atom ([e388afb](https://github.com/artalar/reatom/commit/e388afbbd120aa8fd9aeb3943cb55691e2930f24)) +- **core:** actualization of unsusbscribed atom ([e388afb](https://github.com/artalar/reatom/commit/e388afbbd120aa8fd9aeb3943cb55691e2930f24)) ## [3.6.6](https://github.com/artalar/reatom/compare/core-v3.6.5...core-v3.6.6) (2024-01-21) - ### Bug Fixes -* **core:** prev value of spy callback ([0abdec0](https://github.com/artalar/reatom/commit/0abdec08c50de9c6622e71b67a4f063aaa9f9343)) +- **core:** prev value of spy callback ([0abdec0](https://github.com/artalar/reatom/commit/0abdec08c50de9c6622e71b67a4f063aaa9f9343)) ## [3.6.5](https://github.com/artalar/reatom/compare/core-v3.6.4...core-v3.6.5) (2023-11-20) - ### Bug Fixes -* **core:** nested schedule ([61b3822](https://github.com/artalar/reatom/commit/61b38225b8f7de8eefd7f8f7f6ec079d1ef6de84)) +- **core:** nested schedule ([61b3822](https://github.com/artalar/reatom/commit/61b38225b8f7de8eefd7f8f7f6ec079d1ef6de84)) ## [3.6.4](https://github.com/artalar/reatom/compare/core-v3.6.3...core-v3.6.4) (2023-11-12) - ### Bug Fixes -* **core:** reduce mem usage a little bit ([53c101d](https://github.com/artalar/reatom/commit/53c101de190137a078c18900711dd159373635b5)) +- **core:** reduce mem usage a little bit ([53c101d](https://github.com/artalar/reatom/commit/53c101de190137a078c18900711dd159373635b5)) ## [3.6.3](https://github.com/artalar/reatom/compare/core-v3.6.2...core-v3.6.3) (2023-11-05) - ### Bug Fixes -* **core:** small mem improvement ([fd24970](https://github.com/artalar/reatom/commit/fd249701d4f527460443a8cf651d33b3bf153cb4)) +- **core:** small mem improvement ([fd24970](https://github.com/artalar/reatom/commit/fd249701d4f527460443a8cf651d33b3bf153cb4)) ## [3.6.2](https://github.com/artalar/reatom/compare/core-v3.6.1...core-v3.6.2) (2023-10-31) - ### Bug Fixes -* **core:** conditional deps duplication ([c8968a9](https://github.com/artalar/reatom/commit/c8968a9a98f0554f1164b5857a78910cf61f1da4)) +- **core:** conditional deps duplication ([c8968a9](https://github.com/artalar/reatom/commit/c8968a9a98f0554f1164b5857a78910cf61f1da4)) ## [3.6.1](https://github.com/artalar/reatom/compare/core-v3.6.0...core-v3.6.1) (2023-10-10) - ### Bug Fixes -* **core:** hide spy for updateHooks ([344d2ce](https://github.com/artalar/reatom/commit/344d2ce10676f49999c8c3fb973109e1ac42c57c)) +- **core:** hide spy for updateHooks ([344d2ce](https://github.com/artalar/reatom/commit/344d2ce10676f49999c8c3fb973109e1ac42c57c)) ## [3.6.0](https://github.com/artalar/reatom/compare/core-v3.5.0...core-v3.6.0) (2023-10-09) - ### Features -* **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) +- **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) ## [3.5.0](https://github.com/artalar/reatom/compare/core-v3.4.0...core-v3.5.0) (2023-07-07) - ### Features -* **core:** make cause not nullable for a user ([b62b9ec](https://github.com/artalar/reatom/commit/b62b9ec968327e5364a16c415ae5822a175ed6b7)) - +- **core:** make cause not nullable for a user ([b62b9ec](https://github.com/artalar/reatom/commit/b62b9ec968327e5364a16c415ae5822a175ed6b7)) ### Bug Fixes -* **core:** ensure that ctx collision is impossible ([ecd8d73](https://github.com/artalar/reatom/commit/ecd8d7353cb0c229e80dba26224d0de268bde5ff)) -* **core:** improve cause tracking ([f578511](https://github.com/artalar/reatom/commit/f578511f8b2a44bc91d3b9f82a791229c095193f)) -* **core:** onChange, onCall types ([581e7a0](https://github.com/artalar/reatom/commit/581e7a01f1f7ba033744f0b508af718b287f5f7f)) -* **core:** use only change cause ([21752ad](https://github.com/artalar/reatom/commit/21752ad6255f6c3ba0634c50da05fae3d401b7bd)) +- **core:** ensure that ctx collision is impossible ([ecd8d73](https://github.com/artalar/reatom/commit/ecd8d7353cb0c229e80dba26224d0de268bde5ff)) +- **core:** improve cause tracking ([f578511](https://github.com/artalar/reatom/commit/f578511f8b2a44bc91d3b9f82a791229c095193f)) +- **core:** onChange, onCall types ([581e7a0](https://github.com/artalar/reatom/commit/581e7a01f1f7ba033744f0b508af718b287f5f7f)) +- **core:** use only change cause ([21752ad](https://github.com/artalar/reatom/commit/21752ad6255f6c3ba0634c50da05fae3d401b7bd)) ## [3.4.0](https://github.com/artalar/reatom/compare/core-v3.3.1...core-v3.4.0) (2023-06-12) - ### Features -* **core:** [#578](https://github.com/artalar/reatom/issues/578) add update hooks ([fd5f92a](https://github.com/artalar/reatom/commit/fd5f92abe270f59531ad3af41e8073509eedec4a)) - +- **core:** [#578](https://github.com/artalar/reatom/issues/578) add update hooks ([fd5f92a](https://github.com/artalar/reatom/commit/fd5f92abe270f59531ad3af41e8073509eedec4a)) ### Bug Fixes -* **core:** do not call updateHooks on init ([300281f](https://github.com/artalar/reatom/commit/300281f1f7610cbe37201be914292d5c811d6cdd)) -* **core:** fix type error (nullable atom) ([#574](https://github.com/artalar/reatom/issues/574)) ([06d123b](https://github.com/artalar/reatom/commit/06d123ba118ac98996d6653ab2377e56516ad84b)) -* **core:** onCall types ([d3c6940](https://github.com/artalar/reatom/commit/d3c6940ca1f6001a4136e558cf00965de304a6ab)) -* **core:** unify cause setter ([e565d1f](https://github.com/artalar/reatom/commit/e565d1fd647583bbb6098b3b41024c9b7d458439)) -* **core:** update hook for atom without cache ([d4c164a](https://github.com/artalar/reatom/commit/d4c164ad9b17406adaae7baa7e3e337df0e43a3d)) -* **core:** updateHooks should be called only for computers ([acfa682](https://github.com/artalar/reatom/commit/acfa68243e6d48323a90dceb81755e5826cd9215)) +- **core:** do not call updateHooks on init ([300281f](https://github.com/artalar/reatom/commit/300281f1f7610cbe37201be914292d5c811d6cdd)) +- **core:** fix type error (nullable atom) ([#574](https://github.com/artalar/reatom/issues/574)) ([06d123b](https://github.com/artalar/reatom/commit/06d123ba118ac98996d6653ab2377e56516ad84b)) +- **core:** onCall types ([d3c6940](https://github.com/artalar/reatom/commit/d3c6940ca1f6001a4136e558cf00965de304a6ab)) +- **core:** unify cause setter ([e565d1f](https://github.com/artalar/reatom/commit/e565d1fd647583bbb6098b3b41024c9b7d458439)) +- **core:** update hook for atom without cache ([d4c164a](https://github.com/artalar/reatom/commit/d4c164ad9b17406adaae7baa7e3e337df0e43a3d)) +- **core:** updateHooks should be called only for computers ([acfa682](https://github.com/artalar/reatom/commit/acfa68243e6d48323a90dceb81755e5826cd9215)) ## [3.3.1](https://github.com/artalar/reatom/compare/core-v3.3.0...core-v3.3.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.3.0](https://github.com/artalar/reatom/compare/core-v3.2.0...core-v3.3.0) (2023-05-10) - ### Features -* **core:** fix the order of an atom update and computed ([acdba1f](https://github.com/artalar/reatom/commit/acdba1f241c1bd5dcf52c8bfa49c38da3a8510d9)) +- **core:** fix the order of an atom update and computed ([acdba1f](https://github.com/artalar/reatom/commit/acdba1f241c1bd5dcf52c8bfa49c38da3a8510d9)) diff --git a/packages/core/README.md b/packages/core/README.md index 854d0e6a9..2107a1616 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -81,9 +81,7 @@ const fetchGoods = action(async (ctx, search: string) => { // schedule side-effects // which will be called after successful execution of all computations - const goods = await ctx.schedule(() => - fetch(`/api/goods?search=${search}`).then((r) => r.json()), - ) + const goods = await ctx.schedule(() => fetch(`/api/goods?search=${search}`).then((r) => r.json())) // use `batch` to prevent glitches and extra effects. batch(ctx, () => { @@ -198,10 +196,7 @@ You could create a computed derived atom by passing a function to `atom`. The fi > **Note to TypeScript users**: It is impossible to describe the reducer type with an optional generic state argument, which is returned from the function. If you use the second `state` argument, you should define its type; do not rely on the return type. ```ts -const isCountEvenAtom = atom( - (ctx) => ctx.spy(countAtom) % 2 === 0, - 'isCountEven', -) +const isCountEvenAtom = atom((ctx) => ctx.spy(countAtom) % 2 === 0, 'isCountEven') // isCountEvenAtom: Atom ``` @@ -267,9 +262,7 @@ export const currencyAtom = atom((ctx, state?: string) => { Pipe is a general chain helper, it applies an operator to the atom to map it to another thing. Classic operator interface is `(options?: any) => (anAtom: T) => aNewThing`. The main reason is a readable and type-safe way to apply decorators. ```ts -const countAtom = atom(0).pipe( - withInit(() => localStorage.getItem('COUNT') ?? 0), -) +const countAtom = atom(0).pipe(withInit(() => localStorage.getItem('COUNT') ?? 0)) // equals to const countAtom = withInit(() => localStorage.getItem('COUNT') ?? 0)(atom(0)) ``` @@ -371,7 +364,6 @@ doSome.onCall((ctx, payload, params) => { }) ``` - ## createCtx API A context creation function accepts a few optional parameters that you probably won't want to change in regular use. However, it might be useful for testing and some rare production needs. @@ -476,10 +468,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { const user = await ctx.schedule(() => api.getUser(id)) firstNameAtom(ctx, user.firstName) @@ -502,10 +491,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { const user = await ctx.schedule(() => api.getUser(id)) batch(ctx, () => { @@ -530,10 +516,7 @@ import { action, atom, batch } from '@reatom/core' export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) @@ -562,10 +545,7 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) @@ -590,21 +570,15 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const saveUser = action((ctx, firstName: string, lastName: string) => { firstNameAtom(ctx, firstName) lastNameAtom(ctx, lastName) }, 'saveUser') -export const resolveFetchUser = action( - (ctx, firstName: string, lastName: string) => { - saveUser(ctx, firstName, firstName) - isUserLoadingAtom(ctx, false) - }, - 'resolveFetchUser', -) +export const resolveFetchUser = action((ctx, firstName: string, lastName: string) => { + saveUser(ctx, firstName, firstName) + isUserLoadingAtom(ctx, false) +}, 'resolveFetchUser') export const fetchUser = action(async (ctx, id: string) => { isUserLoadingAtom(ctx, true) try { @@ -624,10 +598,7 @@ import { action, atom, batch } from '@reatom/core' export const isUserLoadingAtom = atom(false, 'isUserLoadingAtom') export const firstNameAtom = atom('', 'firstNameAtom') export const lastNameAtom = atom('', 'lastNameAtom') -export const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - 'fullNameAtom', -) +export const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, 'fullNameAtom') export const fetchUser = action(async (ctx, id: string) => { isUserLoadingAtom(ctx, true) try { diff --git a/packages/core/history-v2.md b/packages/core/history-v2.md index 8908703ab..d38168569 100644 --- a/packages/core/history-v2.md +++ b/packages/core/history-v2.md @@ -18,17 +18,12 @@ const counterAtom = declareAtom(0, (atom) => .id(`counter`) .meta({ updates: 0 }) .on(add, (value, state) => state + value) - .compute(($, state) => - $(shouldSyncCounterAtom) ? $(globalCounterAtom) : state, - ) + .compute(($, state) => ($(shouldSyncCounterAtom) ? $(globalCounterAtom) : state)) .assign({ inc: (value: void, state) => state + 1, }) .change((state, store, meta) => - console.log( - `counterAtom receive new value: ${state}.`, - `It is ${++meta.updates} update`, - ), + console.log(`counterAtom receive new value: ${state}.`, `It is ${++meta.updates} update`), ), ) ``` @@ -42,18 +37,12 @@ const counterAtom = declareAtom(0, { id: `counter`, meta: { updates: 0 }, on: [add.reduce((value, state) => state + value)], - compute: [ - ($, state) => ($(shouldSyncCounterAtom) ? $(globalCounterAtom) : state), - ], + compute: [($, state) => ($(shouldSyncCounterAtom) ? $(globalCounterAtom) : state)], assign: { inc: (value: void, state) => state + 1, }, change: [ - (state, store, meta) => - console.log( - `counterAtom receive new value: ${state}.`, - `It is ${++meta.updates} update`, - ), + (state, store, meta) => console.log(`counterAtom receive new value: ${state}.`, `It is ${++meta.updates} update`), ], }) ``` @@ -73,10 +62,7 @@ const counterAtom = declareAtom( [ `*`, (state) => (store, meta) => - console.log( - `counterAtom receive new value: ${state}.`, - `It is ${++meta.updates} update`, - ), + console.log(`counterAtom receive new value: ${state}.`, `It is ${++meta.updates} update`), ], ) ``` @@ -173,10 +159,7 @@ const counterAtom = declareAtom({ inc: () => null }, ($, state = 0) => { const oldState = state $((store, ctx) => - console.log( - `counterAtom receive new value: ${state}.`, - `It is ${(ctx.updates = (ctx.updates ?? 0) + 1)} update`, - ), + console.log(`counterAtom receive new value: ${state}.`, `It is ${(ctx.updates = (ctx.updates ?? 0) + 1)} update`), ) $({ inc: () => state++ }) @@ -202,10 +185,7 @@ const counterAtom = declareAtom( const oldState = state schedule((dispatch, ctx) => - console.log( - `counterAtom receive new value: ${state}.`, - `It is ${(ctx.updates = (ctx.updates ?? 0) + 1)} update`, - ), + console.log(`counterAtom receive new value: ${state}.`, `It is ${(ctx.updates = (ctx.updates ?? 0) + 1)} update`), ) onAction(`inc`, () => state++) @@ -219,31 +199,29 @@ const counterAtom = declareAtom( ) ``` - ## Chained properties access I write it in times of this API: `atom(ctx => ctx.spy(dataAtom))`. Third version of Reatom is allow dynamic atoms creation which force to describe all data with atoms. Practically it means that we now have a lot of code with nested atom, like `type A = Atom<{ b: { c: Atom<{ d: Atom }> } }>`, which looks really ugly in reading: `ctx.spy(ctx.get(ctx.get(aAtom).b.c).d)` - it is not obvious which structure we inspect and how. The better way is moving data accessor to it source - atom, for example: `aAtom(ctx.get).b.c(ctx.get).d(ctx.spy)`. ```js - // a.b.c.d // ctx.spy(ctx.get(ctx.get(aAtom).b.c).d) // a(trz.ctx).b.c(trz.ctx).d(trz) // a.pipe(ctx.get).b.c.pipe(ctx.get).d(ctx.spy) -const doOne = action(); -const doTwo = action((v /*: 0*/) => v); -const a1 = atom(0); +const doOne = action() +const doTwo = action((v /*: 0*/) => v) +const a1 = atom(0) const a2 = atom( // `trz` mean "transaction" (trz) => { // just read - a1(trz.ctx) * 2; + a1(trz.ctx) * 2 // read and subscribe - a1(trz) * 2; + a1(trz) * 2 // update - just pass the second argument - a1(trz, 123); - a1(trz, (s) => s + 1); + a1(trz, 123) + a1(trz, (s) => s + 1) /* actions are atoms too, but their state is @@ -251,9 +229,9 @@ const a2 = atom( */ // just read - doOne(trz.ctx); // [] + doOne(trz.ctx) // [] // read and subscribe - doOne(trz); // [] + doOne(trz) // [] /* the main question is how to describe @@ -261,28 +239,28 @@ const a2 = atom( */ // (1) undefined - doOne(trz, undefined); - doTwo(trz, 0); + doOne(trz, undefined) + doTwo(trz, 0) // VS (2) arguments array - doOne(trz, []); - doTwo(trz, [0]); + doOne(trz, []) + doTwo(trz, [0]) // VS (3) `call` overload - doOne.call(trz); - doTwo.call(trz, 0); + doOne.call(trz) + doTwo.call(trz, 0) // VS (4) separate `dispatch` method - doOne.dispatch(trz); - doTwo.dispatch(trz, 0); + doOne.dispatch(trz) + doTwo.dispatch(trz, 0) // VS (5) trz `call` - trz.act(doOne); - trz.act(doTwo, 0); + trz.act(doOne) + trz.act(doTwo, 0) // VS (6) old `dispatch` by action overload - trz.dispatch(doOne()); - trz.dispatch(doTwo(0)); + trz.dispatch(doOne()) + trz.dispatch(doTwo(0)) /* result of one call - array with one element */ - doOne(trz.ctx); // [undefined] - doTwo(trz.ctx); // [0] - } -); + doOne(trz.ctx) // [undefined] + doTwo(trz.ctx) // [0] + }, +) ``` diff --git a/packages/core/src/atom.test.ts b/packages/core/src/atom.test.ts index f5dbed663..a3a526825 100644 --- a/packages/core/src/atom.test.ts +++ b/packages/core/src/atom.test.ts @@ -2,18 +2,7 @@ import { test } from 'uvu' import * as assert from 'uvu/assert' import { mockFn } from '@reatom/testing' -import { - action, - Atom, - atom, - AtomProto, - AtomMut, - createCtx as _createCtx, - Ctx, - CtxSpy, - Fn, - AtomCache, -} from './atom' +import { action, Atom, atom, AtomProto, AtomMut, createCtx as _createCtx, Ctx, CtxSpy, Fn, AtomCache } from './atom' const callSafelySilent = (fn: Fn, ...a: any[]) => { try { @@ -91,9 +80,7 @@ test(`linking`, () => { const fn = mockFn() ctx.subscribe((logs) => { - logs.forEach((patch) => - assert.is.not(patch.cause, null, `"${patch.proto.name}" cause is null`), - ) + logs.forEach((patch) => assert.is.not(patch.cause, null, `"${patch.proto.name}" cause is null`)) }) const un = ctx.subscribe(a2, fn) @@ -126,34 +113,19 @@ test(`nested deps`, () => { const touchedAtoms: Array = [] ctx.subscribe((logs) => { - logs.forEach((patch) => - assert.is.not(patch.cause, null, `"${patch.proto.name}" cause is null`), - ) + logs.forEach((patch) => assert.is.not(patch.cause, null, `"${patch.proto.name}" cause is null`)) }) const un = ctx.subscribe(a6, fn) for (const a of [a1, a2, a3, a4, a5, a6]) { - assert.is( - isConnected(ctx, a), - true, - `"${a.__reatom.name}" should not be stale`, - ) + assert.is(isConnected(ctx, a), true, `"${a.__reatom.name}" should not be stale`) } assert.is(fn.calls.length, 1) - assert.equal( - ctx.get((read) => read(a1.__reatom))!.subs, - new Set([a2.__reatom, a3.__reatom]), - ) - assert.equal( - ctx.get((read) => read(a2.__reatom))!.subs, - new Set([a4.__reatom, a5.__reatom]), - ) - assert.equal( - ctx.get((read) => read(a3.__reatom))!.subs, - new Set([a4.__reatom, a5.__reatom]), - ) + assert.equal(ctx.get((read) => read(a1.__reatom))!.subs, new Set([a2.__reatom, a3.__reatom])) + assert.equal(ctx.get((read) => read(a2.__reatom))!.subs, new Set([a4.__reatom, a5.__reatom])) + assert.equal(ctx.get((read) => read(a3.__reatom))!.subs, new Set([a4.__reatom, a5.__reatom])) ctx.subscribe((logs) => logs.forEach(({ proto }) => touchedAtoms.push(proto))) @@ -165,11 +137,7 @@ test(`nested deps`, () => { un() for (const a of [a1, a2, a3, a4, a5, a6]) { - assert.is( - isConnected(ctx, a), - false, - `"${a.__reatom.name}" should be stale`, - ) + assert.is(isConnected(ctx, a), false, `"${a.__reatom.name}" should be stale`) } ;`πŸ‘` //? }) @@ -244,19 +212,10 @@ test(`late effects batch`, async () => { test(`display name`, () => { const firstNameAtom = atom(`John`, `firstName`) const lastNameAtom = atom(`Doe`, `lastName`) - const isFirstNameShortAtom = atom( - (ctx) => ctx.spy(firstNameAtom).length < 10, - `isFirstNameShort`, - ) - const fullNameAtom = atom( - (ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, - `fullName`, - ) + const isFirstNameShortAtom = atom((ctx) => ctx.spy(firstNameAtom).length < 10, `isFirstNameShort`) + const fullNameAtom = atom((ctx) => `${ctx.spy(firstNameAtom)} ${ctx.spy(lastNameAtom)}`, `fullName`) const displayNameAtom = atom( - (ctx) => - ctx.spy(isFirstNameShortAtom) - ? ctx.spy(fullNameAtom) - : ctx.spy(firstNameAtom), + (ctx) => (ctx.spy(isFirstNameShortAtom) ? ctx.spy(fullNameAtom) : ctx.spy(firstNameAtom)), `displayName`, ) const effect = mockFn() @@ -301,9 +260,7 @@ test(`display name`, () => { test(// this test written is more just for example purposes `dynamic lists`, () => { const listAtom = atom(new Array>()) - const sumAtom = atom((ctx) => - ctx.spy(listAtom).reduce((acc, a) => acc + ctx.spy(a), 0), - ) + const sumAtom = atom((ctx) => ctx.spy(listAtom).reduce((acc, a) => acc + ctx.spy(a), 0)) const ctx = createCtx() const sumListener = mockFn((sum: number) => {}) @@ -351,11 +308,7 @@ test('async cause track', () => { act1(ctx) - assert.is( - track.lastInput().find((patch: AtomCache) => patch.proto.name === 'a1') - ?.cause.proto.name, - 'act2', - ) + assert.is(track.lastInput().find((patch: AtomCache) => patch.proto.name === 'a1')?.cause.proto.name, 'act2') ;`πŸ‘` //? }) @@ -364,9 +317,7 @@ test('disconnect tail deps', () => { const track = mockFn((ctx: CtxSpy) => ctx.spy(aAtom)) const bAtom = atom(track, 'bAtom') const isActiveAtom = atom(true, 'isActiveAtom') - const bAtomControlled = atom((ctx, state?: any) => - ctx.spy(isActiveAtom) ? ctx.spy(bAtom) : state, - ) + const bAtomControlled = atom((ctx, state?: any) => (ctx.spy(isActiveAtom) ? ctx.spy(bAtom) : state)) const ctx = createCtx() ctx.subscribe(bAtomControlled, () => {}) @@ -384,9 +335,7 @@ test('deps shift', () => { const deps = [atom(0), atom(0), atom(0)] const track = mockFn() - deps.forEach((dep, i) => - (dep.__reatom.disconnectHooks ??= new Set()).add(() => track(i)), - ) + deps.forEach((dep, i) => (dep.__reatom.disconnectHooks ??= new Set()).add(() => track(i))) const a = atom((ctx) => deps.forEach((dep) => ctx.spy(dep))) const ctx = createCtx() @@ -555,9 +504,7 @@ test('no extra tick by schedule', async () => { let isDoneAsyncInTr = false const ctx = createCtx() - ctx.get(() => - ctx.schedule(async () => {}).then(() => (isDoneAsyncInTr = true)), - ) + ctx.get(() => ctx.schedule(async () => {}).then(() => (isDoneAsyncInTr = true))) await null await null diff --git a/packages/devtools/CHANGELOG.md b/packages/devtools/CHANGELOG.md index 50a67e4f4..cdeb7b34d 100644 --- a/packages/devtools/CHANGELOG.md +++ b/packages/devtools/CHANGELOG.md @@ -2,36 +2,31 @@ ## [0.3.0](https://github.com/artalar/reatom/compare/devtools-v0.2.2...devtools-v0.3.0) (2024-06-26) - ### Features -* **devtools:** support separator function ([563ce6e](https://github.com/artalar/reatom/commit/563ce6effc0f3ccd7dfe925ebab10a846e3df2b9)) - +- **devtools:** support separator function ([563ce6e](https://github.com/artalar/reatom/commit/563ce6effc0f3ccd7dfe925ebab10a846e3df2b9)) ### Bug Fixes -* **devtools:** styles and logs improvements ([5e6428a](https://github.com/artalar/reatom/commit/5e6428a79dfcfd8b07d1ad892b8d4153f651cf7d)) -* **devtools:** types declarations ([e98e758](https://github.com/artalar/reatom/commit/e98e7583cc86b4f7019ccf182843ca12023f5174)) +- **devtools:** styles and logs improvements ([5e6428a](https://github.com/artalar/reatom/commit/5e6428a79dfcfd8b07d1ad892b8d4153f651cf7d)) +- **devtools:** types declarations ([e98e758](https://github.com/artalar/reatom/commit/e98e7583cc86b4f7019ccf182843ca12023f5174)) ## [0.2.2](https://github.com/artalar/reatom/compare/devtools-v0.2.1...devtools-v0.2.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [0.2.1](https://github.com/artalar/reatom/compare/devtools-v0.2.0...devtools-v0.2.1) (2024-06-22) - ### Bug Fixes -* **devtools:** styles ([e0b9cf8](https://github.com/artalar/reatom/commit/e0b9cf8f3d781676cd4bed8266896f24ee06b9ca)) -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **devtools:** styles ([e0b9cf8](https://github.com/artalar/reatom/commit/e0b9cf8f3d781676cd4bed8266896f24ee06b9ca)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [0.2.0](https://github.com/artalar/reatom/compare/devtools-v0.1.0...devtools-v0.2.0) (2024-06-17) - ### Features -* **devtools:** init ([7c38d65](https://github.com/artalar/reatom/commit/7c38d65ea8b0a76589dec7423c17a63b7822fa9e)) +- **devtools:** init ([7c38d65](https://github.com/artalar/reatom/commit/7c38d65ea8b0a76589dec7423c17a63b7822fa9e)) diff --git a/packages/effects/CHANGELOG.md b/packages/effects/CHANGELOG.md index efb82d81f..f032b7175 100644 --- a/packages/effects/CHANGELOG.md +++ b/packages/effects/CHANGELOG.md @@ -2,171 +2,150 @@ ## [3.8.3](https://github.com/artalar/reatom/compare/effects-v3.8.2...effects-v3.8.3) (2024-07-16) - ### Bug Fixes -* **effects:** concurrent outdated abort handling ([23f177e](https://github.com/artalar/reatom/commit/23f177e804546a8d43530a4c0dc764c162737513)) +- **effects:** concurrent outdated abort handling ([23f177e](https://github.com/artalar/reatom/commit/23f177e804546a8d43530a4c0dc764c162737513)) ## [3.8.2](https://github.com/artalar/reatom/compare/effects-v3.8.1...effects-v3.8.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.8.1](https://github.com/artalar/reatom/compare/effects-v3.8.0...effects-v3.8.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.8.0](https://github.com/artalar/reatom/compare/effects-v3.7.3...effects-v3.8.0) (2024-06-04) - ### Features -* **effects:** add reaction API ([4c9d589](https://github.com/artalar/reatom/commit/4c9d5892f733e04e575937133eca3ec51424759f)) - +- **effects:** add reaction API ([4c9d589](https://github.com/artalar/reatom/commit/4c9d5892f733e04e575937133eca3ec51424759f)) ### Bug Fixes -* **effects:** allow to use "concurrent" with CtxSpy ([4da1ab0](https://github.com/artalar/reatom/commit/4da1ab04ecbd1de66d783c2bad2da6671981905a)) +- **effects:** allow to use "concurrent" with CtxSpy ([4da1ab0](https://github.com/artalar/reatom/commit/4da1ab04ecbd1de66d783c2bad2da6671981905a)) ## [3.7.3](https://github.com/artalar/reatom/compare/effects-v3.7.2...effects-v3.7.3) (2024-05-03) - ### Bug Fixes -* **effects:** missed abort... ([66d2afd](https://github.com/artalar/reatom/commit/66d2afd9bb6cfc1ababdffb7b547b7cee3e9425b)) +- **effects:** missed abort... ([66d2afd](https://github.com/artalar/reatom/commit/66d2afd9bb6cfc1ababdffb7b547b7cee3e9425b)) ## [3.7.2](https://github.com/artalar/reatom/compare/effects-v3.7.1...effects-v3.7.2) (2023-11-25) - ### Bug Fixes -* **effects:** more friendly typings for isCausedBy ([5d60c1d](https://github.com/artalar/reatom/commit/5d60c1da8710c90df60b24e5d013e829455260d0)) -* **effects:** prevent unhandled error for abort ([fe884e2](https://github.com/artalar/reatom/commit/fe884e24ac574fc50c7ce4e825459d7059136b73)) +- **effects:** more friendly typings for isCausedBy ([5d60c1d](https://github.com/artalar/reatom/commit/5d60c1da8710c90df60b24e5d013e829455260d0)) +- **effects:** prevent unhandled error for abort ([fe884e2](https://github.com/artalar/reatom/commit/fe884e24ac574fc50c7ce4e825459d7059136b73)) ## [3.7.1](https://github.com/artalar/reatom/compare/effects-v3.7.0...effects-v3.7.1) (2023-11-23) - ### Bug Fixes -* **effects:** concurrent.abortControllerAtom name ([25bdc47](https://github.com/artalar/reatom/commit/25bdc479e62045f946aeee6b9e001a8cb3450a07)) -* **effects:** prevent uncaught rejection for the abort of the concurrent API ([028bf10](https://github.com/artalar/reatom/commit/028bf10baa38bcd85d6ae7445c8294160b50ca07)) +- **effects:** concurrent.abortControllerAtom name ([25bdc47](https://github.com/artalar/reatom/commit/25bdc479e62045f946aeee6b9e001a8cb3450a07)) +- **effects:** prevent uncaught rejection for the abort of the concurrent API ([028bf10](https://github.com/artalar/reatom/commit/028bf10baa38bcd85d6ae7445c8294160b50ca07)) ## [3.7.0](https://github.com/artalar/reatom/compare/effects-v3.6.0...effects-v3.7.0) (2023-11-20) - ### Features -* **effects:** add abortCauseContext ([af28718](https://github.com/artalar/reatom/commit/af28718598a852ba7926e54cd5f1b6a508441951)) -* **effects:** add concurrent ([21b824b](https://github.com/artalar/reatom/commit/21b824b939bd6bd57b3d33d8eaa91ea67d784c41)) -* **effects:** add spawn ([2eab5cb](https://github.com/artalar/reatom/commit/2eab5cbc6b26450b09ed43ad9cb815a997950c1d)) - +- **effects:** add abortCauseContext ([af28718](https://github.com/artalar/reatom/commit/af28718598a852ba7926e54cd5f1b6a508441951)) +- **effects:** add concurrent ([21b824b](https://github.com/artalar/reatom/commit/21b824b939bd6bd57b3d33d8eaa91ea67d784c41)) +- **effects:** add spawn ([2eab5cb](https://github.com/artalar/reatom/commit/2eab5cbc6b26450b09ed43ad9cb815a997950c1d)) ### Bug Fixes -* **effects:** concurrent nested aborts ([6cc5dc4](https://github.com/artalar/reatom/commit/6cc5dc45c8f6bb9e51c2fd76c7b6dda7352ce4c2)) +- **effects:** concurrent nested aborts ([6cc5dc4](https://github.com/artalar/reatom/commit/6cc5dc45c8f6bb9e51c2fd76c7b6dda7352ce4c2)) ## [3.6.0](https://github.com/artalar/reatom/compare/effects-v3.5.1...effects-v3.6.0) (2023-10-20) - ### Features -* **effects:** add isInit method ([4a8aeb1](https://github.com/artalar/reatom/commit/4a8aeb14d0cdf54a545dda498c026e8f9b7c29d2)) -* **effects:** skip mark for take filter ([c136bd8](https://github.com/artalar/reatom/commit/c136bd884df59715ea8a4028e29eaa3e1dc6b076)) - +- **effects:** add isInit method ([4a8aeb1](https://github.com/artalar/reatom/commit/4a8aeb14d0cdf54a545dda498c026e8f9b7c29d2)) +- **effects:** skip mark for take filter ([c136bd8](https://github.com/artalar/reatom/commit/c136bd884df59715ea8a4028e29eaa3e1dc6b076)) ### Bug Fixes -* **effects:** withAbortableSchedule ([399d36f](https://github.com/artalar/reatom/commit/399d36ffb00d3597fa9c234358b8c50a6aeb8a7a)) +- **effects:** withAbortableSchedule ([399d36f](https://github.com/artalar/reatom/commit/399d36ffb00d3597fa9c234358b8c50a6aeb8a7a)) ## [3.5.1](https://github.com/artalar/reatom/compare/effects-v3.5.0...effects-v3.5.1) (2023-09-14) - ### Bug Fixes -* **effects:** withAbortableSchedule sync abort ([ebede8d](https://github.com/artalar/reatom/commit/ebede8d8f652da58bf2e29d6b5ec58966199059b)) +- **effects:** withAbortableSchedule sync abort ([ebede8d](https://github.com/artalar/reatom/commit/ebede8d8f652da58bf2e29d6b5ec58966199059b)) ## [3.5.0](https://github.com/artalar/reatom/compare/effects-v3.4.0...effects-v3.5.0) (2023-09-13) - ### Features -* **effects:** add withAbortableSchedule ([9e219a7](https://github.com/artalar/reatom/commit/9e219a7d61c18cc15bcff28f310938166d10de2c)) - +- **effects:** add withAbortableSchedule ([9e219a7](https://github.com/artalar/reatom/commit/9e219a7d61c18cc15bcff28f310938166d10de2c)) ### Bug Fixes -* **effects:** prevent Uncaught DOMException for aborts ([79f719b](https://github.com/artalar/reatom/commit/79f719bbdd6e97cb56c3399a841c33764822d598)) +- **effects:** prevent Uncaught DOMException for aborts ([79f719b](https://github.com/artalar/reatom/commit/79f719bbdd6e97cb56c3399a841c33764822d598)) ## [3.4.0](https://github.com/artalar/reatom/compare/effects-v3.3.1...effects-v3.4.0) (2023-08-05) - ### Features -* **effects:** add CauseContext ([65baab5](https://github.com/artalar/reatom/commit/65baab5cdc1256619b1fa779376f3e7508fc0c8d)) -* **effects:** add isCausedBy ([02a6406](https://github.com/artalar/reatom/commit/02a64069e272387cb64b1573a765a3d70abac825)) - +- **effects:** add CauseContext ([65baab5](https://github.com/artalar/reatom/commit/65baab5cdc1256619b1fa779376f3e7508fc0c8d)) +- **effects:** add isCausedBy ([02a6406](https://github.com/artalar/reatom/commit/02a64069e272387cb64b1573a765a3d70abac825)) ### Bug Fixes -* **effects:** add has method to CauseContext ([0b442c1](https://github.com/artalar/reatom/commit/0b442c1fdbb119c2828951aff0b97d490efdb397)) +- **effects:** add has method to CauseContext ([0b442c1](https://github.com/artalar/reatom/commit/0b442c1fdbb119c2828951aff0b97d490efdb397)) ## [3.3.1](https://github.com/artalar/reatom/compare/effects-v3.3.0...effects-v3.3.1) (2023-07-07) - ### Bug Fixes -* **effects:** __thenReatomed error propagation ([6a7536f](https://github.com/artalar/reatom/commit/6a7536f7b5afcad22fc90fc0afbfc7b71bfd71ec)) -* **effects:** improve onCtxAbort ([16ee497](https://github.com/artalar/reatom/commit/16ee497b08810aef908bcd7b2b2e7151d5f4ff12)) +- **effects:** \_\_thenReatomed error propagation ([6a7536f](https://github.com/artalar/reatom/commit/6a7536f7b5afcad22fc90fc0afbfc7b71bfd71ec)) +- **effects:** improve onCtxAbort ([16ee497](https://github.com/artalar/reatom/commit/16ee497b08810aef908bcd7b2b2e7151d5f4ff12)) ## [3.3.0](https://github.com/artalar/reatom/compare/effects-v3.2.1...effects-v3.3.0) (2023-06-12) - ### Features -* **effects:** change the behavior of __thenReatomed ([6a7ccba](https://github.com/artalar/reatom/commit/6a7ccba6a46521807d5d1e5eef3c3ad219454779)) - +- **effects:** change the behavior of \_\_thenReatomed ([6a7ccba](https://github.com/artalar/reatom/commit/6a7ccba6a46521807d5d1e5eef3c3ad219454779)) ### Bug Fixes -* **effects:** onCtxAbort for sync and async usage ([bc52760](https://github.com/artalar/reatom/commit/bc52760aa54d767744ba07ce17124d1f48f0a4ee)) +- **effects:** onCtxAbort for sync and async usage ([bc52760](https://github.com/artalar/reatom/commit/bc52760aa54d767744ba07ce17124d1f48f0a4ee)) ## [3.2.1](https://github.com/artalar/reatom/compare/effects-v3.2.0...effects-v3.2.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/effects/README.md b/packages/effects/README.md index 56b40fe6a..118ee5ebc 100644 --- a/packages/effects/README.md +++ b/packages/effects/README.md @@ -265,9 +265,7 @@ const someRequest = reatomRequest<{ data: Data } | { error: string }>() ```ts // type-safe destructuring -const { data } = await take(ctx, someRequest, (ctx, payload, skip) => - 'error' in payload ? skip : payload, -) +const { data } = await take(ctx, someRequest, (ctx, payload, skip) => ('error' in payload ? skip : payload)) ``` ### takeNested diff --git a/packages/eslint-plugin/.eslintrc.js b/packages/eslint-plugin/.eslintrc.js index 0377cb80b..6c7dde988 100644 --- a/packages/eslint-plugin/.eslintrc.js +++ b/packages/eslint-plugin/.eslintrc.js @@ -5,11 +5,7 @@ module.exports = { ecmaVersion: 'latest', }, root: true, - extends: [ - 'eslint:recommended', - 'plugin:eslint-plugin/recommended', - 'plugin:node/recommended', - ], + extends: ['eslint:recommended', 'plugin:eslint-plugin/recommended', 'plugin:node/recommended'], env: { es6: true, }, diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index b618c571b..9b8251e68 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -2,87 +2,75 @@ ## [3.6.2](https://github.com/artalar/reatom/compare/eslint-plugin-v3.6.1...eslint-plugin-v3.6.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.6.1](https://github.com/artalar/reatom/compare/eslint-plugin-v3.6.0...eslint-plugin-v3.6.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.6.0](https://github.com/artalar/reatom/compare/eslint-plugin-v3.5.0...eslint-plugin-v3.6.0) (2024-02-16) - ### Features -* **eslint-plugin:** enforce domain in names ([#764](https://github.com/artalar/reatom/issues/764)) ([0b4ccff](https://github.com/artalar/reatom/commit/0b4ccffa80fa738233f2160fc9b3a70269c6d4d5)) +- **eslint-plugin:** enforce domain in names ([#764](https://github.com/artalar/reatom/issues/764)) ([0b4ccff](https://github.com/artalar/reatom/commit/0b4ccffa80fa738233f2160fc9b3a70269c6d4d5)) ## [3.5.0](https://github.com/artalar/reatom/compare/eslint-plugin-v3.4.3...eslint-plugin-v3.5.0) (2024-02-05) - ### Features -* **eslint-plugin:** add async-rule & unit-naming-rule ([#709](https://github.com/artalar/reatom/issues/709)) ([04de10c](https://github.com/artalar/reatom/commit/04de10cb9bd3d39d2deec3ed1a5e0aface9de3c9)) +- **eslint-plugin:** add async-rule & unit-naming-rule ([#709](https://github.com/artalar/reatom/issues/709)) ([04de10c](https://github.com/artalar/reatom/commit/04de10cb9bd3d39d2deec3ed1a5e0aface9de3c9)) ## [3.4.3](https://github.com/artalar/reatom/compare/eslint-plugin-v3.4.2...eslint-plugin-v3.4.3) (2023-10-20) - ### Bug Fixes -* **lens:** match default case ([27061af](https://github.com/artalar/reatom/commit/27061af1ac338f5223190d110cf6e090d884e203)) +- **lens:** match default case ([27061af](https://github.com/artalar/reatom/commit/27061af1ac338f5223190d110cf6e090d884e203)) ## [3.4.2](https://github.com/artalar/reatom/compare/eslint-plugin-v3.4.1...eslint-plugin-v3.4.2) (2023-10-10) - ### Bug Fixes -* **eslint-plugin:** allow any template literal ([#661](https://github.com/artalar/reatom/issues/661)) ([5539cdd](https://github.com/artalar/reatom/commit/5539cddb5628de56f7e7f011c41ec4230d7a4ae5)) -* **eslint-plugin:** allow explicit empty atomPostfix ([7bac17f](https://github.com/artalar/reatom/commit/7bac17f83350349b8e428688a1aceb16cfefbe11)) +- **eslint-plugin:** allow any template literal ([#661](https://github.com/artalar/reatom/issues/661)) ([5539cdd](https://github.com/artalar/reatom/commit/5539cddb5628de56f7e7f011c41ec4230d7a4ae5)) +- **eslint-plugin:** allow explicit empty atomPostfix ([7bac17f](https://github.com/artalar/reatom/commit/7bac17f83350349b8e428688a1aceb16cfefbe11)) ## [3.4.1](https://github.com/artalar/reatom/compare/eslint-plugin-v3.4.0...eslint-plugin-v3.4.1) (2023-09-13) - ### Bug Fixes -* **esling-plugin:** highlight atom name, not atom declaration in case of wrong name ([#624](https://github.com/artalar/reatom/issues/624)) ([39b0d01](https://github.com/artalar/reatom/commit/39b0d015b6de6a5cfa83da17871bbcb52e101994)) +- **esling-plugin:** highlight atom name, not atom declaration in case of wrong name ([#624](https://github.com/artalar/reatom/issues/624)) ([39b0d01](https://github.com/artalar/reatom/commit/39b0d015b6de6a5cfa83da17871bbcb52e101994)) ## [3.4.0](https://github.com/artalar/reatom/compare/eslint-plugin-v3.3.0...eslint-plugin-v3.4.0) (2023-05-21) - ### Features -* **eslint-plugin:** postfix atom rule ([#552](https://github.com/artalar/reatom/issues/552)) ([80c8710](https://github.com/artalar/reatom/commit/80c8710e3710b6c6696a0540e65dd90a44112343)) - +- **eslint-plugin:** postfix atom rule ([#552](https://github.com/artalar/reatom/issues/552)) ([80c8710](https://github.com/artalar/reatom/commit/80c8710e3710b6c6696a0540e65dd90a44112343)) ### Bug Fixes -* **eslint-plugin:** [#541](https://github.com/artalar/reatom/issues/541) not fired inside the object ([#546](https://github.com/artalar/reatom/issues/546)) ([e6c6778](https://github.com/artalar/reatom/commit/e6c677860697e020d7715426484c8348d1552342)) -* **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **eslint-plugin:** [#541](https://github.com/artalar/reatom/issues/541) not fired inside the object ([#546](https://github.com/artalar/reatom/issues/546)) ([e6c6778](https://github.com/artalar/reatom/commit/e6c677860697e020d7715426484c8348d1552342)) +- **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) ## [3.3.0](https://github.com/artalar/reatom/compare/eslint-plugin-v3.1.3...eslint-plugin-v3.2.0) (2023-05-10) - ### Features -* **eslint-plugin:** postfix atom rule ([#552](https://github.com/artalar/reatom/issues/552)) ([80c8710](https://github.com/artalar/reatom/commit/80c8710e3710b6c6696a0540e65dd90a44112343)) +- **eslint-plugin:** postfix atom rule ([#552](https://github.com/artalar/reatom/issues/552)) ([80c8710](https://github.com/artalar/reatom/commit/80c8710e3710b6c6696a0540e65dd90a44112343)) ## [3.1.3](https://github.com/artalar/reatom/compare/eslint-plugin-v3.1.2...eslint-plugin-v3.1.3) (2023-04-18) - ### Bug Fixes -* **eslint-plugin:** [#541](https://github.com/artalar/reatom/issues/541) not fired inside the object ([#546](https://github.com/artalar/reatom/issues/546)) ([e6c6778](https://github.com/artalar/reatom/commit/e6c677860697e020d7715426484c8348d1552342)) -* **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) +- **eslint-plugin:** [#541](https://github.com/artalar/reatom/issues/541) not fired inside the object ([#546](https://github.com/artalar/reatom/issues/546)) ([e6c6778](https://github.com/artalar/reatom/commit/e6c677860697e020d7715426484c8348d1552342)) +- **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) ## [3.1.1](https://github.com/artalar/reatom/compare/eslint-plugin-v3.1.0...eslint-plugin-v3.1.1) (2023-04-10) - ### Bug Fixes -* **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) +- **eslint-plugin:** the detection of uncorrect name when importing through aliases ([#538](https://github.com/artalar/reatom/issues/538)) ([c23d920](https://github.com/artalar/reatom/commit/c23d920c8efe80e80fc3a66fdd1f626f7a0c180b)) diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index b41220b47..8df0bcece 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -84,13 +84,7 @@ const someNamespace = { When creating atoms dynamically with factories, you can also specify the "namespace" of the name before the `.` symbol: ```ts -const reatomFood = (config: { - name: string - calories: number - fat: number - carbs: number - protein: number -}) => { +const reatomFood = (config: { name: string; calories: number; fat: number; carbs: number; protein: number }) => { const { name } = config.name const calories = atom(config.calories, `${name}.calories`) const fat = atom(config.fat, `${name}.fat`) diff --git a/packages/eslint-plugin/src/rules/async-rule.test.ts b/packages/eslint-plugin/src/rules/async-rule.test.ts index 483ec6454..d266a488f 100644 --- a/packages/eslint-plugin/src/rules/async-rule.test.ts +++ b/packages/eslint-plugin/src/rules/async-rule.test.ts @@ -9,8 +9,7 @@ const tester = new RuleTester({ }) const ImportReatomAsync = 'import {reatomAsync} from "@reatom/framework"' -const ImportReatomAsyncAlias = - 'import {reatomAsync as createAsync} from "@reatom/framework"' +const ImportReatomAsyncAlias = 'import {reatomAsync as createAsync} from "@reatom/framework"' tester.run('async-rule', asyncRule, { valid: [ diff --git a/packages/eslint-plugin/src/rules/async-rule.ts b/packages/eslint-plugin/src/rules/async-rule.ts index f8cf3954e..4032dff2a 100644 --- a/packages/eslint-plugin/src/rules/async-rule.ts +++ b/packages/eslint-plugin/src/rules/async-rule.ts @@ -9,12 +9,10 @@ export const asyncRule: Rule.RuleModule = { type: 'suggestion', docs: { recommended: true, - description: - 'Ensures that asynchronous interactions within Reatom functions are wrapped with `ctx.schedule`.', + description: 'Ensures that asynchronous interactions within Reatom functions are wrapped with `ctx.schedule`.', }, messages: { - scheduleMissing: - 'Asynchronous interactions within Reatom functions should be wrapped with `ctx.schedule`', + scheduleMissing: 'Asynchronous interactions within Reatom functions should be wrapped with `ctx.schedule`', }, fixable: 'code', }, @@ -54,10 +52,7 @@ const isCtxSchedule = (node: estree.Node) => { ) } -const wrapScheduleFix = ( - fixer: Rule.RuleFixer, - node: estree.AwaitExpression, -) => [ +const wrapScheduleFix = (fixer: Rule.RuleFixer, node: estree.AwaitExpression) => [ fixer.insertTextBefore(node.argument, 'ctx.schedule(() => '), fixer.insertTextAfter(node.argument, ')'), ] diff --git a/packages/eslint-plugin/src/rules/unit-naming-rule.ts b/packages/eslint-plugin/src/rules/unit-naming-rule.ts index 392be1a73..c0d83d0b0 100644 --- a/packages/eslint-plugin/src/rules/unit-naming-rule.ts +++ b/packages/eslint-plugin/src/rules/unit-naming-rule.ts @@ -1,11 +1,6 @@ import * as estree from 'estree' import { Rule } from 'eslint' -import { - ascend, - createImportMap, - getFunctionNameDeclarations, - isReatomFactoryName, -} from '../shared' +import { ascend, createImportMap, getFunctionNameDeclarations, isReatomFactoryName } from '../shared' type AutoDomain = typeof AutoDomain const AutoDomain = Symbol('AutoDomain') @@ -87,9 +82,7 @@ function checkNaming({ if ( unitBody.type !== 'CallExpression' || unitBody.callee.type !== 'Identifier' || - !isReatomFactoryName( - imports.get(unitBody.callee.name) || unitBody.callee.name, - ) + !isReatomFactoryName(imports.get(unitBody.callee.name) || unitBody.callee.name) ) { return } @@ -97,11 +90,7 @@ function checkNaming({ const [initArg, nameArg] = unitBody.arguments if (!initArg) return - let factory: - | estree.FunctionDeclaration - | estree.FunctionExpression - | estree.ArrowFunctionExpression - | undefined + let factory: estree.FunctionDeclaration | estree.FunctionExpression | estree.ArrowFunctionExpression | undefined let domain: string | null | AutoDomain = null setFactory: { @@ -121,9 +110,7 @@ function checkNaming({ } } - const nameVaryDeclared = factory - ? getFunctionNameDeclarations(factory, ['name']).length === 1 - : false + const nameVaryDeclared = factory ? getFunctionNameDeclarations(factory, ['name']).length === 1 : false if (nameVaryDeclared) domain = AutoDomain if (!nameArg) { @@ -150,19 +137,15 @@ function checkNaming({ if (nameArg.type !== 'TemplateLiteral') break validate if (nameArg.expressions.length !== 1) break validate - const [emptyQuasy, nameSelf] = nameArg.quasis.map( - (quasy) => quasy.value.cooked, - ) + const [emptyQuasy, nameSelf] = nameArg.quasis.map((quasy) => quasy.value.cooked) const nameDomain = nameArg.expressions[0]! if (emptyQuasy !== '') break validate - if (nameDomain.type !== 'Identifier' || nameDomain.name !== 'name') - break validate + if (nameDomain.type !== 'Identifier' || nameDomain.name !== 'name') break validate if (!nameSelf!.startsWith('.')) break validate isPrivate = nameSelf![1] === '_' - if (nameSelf!.slice(isPrivate ? 2 : 1) !== unitIdentifier.name) - break validate + if (nameSelf!.slice(isPrivate ? 2 : 1) !== unitIdentifier.name) break validate valid = true } @@ -179,8 +162,7 @@ function checkNaming({ if (nameDomain !== domain) break validate isPrivate = nameSelf![0] === '_' - if (nameSelf!.slice(isPrivate ? 1 : 0) !== unitIdentifier.name) - break validate + if (nameSelf!.slice(isPrivate ? 1 : 0) !== unitIdentifier.name) break validate valid = true } @@ -217,10 +199,7 @@ function checkNaming({ isPrivate, domain, }) - return [ - fixer.replaceText(nameArg, x), - fixer.replaceText(unitIdentifier, atomPrefix + unitIdentifier.name), - ] + return [fixer.replaceText(nameArg, x), fixer.replaceText(unitIdentifier, atomPrefix + unitIdentifier.name)] }, }) return @@ -235,13 +214,7 @@ function checkNaming({ isPrivate, domain, }) - return [ - fixer.replaceText(nameArg, x), - fixer.replaceText( - unitIdentifier, - unitIdentifier.name + atomPostfix, - ), - ] + return [fixer.replaceText(nameArg, x), fixer.replaceText(unitIdentifier, unitIdentifier.name + atomPostfix)] }, }) return diff --git a/packages/eslint-plugin/src/shared.ts b/packages/eslint-plugin/src/shared.ts index 4ff2a7fb0..0f59484eb 100644 --- a/packages/eslint-plugin/src/shared.ts +++ b/packages/eslint-plugin/src/shared.ts @@ -40,10 +40,7 @@ export const createImportMap = (packagePrefix: string) => { } } -export const ascend = ( - node: estree.Node, - ...types: Array -) => { +export const ascend = (node: estree.Node, ...types: Array) => { while (node && !types.includes(node.type as any)) { node = (node as any).parent } @@ -52,10 +49,7 @@ export const ascend = ( } export const getFunctionNameDeclarations = ( - fn: - | estree.FunctionExpression - | estree.ArrowFunctionExpression - | estree.FunctionDeclaration, + fn: estree.FunctionExpression | estree.ArrowFunctionExpression | estree.FunctionDeclaration, names: string[], ) => { const stack = [...fn.params] as estree.Node[] diff --git a/packages/esm-import-check/src/hello.ts b/packages/esm-import-check/src/hello.ts index 85369f0e5..2739b7773 100644 --- a/packages/esm-import-check/src/hello.ts +++ b/packages/esm-import-check/src/hello.ts @@ -1,4 +1,4 @@ -import {atom, createCtx} from '@reatom/framework' +import { atom, createCtx } from '@reatom/framework' const me = atom('check βœ…', 'me') const ctx = createCtx() -console.log('ESM import:', ctx.get(me)) \ No newline at end of file +console.log('ESM import:', ctx.get(me)) diff --git a/packages/form-web/CHANGELOG.md b/packages/form-web/CHANGELOG.md index ca068de20..5a082692e 100644 --- a/packages/form-web/CHANGELOG.md +++ b/packages/form-web/CHANGELOG.md @@ -2,64 +2,59 @@ ## [3.2.2](https://github.com/artalar/reatom/compare/form-web-v3.2.1...form-web-v3.2.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.2.1](https://github.com/artalar/reatom/compare/form-web-v3.2.0...form-web-v3.2.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.2.0](https://github.com/artalar/reatom/compare/form-web-v3.1.5...form-web-v3.2.0) (2023-11-05) - ### Features -* **form-web:** sync state changes with the element ([8e097f6](https://github.com/artalar/reatom/commit/8e097f6e26b24ba9b081cd9ca5819457d0f3038a)) +- **form-web:** sync state changes with the element ([8e097f6](https://github.com/artalar/reatom/commit/8e097f6e26b24ba9b081cd9ca5819457d0f3038a)) ## [3.1.5](https://github.com/artalar/reatom/compare/form-web-v3.1.4...form-web-v3.1.5) (2023-10-09) - ### Bug Fixes -* **form-web:** keep original element type ([#651](https://github.com/artalar/reatom/issues/651)) ([8b35c31](https://github.com/artalar/reatom/commit/8b35c31dfb8c2eef7b5a00d2dbf6218d69b374b6)) +- **form-web:** keep original element type ([#651](https://github.com/artalar/reatom/issues/651)) ([8b35c31](https://github.com/artalar/reatom/commit/8b35c31dfb8c2eef7b5a00d2dbf6218d69b374b6)) ## [3.1.4](https://github.com/artalar/reatom/compare/form-web-v3.1.3...form-web-v3.1.4) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/form-web/src/index.ts b/packages/form-web/src/index.ts index 4d9b2e824..6c9967247 100644 --- a/packages/form-web/src/index.ts +++ b/packages/form-web/src/index.ts @@ -1,57 +1,26 @@ -import { - action, - Action, - Atom, - atom, - AtomMut, - Ctx, - Fn, - Rec, - throwReatomError, - Unsubscribe, -} from '@reatom/core' +import { action, Action, Atom, atom, AtomMut, Ctx, Fn, Rec, throwReatomError, Unsubscribe } from '@reatom/core' import { addOnUpdate } from '@reatom/hooks' import { reatomRecord, RecordAtom } from '@reatom/primitives' import { isObject } from '@reatom/utils' -import { - FieldAtom, - FieldOptions, - Form, - FormOptions, - reatomForm, -} from '@reatom/form' - -export interface HTMLFieldOptions - extends FieldOptions { +import { FieldAtom, FieldOptions, Form, FormOptions, reatomForm } from '@reatom/form' + +export interface HTMLFieldOptions extends FieldOptions { // TODO is it possible to remove it? atomName?: string } -export interface HTMLFieldAtom< - T = string, - El extends HTMLElement = HTMLInputElement, -> extends FieldAtom { +export interface HTMLFieldAtom extends FieldAtom { // TODO rename to `propertiesAtom` or change the types to real attributes attributesAtom: RecordAtom> elementAtom: Atom register: Action<[null | El]> } -export type HTMLInputAttributes = Partial< - Omit -> & { +export type HTMLInputAttributes = Partial> & { type?: T } -export const UNSUPPORTED_INPUT_TYPES = [ - 'button', - 'file', - 'hidden', - 'image', - 'range', - 'submit', - 'datetime', -] as const +export const UNSUPPORTED_INPUT_TYPES = ['button', 'file', 'hidden', 'image', 'range', 'submit', 'datetime'] as const export const INPUT_TEXT_TYPES = [ 'color', @@ -81,10 +50,7 @@ const getType = (initState: unknown) => { return 'text' } -const getElementValue = ( - el: HTMLInputElement | HTMLSelectElement, - type: string, -) => { +const getElementValue = (el: HTMLInputElement | HTMLSelectElement, type: string) => { if (type === 'number') { return +(el as HTMLInputElement).value } @@ -125,18 +91,10 @@ const getAttributes = (el: HTMLElement) => // @ts-ignore export const withHtmlRegistration: { - ( - attributes?: HTMLInputAttributes, - ): Fn<[FieldAtom], HTMLFieldAtom> - ( - attributes?: HTMLInputAttributes<'number'>, - ): Fn<[FieldAtom], HTMLFieldAtom> - ( - attributes?: HTMLInputAttributes<'checkbox'>, - ): Fn<[FieldAtom], HTMLFieldAtom> - ( - attributes?: Partial, - ): Fn<[FieldAtom], HTMLFieldAtom> + (attributes?: HTMLInputAttributes): Fn<[FieldAtom], HTMLFieldAtom> + (attributes?: HTMLInputAttributes<'number'>): Fn<[FieldAtom], HTMLFieldAtom> + (attributes?: HTMLInputAttributes<'checkbox'>): Fn<[FieldAtom], HTMLFieldAtom> + (attributes?: Partial): Fn<[FieldAtom], HTMLFieldAtom> // TODO // >(options: { multiple?: boolean }): Fn<[T], HTMLFieldAtom> // >(options: { multiple?: boolean }): Fn<[T], HTMLFieldAtom> @@ -144,18 +102,11 @@ export const withHtmlRegistration: { } = (attributes: HTMLInputAttributes | Partial = {}) => (fieldAtom: FieldAtom) => { - const { type = getType(fieldAtom.initState) } = - attributes as HTMLInputAttributes + const { type = getType(fieldAtom.initState) } = attributes as HTMLInputAttributes const name = fieldAtom.__reatom.name?.replace('.dataAtom', '') // TODO onUpdate(fieldAtom.reset, (ctx) => attributesAtom(ctx, attributes)) - const attributesAtom = reatomRecord( - attributes, - name?.concat('.attributesAtom'), - ) - const elementAtom = atom( - null, - name?.concat('.elementAtom'), - ) + const attributesAtom = reatomRecord(attributes, name?.concat('.attributesAtom')) + const elementAtom = atom(null, name?.concat('.elementAtom')) const unsubscribeAtom = atom(null) const register: HTMLFieldAtom['register'] = action( @@ -192,9 +143,7 @@ export const withHtmlRegistration: { (ctx) => { const validation = ctx.get(fieldAtom.validationAtom) - return validation.validating || - !validation.valid || - el.checkValidity() + return validation.validating || !validation.valid || el.checkValidity() ? validation : fieldAtom.validationAtom(ctx, (state) => ({ ...state, @@ -213,10 +162,7 @@ export const withHtmlRegistration: { const changeInput = action( (ctx) => { const _lastInput = lastInput - ctx.schedule( - () => setElementValue(el, type, (lastInput = _lastInput)), - -1, - ) + ctx.schedule(() => setElementValue(el, type, (lastInput = _lastInput)), -1) const input = getElementValue(el, type) @@ -229,10 +175,7 @@ export const withHtmlRegistration: { name?.concat('.changeInput'), ) const handleChange = () => changeInput(ctx) - const handleChangeAttributes = ( - ctx: Ctx, - { state }: { state: typeof attributes }, - ) => { + const handleChangeAttributes = (ctx: Ctx, { state }: { state: typeof attributes }) => { Object.assign(el, state) ctx.schedule(() => { // TODO @@ -298,33 +241,18 @@ export type HTMLForm = Form & { (initState: string, name?: string): HTMLFieldAtom (initState: number, name?: string): HTMLFieldAtom (initState: boolean, name?: string): HTMLFieldAtom - ( - initState: [], - name?: string, - ): HTMLFieldAtom, HTMLSelectElement> - ( - initState: Array, - name?: string, - ): HTMLFieldAtom, HTMLSelectElement> + (initState: [], name?: string): HTMLFieldAtom, HTMLSelectElement> + (initState: Array, name?: string): HTMLFieldAtom, HTMLSelectElement> >( initState: T, name?: string, ): AtomMut & { [K in keyof T]: HTMLFieldAtom } - ( - options: HTMLFieldOptions & HTMLInputAttributes, - ): HTMLFieldAtom - ( - options: HTMLFieldOptions & HTMLInputAttributes<'number'>, - ): HTMLFieldAtom - ( - options: HTMLFieldOptions & - HTMLInputAttributes<'checkbox' | 'radio'>, - ): HTMLFieldAtom - ( - options: HTMLFieldOptions<[]> & Partial, - ): HTMLFieldAtom + (options: HTMLFieldOptions & HTMLInputAttributes): HTMLFieldAtom + (options: HTMLFieldOptions & HTMLInputAttributes<'number'>): HTMLFieldAtom + (options: HTMLFieldOptions & HTMLInputAttributes<'checkbox' | 'radio'>): HTMLFieldAtom + (options: HTMLFieldOptions<[]> & Partial): HTMLFieldAtom ( options: HTMLFieldOptions> & Partial, ): HTMLFieldAtom, HTMLSelectElement> @@ -334,17 +262,7 @@ export type HTMLForm = Form & { [K in keyof T]: HTMLFieldAtom } } - reatomHTMLFields< - T extends Rec< - | string - | number - | boolean - | [] - | Array - | Rec - | HTMLFieldOptions - >, - >( + reatomHTMLFields | Rec | HTMLFieldOptions>>( fieldsOptions: T, ): { [K in keyof T]: T[K] extends string @@ -361,21 +279,17 @@ export type HTMLForm = Form & { ? AtomMut & { [k in keyof T[K]]: HTMLFieldAtom } - : T[K] extends HTMLFieldOptions & - HTMLInputAttributes + : T[K] extends HTMLFieldOptions & HTMLInputAttributes ? HTMLFieldAtom : T[K] extends HTMLFieldOptions & HTMLInputAttributes<'number'> ? HTMLFieldAtom - : T[K] extends HTMLFieldOptions & - HTMLInputAttributes<'checkbox' | 'radio'> + : T[K] extends HTMLFieldOptions & HTMLInputAttributes<'checkbox' | 'radio'> ? HTMLFieldAtom : T[K] extends HTMLFieldOptions<[]> & Partial ? HTMLFieldAtom - : T[K] extends HTMLFieldOptions> & - Partial + : T[K] extends HTMLFieldOptions> & Partial ? HTMLFieldAtom, HTMLSelectElement> - : T[K] extends HTMLFieldOptions> & - HTMLInputAttributes<'radio'> + : T[K] extends HTMLFieldOptions> & HTMLInputAttributes<'radio'> ? AtomMut & { [k in keyof T[K]]: HTMLFieldAtom } : never } @@ -422,10 +336,7 @@ export const reatomHTMLForm = ({ const form = reatomForm(options) - const elementAtom = atom( - null, - name?.concat('.elementAtom'), - ) + const elementAtom = atom(null, name?.concat('.elementAtom')) const unsubscribeAtom = atom(null) const register: HTMLForm['register'] = action( @@ -459,16 +370,8 @@ export const reatomHTMLForm = ({ // @ts-expect-error const reatomHTMLField: HTMLForm['reatomHTMLField'] = (options, fieldName) => { - const { - filter, - initState, - validate, - validationTrigger, - ...attributes - }: HTMLFieldOptions = - isObject(options) && 'initState' in options - ? options - : { initState: options } + const { filter, initState, validate, validationTrigger, ...attributes }: HTMLFieldOptions = + isObject(options) && 'initState' in options ? options : { initState: options } attributes.name ??= fieldName const { atomName = attributes.name } = attributes @@ -476,10 +379,7 @@ export const reatomHTMLForm = ({ if (isObject(initState) && !Array.isArray(initState)) { const kv = Object.entries(initState) - throwReatomError( - kv.length === 0 || kv.some(([k, v]) => typeof v !== 'boolean'), - 'unexpected initState', - ) + throwReatomError(kv.length === 0 || kv.some(([k, v]) => typeof v !== 'boolean'), 'unexpected initState') // TODO throwReatomError(!attributes.name, 'radio required name') attributes.name ??= `radio${(Math.random() * 1e5) | 0}` @@ -500,11 +400,7 @@ export const reatomHTMLForm = ({ const theAtom = Object.assign( atom( - (ctx) => - kv.reduce( - (acc, [value]) => (ctx.spy(radios[value]) ? value : acc), - undefined, - ), + (ctx) => kv.reduce((acc, [value]) => (ctx.spy(radios[value]) ? value : acc), undefined), name ? `${name}.${attributes.name}` : attributes.name, ), radios, diff --git a/packages/form/CHANGELOG.md b/packages/form/CHANGELOG.md index b69eb9c79..be99bebb2 100644 --- a/packages/form/CHANGELOG.md +++ b/packages/form/CHANGELOG.md @@ -2,70 +2,64 @@ ## [3.3.2](https://github.com/artalar/reatom/compare/form-v3.3.1...form-v3.3.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.3.1](https://github.com/artalar/reatom/compare/form-v3.3.0...form-v3.3.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.3.0](https://github.com/artalar/reatom/compare/form-v3.2.0...form-v3.3.0) (2023-11-07) - ### Features -* **form:** correct "valid" behavior ([a47f1f2](https://github.com/artalar/reatom/commit/a47f1f2c99fb2f45ab900721ef3806dc47961916)) +- **form:** correct "valid" behavior ([a47f1f2](https://github.com/artalar/reatom/commit/a47f1f2c99fb2f45ab900721ef3806dc47961916)) ## [3.2.0](https://github.com/artalar/reatom/compare/form-v3.1.6...form-v3.2.0) (2023-10-31) - ### Features -* **form:** improve form validation and field value handling ([cf0e297](https://github.com/artalar/reatom/commit/cf0e297e674e1e481fe877477a0f1bb3a4c1d6c8)) -* **form:** many docs, small refactoring ([2c2952b](https://github.com/artalar/reatom/commit/2c2952b583701cbaa56f9786b6548cde8b069d13)) - +- **form:** improve form validation and field value handling ([cf0e297](https://github.com/artalar/reatom/commit/cf0e297e674e1e481fe877477a0f1bb3a4c1d6c8)) +- **form:** many docs, small refactoring ([2c2952b](https://github.com/artalar/reatom/commit/2c2952b583701cbaa56f9786b6548cde8b069d13)) ### Bug Fixes -* **form:** touched behavior ([caaedd1](https://github.com/artalar/reatom/commit/caaedd11fa5b938731e298c2b6ee484408b91eb5)) +- **form:** touched behavior ([caaedd1](https://github.com/artalar/reatom/commit/caaedd11fa5b938731e298c2b6ee484408b91eb5)) ## [3.1.6](https://github.com/artalar/reatom/compare/form-v3.1.5...form-v3.1.6) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/form/README.md b/packages/form/README.md index cf2174a85..ee66285cb 100644 --- a/packages/form/README.md +++ b/packages/form/README.md @@ -33,8 +33,7 @@ export const passwordField = loginForm.reatomField( { initState: '', validate(ctx, { state }) { - if (state.length < 6) - throw new Error('The password should have at least six characters.') + if (state.length < 6) throw new Error('The password should have at least six characters.') }, }, 'passwordField', diff --git a/packages/form/src/reatomField.ts b/packages/form/src/reatomField.ts index e882435ce..9a8dc03fa 100644 --- a/packages/form/src/reatomField.ts +++ b/packages/form/src/reatomField.ts @@ -1,13 +1,4 @@ -import { - action, - Action, - Atom, - atom, - AtomMut, - Ctx, - __count, - AtomCache, -} from '@reatom/core' +import { action, Action, Atom, atom, AtomMut, Ctx, __count, AtomCache } from '@reatom/core' import { __thenReatomed } from '@reatom/effects' import { RecordAtom, reatomRecord } from '@reatom/primitives' import { isDeepEqual, noop, toAbortError } from '@reatom/utils' @@ -52,9 +43,7 @@ export interface FieldActions { validate: Action<[], FieldValidation> } -export interface FieldAtom - extends AtomMut, - FieldActions { +export interface FieldAtom extends AtomMut, FieldActions { /** Atom of an object with all related focus statuses. */ focusAtom: RecordAtom @@ -184,15 +173,9 @@ export const reatomField = ( interface This extends FieldAtom {} const fieldAtom = atom(initState, `${name}.fieldAtom`) as This - const valueAtom: This['valueAtom'] = atom( - (ctx) => fromState(ctx, ctx.spy(fieldAtom)), - `${name}.valueAtom`, - ) + const valueAtom: This['valueAtom'] = atom((ctx) => fromState(ctx, ctx.spy(fieldAtom)), `${name}.valueAtom`) - const focusAtom: This['focusAtom'] = reatomRecord( - fieldInitFocus, - `${name}.focusAtom`, - ) + const focusAtom: This['focusAtom'] = reatomRecord(fieldInitFocus, `${name}.focusAtom`) // @ts-expect-error focusAtom.__reatom.computer = (ctx, state: FieldFocus) => { const dirty = isDirty(ctx, ctx.spy(valueAtom), fromState(ctx, initState)) @@ -211,10 +194,7 @@ export const reatomField = ( } } - const validateControllerAtom = atom( - new AbortController(), - `${name}.validateControllerAtom`, - ) + const validateControllerAtom = atom(new AbortController(), `${name}.validateControllerAtom`) // prevent collisions for different contexts validateControllerAtom.__reatom.initState = () => new AbortController() const validate: This['validate'] = action((ctx) => { @@ -224,10 +204,7 @@ export const reatomField = ( if (!validateFn) return validationAtom.merge(ctx, { valid: true }) ctx.get(validateControllerAtom).abort(toAbortError('concurrent')) - const controller = validateControllerAtom( - ctx, - ((ctx.cause as AbortableCause).controller = new AbortController()), - ) + const controller = validateControllerAtom(ctx, ((ctx.cause as AbortableCause).controller = new AbortController())) const state = ctx.get(fieldAtom) const value = ctx.get(valueAtom) diff --git a/packages/form/src/reatomForm.ts b/packages/form/src/reatomForm.ts index 17ac45d42..1638d113a 100644 --- a/packages/form/src/reatomForm.ts +++ b/packages/form/src/reatomForm.ts @@ -15,8 +15,7 @@ import { AsyncAction, reatomAsync, withAbort } from '@reatom/async' import { reatomRecord } from '@reatom/primitives' import { toError } from './utils' -export interface FormFieldAtom - extends FieldAtom { +export interface FormFieldAtom extends FieldAtom { remove: Action<[], void> } @@ -28,10 +27,7 @@ export interface Form { /** Submit async handler. It checks the validation of all the fields in `fieldsListAtom`, calls the form's `validate` options handler, and then the `onSubmit` options handler. Check the additional options properties of async action: https://www.reatom.dev/package/async/. */ onSubmit: AsyncAction<[], void> /** The same `reatomField` method, but with bindings to `fieldsListAtom`. */ - reatomField( - options: FieldOptions, - name?: string, - ): FormFieldAtom + reatomField(options: FieldOptions, name?: string): FormFieldAtom /** Action to reset the state, the value, the validation, and the focus states. */ reset: Action<[], void> /** Atom with validation state of the form, computed from all the fields in `fieldsListAtom` */ @@ -55,10 +51,7 @@ export const reatomForm = ( // this is out of the options for eslint compatibility name = optionsName ?? __count('form'), ): Form => { - const fieldsListAtom = atom>( - [], - `${name}.fieldsListAtom`, - ) + const fieldsListAtom = atom>([], `${name}.fieldsListAtom`) const focusAtom = atom((ctx, state = fieldInitFocus) => { const formFocus = { ...fieldInitFocus } for (const fieldAtom of ctx.spy(fieldsListAtom)) { @@ -107,11 +100,7 @@ export const reatomForm = ( let { valid, validating } = ctx.get(validationAtom) if (validating) { - valid = await take( - ctx, - validationAtom, - (ctx, { validating, valid }, skip) => (validating ? skip : valid), - ) + valid = await take(ctx, validationAtom, (ctx, { validating, valid }, skip) => (validating ? skip : valid)) } if (valid) { diff --git a/packages/form/src/utils.ts b/packages/form/src/utils.ts index 89b6035a6..d8afa1dbb 100644 --- a/packages/form/src/utils.ts +++ b/packages/form/src/utils.ts @@ -1,2 +1 @@ -export const toError = (thing: unknown) => - thing instanceof Error ? thing.message : String(thing ?? 'Unknown error') +export const toError = (thing: unknown) => (thing instanceof Error ? thing.message : String(thing ?? 'Unknown error')) diff --git a/packages/form/vite.config.ts b/packages/form/vite.config.ts index b1b5f91e5..5a33944a9 100644 --- a/packages/form/vite.config.ts +++ b/packages/form/vite.config.ts @@ -3,5 +3,5 @@ import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [react()] + plugins: [react()], }) diff --git a/packages/framework/src/index.test.ts b/packages/framework/src/index.test.ts index f78065116..243d3c454 100644 --- a/packages/framework/src/index.test.ts +++ b/packages/framework/src/index.test.ts @@ -11,16 +11,7 @@ import * as primitives from '@reatom/primitives' import * as utils from '@reatom/utils' test(`base API`, async () => { - const packages = [ - async, - core, - effects, - hooks, - lens, - logger, - primitives, - utils, - ] + const packages = [async, core, effects, hooks, lens, logger, primitives, utils] const allExports = packages .reduce((acc, v) => [...acc, ...Object.keys(v)], new Array()) diff --git a/packages/hooks/CHANGELOG.md b/packages/hooks/CHANGELOG.md index bafce3682..bbd8e1f38 100644 --- a/packages/hooks/CHANGELOG.md +++ b/packages/hooks/CHANGELOG.md @@ -2,108 +2,97 @@ ## [3.5.5](https://github.com/artalar/reatom/compare/hooks-v3.5.4...hooks-v3.5.5) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.5.4](https://github.com/artalar/reatom/compare/hooks-v3.5.3...hooks-v3.5.4) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.5.3](https://github.com/artalar/reatom/compare/hooks-v3.5.2...hooks-v3.5.3) (2024-03-06) - ### Bug Fixes -* **hooks:** correct isInit behaviour ([#775](https://github.com/artalar/reatom/issues/775)) ([f61d4ac](https://github.com/artalar/reatom/commit/f61d4acbe5603628f18c84f1ef6b3d869ec83b71)) +- **hooks:** correct isInit behaviour ([#775](https://github.com/artalar/reatom/issues/775)) ([f61d4ac](https://github.com/artalar/reatom/commit/f61d4acbe5603628f18c84f1ef6b3d869ec83b71)) ## [3.5.2](https://github.com/artalar/reatom/compare/hooks-v3.5.1...hooks-v3.5.2) (2023-11-23) - ### Bug Fixes -* **hooks:** improve isInit perf ([be6a829](https://github.com/artalar/reatom/commit/be6a82984f798fd63786bace9a324bdf3d8ff3c6)) +- **hooks:** improve isInit perf ([be6a829](https://github.com/artalar/reatom/commit/be6a82984f798fd63786bace9a324bdf3d8ff3c6)) ## [3.5.1](https://github.com/artalar/reatom/compare/hooks-v3.5.0...hooks-v3.5.1) (2023-11-20) - ### Bug Fixes -* **hooks:** use abortCauseContext ([dcb437c](https://github.com/artalar/reatom/commit/dcb437c44ef4346df73f4cc94c90d132fe09edc0)) +- **hooks:** use abortCauseContext ([dcb437c](https://github.com/artalar/reatom/commit/dcb437c44ef4346df73f4cc94c90d132fe09edc0)) ## [3.5.0](https://github.com/artalar/reatom/compare/hooks-v3.4.3...hooks-v3.5.0) (2023-10-21) - ### Features -* **hooks:** add isInit method ([5138685](https://github.com/artalar/reatom/commit/51386857231c036534596df0bd63b6240a30454a)) +- **hooks:** add isInit method ([5138685](https://github.com/artalar/reatom/commit/51386857231c036534596df0bd63b6240a30454a)) ## [3.4.3](https://github.com/artalar/reatom/compare/hooks-v3.4.2...hooks-v3.4.3) (2023-10-20) - ### Bug Fixes -* **hooks:** do not change onConnect promise caught behavior ([3754d73](https://github.com/artalar/reatom/commit/3754d73a8c312ef300b8569dc1d415fa7a462488)) +- **hooks:** do not change onConnect promise caught behavior ([3754d73](https://github.com/artalar/reatom/commit/3754d73a8c312ef300b8569dc1d415fa7a462488)) ## [3.4.2](https://github.com/artalar/reatom/compare/hooks-v3.4.1...hooks-v3.4.2) (2023-06-12) - ### Bug Fixes -* **hooks:** pretty code ([4b06eed](https://github.com/artalar/reatom/commit/4b06eed424cae8b73963db5498c000e694f697ce)) +- **hooks:** pretty code ([4b06eed](https://github.com/artalar/reatom/commit/4b06eed424cae8b73963db5498c000e694f697ce)) ## [3.4.1](https://github.com/artalar/reatom/compare/hooks-v3.4.0...hooks-v3.4.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.4.0](https://github.com/artalar/reatom/compare/hooks-v3.3.1...hooks-v3.4.0) (2023-04-18) - ### Features -* **hooks:** handle lensed atoms in onUpdate ([c296746](https://github.com/artalar/reatom/commit/c2967468b574446100f6729f089ecf4e5d1490de)) - +- **hooks:** handle lensed atoms in onUpdate ([c296746](https://github.com/artalar/reatom/commit/c2967468b574446100f6729f089ecf4e5d1490de)) ### Bug Fixes -* **hooks:** _onUpdate ([fed4037](https://github.com/artalar/reatom/commit/fed40372ef6ee530a4d9df5d0924b0aaaa7d235f)) -* **hooks:** use utils isAbort ([1361663](https://github.com/artalar/reatom/commit/1361663aae8bff32f8b1fe96948222def408936a)) +- **hooks:** \_onUpdate ([fed4037](https://github.com/artalar/reatom/commit/fed40372ef6ee530a4d9df5d0924b0aaaa7d235f)) +- **hooks:** use utils isAbort ([1361663](https://github.com/artalar/reatom/commit/1361663aae8bff32f8b1fe96948222def408936a)) ## [3.3.0](https://github.com/artalar/reatom/commit/c2967468b574446100f6729f089ecf4e5d1490de) (2023-04-12) diff --git a/packages/hooks/README.md b/packages/hooks/README.md index eacffe237..9a9e21146 100644 --- a/packages/hooks/README.md +++ b/packages/hooks/README.md @@ -40,10 +40,10 @@ For example, in React you should manage abort strategy by yourself by `useEffect import { reatomAsync, withAbort, withDataAtom } from '@reatom/async' import { useAtom, useAction } from '@reatom/npm-react' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withAbort(), withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe( + withAbort(), + withDataAtom([]), +) export const List = () => { const [list] = useAtom(fetchList.dataAtom) @@ -65,10 +65,7 @@ With Reatom, you can simplify it and make it more readable. import { reatomAsync, onConnect, withDataAtom } from '@reatom/framework' import { useAtom } from '@reatom/npm-react' -export const fetchList = reatomAsync( - (ctx) => request('api/list', ctx.controller), - 'fetchList', -).pipe(withDataAtom([])) +export const fetchList = reatomAsync((ctx) => request('api/list', ctx.controller), 'fetchList').pipe(withDataAtom([])) onConnect(fetchList.dataAtom, fetchList) export const List = () => { diff --git a/packages/hooks/src/index.ts b/packages/hooks/src/index.ts index da482d0c5..0ce06d008 100644 --- a/packages/hooks/src/index.ts +++ b/packages/hooks/src/index.ts @@ -15,27 +15,19 @@ import { import { noop, toAbortError, merge } from '@reatom/utils' import { abortCauseContext } from '@reatom/effects' -export const getRootCause = (cause: AtomCache): AtomCache => - cause.cause === null ? cause : getRootCause(cause.cause) +export const getRootCause = (cause: AtomCache): AtomCache => (cause.cause === null ? cause : getRootCause(cause.cause)) -export const isSameCtx = (ctx1: Ctx, ctx2: Ctx) => - getRootCause(ctx1.cause) === getRootCause(ctx2.cause) +export const isSameCtx = (ctx1: Ctx, ctx2: Ctx) => getRootCause(ctx1.cause) === getRootCause(ctx2.cause) -export const addOnConnect = (anAtom: Atom, cb: Fn<[Ctx]>) => - (anAtom.__reatom.connectHooks ??= new Set()).add(cb) +export const addOnConnect = (anAtom: Atom, cb: Fn<[Ctx]>) => (anAtom.__reatom.connectHooks ??= new Set()).add(cb) -export const addOnDisconnect = (anAtom: Atom, cb: Fn<[Ctx]>) => - (anAtom.__reatom.disconnectHooks ??= new Set()).add(cb) +export const addOnDisconnect = (anAtom: Atom, cb: Fn<[Ctx]>) => (anAtom.__reatom.disconnectHooks ??= new Set()).add(cb) -export const addOnUpdate = ( - anAtom: T, - cb: Fn<[Ctx, AtomCache>]>, -) => (anAtom.__reatom.updateHooks ??= new Set()).add(cb) +export const addOnUpdate = (anAtom: T, cb: Fn<[Ctx, AtomCache>]>) => + (anAtom.__reatom.updateHooks ??= new Set()).add(cb) export const withInit = - ( - createState: Fn<[Ctx, T['__reatom']['initState']], AtomState>, - ): Fn<[T], T> => + (createState: Fn<[Ctx, T['__reatom']['initState']], AtomState>): Fn<[T], T> => (anAtom) => { const { initState, isAction } = anAtom.__reatom @@ -48,10 +40,7 @@ export const withInit = export const onConnect = ( anAtom: Atom, - cb: Fn< - [Ctx & { controller: AbortController; isConnected(): boolean }], - (() => void) | unknown - >, + cb: Fn<[Ctx & { controller: AbortController; isConnected(): boolean }], (() => void) | unknown>, ): Unsubscribe => { const connectHook = (ctx: Ctx) => { const cause = merge( @@ -77,11 +66,7 @@ export const onConnect = ( // TODO: abort on `connectHooks.delete`? const cleanupHook = (_ctx: Ctx) => { - if ( - isSameCtx(ctx, _ctx) && - disconnectHooks.delete(cleanupHook) && - connectHooks.has(connectHook) - ) { + if (isSameCtx(ctx, _ctx) && disconnectHooks.delete(cleanupHook) && connectHooks.has(connectHook)) { controller.abort(toAbortError(`${anAtom.__reatom.name} disconnect`)) typeof cleanup === 'function' && cleanup() } @@ -95,29 +80,16 @@ export const onConnect = ( return () => connectHooks.delete(connectHook) } -export const onDisconnect = (anAtom: Atom, cb: Fn<[Ctx]>): Unsubscribe => - onConnect(anAtom, (ctx) => () => cb(ctx)) +export const onDisconnect = (anAtom: Atom, cb: Fn<[Ctx]>): Unsubscribe => onConnect(anAtom, (ctx) => () => cb(ctx)) // @ts-expect-error const _onUpdate: { ( anAction: Action & { deps?: Array }, - cb?: Fn< - [ - Ctx, - Payload, - AtomCache>> & { params: Params }, - ] - >, + cb?: Fn<[Ctx, Payload, AtomCache>> & { params: Params }]>, ): Unsubscribe - ( - anAtom: Atom & { deps?: Array }, - cb?: Fn<[Ctx, T, AtomCache]>, - ): Unsubscribe -} = ( - anAtom: Action | Atom, - cb: Fn<[Ctx, T, AtomCache]> = noop, -) => { + (anAtom: Atom & { deps?: Array }, cb?: Fn<[Ctx, T, AtomCache]>): Unsubscribe +} = (anAtom: Action | Atom, cb: Fn<[Ctx, T, AtomCache]> = noop) => { const hook = (ctx: Ctx, patch: AtomCache & { params?: unknown[] }) => { let { state } = patch if (anAtom.__reatom.isAction) { @@ -165,13 +137,7 @@ export const spyChange: { } export const controlConnection = - ( - initState = true, - name?: string, - ): Fn< - [Atom], - Atom & { toggleConnection: Action<[boolean?], boolean> } - > => + (initState = true, name?: string): Fn<[Atom], Atom & { toggleConnection: Action<[boolean?], boolean> }> => (anAtom) => { name ??= `${anAtom.__reatom.name}.controlConnection` @@ -184,10 +150,7 @@ export const controlConnection = `${name}.toggleConnection`, ), }, - atom( - (ctx, state?: any) => (ctx.spy(isActiveAtom) ? ctx.spy(anAtom) : state), - name, - ), + atom((ctx, state?: any) => (ctx.spy(isActiveAtom) ? ctx.spy(anAtom) : state), name), ) } @@ -197,10 +160,7 @@ export const isConnected = (ctx: Ctx, { __reatom: proto }: Atom) => return !!cache && cache.subs.size + cache.listeners.size > 0 }) -const initializations = atom( - null! as WeakMap, - 'initializations', -) +const initializations = atom(null! as WeakMap, 'initializations') initializations.__reatom.initState = () => new WeakMap() export const isInit = (ctx: Ctx) => { const inits = ctx.get(initializations) diff --git a/packages/jsx/CHANGELOG.md b/packages/jsx/CHANGELOG.md index 7fbc51ff3..50ddecddd 100644 --- a/packages/jsx/CHANGELOG.md +++ b/packages/jsx/CHANGELOG.md @@ -2,119 +2,101 @@ ## [3.11.3](https://github.com/artalar/reatom/compare/jsx-v3.11.2...jsx-v3.11.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.11.2](https://github.com/artalar/reatom/compare/jsx-v3.11.1...jsx-v3.11.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.11.1](https://github.com/artalar/reatom/compare/jsx-v3.11.0...jsx-v3.11.1) (2024-06-12) - ### Bug Fixes -* **jsx:** render Atom<SVGElement> ([#869](https://github.com/artalar/reatom/issues/869)) ([3db6ce8](https://github.com/artalar/reatom/commit/3db6ce87dd0af2395f127561a7b098a7f64f34c0)) +- **jsx:** render Atom<SVGElement> ([#869](https://github.com/artalar/reatom/issues/869)) ([3db6ce8](https://github.com/artalar/reatom/commit/3db6ce87dd0af2395f127561a7b098a7f64f34c0)) ## [3.11.0](https://github.com/artalar/reatom/compare/jsx-v3.10.0...jsx-v3.11.0) (2024-06-07) - ### Features -* **jsx:** handle falsy like React ([#861](https://github.com/artalar/reatom/issues/861)) ([ad52a74](https://github.com/artalar/reatom/commit/ad52a74b8104301b702d503f050b31754c2816d4)) +- **jsx:** handle falsy like React ([#861](https://github.com/artalar/reatom/issues/861)) ([ad52a74](https://github.com/artalar/reatom/commit/ad52a74b8104301b702d503f050b31754c2816d4)) ## [3.10.0](https://github.com/artalar/reatom/compare/jsx-v3.9.0...jsx-v3.10.0) (2024-04-28) - ### Features -* **jsx:** support reatomLinkedList ([0e027e8](https://github.com/artalar/reatom/commit/0e027e8317e10a59b9f65b0ec1070e3a5637aeca)) +- **jsx:** support reatomLinkedList ([0e027e8](https://github.com/artalar/reatom/commit/0e027e8317e10a59b9f65b0ec1070e3a5637aeca)) ## [3.9.0](https://github.com/artalar/reatom/compare/jsx-v3.8.0...jsx-v3.9.0) (2024-04-12) - ### Features -* **jsx:** support array children ([c555206](https://github.com/artalar/reatom/commit/c555206b4349494912cc278b5157d9b167cc54a0)) - +- **jsx:** support array children ([c555206](https://github.com/artalar/reatom/commit/c555206b4349494912cc278b5157d9b167cc54a0)) ### Bug Fixes -* **jsx:** mount fragment as child ([#782](https://github.com/artalar/reatom/issues/782)) ([33a0fda](https://github.com/artalar/reatom/commit/33a0fda91bd1ed5a384d8214baa08f0b295861f2)) +- **jsx:** mount fragment as child ([#782](https://github.com/artalar/reatom/issues/782)) ([33a0fda](https://github.com/artalar/reatom/commit/33a0fda91bd1ed5a384d8214baa08f0b295861f2)) ## [3.8.0](https://github.com/artalar/reatom/compare/jsx-v3.7.0...jsx-v3.8.0) (2024-03-31) - ### Features -* **jsx:** huge refactor, add css support ([8e0019e](https://github.com/artalar/reatom/commit/8e0019e17299eba58d897eda18affb56c5de8b71)) +- **jsx:** huge refactor, add css support ([8e0019e](https://github.com/artalar/reatom/commit/8e0019e17299eba58d897eda18affb56c5de8b71)) ## [3.7.0](https://github.com/artalar/reatom/compare/jsx-v3.6.0...jsx-v3.7.0) (2024-02-20) - ### Features -* **jsx:** complete jsx renderer ([#733](https://github.com/artalar/reatom/issues/733)) ([f251172](https://github.com/artalar/reatom/commit/f251172ead5a386ec95e145ccc12845b428d93da)) +- **jsx:** complete jsx renderer ([#733](https://github.com/artalar/reatom/issues/733)) ([f251172](https://github.com/artalar/reatom/commit/f251172ead5a386ec95e145ccc12845b428d93da)) ## [3.6.0](https://github.com/artalar/reatom/compare/jsx-v3.5.0...jsx-v3.6.0) (2023-10-20) - ### Features -* **jsx:** support 'style' and 'styles' props ([#674](https://github.com/artalar/reatom/issues/674)) ([a3ad7d8](https://github.com/artalar/reatom/commit/a3ad7d8a5407635b876869ba5b9ab097a0f6835e)) - +- **jsx:** support 'style' and 'styles' props ([#674](https://github.com/artalar/reatom/issues/674)) ([a3ad7d8](https://github.com/artalar/reatom/commit/a3ad7d8a5407635b876869ba5b9ab097a0f6835e)) ### Bug Fixes -* **jsx:** fix $attrs ([#681](https://github.com/artalar/reatom/issues/681)) ([d21daa1](https://github.com/artalar/reatom/commit/d21daa1fed6f26d61afccb6d546773f866ffcf84)) +- **jsx:** fix $attrs ([#681](https://github.com/artalar/reatom/issues/681)) ([d21daa1](https://github.com/artalar/reatom/commit/d21daa1fed6f26d61afccb6d546773f866ffcf84)) ## [3.5.0](https://github.com/artalar/reatom/compare/jsx-v3.4.0...jsx-v3.5.0) (2023-10-09) - ### Features -* **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) - +- **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) ### Bug Fixes -* **jsx:** do not allow an array in child atom ([cf178ce](https://github.com/artalar/reatom/commit/cf178ceb951fad577ef0aad86c54d4effcb28391)) -* **jsx:** typed props ([#643](https://github.com/artalar/reatom/issues/643)) ([1cd2932](https://github.com/artalar/reatom/commit/1cd29325cf686baa9fd2668f282b3020b2512ad6)) +- **jsx:** do not allow an array in child atom ([cf178ce](https://github.com/artalar/reatom/commit/cf178ceb951fad577ef0aad86c54d4effcb28391)) +- **jsx:** typed props ([#643](https://github.com/artalar/reatom/issues/643)) ([1cd2932](https://github.com/artalar/reatom/commit/1cd29325cf686baa9fd2668f282b3020b2512ad6)) ## [3.4.0](https://github.com/artalar/reatom/compare/jsx-v3.3.1...jsx-v3.4.0) (2023-09-26) - ### Features -* **jsx:** [#637](https://github.com/artalar/reatom/issues/637) add SVG types ([2ecb8c0](https://github.com/artalar/reatom/commit/2ecb8c0b3605032741ce7fe57f3f657452622bc0)) - +- **jsx:** [#637](https://github.com/artalar/reatom/issues/637) add SVG types ([2ecb8c0](https://github.com/artalar/reatom/commit/2ecb8c0b3605032741ce7fe57f3f657452622bc0)) ### Bug Fixes -* **jsx:** uses createElementNS for svg ([d57bd7c](https://github.com/artalar/reatom/commit/d57bd7c42e00ff5bdeb3d810061de0fb3b66ade8)) +- **jsx:** uses createElementNS for svg ([d57bd7c](https://github.com/artalar/reatom/commit/d57bd7c42e00ff5bdeb3d810061de0fb3b66ade8)) ## [3.3.1](https://github.com/artalar/reatom/compare/jsx-v3.3.0...jsx-v3.3.1) (2023-09-26) - ### Bug Fixes -* **testing,jsx:** type improvements ([#639](https://github.com/artalar/reatom/issues/639)) ([d343ac4](https://github.com/artalar/reatom/commit/d343ac4f9549258851235a60e6ef01c24bc2084e)) +- **testing,jsx:** type improvements ([#639](https://github.com/artalar/reatom/issues/639)) ([d343ac4](https://github.com/artalar/reatom/commit/d343ac4f9549258851235a60e6ef01c24bc2084e)) ## [3.3.0](https://github.com/artalar/reatom/compare/jsx-v3.2.0...jsx-v3.3.0) (2023-09-22) - ### Features -* **jsx:** add mount method to handle unsubscribe ([15c4681](https://github.com/artalar/reatom/commit/15c46813eb96e6254bc769afda2e442d47ad8ad4)) -* **jsx:** fix types, add docs ([612b43f](https://github.com/artalar/reatom/commit/612b43fa4114f66c96fb618ad4b01c67b6143408)) - +- **jsx:** add mount method to handle unsubscribe ([15c4681](https://github.com/artalar/reatom/commit/15c46813eb96e6254bc769afda2e442d47ad8ad4)) +- **jsx:** fix types, add docs ([612b43f](https://github.com/artalar/reatom/commit/612b43fa4114f66c96fb618ad4b01c67b6143408)) ### Bug Fixes -* **jsx:** dts files ([3c4cc36](https://github.com/artalar/reatom/commit/3c4cc36b64ddc32936521faf7491ab063b905f32)) +- **jsx:** dts files ([3c4cc36](https://github.com/artalar/reatom/commit/3c4cc36b64ddc32936521faf7491ab063b905f32)) diff --git a/packages/jsx/README.md b/packages/jsx/README.md index 2e27ce573..1acd7b6ab 100644 --- a/packages/jsx/README.md +++ b/packages/jsx/README.md @@ -52,7 +52,7 @@ You can use different JSX pragmas in different files. For example, if you have a ```tsx // @jsxRuntime classic // @jsx h -import { h } from '@reatom/jsx'; +import { h } from '@reatom/jsx' ``` ## Example @@ -65,9 +65,7 @@ Define a component: import { atom, action } from '@reatom/core' export const inputAtom = atom('') -const onInput = action((ctx, event) => - inputAtom(ctx, event.currentTarget.value), -) +const onInput = action((ctx, event) => inputAtom(ctx, event.currentTarget.value)) export const Input = () => ``` @@ -185,11 +183,7 @@ In Reatom, there is no concept of "rerender" like React. Instead, we have a spec ```tsx
    - ctx.spy(valid) - ? { disabled: true, readonly: true } - : { disabled: false, readonly: false }, - )} + $spread={atom((ctx) => (ctx.spy(valid) ? { disabled: true, readonly: true } : { disabled: false, readonly: false }))} /> ``` diff --git a/packages/jsx/src/index.ts b/packages/jsx/src/index.ts index e6a8a90b8..8b7098a6c 100644 --- a/packages/jsx/src/index.ts +++ b/packages/jsx/src/index.ts @@ -1,38 +1,17 @@ -import { - action, - Atom, - AtomMut, - createCtx, - Ctx, - Fn, - isAtom, - Rec, - throwReatomError, - Unsubscribe, -} from '@reatom/core' +import { action, Atom, AtomMut, createCtx, Ctx, Fn, isAtom, Rec, throwReatomError, Unsubscribe } from '@reatom/core' import { isObject, random } from '@reatom/utils' -import { - type LinkedList, - type LLNode, - isLinkedListAtom, - LL_NEXT, -} from '@reatom/primitives' +import { type LinkedList, type LLNode, isLinkedListAtom, LL_NEXT } from '@reatom/primitives' import type { JSX } from './jsx' declare type JSXElement = JSX.Element export type { JSXElement, JSX } type DomApis = Pick< typeof window, - | 'document' - | 'Node' - | 'Text' - | 'Element' - | 'MutationObserver' - | 'HTMLElement' - | 'DocumentFragment' + 'document' | 'Node' | 'Text' | 'Element' | 'MutationObserver' | 'HTMLElement' | 'DocumentFragment' > -const isSkipped = (value: unknown): value is boolean | '' | null | undefined => typeof value === 'boolean' || value === '' || value == null +const isSkipped = (value: unknown): value is boolean | '' | null | undefined => + typeof value === 'boolean' || value === '' || value == null let unsubscribesMap = new WeakMap>() let unlink = (parent: any, un: Unsubscribe) => { @@ -42,21 +21,14 @@ let unlink = (parent: any, un: Unsubscribe) => { Promise.resolve().then(() => { if (!parent.isConnected) un() else { - while ( - parent.parentElement && - !unsubscribesMap.get(parent)?.push(() => parent.isConnected || un()) - ) { + while (parent.parentElement && !unsubscribesMap.get(parent)?.push(() => parent.isConnected || un())) { parent = parent.parentElement } } }) } -const walkLinkedList = ( - ctx: Ctx, - el: JSX.Element, - list: Atom>>, -) => { +const walkLinkedList = (ctx: Ctx, el: JSX.Element, list: Atom>>) => { let lastVersion = -1 unlink( el, @@ -177,12 +149,7 @@ export const reatomJsx = (ctx: Ctx, DOM: DomApis = globalThis.window) => { if (k.startsWith('model:')) { let name = (k = k.slice(6)) set(element, 'on:input', (ctx: Ctx, event: any) => { - ;(prop as AtomMut)( - ctx, - name === 'valueAsNumber' - ? +event.target.value - : event.target[name], - ) + ;(prop as AtomMut)(ctx, name === 'valueAsNumber' ? +event.target.value : event.target[name]) }) if (k === 'valueAsNumber') k = 'value' k = 'prop:' + k @@ -210,17 +177,11 @@ export const reatomJsx = (ctx: Ctx, DOM: DomApis = globalThis.window) => { if (isLinkedListAtom(child)) { walkLinkedList(ctx, element, child) } else if (isAtom(child)) { - let innerChild = DOM.document.createTextNode('') as - | ChildNode - | DocumentFragment + let innerChild = DOM.document.createTextNode('') as ChildNode | DocumentFragment let error: any var un: undefined | Unsubscribe = ctx.subscribe(child, (v): void => { try { - if ( - un && - !innerChild.isConnected && - innerChild instanceof DOM.DocumentFragment === false - ) { + if (un && !innerChild.isConnected && innerChild instanceof DOM.DocumentFragment === false) { un() } else { throwReatomError( diff --git a/packages/jsx/src/jsx.d.ts b/packages/jsx/src/jsx.d.ts index 26500b25c..226815883 100644 --- a/packages/jsx/src/jsx.d.ts +++ b/packages/jsx/src/jsx.d.ts @@ -19,14 +19,7 @@ type ElementsAttributesAtomMaybe> = { export namespace JSX { type Element = HTMLElement | SVGElement - type ElementPrimitiveChildren = - | Node - | Element - | (string & {}) - | number - | boolean - | null - | undefined + type ElementPrimitiveChildren = Node | Element | (string & {}) | number | boolean | null | undefined type ElementChildren = | Array> @@ -73,12 +66,7 @@ export namespace JSX { ctx: Ctx, e: E & { currentTarget: T - target: T extends - | HTMLInputElement - | HTMLSelectElement - | HTMLTextAreaElement - ? T - : Element + target: T extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement ? T : Element }, ): void } @@ -88,12 +76,7 @@ export namespace JSX { ctx: Ctx, e: E & { currentTarget: T - target: T extends - | HTMLInputElement - | HTMLSelectElement - | HTMLTextAreaElement - ? T - : Element + target: T extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement ? T : Element }, ): void } @@ -103,12 +86,7 @@ export namespace JSX { ctx: Ctx, e: E & { currentTarget: T - target: T extends - | HTMLInputElement - | HTMLSelectElement - | HTMLTextAreaElement - ? T - : Element + target: T extends HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement ? T : Element }, ): void } @@ -140,9 +118,7 @@ export namespace JSX { [Key in keyof Directives as `use:${Key}`]?: Directives[Key] } type DirectiveFunctionAttributes = { - [K in keyof DirectiveFunctions as string extends K - ? never - : `use:${K}`]?: DirectiveFunctions[K] extends ( + [K in keyof DirectiveFunctions as string extends K ? never : `use:${K}`]?: DirectiveFunctions[K] extends ( el: infer E, // will be unknown if not provided ...rest: infer R // use rest so that we can check whether it's provided or not ) => void @@ -156,26 +132,16 @@ export namespace JSX { : never // it isn't a function } type PropAttributes = { - [Key in keyof ExplicitProperties as `prop:${Key}`]?: AtomMaybe< - ExplicitProperties[Key] - > + [Key in keyof ExplicitProperties as `prop:${Key}`]?: AtomMaybe } type AttrAttributes = { - [Key in keyof ExplicitAttributes as `attr:${Key}`]?: AtomMaybe< - ExplicitAttributes[Key] - > + [Key in keyof ExplicitAttributes as `attr:${Key}`]?: AtomMaybe } type OnAttributes = { - [Key in keyof CustomEvents as `on:${Key}`]?: EventHandler< - T, - CustomEvents[Key] - > + [Key in keyof CustomEvents as `on:${Key}`]?: EventHandler } type OnCaptureAttributes = { - [Key in keyof CustomCaptureEvents as `oncapture:${Key}`]?: EventHandler< - T, - CustomCaptureEvents[Key] - > + [Key in keyof CustomCaptureEvents as `oncapture:${Key}`]?: EventHandler } interface DOMAttributes extends CustomAttributes, @@ -295,13 +261,7 @@ export namespace JSX { } /** Controls automatic capitalization in inputted text. */ - type HTMLAutocapitalize = - | 'off' - | 'none' - | 'on' - | 'sentences' - | 'words' - | 'characters' + type HTMLAutocapitalize = 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters' // TODO add combinations /** * The autocomplete attribute provides a hint to the user agent specifying how to, or indeed whether to, prefill a form control. @@ -375,10 +335,7 @@ export namespace JSX { | `section-${string}` | (string & {}) type HTMLDir = 'ltr' | 'rtl' | 'auto' - type HTMLFormEncType = - | 'application/x-www-form-urlencoded' - | 'multipart/form-data' - | 'text/plain' + type HTMLFormEncType = 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text/plain' type HTMLFormMethod = 'post' | 'get' | 'dialog' type HTMLCrossorigin = 'anonymous' | 'use-credentials' | '' type HTMLReferrerPolicy = @@ -459,15 +416,7 @@ export namespace JSX { */ 'aria-controls'?: string /** Indicates the element that represents the current item within a container or set of related elements. */ - 'aria-current'?: - | boolean - | 'false' - | 'true' - | 'page' - | 'step' - | 'location' - | 'date' - | 'time' + 'aria-current'?: boolean | 'false' | 'true' | 'page' | 'step' | 'location' | 'date' | 'time' /** * Identifies the element (or elements) that describes the object. * @see aria-labelledby @@ -506,15 +455,7 @@ export namespace JSX { */ 'aria-grabbed'?: boolean | 'false' | 'true' /** Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. */ - 'aria-haspopup'?: - | boolean - | 'false' - | 'true' - | 'menu' - | 'listbox' - | 'tree' - | 'grid' - | 'dialog' + 'aria-haspopup'?: boolean | 'false' | 'true' | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog' /** * Indicates whether the element is exposed to an accessibility API. * @see aria-disabled. @@ -755,15 +696,7 @@ export namespace JSX { itemref?: string part?: string exportparts?: string - inputmode?: - | 'none' - | 'text' - | 'tel' - | 'url' - | 'email' - | 'numeric' - | 'decimal' - | 'search' + inputmode?: 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search' } interface AnchorHTMLAttributes extends HTMLAttributes { download?: any @@ -896,14 +829,7 @@ export namespace JSX { checked?: boolean crossorigin?: HTMLCrossorigin disabled?: boolean - enterkeyhint?: - | 'enter' - | 'done' - | 'go' - | 'next' - | 'previous' - | 'search' - | 'send' + enterkeyhint?: 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' form?: string formaction?: string | SerializableAttributeValue formenctype?: HTMLFormEncType @@ -927,9 +853,8 @@ export namespace JSX { size?: number | string src?: string step?: number | string - type?: - /** A push button with no default behavior displaying the value of the value attribute, empty by default. */ - | 'button' + type?: /** A push button with no default behavior displaying the value of the value attribute, empty by default. */ + | 'button' /** A check box allowing single values to be selected/deselected. */ | 'checkbox' /** A control for specifying a color; opening a color picker when active in supporting browsers. */ @@ -1112,8 +1037,7 @@ export namespace JSX { size?: number | string value?: string | string[] | number } - interface HTMLSlotElementAttributes - extends HTMLAttributes { + interface HTMLSlotElementAttributes extends HTMLAttributes { name?: string } interface SourceHTMLAttributes extends HTMLAttributes { @@ -1134,8 +1058,7 @@ export namespace JSX { headers?: string rowspan?: number | string } - interface TemplateHTMLAttributes - extends HTMLAttributes { + interface TemplateHTMLAttributes extends HTMLAttributes { content?: DocumentFragment } interface TextareaHTMLAttributes extends HTMLAttributes { @@ -1144,14 +1067,7 @@ export namespace JSX { cols?: number | string dirname?: string disabled?: boolean - enterkeyhint?: - | 'enter' - | 'done' - | 'go' - | 'next' - | 'previous' - | 'search' - | 'send' + enterkeyhint?: 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' form?: string maxlength?: number | string minlength?: number | string @@ -1377,53 +1293,24 @@ export namespace JSX { | 'all' | 'none' | 'inherit' - 'shape-rendering'?: - | 'auto' - | 'optimizeSpeed' - | 'crispEdges' - | 'geometricPrecision' - | 'inherit' + 'shape-rendering'?: 'auto' | 'optimizeSpeed' | 'crispEdges' | 'geometricPrecision' | 'inherit' 'stop-color'?: string 'stop-opacity'?: number | string | 'inherit' stroke?: string 'stroke-dasharray'?: string 'stroke-dashoffset'?: number | string 'stroke-linecap'?: 'butt' | 'round' | 'square' | 'inherit' - 'stroke-linejoin'?: - | 'arcs' - | 'bevel' - | 'miter' - | 'miter-clip' - | 'round' - | 'inherit' + 'stroke-linejoin'?: 'arcs' | 'bevel' | 'miter' | 'miter-clip' | 'round' | 'inherit' 'stroke-miterlimit'?: number | string | 'inherit' 'stroke-opacity'?: number | string | 'inherit' 'stroke-width'?: number | string 'text-anchor'?: 'start' | 'middle' | 'end' | 'inherit' - 'text-decoration'?: - | 'none' - | 'underline' - | 'overline' - | 'line-through' - | 'blink' - | 'inherit' - 'text-rendering'?: - | 'auto' - | 'optimizeSpeed' - | 'optimizeLegibility' - | 'geometricPrecision' - | 'inherit' + 'text-decoration'?: 'none' | 'underline' | 'overline' | 'line-through' | 'blink' | 'inherit' + 'text-rendering'?: 'auto' | 'optimizeSpeed' | 'optimizeLegibility' | 'geometricPrecision' | 'inherit' 'unicode-bidi'?: string visibility?: 'visible' | 'hidden' | 'collapse' | 'inherit' 'word-spacing'?: number | string - 'writing-mode'?: - | 'lr-tb' - | 'rl-tb' - | 'tb-rl' - | 'lr' - | 'rl' - | 'tb' - | 'inherit' + 'writing-mode'?: 'lr-tb' | 'rl-tb' | 'tb-rl' | 'lr' | 'rl' | 'tb' | 'inherit' } interface AnimationElementSVGAttributes extends CoreSVGAttributes, @@ -1555,10 +1442,7 @@ export namespace JSX { AnimationTimingSVGAttributes, AnimationValueSVGAttributes, AnimationAdditionSVGAttributes, - Pick< - PresentationSVGAttributes, - 'color-interpolation' | 'color-rendering' - > {} + Pick {} interface AnimateMotionSVGAttributes extends AnimationElementSVGAttributes, AnimationTimingSVGAttributes, @@ -1602,9 +1486,7 @@ export namespace JSX { ExternalResourceSVGAttributes, StylableSVGAttributes, TransformableSVGAttributes {} - interface DescSVGAttributes - extends CoreSVGAttributes, - StylableSVGAttributes {} + interface DescSVGAttributes extends CoreSVGAttributes, StylableSVGAttributes {} interface EllipseSVGAttributes extends GraphicsElementSVGAttributes, ShapeElementSVGAttributes, @@ -1675,8 +1557,7 @@ export namespace JSX { xChannelSelector?: 'R' | 'G' | 'B' | 'A' yChannelSelector?: 'R' | 'G' | 'B' | 'A' } - interface FeDistantLightSVGAttributes - extends LightSourceElementSVGAttributes { + interface FeDistantLightSVGAttributes extends LightSourceElementSVGAttributes { azimuth?: number | string elevation?: number | string } @@ -1684,10 +1565,7 @@ export namespace JSX { extends CoreSVGAttributes, FilterPrimitiveElementSVGAttributes, StylableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'color' | 'flood-color' | 'flood-opacity' - > { + Pick { dx?: number | string dy?: number | string stdDeviation?: number | string @@ -1695,10 +1573,7 @@ export namespace JSX { interface FeFloodSVGAttributes extends FilterPrimitiveElementSVGAttributes, StylableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'color' | 'flood-color' | 'flood-opacity' - > {} + Pick {} interface FeFuncSVGAttributes extends CoreSVGAttributes { type?: 'identity' | 'table' | 'discrete' | 'linear' | 'gamma' tableValues?: string @@ -1721,12 +1596,8 @@ export namespace JSX { preserveAspectRatio?: SVGPreserveAspectRatio href?: string } - interface FeMergeSVGAttributes - extends FilterPrimitiveElementSVGAttributes, - StylableSVGAttributes {} - interface FeMergeNodeSVGAttributes - extends CoreSVGAttributes, - SingleInputFilterSVGAttributes {} + interface FeMergeSVGAttributes extends FilterPrimitiveElementSVGAttributes, StylableSVGAttributes {} + interface FeMergeNodeSVGAttributes extends CoreSVGAttributes, SingleInputFilterSVGAttributes {} interface FeMorphologySVGAttributes extends FilterPrimitiveElementSVGAttributes, SingleInputFilterSVGAttributes, @@ -1741,8 +1612,7 @@ export namespace JSX { dx?: number | string dy?: number | string } - interface FePointLightSVGAttributes - extends LightSourceElementSVGAttributes { + interface FePointLightSVGAttributes extends LightSourceElementSVGAttributes { x?: number | string y?: number | string z?: number | string @@ -1757,8 +1627,7 @@ export namespace JSX { specularExponent?: string kernelUnitLength?: number | string } - interface FeSpotLightSVGAttributes - extends LightSourceElementSVGAttributes { + interface FeSpotLightSVGAttributes extends LightSourceElementSVGAttributes { x?: number | string y?: number | string z?: number | string @@ -1772,19 +1641,14 @@ export namespace JSX { extends FilterPrimitiveElementSVGAttributes, SingleInputFilterSVGAttributes, StylableSVGAttributes {} - interface FeTurbulanceSVGAttributes - extends FilterPrimitiveElementSVGAttributes, - StylableSVGAttributes { + interface FeTurbulanceSVGAttributes extends FilterPrimitiveElementSVGAttributes, StylableSVGAttributes { baseFrequency?: number | string numOctaves?: number | string seed?: number | string stitchTiles?: 'stitch' | 'noStitch' type?: 'fractalNoise' | 'turbulence' } - interface FilterSVGAttributes - extends CoreSVGAttributes, - ExternalResourceSVGAttributes, - StylableSVGAttributes { + interface FilterSVGAttributes extends CoreSVGAttributes, ExternalResourceSVGAttributes, StylableSVGAttributes { filterUnits?: SVGUnits primitiveUnits?: SVGUnits x?: number | string @@ -1833,17 +1697,13 @@ export namespace JSX { ExternalResourceSVGAttributes, StylableSVGAttributes, TransformableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'marker-start' | 'marker-mid' | 'marker-end' - > { + Pick { x1?: number | string y1?: number | string x2?: number | string y2?: number | string } - interface LinearGradientSVGAttributes - extends GradientElementSVGAttributes { + interface LinearGradientSVGAttributes extends GradientElementSVGAttributes { x1?: number | string x2?: number | string y1?: number | string @@ -1883,10 +1743,7 @@ export namespace JSX { ExternalResourceSVGAttributes, StylableSVGAttributes, TransformableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'marker-start' | 'marker-mid' | 'marker-end' - > { + Pick { d?: string pathLength?: number | string } @@ -1913,10 +1770,7 @@ export namespace JSX { ExternalResourceSVGAttributes, StylableSVGAttributes, TransformableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'marker-start' | 'marker-mid' | 'marker-end' - > { + Pick { points?: string } interface PolylineSVGAttributes @@ -1926,14 +1780,10 @@ export namespace JSX { ExternalResourceSVGAttributes, StylableSVGAttributes, TransformableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'marker-start' | 'marker-mid' | 'marker-end' - > { + Pick { points?: string } - interface RadialGradientSVGAttributes - extends GradientElementSVGAttributes { + interface RadialGradientSVGAttributes extends GradientElementSVGAttributes { cx?: number | string cy?: number | string r?: number | string @@ -1954,10 +1804,7 @@ export namespace JSX { rx?: number | string ry?: number | string } - interface SetSVGAttributes - extends CoreSVGAttributes, - StylableSVGAttributes, - AnimationTimingSVGAttributes {} + interface SetSVGAttributes extends CoreSVGAttributes, StylableSVGAttributes, AnimationTimingSVGAttributes {} interface StopSVGAttributes extends CoreSVGAttributes, StylableSVGAttributes, @@ -2026,10 +1873,7 @@ export namespace JSX { ConditionalProcessingSVGAttributes, ExternalResourceSVGAttributes, StylableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'alignment-baseline' | 'baseline-shift' | 'display' | 'visibility' - > { + Pick { startOffset?: number | string method?: 'align' | 'stretch' spacing?: 'auto' | 'exact' @@ -2040,10 +1884,7 @@ export namespace JSX { ConditionalProcessingSVGAttributes, ExternalResourceSVGAttributes, StylableSVGAttributes, - Pick< - PresentationSVGAttributes, - 'alignment-baseline' | 'baseline-shift' | 'display' | 'visibility' - > { + Pick { x?: number | string y?: number | string dx?: number | string diff --git a/packages/lens/CHANGELOG.md b/packages/lens/CHANGELOG.md index 71281c765..638f58d00 100644 --- a/packages/lens/CHANGELOG.md +++ b/packages/lens/CHANGELOG.md @@ -2,160 +2,142 @@ ## [3.11.2](https://github.com/artalar/reatom/compare/lens-v3.11.1...lens-v3.11.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.11.1](https://github.com/artalar/reatom/compare/lens-v3.11.0...lens-v3.11.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **lens:** match ctx instead of cause in select ([3f1fd8e](https://github.com/artalar/reatom/commit/3f1fd8e21d457cc9f78d2a71ef9f8caa585fcf99)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **lens:** match ctx instead of cause in select ([3f1fd8e](https://github.com/artalar/reatom/commit/3f1fd8e21d457cc9f78d2a71ef9f8caa585fcf99)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.11.0](https://github.com/artalar/reatom/compare/lens-v3.10.0...lens-v3.11.0) (2024-06-12) - ### Features -* **lens:** support linked list in parseAtoms ([534d745](https://github.com/artalar/reatom/commit/534d74583af3890fe9ac18baf08b11c25dd1f957)) - +- **lens:** support linked list in parseAtoms ([534d745](https://github.com/artalar/reatom/commit/534d74583af3890fe9ac18baf08b11c25dd1f957)) ### Bug Fixes -* **lens:** do not perform actions in parseAtoms ([cff48d0](https://github.com/artalar/reatom/commit/cff48d060ae2a6a0afab4612353bac1926f39c21)) +- **lens:** do not perform actions in parseAtoms ([cff48d0](https://github.com/artalar/reatom/commit/cff48d060ae2a6a0afab4612353bac1926f39c21)) ## [3.10.0](https://github.com/artalar/reatom/compare/lens-v3.9.1...lens-v3.10.0) (2024-05-25) - ### Features -* **lens:** allow to call select multiple times ([6ea0223](https://github.com/artalar/reatom/commit/6ea022382ace846f5bb15abdf41bc35783764555)) +- **lens:** allow to call select multiple times ([6ea0223](https://github.com/artalar/reatom/commit/6ea022382ace846f5bb15abdf41bc35783764555)) ## [3.9.1](https://github.com/artalar/reatom/compare/lens-v3.9.0...lens-v3.9.1) (2023-11-25) - ### Bug Fixes -* **lens:** select export ([fd776cf](https://github.com/artalar/reatom/commit/fd776cf6a5ca36beb9e3581315abb8afe7ca2050)) +- **lens:** select export ([fd776cf](https://github.com/artalar/reatom/commit/fd776cf6a5ca36beb9e3581315abb8afe7ca2050)) ## [3.9.0](https://github.com/artalar/reatom/compare/lens-v3.8.0...lens-v3.9.0) (2023-11-23) - ### Features -* **lens:** add select api ([f39f1dd](https://github.com/artalar/reatom/commit/f39f1dd5434462f8466c892342dd8b6c29d70162)) +- **lens:** add select api ([f39f1dd](https://github.com/artalar/reatom/commit/f39f1dd5434462f8466c892342dd8b6c29d70162)) ## [3.8.0](https://github.com/artalar/reatom/compare/lens-v3.7.0...lens-v3.8.0) (2023-10-20) - ### Features -* **lens:** add match().with ([f61dddc](https://github.com/artalar/reatom/commit/f61dddc858fda9921117acd472cc4e4e71cdd47e)) -* **utils:** add isRec ([444c4f6](https://github.com/artalar/reatom/commit/444c4f6c528092a59bdd332218b3d42c88351999)) - +- **lens:** add match().with ([f61dddc](https://github.com/artalar/reatom/commit/f61dddc858fda9921117acd472cc4e4e71cdd47e)) +- **utils:** add isRec ([444c4f6](https://github.com/artalar/reatom/commit/444c4f6c528092a59bdd332218b3d42c88351999)) ### Bug Fixes -* **lens:** dont allow non-recs as match.with input ([#675](https://github.com/artalar/reatom/issues/675)) ([3c6e8bc](https://github.com/artalar/reatom/commit/3c6e8bc1425cb0c5a6fbde4d78f81c1ab15c7cf9)) -* **lens:** match default case ([27061af](https://github.com/artalar/reatom/commit/27061af1ac338f5223190d110cf6e090d884e203)) +- **lens:** dont allow non-recs as match.with input ([#675](https://github.com/artalar/reatom/issues/675)) ([3c6e8bc](https://github.com/artalar/reatom/commit/3c6e8bc1425cb0c5a6fbde4d78f81c1ab15c7cf9)) +- **lens:** match default case ([27061af](https://github.com/artalar/reatom/commit/27061af1ac338f5223190d110cf6e090d884e203)) ## [3.7.0](https://github.com/artalar/reatom/compare/lens-v3.6.2...lens-v3.7.0) (2023-10-09) - ### Features -* **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) -* **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) -* **match:** api redesign (it isn't published yet) ([0e9554e](https://github.com/artalar/reatom/commit/0e9554e8e493b70912f6efd23c69731cfa60ebb8)) -* **match:** remove equal method ([c1d077c](https://github.com/artalar/reatom/commit/c1d077c99944116a301f0cc0c24aade6a27e7351)) +- **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) +- **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) +- **match:** api redesign (it isn't published yet) ([0e9554e](https://github.com/artalar/reatom/commit/0e9554e8e493b70912f6efd23c69731cfa60ebb8)) +- **match:** remove equal method ([c1d077c](https://github.com/artalar/reatom/commit/c1d077c99944116a301f0cc0c24aade6a27e7351)) ## [3.6.2](https://github.com/artalar/reatom/compare/lens-v3.6.1...lens-v3.6.2) (2023-09-13) - ### Bug Fixes -* **lens:** remove spy from effect ([ece3c86](https://github.com/artalar/reatom/commit/ece3c8662a1fe69e8dc897b8ff252feb57cd5273)) +- **lens:** remove spy from effect ([ece3c86](https://github.com/artalar/reatom/commit/ece3c8662a1fe69e8dc897b8ff252feb57cd5273)) ## [3.6.1](https://github.com/artalar/reatom/compare/lens-v3.6.0...lens-v3.6.1) (2023-08-10) - ### Bug Fixes -* **lens:** mapName usage ([8865ce0](https://github.com/artalar/reatom/commit/8865ce0654bf468e560ef3f238611f5168db5925)) +- **lens:** mapName usage ([8865ce0](https://github.com/artalar/reatom/commit/8865ce0654bf468e560ef3f238611f5168db5925)) ## [3.6.0](https://github.com/artalar/reatom/compare/lens-v3.5.1...lens-v3.6.0) (2023-06-12) - ### Features -* **lens:** add abortable option to delay ([d532f1c](https://github.com/artalar/reatom/commit/d532f1c0c3f207827136cd5804d57aa337e4665d)) -* **lens:** use onChange and onCall in "plain" and "readonly" ([fb50938](https://github.com/artalar/reatom/commit/fb50938aba30ffefb99f570b894a58df489d2d44)) +- **lens:** add abortable option to delay ([d532f1c](https://github.com/artalar/reatom/commit/d532f1c0c3f207827136cd5804d57aa337e4665d)) +- **lens:** use onChange and onCall in "plain" and "readonly" ([fb50938](https://github.com/artalar/reatom/commit/fb50938aba30ffefb99f570b894a58df489d2d44)) ## [3.5.1](https://github.com/artalar/reatom/compare/lens-v3.5.0...lens-v3.5.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.5.0](https://github.com/artalar/reatom/compare/lens-v3.4.0...lens-v3.5.0) (2023-05-10) - ### Features -* **lens:** make parseAtoms reactive ([5fc55f9](https://github.com/artalar/reatom/commit/5fc55f956ab010118dbbae5354fc53d5c5c72236)) -* **lens:** map function for toAction ([691801d](https://github.com/artalar/reatom/commit/691801d675bb9d62b15e46b923cd12f19ebb4de6)) - +- **lens:** make parseAtoms reactive ([5fc55f9](https://github.com/artalar/reatom/commit/5fc55f956ab010118dbbae5354fc53d5c5c72236)) +- **lens:** map function for toAction ([691801d](https://github.com/artalar/reatom/commit/691801d675bb9d62b15e46b923cd12f19ebb4de6)) ### Bug Fixes -* **lens:** add DelayOptions interface ([535c406](https://github.com/artalar/reatom/commit/535c40685a03655c074f8e165c38e9f097bf06db)) +- **lens:** add DelayOptions interface ([535c406](https://github.com/artalar/reatom/commit/535c40685a03655c074f8e165c38e9f097bf06db)) ## [3.4.0](https://github.com/artalar/reatom/compare/lens-v3.3.3...lens-v3.4.0) (2023-04-18) - ### Features -* **lens:** add new operators and onLensUpdate ([#533](https://github.com/artalar/reatom/issues/533)) ([2442ca3](https://github.com/artalar/reatom/commit/2442ca34b6ab1fdc0c0aff52d18e85999e6de607)) - +- **lens:** add new operators and onLensUpdate ([#533](https://github.com/artalar/reatom/issues/533)) ([2442ca3](https://github.com/artalar/reatom/commit/2442ca34b6ab1fdc0c0aff52d18e85999e6de607)) ### Bug Fixes -* **lens:** add LensEffect ([c304f4d](https://github.com/artalar/reatom/commit/c304f4d5a6d5230a906fdad3891043c5f1543a63)) -* **lens:** mapPayload 4 arg state ([0f527ed](https://github.com/artalar/reatom/commit/0f527ed66a9b1f7d622f17fa77d995652edfe7d7)) -* **lens:** use onUpdate ([230a9c9](https://github.com/artalar/reatom/commit/230a9c9f920273d16eae67344acc309ad583f068)) +- **lens:** add LensEffect ([c304f4d](https://github.com/artalar/reatom/commit/c304f4d5a6d5230a906fdad3891043c5f1543a63)) +- **lens:** mapPayload 4 arg state ([0f527ed](https://github.com/artalar/reatom/commit/0f527ed66a9b1f7d622f17fa77d995652edfe7d7)) +- **lens:** use onUpdate ([230a9c9](https://github.com/artalar/reatom/commit/230a9c9f920273d16eae67344acc309ad583f068)) ## [3.3.0](https://github.com/artalar/reatom/compare/lens-v3.2.0...lens-v3.3.0) (2023-04-10) diff --git a/packages/lens/README.md b/packages/lens/README.md index c342446f9..d7deb28cf 100644 --- a/packages/lens/README.md +++ b/packages/lens/README.md @@ -11,9 +11,7 @@ import { mapState } from '@reatom/lens' // this is a typical code which have a problem with extra updates // in case when an element of the list changes not `myProp` -export const filteredListAtom = atom((ctx) => - ctx.spy(listAtom).map((obj) => obj.myProp), -) +export const filteredListAtom = atom((ctx) => ctx.spy(listAtom).map((obj) => obj.myProp)) // `mapState` could help to solve this problem, as it pass previous state as a second argument export const bAtom = listAtom.pipe( mapState((ctx, list, prevState) => { @@ -35,9 +33,7 @@ import { isShallowEqual } from '@reatom/utils' export const listMemoAtom = filteredListAtom.pipe(filter()) // equals to -export const listMemoAtom = filteredListAtom.pipe( - filter((ctx, next, prev) => !isShallowEqual(next, prev)), -) +export const listMemoAtom = filteredListAtom.pipe(filter((ctx, next, prev) => !isShallowEqual(next, prev))) ``` This operator could filter actions too! @@ -45,9 +41,7 @@ This operator could filter actions too! ```ts import { filter } from '@reatom/lens' -export const linkClicked = onDocumentClick.pipe( - filter((ctx, event) => event.target.tagName === 'A'), -) +export const linkClicked = onDocumentClick.pipe(filter((ctx, event) => event.target.tagName === 'A')) ``` ## `mapPayload` @@ -57,9 +51,7 @@ Map payload of each action call. Resulted action is not callable. ```ts import { mapPayload } from '@reatom/lens' -export const changeFullname = changeName.pipe( - mapPayload((ctx, { firstName, lastName }) => `${firstName} ${lastName}`), -) +export const changeFullname = changeName.pipe(mapPayload((ctx, { firstName, lastName }) => `${firstName} ${lastName}`)) ``` You could pass initial state by first argument to create an atom. @@ -69,9 +61,7 @@ import { action } from '@reatom/core' import { mapPayload } from '@reatom/lens' export const onInput = action('onInput') -export const inputAtom = onInput.pipe( - mapPayload('', (ctx, event) => event.currentTarget.value, 'inputAtom'), -) +export const inputAtom = onInput.pipe(mapPayload('', (ctx, event) => event.currentTarget.value, 'inputAtom')) ``` ## `mapPayloadAwaited` @@ -83,9 +73,7 @@ import { mapPayloadAwaited } from '@reatom/lens' export const newData = fetchData.pipe(mapPayloadAwaited()) // OR pick needed value -export const newData = fetchData.pipe( - mapPayloadAwaited((ctx, response) => response.data), -) +export const newData = fetchData.pipe(mapPayloadAwaited((ctx, response) => response.data)) ``` You could pass initial state by first argument to create an atom. @@ -109,9 +97,7 @@ import { atom } from '@reatom/core' import { mapInput } from '@reatom/lens' export const inputAtom = atom('', 'inputAtom') -export const changeInput = inputAtom.pipe( - mapInput((ctx, event) => event.currentTarget.value, 'changeInput'), -) +export const changeInput = inputAtom.pipe(mapInput((ctx, event) => event.currentTarget.value, 'changeInput')) ``` ## `debounce` @@ -218,8 +204,7 @@ const getField = (id: number, name: string, value: string): Field => { export const listAtom = atom>([], 'listAtom').pipe( withLocalStorage({ toSnapshot: (state) => parseAtoms(state), - fromSnapshot: (snapshot: any) => - getField(snapshot.id, snapshot.name, snapshot.value), + fromSnapshot: (snapshot: any) => getField(snapshot.id, snapshot.name, snapshot.value), }), ) ``` @@ -339,11 +324,7 @@ export const ListSum = reatomComponent(({ ctx }) => { // correct optimal way, the component will rerender only on `length` change const length = select(ctx, (selectCtx) => selectCtx.spy(listAtom).length) // you could call `select` many times - const sum = select(ctx, (selectCtx) => - selectCtx - .spy(listAtom) - .reduce((acc, el) => acc + selectCtx.spy(el.value), 0), - ) + const sum = select(ctx, (selectCtx) => selectCtx.spy(listAtom).reduce((acc, el) => acc + selectCtx.spy(el.value), 0)) return (
    @@ -358,9 +339,5 @@ Under the hood `select` creates additional atom, so you can perform all regular Note for a rare cases. A created atom is momorized for the each select by the passed function sources from "toString()" method, so every computed callback in different selects of the same atom should contains differen code. This will throw an error, as the select called multiple times in the same atom with the same string represantation of the passed callback: ```ts -const sumAtom = atom((ctx) => - ctx - .spy(listAtom) - .reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0), -) +const sumAtom = atom((ctx) => ctx.spy(listAtom).reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0)) ``` diff --git a/packages/lens/src/delay.ts b/packages/lens/src/delay.ts index e7614f489..901f2ef75 100644 --- a/packages/lens/src/delay.ts +++ b/packages/lens/src/delay.ts @@ -1,14 +1,4 @@ -import { - Action, - atom, - Atom, - AtomCache, - AtomState, - Ctx, - Fn, - isAtom, - throwReatomError, -} from '@reatom/core' +import { Action, atom, Atom, AtomCache, AtomState, Ctx, Fn, isAtom, throwReatomError } from '@reatom/core' import { onCtxAbort } from '@reatom/effects' import { mapName } from './utils' import { type LensAtom, type LensAction } from './' @@ -39,39 +29,24 @@ export interface DelayOptions { /** Flexible updates delayer */ export const delay: { // TODO for some reason an atom not handled by overloads, if an action overload is first - (options: DelayOptions, name?: string): Fn< - [T], - T extends Action - ? LensAction - : LensAtom> - > + ( + options: DelayOptions, + name?: string, + ): Fn<[T], T extends Action ? LensAction : LensAtom>> } = (options, name) => (anAtom: Atom) => { // listeners is a unique object for each atom instance const running = new WeakMap() - let { - min: minOption, - max: maxOption, - leading = true, - trailing = true, - abortable = true, - } = options - - throwReatomError( - minOption === undefined && maxOption === undefined, - 'wrong options', - ) + let { min: minOption, max: maxOption, leading = true, trailing = true, abortable = true } = options + + throwReatomError(minOption === undefined && maxOption === undefined, 'wrong options') minOption ??= maxOption maxOption ??= Number.MAX_SAFE_INTEGER name = mapName(anAtom, 'delay', name) - const minAtom = isAtom(minOption) - ? minOption - : atom(minOption, `${name}._minAtom`) - const maxAtom = isAtom(maxOption) - ? maxOption - : atom(maxOption, `${name}._maxAtom`) + const minAtom = isAtom(minOption) ? minOption : atom(minOption, `${name}._minAtom`) + const maxAtom = isAtom(maxOption) ? maxOption : atom(maxOption, `${name}._maxAtom`) // @ts-expect-error const theAtom: LensAtom & LensAction = atom((ctx, prevState?: any) => { @@ -79,11 +54,7 @@ export const delay: { const max = ctx.get(maxAtom) const startsKey = ctx.cause.listeners const depState = ctx.spy(anAtom) - let state = ctx.cause.pubs.length - ? prevState - : proto.isAction - ? [] - : depState + let state = ctx.cause.pubs.length ? prevState : proto.isAction ? [] : depState if (!ctx.cause.pubs.length) { state = proto.isAction ? [] : depState @@ -131,35 +102,25 @@ export const delay: { /** Delay updates by timeout */ export const debounce: { // TODO for some reason an atom not handled by overloads, if an action overload is first - (wait: DelayOptions['min'], name?: string): Fn< - [T], - T extends Action - ? LensAction - : LensAtom> - > + ( + wait: DelayOptions['min'], + name?: string, + ): Fn<[T], T extends Action ? LensAction : LensAtom>> } = (min = 1, name) => (anAtom) => // @ts-expect-error - delay( - { min, leading: false, trailing: true }, - mapName(anAtom, 'debounce', name), - )(anAtom) + delay({ min, leading: false, trailing: true }, mapName(anAtom, 'debounce', name))(anAtom) /** Skip updates by interval */ export const throttle: { // TODO for some reason an atom not handled by overloads, if an action overload is first - (wait: DelayOptions['max'], name?: string): Fn< - [T], - T extends Action - ? LensAction - : LensAtom> - > + ( + wait: DelayOptions['max'], + name?: string, + ): Fn<[T], T extends Action ? LensAction : LensAtom>> } = (max = 1, name) => (anAtom) => // @ts-expect-error - delay( - { max, leading: true, trailing: false }, - mapName(anAtom, 'throttle', name), - )(anAtom) + delay({ max, leading: true, trailing: false }, mapName(anAtom, 'throttle', name))(anAtom) diff --git a/packages/lens/src/effect.ts b/packages/lens/src/effect.ts index 41b26d063..3e347aa57 100644 --- a/packages/lens/src/effect.ts +++ b/packages/lens/src/effect.ts @@ -1,21 +1,11 @@ -import { - Action, - atom, - Atom, - AtomCache, - AtomState, - Ctx, - Fn, - Unsubscribe, -} from '@reatom/core' +import { Action, atom, Atom, AtomCache, AtomState, Ctx, Fn, Unsubscribe } from '@reatom/core' import { __thenReatomed } from '@reatom/effects' import { mapName } from './utils' import { type LensAction } from './' import { onUpdate } from '@reatom/hooks' import { noop } from '@reatom/utils' -export interface LensEffect - extends LensAction { +export interface LensEffect extends LensAction { unstable_unhook: Unsubscribe } @@ -40,52 +30,42 @@ export const effect: { ], Awaited > - : LensEffect< - [{ params: [AtomState]; payload: AtomState }], - Awaited - > + : LensEffect<[{ params: [AtomState]; payload: AtomState }], Awaited> > } = (fn: Fn, name?: string) => (anAtom: Atom) => { const { isAction } = anAtom.__reatom // TODO better error handling // @ts-expect-error - const theAtom: LensEffect = atom((ctx, state = []) => { - const resolve = (params: any[], payload: any) => - ctx.get((read, acualize) => { - if (payload instanceof Promise) { - payload.then((payload) => resolve(params, payload)) + const theAtom: LensEffect = atom( + (ctx, state = []) => { + const resolve = (params: any[], payload: any) => + ctx.get((read, acualize) => { + if (payload instanceof Promise) { + payload.then((payload) => resolve(params, payload)) + } else { + acualize!(ctx, ctx.cause.proto, (patchCtx: Ctx, patch: AtomCache) => { + patch.state = [{ params, payload }] + }) + } + }) + + ctx.spy(anAtom, (value) => { + if (isAction && value.payload instanceof Promise) { + __thenReatomed(ctx, value.payload, (payload) => + ctx.schedule(() => resolve([{ params: value.params, payload }], fn(ctx, payload, value.params))), + ) } else { - acualize!(ctx, ctx.cause.proto, (patchCtx: Ctx, patch: AtomCache) => { - patch.state = [{ params, payload }] - }) + ctx.schedule(() => resolve([value], isAction ? fn(ctx, value.payload, value.params) : fn(ctx, value))) } }) - ctx.spy(anAtom, (value) => { - if (isAction && value.payload instanceof Promise) { - __thenReatomed(ctx, value.payload, (payload) => - ctx.schedule(() => - resolve( - [{ params: value.params, payload }], - fn(ctx, payload, value.params), - ), - ), - ) - } else { - ctx.schedule(() => - resolve( - [value], - isAction ? fn(ctx, value.payload, value.params) : fn(ctx, value), - ), - ) - } - }) - - // @ts-expect-error we don't need `spy` anymore - ctx.spy = undefined + // @ts-expect-error we don't need `spy` anymore + ctx.spy = undefined - return state ?? [] - }, mapName(anAtom, 'effect', name)) + return state ?? [] + }, + mapName(anAtom, 'effect', name), + ) theAtom.__reatom.isAction = true theAtom.deps = [anAtom] theAtom.unstable_unhook = onUpdate(theAtom, noop) diff --git a/packages/lens/src/filter.ts b/packages/lens/src/filter.ts index 5bf4ffee4..8cdd2b32d 100644 --- a/packages/lens/src/filter.ts +++ b/packages/lens/src/filter.ts @@ -27,20 +27,22 @@ export const filter: { predicate ??= isAction ? () => true : (ctx, a, b) => !isShallowEqual(a, b) // @ts-expect-error - const theAtom: LensAtom & LensAction = atom((ctx, prevState?: any) => { - const isInit = ctx.cause.pubs.length === 0 - const state = ctx.spy(anAtom) + const theAtom: LensAtom & LensAction = atom( + (ctx, prevState?: any) => { + const isInit = ctx.cause.pubs.length === 0 + const state = ctx.spy(anAtom) - return isAction - ? state.reduce( - (acc: any, call: any) => - predicate!(ctx, call.payload, call.params) ? [call] : acc, - prevState ?? [], - ) - : isInit || predicate!(ctx, state, prevState) - ? state - : prevState - }, mapName(anAtom, 'filter', name)) + return isAction + ? state.reduce( + (acc: any, call: any) => (predicate!(ctx, call.payload, call.params) ? [call] : acc), + prevState ?? [], + ) + : isInit || predicate!(ctx, state, prevState) + ? state + : prevState + }, + mapName(anAtom, 'filter', name), + ) theAtom.deps = [anAtom] theAtom.__reatom.isAction = isAction diff --git a/packages/lens/src/index.test.ts b/packages/lens/src/index.test.ts index 560513a88..61fa4de05 100644 --- a/packages/lens/src/index.test.ts +++ b/packages/lens/src/index.test.ts @@ -62,10 +62,7 @@ test(`readonly and plain`, () => { }) test(`mapPayload, mapPayloadAwaited, toAtom`, async () => { - const a = action( - (ctx, v: number) => ctx.schedule(() => sleep(10).then(() => v)), - 'a', - ) + const a = action((ctx, v: number) => ctx.schedule(() => sleep(10).then(() => v)), 'a') const aMaybeString = a.pipe(mapPayloadAwaited((ctx, v) => v.toString())) const aString = aMaybeString.pipe(toAtom('0')) const aNumber = aMaybeString.pipe( @@ -368,10 +365,7 @@ test('withOnUpdate and sampleBuffer example', () => { const sampleBuffer = (signal: Atom) => (anAction: Action<[T], T>) => { - const bufferAtom = atom( - new Array(), - `${anAction.__reatom.name}._sampleBuffer`, - ) + const bufferAtom = atom(new Array(), `${anAction.__reatom.name}._sampleBuffer`) return anAction.pipe( mapPayload((ctx, value) => bufferAtom(ctx, (v) => [...v, value])), sample(signal), diff --git a/packages/lens/src/index.ts b/packages/lens/src/index.ts index 5d52604d7..aaa0e4c89 100644 --- a/packages/lens/src/index.ts +++ b/packages/lens/src/index.ts @@ -34,8 +34,7 @@ export * from './withReset' export interface LensAtom extends Atom { deps: Array } -export interface LensAction - extends Action { +export interface LensAction extends Action { deps: Array } @@ -114,8 +113,7 @@ export const mapState = ): Fn<[T], LensAtom> => (anAtom: Atom) => { const theAtom = atom( - (ctx, state?: any) => - mapper(ctx, ctx.spy(anAtom), ctx.cause!.pubs.at(0)?.state, state), + (ctx, state?: any) => mapper(ctx, ctx.spy(anAtom), ctx.cause!.pubs.at(0)?.state, state), mapName(anAtom, 'mapState', name), ) as LensAtom theAtom.deps = [anAtom] @@ -129,14 +127,8 @@ export const mapPayload: { map: Fn<[Ctx, Payload, Params], T>, name?: string, ): Fn<[Action], LensAction<[], T>> - ( - fallback: ActionPayload, - name?: string, - ): Fn<[T], LensAtom>> - ( - fallback: State, - name?: string, - ): Fn<[Action], LensAtom> + (fallback: ActionPayload, name?: string): Fn<[T], LensAtom>> + (fallback: State, name?: string): Fn<[Action], LensAtom> ( fallback: State, map: Fn<[Ctx, Payload, Params, State], State>, @@ -168,9 +160,7 @@ export const mapPayload: { ((ctx.spy = undefined), depState.reduce((acc: any, v) => { const payload = map(ctx, v.payload, v.params) - return payload === SKIP - ? acc - : [...acc, { params: [v], payload }] + return payload === SKIP ? acc : [...acc, { params: [v], payload }] }, prevState)) : depState.reduce((acc, { payload, params }) => { const state = map(ctx, payload, params, acc) @@ -192,42 +182,24 @@ export const mapPayloadAwaited: { mapper?: Fn<[Ctx, Awaited], Payload>, name?: string, ): Fn<[Action], LensAction<[], Payload>> - ( - fallback: Awaited>, - name?: string, - ): Fn<[T], LensAtom>>> - ( - fallback: State, - name?: string, - ): Fn<[Action], LensAtom>> - ( - fallback: State, - map: Fn<[Ctx, Awaited], State>, - name?: string, - ): Fn<[Action], LensAtom> + (fallback: Awaited>, name?: string): Fn<[T], LensAtom>>> + (fallback: State, name?: string): Fn<[Action], LensAtom>> + (fallback: State, map: Fn<[Ctx, Awaited], State>, name?: string): Fn<[Action], LensAtom> } = (...a: [any?, any?, any?]) => (anAction: Action): any => { const isAction = a.length === 0 || typeof a[0] === 'function' - const [fallback, map = (ctx: Ctx, v: any) => v, name] = isAction - ? [[], a[0], a[1]] - : a + const [fallback, map = (ctx: Ctx, v: any) => v, name] = isAction ? [[], a[0], a[1]] : a const params = isAction ? [] : [fallback] params.push( (ctx: Ctx, promise: any) => { if (promise instanceof Promise) { __thenReatomed(ctx, promise, (v, read, actualize) => - actualize!( - ctx, - ctx.cause!.proto, - (patchCtx: Ctx, patch: AtomCache) => { - patch.cause = ctx.cause.cause - const payload = map(ctx, v) - patch.state = isAction - ? [...patch.state, { params: [v], payload }] - : payload - }, - ), + actualize!(ctx, ctx.cause!.proto, (patchCtx: Ctx, patch: AtomCache) => { + patch.cause = ctx.cause.cause + const payload = map(ctx, v) + patch.state = isAction ? [...patch.state, { params: [v], payload }] : payload + }), ) return SKIP @@ -267,38 +239,19 @@ export const mapInput = /** Convert action to atom with optional fallback state */ // @ts-expect-error export const toAtom: { - ( - fallback: ReturnType, - name?: string, - ): Fn<[T], LensAtom>> - ( - fallback?: undefined, - name?: string, - ): Fn<[T], LensAtom>> - ( - fallback: State, - name?: string, - ): Fn<[T], LensAtom>> + (fallback: ReturnType, name?: string): Fn<[T], LensAtom>> + (fallback?: undefined, name?: string): Fn<[T], LensAtom>> + (fallback: State, name?: string): Fn<[T], LensAtom>> } = (fallback?: any, name?: string): Fn<[Action], Atom> => (anAction) => - mapPayload( - fallback, - (ctx, v: any) => v, - mapName(anAction, 'toAtom', name), - )(anAction) + mapPayload(fallback, (ctx, v: any) => v, mapName(anAction, 'toAtom', name))(anAction) // https://rxjs.dev/api/operators/tap export const withOnUpdate = ( cb: T extends Action - ? Fn< - [ - Ctx, - Payload, - AtomCache>> & { params: Params }, - ] - > + ? Fn<[Ctx, Payload, AtomCache>> & { params: Params }]> : Fn<[Ctx, AtomState, AtomCache>]>, ) => (anAtom: T): T => { @@ -309,10 +262,7 @@ export const withOnUpdate = /** Convert an atom to action */ export const toAction: { - ( - map: Fn<[ctx: Ctx, state: State], T>, - name?: string, - ): Fn<[Atom], LensAction<[State], T>> + (map: Fn<[ctx: Ctx, state: State], T>, name?: string): Fn<[Atom], LensAction<[State], T>> (name?: string): Fn<[Atom], LensAction<[T], T>> } = (map?: string | Fn, name?: string) => (anAtom: Atom) => { throwReatomError(anAtom.__reatom.isAction, 'atom expected') @@ -329,9 +279,7 @@ export const toAction: { // TODO handle atom mutation in the same transaction const isInit = ctx.cause.pubs.length === 0 const state = ctx.spy(anAtom) - return isInit - ? [] - : [{ params: [state], payload: (map as Fn)(ctx, state) }] + return isInit ? [] : [{ params: [state], payload: (map as Fn)(ctx, state) }] }, mapName(anAtom, 'toAction', name), ) diff --git a/packages/lens/src/match.test.ts b/packages/lens/src/match.test.ts index f0ec49b34..7eb73fb05 100644 --- a/packages/lens/src/match.test.ts +++ b/packages/lens/src/match.test.ts @@ -9,31 +9,16 @@ const test = suite('match') test('is method', () => { const ctx = createTestCtx() - const expressions = [ - 'a', - () => 'a', - atom('a'), - (ctx: CtxSpy) => ctx.spy(atom('a')), - ] - const statements = [ - true, - (ctx: Ctx, value: any) => value === 'a', - (ctx: Ctx) => ctx.get(atom(true)), - ] + const expressions = ['a', () => 'a', atom('a'), (ctx: CtxSpy) => ctx.spy(atom('a'))] + const statements = [true, (ctx: Ctx, value: any) => value === 'a', (ctx: Ctx) => ctx.get(atom(true))] for (const expression of expressions) { for (const statement of statements) { assert.is(ctx.get(match(expression)), undefined) assert.is(ctx.get(match(expression).is('b', statement)), undefined) assert.is(ctx.get(match(expression).is('a', statement)), true) - assert.is( - ctx.get(match(expression).is('a', statement).is('b', true)), - true, - ) - assert.is( - ctx.get(match(expression).is('b', statement).is('a', true)), - true, - ) + assert.is(ctx.get(match(expression).is('a', statement).is('b', true)), true) + assert.is(ctx.get(match(expression).is('b', statement).is('a', true)), true) assert.is(ctx.get(match(expression).default(statement)), true) } } @@ -54,10 +39,7 @@ test('with', () => { type Data = { type: 'text' } | { type: 'img' } - type Result = - | { type: 'ok'; data: Data } - | { type: 'error' } - | { type: 'unknown' } + type Result = { type: 'ok'; data: Data } | { type: 'error' } | { type: 'unknown' } const result = atom(null! as Result) diff --git a/packages/lens/src/match.ts b/packages/lens/src/match.ts index 771a43300..df3571134 100644 --- a/packages/lens/src/match.ts +++ b/packages/lens/src/match.ts @@ -1,13 +1,4 @@ -import { - Atom, - Ctx, - CtxSpy, - Fn, - __count, - atom, - isAtom, - throwReatomError, -} from '@reatom/core' +import { Atom, Ctx, CtxSpy, Fn, __count, atom, isAtom, throwReatomError } from '@reatom/core' import { isRec, isShallowEqual, merge } from '@reatom/utils' type Primitive = null | undefined | string | number | boolean | symbol | bigint @@ -24,37 +15,25 @@ export type PartialDeep = T extends BuiltIns } : unknown -interface Match - extends Atom { +interface Match extends Atom { is( - clause: - | Expression - | Atom - | ((ctx: Ctx, expression: Expression) => boolean), + clause: Expression | Atom | ((ctx: Ctx, expression: Expression) => boolean), statement: T | Atom | ((ctx: CtxSpy, expression: Expression) => T), ): Match with>( part: Part, - statement?: - | T - | Atom - | ((ctx: CtxSpy, expression: Part & Expression) => T), + statement?: T | Atom | ((ctx: CtxSpy, expression: Part & Expression) => T), ): Match, T> truthy( statement: T | Atom | ((ctx: CtxSpy, expression: Expression) => T), ): Match - falsy( - statement: T | Atom | ((ctx: CtxSpy, expression: Expression) => T), - ): Match + falsy(statement: T | Atom | ((ctx: CtxSpy, expression: Expression) => T)): Match default( statement?: T | Atom | ((ctx: CtxSpy, expression: Expression) => T), ): Match } -export function match( - expression: T | Atom | ((ctx: CtxSpy) => T), - name = __count('match'), -): Match { +export function match(expression: T | Atom | ((ctx: CtxSpy) => T), name = __count('match')): Match { type Case = { clause: (ctx: Ctx, expression: T) => boolean statement: {} | Atom | ((ctx: Ctx, expression: T) => any) diff --git a/packages/lens/src/parseAtoms.ts b/packages/lens/src/parseAtoms.ts index a818335b5..7387c94dd 100644 --- a/packages/lens/src/parseAtoms.ts +++ b/packages/lens/src/parseAtoms.ts @@ -1,9 +1,5 @@ import { Action, Atom, Ctx, isAction, isAtom, Rec } from '@reatom/core' -import { - isLinkedListAtom, - LinkedList, - LinkedListLikeAtom, -} from '@reatom/primitives' +import { isLinkedListAtom, LinkedList, LinkedListLikeAtom } from '@reatom/primitives' import { isRec } from '@reatom/utils' export type ParseAtoms = T extends Action @@ -24,10 +20,7 @@ export type ParseAtoms = T extends Action } : T -export const parseAtoms = ( - ctx: Ctx, - value: Value, -): ParseAtoms => { +export const parseAtoms = (ctx: Ctx, value: Value): ParseAtoms => { if (isAction(value)) return value as ParseAtoms if (isLinkedListAtom(value)) value = value.array as any diff --git a/packages/lens/src/sample.ts b/packages/lens/src/sample.ts index 469cbb42c..5ac8c085d 100644 --- a/packages/lens/src/sample.ts +++ b/packages/lens/src/sample.ts @@ -8,12 +8,10 @@ import { type LensAtom, type LensAction } from './' // @ts-expect-error export const sample: { // TODO for some reason an atom not handled by overloads, if an action overload is first - (signal: Atom, name?: string): Fn< - [T], - T extends Action - ? LensAction<[], Payload> - : LensAtom> - > + ( + signal: Atom, + name?: string, + ): Fn<[T], T extends Action ? LensAction<[], Payload> : LensAtom>> } = (signal: Atom, name?: string) => // @ts-ignore diff --git a/packages/lens/src/select.test.ts b/packages/lens/src/select.test.ts index 2fc236242..cb48aa70c 100644 --- a/packages/lens/src/select.test.ts +++ b/packages/lens/src/select.test.ts @@ -34,9 +34,7 @@ test('many selects should work', () => { const list = atom(new Array<{ value: AtomMut }>()) const target = atom((ctx) => { const length = select(ctx, (ctx) => ctx.spy(list).length) - const sum = select(ctx, (ctx) => - ctx.spy(list).reduce((acc, el) => acc + ctx.spy(el.value), 0), - ) + const sum = select(ctx, (ctx) => ctx.spy(list).reduce((acc, el) => acc + ctx.spy(el.value), 0)) return { length, sum } }) @@ -61,11 +59,7 @@ test('many selects should work', () => { test('prevent select memoization errors', () => { const list = atom(new Array>()) - const sum = atom((ctx) => - ctx - .spy(list) - .reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0), - ) + const sum = atom((ctx) => ctx.spy(list).reduce((acc, el) => acc + select(ctx, (ctx) => ctx.spy(el).value), 0)) const ctx = createTestCtx() const track = ctx.subscribeTrack(sum) @@ -73,8 +67,7 @@ test('prevent select memoization errors', () => { assert.is(ctx.get(sum), 0) assert.throws( - () => - list(ctx, [atom({ name: 'a', value: 1 }), atom({ name: 'b', value: 2 })]), + () => list(ctx, [atom({ name: 'a', value: 1 }), atom({ name: 'b', value: 2 })]), 'Reatom error: multiple select with the same "toString" representation is not allowed', ) // assert.is(track.calls.length, 2) diff --git a/packages/lens/src/select.ts b/packages/lens/src/select.ts index d3481ff59..33d8c178f 100644 --- a/packages/lens/src/select.ts +++ b/packages/lens/src/select.ts @@ -1,19 +1,8 @@ -import { - Atom, - AtomProto, - Ctx, - CtxSpy, - __count, - atom, - throwReatomError, -} from '@reatom/core' +import { Atom, AtomProto, Ctx, CtxSpy, __count, atom, throwReatomError } from '@reatom/core' type FunctionSource = string -const mapAtom = atom( - null as any as WeakMap>, - 'select._map', -) +const mapAtom = atom(null as any as WeakMap>, 'select._map') mapAtom.__reatom.initState = () => new WeakMap() const touchedMap = new WeakMap>() diff --git a/packages/lens/src/withReset.ts b/packages/lens/src/withReset.ts index a71879a5a..a567396bb 100644 --- a/packages/lens/src/withReset.ts +++ b/packages/lens/src/withReset.ts @@ -11,8 +11,7 @@ export const withReset = actualize!( ctx, anAtom.__reatom, - (patchCtx: Ctx, patch: AtomCache) => - (patch.state = patch.proto.initState(ctx)), + (patchCtx: Ctx, patch: AtomCache) => (patch.state = patch.proto.initState(ctx)), ).state, ), `${anAtom.__reatom.name}._reset`, diff --git a/packages/logger/CHANGELOG.md b/packages/logger/CHANGELOG.md index 51c490a71..1df0161a0 100644 --- a/packages/logger/CHANGELOG.md +++ b/packages/logger/CHANGELOG.md @@ -2,146 +2,130 @@ ## [3.8.4](https://github.com/artalar/reatom/compare/logger-v3.8.3...logger-v3.8.4) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.8.3](https://github.com/artalar/reatom/compare/logger-v3.8.2...logger-v3.8.3) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **logger:** wrap nullish atom name ([#878](https://github.com/artalar/reatom/issues/878)) ([65c90ed](https://github.com/artalar/reatom/commit/65c90ed75c88d737a32c1e459d127980df1e4161)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **logger:** wrap nullish atom name ([#878](https://github.com/artalar/reatom/issues/878)) ([65c90ed](https://github.com/artalar/reatom/commit/65c90ed75c88d737a32c1e459d127980df1e4161)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.8.2](https://github.com/artalar/reatom/compare/logger-v3.8.1...logger-v3.8.2) (2024-06-17) - ### Bug Fixes -* **logger:** remove devtools ([613c2dc](https://github.com/artalar/reatom/commit/613c2dcb8eb894ad49a7de71d852ed9f86ee6834)) +- **logger:** remove devtools ([613c2dc](https://github.com/artalar/reatom/commit/613c2dcb8eb894ad49a7de71d852ed9f86ee6834)) ## [3.8.1](https://github.com/artalar/reatom/compare/logger-v3.8.0...logger-v3.8.1) (2024-06-14) - ### Bug Fixes -* **logger:** discoveryjs bundle ([0200114](https://github.com/artalar/reatom/commit/020011447a3f379a182f4b9dbb4dc754b46a4a85)) +- **logger:** discoveryjs bundle ([0200114](https://github.com/artalar/reatom/commit/020011447a3f379a182f4b9dbb4dc754b46a4a85)) ## [3.8.0](https://github.com/artalar/reatom/compare/logger-v3.7.0...logger-v3.8.0) (2024-06-12) - ### Features -* **logger:** use discoveryjs ([eef7808](https://github.com/artalar/reatom/commit/eef7808cff6e36f16dfd97396e95f230f6dc4bda)) +- **logger:** use discoveryjs ([eef7808](https://github.com/artalar/reatom/commit/eef7808cff6e36f16dfd97396e95f230f6dc4bda)) ## [3.7.0](https://github.com/artalar/reatom/compare/logger-v3.6.1...logger-v3.7.0) (2024-06-11) - ### Features -* **logger:** add experimental_reatomInspector ([f1de3db](https://github.com/artalar/reatom/commit/f1de3db5eafb1822d974e43325b73f1f606591b5)) +- **logger:** add experimental_reatomInspector ([f1de3db](https://github.com/artalar/reatom/commit/f1de3db5eafb1822d974e43325b73f1f606591b5)) ## [3.6.1](https://github.com/artalar/reatom/compare/logger-v3.6.0...logger-v3.6.1) (2024-04-28) - ### Bug Fixes -* **logger:** cause infinity loop ([a8af2b8](https://github.com/artalar/reatom/commit/a8af2b824e4772e64eb6c85695eba494856e52cf)) +- **logger:** cause infinity loop ([a8af2b8](https://github.com/artalar/reatom/commit/a8af2b824e4772e64eb6c85695eba494856e52cf)) ## [3.6.0](https://github.com/artalar/reatom/compare/logger-v3.5.0...logger-v3.6.0) (2023-10-09) - ### Features -* **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) +- **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) ## [3.5.0](https://github.com/artalar/reatom/compare/logger-v3.4.5...logger-v3.5.0) (2023-09-08) - ### Features -* **logger:** add shouldLogGraph ([39ea646](https://github.com/artalar/reatom/commit/39ea64680cdf0dea23045e7d13c6a45e927df541)) - +- **logger:** add shouldLogGraph ([39ea646](https://github.com/artalar/reatom/commit/39ea64680cdf0dea23045e7d13c6a45e927df541)) ### Bug Fixes -* **logger:** filter changed state ([d9019a2](https://github.com/artalar/reatom/commit/d9019a2acb34bdd09d89aa0028754269d4024852)) +- **logger:** filter changed state ([d9019a2](https://github.com/artalar/reatom/commit/d9019a2acb34bdd09d89aa0028754269d4024852)) ## [3.4.5](https://github.com/artalar/reatom/compare/logger-v3.4.4...logger-v3.4.5) (2023-08-10) - ### Bug Fixes -* **logger:** filtered action handler ([0fc5587](https://github.com/artalar/reatom/commit/0fc5587cc28df45f23aca0985ce822fbaf410812)) +- **logger:** filtered action handler ([0fc5587](https://github.com/artalar/reatom/commit/0fc5587cc28df45f23aca0985ce822fbaf410812)) ## [3.4.4](https://github.com/artalar/reatom/compare/logger-v3.4.3...logger-v3.4.4) (2023-08-09) - ### Bug Fixes -* **logger:** catch error in logs processing ([48f2884](https://github.com/artalar/reatom/commit/48f2884e9cfc65ba2d952d586125493474c19ec6)) +- **logger:** catch error in logs processing ([48f2884](https://github.com/artalar/reatom/commit/48f2884e9cfc65ba2d952d586125493474c19ec6)) ## [3.4.3](https://github.com/artalar/reatom/compare/logger-v3.4.2...logger-v3.4.3) (2023-07-07) - ### Bug Fixes -* **logger:** add transactions list log ([f171f99](https://github.com/artalar/reatom/commit/f171f99af5d120235222bb7bc293c808c95c84d9)) -* **logger:** improve getCause ([856853d](https://github.com/artalar/reatom/commit/856853d4dad0387773dd54358c3f8addc7e11cce)) +- **logger:** add transactions list log ([f171f99](https://github.com/artalar/reatom/commit/f171f99af5d120235222bb7bc293c808c95c84d9)) +- **logger:** improve getCause ([856853d](https://github.com/artalar/reatom/commit/856853d4dad0387773dd54358c3f8addc7e11cce)) ## [3.4.2](https://github.com/artalar/reatom/compare/logger-v3.4.1...logger-v3.4.2) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.4.1](https://github.com/artalar/reatom/compare/logger-v3.4.0...logger-v3.4.1) (2023-05-10) - ### Bug Fixes -* **logger:** display the whole logs in the end of group ([98f8217](https://github.com/artalar/reatom/commit/98f8217ae297a508b5b781b696eaf60cb870228d)) -* **logger:** improve action params logging ([6a801de](https://github.com/artalar/reatom/commit/6a801de57b1916adb5a7b0feae7c25ad2095fce6)) -* **logger:** save init patch in history ([3c28526](https://github.com/artalar/reatom/commit/3c28526c4bc0b02787c69bd297d3af2729fa971d)) +- **logger:** display the whole logs in the end of group ([98f8217](https://github.com/artalar/reatom/commit/98f8217ae297a508b5b781b696eaf60cb870228d)) +- **logger:** improve action params logging ([6a801de](https://github.com/artalar/reatom/commit/6a801de57b1916adb5a7b0feae7c25ad2095fce6)) +- **logger:** save init patch in history ([3c28526](https://github.com/artalar/reatom/commit/3c28526c4bc0b02787c69bd297d3af2729fa971d)) ## [3.4.0](https://github.com/artalar/reatom/compare/logger-v3.3.1...logger-v3.4.0) (2023-04-18) - ### Features -* **logger:** add optional domain for customize logs group ([e09b4a6](https://github.com/artalar/reatom/commit/e09b4a65cbfb2bbf0e3559f1c3518622f688db48)) +- **logger:** add optional domain for customize logs group ([e09b4a6](https://github.com/artalar/reatom/commit/e09b4a65cbfb2bbf0e3559f1c3518622f688db48)) ## 3.4.0 (2023-04-15) diff --git a/packages/logger/src/graphView.ts b/packages/logger/src/graphView.ts index 610437a84..c2a7594d0 100644 --- a/packages/logger/src/graphView.ts +++ b/packages/logger/src/graphView.ts @@ -6,8 +6,7 @@ export const logGraph = (logsSet: Set) => { if ( patch.cause && patch.cause.proto !== __root && - (!patch.cause.proto.name!.startsWith('_') || - !patch.cause.proto.name!.includes('._')) && + (!patch.cause.proto.name!.startsWith('_') || !patch.cause.proto.name!.includes('._')) && !logsSet.has(patch.cause) && !visited.has(patch.cause) ) { @@ -21,8 +20,7 @@ export const logGraph = (logsSet: Set) => { const xGap = r * 2 const yGap = r * 3 const maxDistance = logs.reduce( - (acc, patch, i) => - Math.max(acc, i - ((patch.cause && logs.indexOf(patch.cause)) ?? i)), + (acc, patch, i) => Math.max(acc, i - ((patch.cause && logs.indexOf(patch.cause)) ?? i)), 0, ) const shiftRatio = maxDistance * xGap @@ -36,15 +34,9 @@ export const logGraph = (logsSet: Set) => { for (const patch of logs) { // if (!patch.cause) continue; const { isAction, name } = patch.proto - const color = isAction - ? name!.endsWith('.onFulfill') - ? '#E6DC73' - : '#ffff80' - : '#151134' + const color = isAction ? (name!.endsWith('.onFulfill') ? '#E6DC73' : '#ffff80') : '#151134' body += `` - body += `${name}` + body += `${name}` y += yGap width = Math.max(width, x + name!.length * r) } diff --git a/packages/logger/src/index.test.ts b/packages/logger/src/index.test.ts index aa22e6a64..436793bc6 100644 --- a/packages/logger/src/index.test.ts +++ b/packages/logger/src/index.test.ts @@ -55,13 +55,8 @@ test.skip(`base`, async () => { test.skip(`cause`, async () => { // should correct calculate cause for complex async transactions - const doAsync = action( - (ctx, v: number) => ctx.schedule(() => Promise.resolve(v)), - `doAsync`, - ) - const asyncResAtom = doAsync.pipe( - mapPayloadAwaited((ctx, v) => v, `asyncResAtom`), - ) + const doAsync = action((ctx, v: number) => ctx.schedule(() => Promise.resolve(v)), `doAsync`) + const asyncResAtom = doAsync.pipe(mapPayloadAwaited((ctx, v) => v, `asyncResAtom`)) const resMapAtom = atom((ctx) => ctx.spy(asyncResAtom), 'resMapAtom') const ctx = createCtx() diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index c18512b02..0c6e6ed84 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -23,10 +23,7 @@ export interface LogMsg { export const getCause = (patch: AtomCache, log = ''): string => { if (log.length > 10_000) return `${log} ...` if (patch.cause !== null && patch.cause.proto !== __root) - return getCause( - patch.cause, - log + ' <-- ' + (patch.cause.proto.name ?? 'unnamed'), - ) + return getCause(patch.cause, log + ' <-- ' + (patch.cause.proto.name ?? 'unnamed')) else { return log || 'root' } @@ -71,16 +68,13 @@ export const createLogBatched = ({ setTimeout( (length) => { - isBatching = - queue.length !== length && Date.now() - batchingStart < limit + isBatching = queue.length !== length && Date.now() - batchingStart < limit if (isBatching) return const isFewTransactions = queue.length > 0 - console.groupCollapsed( - `Reatom ${domain}${length} transaction${length > 1 ? 's' : ''}`, - ) + console.groupCollapsed(`Reatom ${domain}${length} transaction${length > 1 ? 's' : ''}`) if (shouldLogGraph) { logGraph( @@ -94,21 +88,14 @@ export const createLogBatched = ({ } for (const { changes, time, error } of queue) { - console.log( - `%c ${time}`, - `padding-left: calc(50% - ${ - time.length / 2 - }em); font-size: 0.7rem;`, - ) + console.log(`%c ${time}`, `padding-left: calc(50% - ${time.length / 2}em); font-size: 0.7rem;`) if (error) console.error(error) let inGroup = false Object.entries(changes).forEach(([k, change], i, arr) => { const isAction = 'payload' in change - const color = isAction - ? 'background: #ffff80; color: #151134;' - : 'background: #151134; color: white;' + const color = isAction ? 'background: #ffff80; color: #151134;' : 'background: #151134; color: white;' const style = `${color}font-weight: 400; padding: 0.15em; padding-right: 1ch;` const name = k.replace(/(\d)*\./, '') @@ -218,16 +205,11 @@ export const connectLogger = ( let atomHistory = history.get(proto) ?? [] if (historyLength) { atomHistory = atomHistory.slice(0, historyLength - 1) - atomHistory.unshift( - isAction ? { ...patch, state: [...state] } : patch, - ) + atomHistory.unshift(isAction ? { ...patch, state: [...state] } : patch) history.set(proto, atomHistory) } - const isConnection = - !oldCache && - cause!.proto.name === 'root' && - (!isAction || state.length === 0) + const isConnection = !oldCache && cause!.proto.name === 'root' && (!isAction || state.length === 0) if (isConnection) continue diff --git a/packages/npm-cookie-baker/CHANGELOG.md b/packages/npm-cookie-baker/CHANGELOG.md index 84fb3de57..7560a77b8 100644 --- a/packages/npm-cookie-baker/CHANGELOG.md +++ b/packages/npm-cookie-baker/CHANGELOG.md @@ -2,50 +2,47 @@ ## [3.1.5](https://github.com/artalar/reatom/compare/npm-cookie-baker-v3.1.4...npm-cookie-baker-v3.1.5) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.4](https://github.com/artalar/reatom/compare/npm-cookie-baker-v3.1.3...npm-cookie-baker-v3.1.4) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.3](https://github.com/artalar/reatom/compare/npm-cookie-baker-v3.1.2...npm-cookie-baker-v3.1.3) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/npm-cookie-baker/README.md b/packages/npm-cookie-baker/README.md index 48de3aeca..b1ecbe616 100644 --- a/packages/npm-cookie-baker/README.md +++ b/packages/npm-cookie-baker/README.md @@ -37,14 +37,11 @@ npm install @cookie-baker/node ``` ## Example -[codesandbox](https://codesandbox.io/s/reatom-cookie-baker-ec6h63-ec6h63?file=/src/App.tsx) +[codesandbox](https://codesandbox.io/s/reatom-cookie-baker-ec6h63-ec6h63?file=/src/App.tsx) ```ts -import { - Cookie as CookieClient, - createRealTimeCookie, -} from '@cookie-baker/browser' +import { Cookie as CookieClient, createRealTimeCookie } from '@cookie-baker/browser' import { reatomCookie } from '@reatom/npm-cookie-baker' type CookieModel = { diff --git a/packages/npm-cookie-baker/src/index.ts b/packages/npm-cookie-baker/src/index.ts index c3aa323db..7158ea48f 100644 --- a/packages/npm-cookie-baker/src/index.ts +++ b/packages/npm-cookie-baker/src/index.ts @@ -1,18 +1,12 @@ import { action, atom } from '@reatom/core' -import { - CookieController, - CookieObjectModel, - RealTimeCookie, -} from '@cookie-baker/core' +import { CookieController, CookieObjectModel, RealTimeCookie } from '@cookie-baker/core' import { onConnect, withInit } from '@reatom/hooks' export const reatomCookie = ( cookie: CookieController, realTimeCookie: RealTimeCookie, ) => { - const cookieAtom = atom({} as ReturnType).pipe( - withInit(() => cookie.get()), - ) + const cookieAtom = atom({} as ReturnType).pipe(withInit(() => cookie.get())) let updateCookie: Parameters[0] onConnect(cookieAtom, (ctx) => { updateCookie = (newCookie) => cookieAtom(ctx, newCookie) @@ -24,27 +18,23 @@ export const reatomCookie = ( } }) - const remove = action['remove']>>( - (ctx, name) => { - cookieAtom(ctx, (oldCookie) => { - const newCookie = { ...oldCookie } - delete newCookie[name] - return newCookie - }) - ctx.schedule(() => { - cookie.remove(name) - }) - }, - ) + const remove = action['remove']>>((ctx, name) => { + cookieAtom(ctx, (oldCookie) => { + const newCookie = { ...oldCookie } + delete newCookie[name] + return newCookie + }) + ctx.schedule(() => { + cookie.remove(name) + }) + }) - const set = action['set']>>( - (ctx, name, value, options) => { - cookieAtom(ctx, (oldCookie) => ({ ...oldCookie, [name]: value })) - ctx.schedule(() => { - cookie.set(name, value, options) - }) - }, - ) + const set = action['set']>>((ctx, name, value, options) => { + cookieAtom(ctx, (oldCookie) => ({ ...oldCookie, [name]: value })) + ctx.schedule(() => { + cookie.set(name, value, options) + }) + }) return { cookieAtom, diff --git a/packages/npm-history/CHANGELOG.md b/packages/npm-history/CHANGELOG.md index f34a6034e..6bb4db9e7 100644 --- a/packages/npm-history/CHANGELOG.md +++ b/packages/npm-history/CHANGELOG.md @@ -2,57 +2,53 @@ ## [3.1.8](https://github.com/artalar/reatom/compare/npm-history-v3.1.7...npm-history-v3.1.8) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.7](https://github.com/artalar/reatom/compare/npm-history-v3.1.6...npm-history-v3.1.7) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.6](https://github.com/artalar/reatom/compare/npm-history-v3.1.5...npm-history-v3.1.6) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.1.5](https://github.com/artalar/reatom/compare/npm-history-v3.1.4...npm-history-v3.1.5) (2023-05-10) - ### Bug Fixes -* **npm-history:** computer order, better cause logging ([92f5b82](https://github.com/artalar/reatom/commit/92f5b82c97c7d8eb3fdc9db85dbdf5b9c90a8404)) +- **npm-history:** computer order, better cause logging ([92f5b82](https://github.com/artalar/reatom/commit/92f5b82c97c7d8eb3fdc9db85dbdf5b9c90a8404)) diff --git a/packages/npm-history/src/index.test.ts b/packages/npm-history/src/index.test.ts index a766f5f8a..15a9c26c2 100644 --- a/packages/npm-history/src/index.test.ts +++ b/packages/npm-history/src/index.test.ts @@ -18,9 +18,7 @@ type Routes = T extends '' ? never : RouteAtom & { routes: { - [K in keyof T as K extends `${infer K}/${string}` - ? K - : K]: K extends `${string}/:${infer Param}` + [K in keyof T as K extends `${infer K}/${string}` ? K : K]: K extends `${string}/:${infer Param}` ? // @ts-ignore Routes> & { param: Atom } : // @ts-ignore @@ -28,10 +26,7 @@ type Routes = T extends '' } } -export const unstable_reatomRoutes = ( - base: string, - routes: T, -): Routes => { +export const unstable_reatomRoutes = (base: string, routes: T): Routes => { const path = parsePath(base) const routeAtom = atom((ctx) => // TODO tests @@ -42,9 +37,7 @@ export const unstable_reatomRoutes = ( go: action((ctx) => historyAtom.push(ctx, base)), routes: Object.keys(routes).reduce((acc, key) => { const nextIdx = key.indexOf('/') - const subRoutes = ( - typeof routes[key] === 'object' ? routes[key] : {} - ) as Rec + const subRoutes = (typeof routes[key] === 'object' ? routes[key] : {}) as Rec let isDynamic = false if ((isDynamic = key.startsWith(':'))) { @@ -56,10 +49,7 @@ export const unstable_reatomRoutes = ( key = key.slice(0, nextIdx) } - const routeAtom = (acc[key] = unstable_reatomRoutes( - `${base === '/' ? '' : base}/${key}`, - subRoutes, - )) + const routeAtom = (acc[key] = unstable_reatomRoutes(`${base === '/' ? '' : base}/${key}`, subRoutes)) if (isDynamic) { console.log({ isDynamic, base }) diff --git a/packages/npm-history/src/index.ts b/packages/npm-history/src/index.ts index cb74fac16..07c27b003 100644 --- a/packages/npm-history/src/index.ts +++ b/packages/npm-history/src/index.ts @@ -1,13 +1,6 @@ import { History, Location, To, Blocker } from 'history' -import { - action, - atom, - Atom, - AtomMut, - Action, - throwReatomError, -} from '@reatom/core' +import { action, atom, Atom, AtomMut, Action, throwReatomError } from '@reatom/core' import { onUpdate } from '@reatom/hooks' import { isShallowEqual } from '@reatom/utils' @@ -36,8 +29,7 @@ onUpdate(historyAtom, (ctx, history) => { // @ts-expect-error const locationAtom: HistoryAtom['location'] = atom(null, 'historyAtom.location') -locationAtom.__reatom.computer = (ctx) => - Object.assign({}, ctx.spy(historyAtom).location) +locationAtom.__reatom.computer = (ctx) => Object.assign({}, ctx.spy(historyAtom).location) const push: HistoryAtom['push'] = action((ctx, to: To, state?: any) => { const history = ctx.get(historyAtom) diff --git a/packages/npm-history/vite.config.ts b/packages/npm-history/vite.config.ts index b1b5f91e5..5a33944a9 100644 --- a/packages/npm-history/vite.config.ts +++ b/packages/npm-history/vite.config.ts @@ -3,5 +3,5 @@ import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ - plugins: [react()] + plugins: [react()], }) diff --git a/packages/npm-lit/CHANGELOG.md b/packages/npm-lit/CHANGELOG.md index 325b3b644..7a3969c9b 100644 --- a/packages/npm-lit/CHANGELOG.md +++ b/packages/npm-lit/CHANGELOG.md @@ -2,50 +2,47 @@ ## [3.1.3](https://github.com/artalar/reatom/compare/npm-lit-v3.1.2...npm-lit-v3.1.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.2](https://github.com/artalar/reatom/compare/npm-lit-v3.1.1...npm-lit-v3.1.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.1](https://github.com/artalar/reatom/compare/npm-lit-v3.1.0...npm-lit-v3.1.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/npm-lit/src/index.ts b/packages/npm-lit/src/index.ts index 161129695..57391d416 100644 --- a/packages/npm-lit/src/index.ts +++ b/packages/npm-lit/src/index.ts @@ -5,8 +5,7 @@ import { LitElement, PropertyValues } from 'lit' type Constructor = new (...args: any[]) => T -const isShallowEqual = (a: unknown[], b: unknown[]) => - a.length === b.length && a.every((el, i) => el === b[i]) +const isShallowEqual = (a: unknown[], b: unknown[]) => a.length === b.length && a.every((el, i) => el === b[i]) let ctx: Ctx @@ -29,9 +28,7 @@ export const setupCtx = (value: Ctx): void => { * * @returns class with ctx property */ -export const withReatom = >( - superClass: T, -): T & Constructor<{ ctx: CtxSpy }> => { +export const withReatom = >(superClass: T): T & Constructor<{ ctx: CtxSpy }> => { return class ReatomLit extends superClass { private unsub?: Unsubscribe private deps: Array = [] diff --git a/packages/npm-react/CHANGELOG.md b/packages/npm-react/CHANGELOG.md index 696932f11..f2de645e6 100644 --- a/packages/npm-react/CHANGELOG.md +++ b/packages/npm-react/CHANGELOG.md @@ -2,203 +2,178 @@ ## [3.8.10](https://github.com/artalar/reatom/compare/npm-react-v3.8.9...npm-react-v3.8.10) (2024-06-26) - ### Bug Fixes -* **npm-react:** name should contain Component word ([94e1367](https://github.com/artalar/reatom/commit/94e1367d454cb7000115ba679dc2f424ec863a2f)) +- **npm-react:** name should contain Component word ([94e1367](https://github.com/artalar/reatom/commit/94e1367d454cb7000115ba679dc2f424ec863a2f)) ## [3.8.9](https://github.com/artalar/reatom/compare/npm-react-v3.8.8...npm-react-v3.8.9) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.8.8](https://github.com/artalar/reatom/compare/npm-react-v3.8.7...npm-react-v3.8.8) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.8.7](https://github.com/artalar/reatom/compare/npm-react-v3.8.6...npm-react-v3.8.7) (2024-05-26) - ### Bug Fixes -* **npm-react:** wrong bundle ([7ac1d74](https://github.com/artalar/reatom/commit/7ac1d749425cf7d33890a1e9ff036b4553693a94)) +- **npm-react:** wrong bundle ([7ac1d74](https://github.com/artalar/reatom/commit/7ac1d749425cf7d33890a1e9ff036b4553693a94)) ## [3.8.6](https://github.com/artalar/reatom/compare/npm-react-v3.8.5...npm-react-v3.8.6) (2024-05-03) - ### Bug Fixes -* **npm-react:** concurrent react ([ebb4e85](https://github.com/artalar/reatom/commit/ebb4e852a72346093cfb4a92abaf840268ae1767)) +- **npm-react:** concurrent react ([ebb4e85](https://github.com/artalar/reatom/commit/ebb4e852a72346093cfb4a92abaf840268ae1767)) ## [3.8.5](https://github.com/artalar/reatom/compare/npm-react-v3.8.4...npm-react-v3.8.5) (2024-04-28) - ### Bug Fixes -* **npm-react:** add withAbortableSchedule to reatomComponent ([4aadfb5](https://github.com/artalar/reatom/commit/4aadfb5c5e48193d5b3b1cddb0c148141ea8c7df)) +- **npm-react:** add withAbortableSchedule to reatomComponent ([4aadfb5](https://github.com/artalar/reatom/commit/4aadfb5c5e48193d5b3b1cddb0c148141ea8c7df)) ## [3.8.4](https://github.com/artalar/reatom/compare/npm-react-v3.8.3...npm-react-v3.8.4) (2024-02-20) - ### Bug Fixes -* **npm-react:** reatomComponent displayName ([bb85630](https://github.com/artalar/reatom/commit/bb85630d50f4ea73154c9136b29086a20046f4d1)) +- **npm-react:** reatomComponent displayName ([bb85630](https://github.com/artalar/reatom/commit/bb85630d50f4ea73154c9136b29086a20046f4d1)) ## [3.8.3](https://github.com/artalar/reatom/compare/npm-react-v3.8.2...npm-react-v3.8.3) (2024-02-05) - ### Bug Fixes -* **npm-react:** allow ReactNode return in reatomComponent ([7c479cd](https://github.com/artalar/reatom/commit/7c479cd98c316ce3295451e74beffea7d862f8f9)) +- **npm-react:** allow ReactNode return in reatomComponent ([7c479cd](https://github.com/artalar/reatom/commit/7c479cd98c316ce3295451e74beffea7d862f8f9)) ## [3.8.2](https://github.com/artalar/reatom/compare/npm-react-v3.8.1...npm-react-v3.8.2) (2024-01-21) - ### Bug Fixes -* **npm-react:** check useAction argument ([01ad380](https://github.com/artalar/reatom/commit/01ad3807dc86b3bc34793ab9dcc8556cc8d67d96)) +- **npm-react:** check useAction argument ([01ad380](https://github.com/artalar/reatom/commit/01ad3807dc86b3bc34793ab9dcc8556cc8d67d96)) ## [3.8.1](https://github.com/artalar/reatom/compare/npm-react-v3.8.0...npm-react-v3.8.1) (2023-11-26) - ### Bug Fixes -* **npm-react:** wrong ctx ([05f6037](https://github.com/artalar/reatom/commit/05f6037ea237ac7bb69c40ef0d567ec06f2eb64b)) +- **npm-react:** wrong ctx ([05f6037](https://github.com/artalar/reatom/commit/05f6037ea237ac7bb69c40ef0d567ec06f2eb64b)) ## [3.8.0](https://github.com/artalar/reatom/compare/npm-react-v3.7.4...npm-react-v3.8.0) (2023-11-25) - ### Features -* **npm-react:** reatomComponent ctx with unmount abort ([335621c](https://github.com/artalar/reatom/commit/335621c0f628b53d2a739dd1bc3e3b47a61ea71e)) +- **npm-react:** reatomComponent ctx with unmount abort ([335621c](https://github.com/artalar/reatom/commit/335621c0f628b53d2a739dd1bc3e3b47a61ea71e)) ## [3.7.4](https://github.com/artalar/reatom/compare/npm-react-v3.7.3...npm-react-v3.7.4) (2023-11-12) - ### Bug Fixes -* **npm-react:** useIsomorphicEffect ([a46b4a3](https://github.com/artalar/reatom/commit/a46b4a3b0635b08ccc0a2f2e75ba2669441a8371)) +- **npm-react:** useIsomorphicEffect ([a46b4a3](https://github.com/artalar/reatom/commit/a46b4a3b0635b08ccc0a2f2e75ba2669441a8371)) ## [3.7.3](https://github.com/artalar/reatom/compare/npm-react-v3.7.2...npm-react-v3.7.3) (2023-11-05) - ### Bug Fixes -* **npm-react:** reatomComponent props.ctx ([6297def](https://github.com/artalar/reatom/commit/6297deffa3e2a18669f2162a84f42621c56d91ae)) +- **npm-react:** reatomComponent props.ctx ([6297def](https://github.com/artalar/reatom/commit/6297deffa3e2a18669f2162a84f42621c56d91ae)) ## [3.7.2](https://github.com/artalar/reatom/compare/npm-react-v3.7.1...npm-react-v3.7.2) (2023-10-31) - ### Bug Fixes -* **npm-react:** reatomComponent bind stability ([2516c6c](https://github.com/artalar/reatom/commit/2516c6c4ea4e7ca0c04d24e1f943b975cdd9c87a)) +- **npm-react:** reatomComponent bind stability ([2516c6c](https://github.com/artalar/reatom/commit/2516c6c4ea4e7ca0c04d24e1f943b975cdd9c87a)) ## [3.7.1](https://github.com/artalar/reatom/compare/npm-react-v3.7.0...npm-react-v3.7.1) (2023-10-21) - ### Bug Fixes -* **npm-react:** reatomComponent wrong cause ([0f4a714](https://github.com/artalar/reatom/commit/0f4a71420645ff32efbbf223c685967ce1fe0654)) +- **npm-react:** reatomComponent wrong cause ([0f4a714](https://github.com/artalar/reatom/commit/0f4a71420645ff32efbbf223c685967ce1fe0654)) ## [3.7.0](https://github.com/artalar/reatom/compare/npm-react-v3.6.1...npm-react-v3.7.0) (2023-10-20) - ### Features -* **async:** deprecate reatomAsyncReaction in favor of reatomReactiveAsync ([678adf2](https://github.com/artalar/reatom/commit/678adf2b337b6a895f94e5997739274822332c4f)) - +- **async:** deprecate reatomAsyncReaction in favor of reatomReactiveAsync ([678adf2](https://github.com/artalar/reatom/commit/678adf2b337b6a895f94e5997739274822332c4f)) ### Bug Fixes -* **npm-react:** drop ctx.cause.cause for reatomComponent ([5227f3d](https://github.com/artalar/reatom/commit/5227f3ddd3eb70c777dbd3d8279300d5e1e61f1f)) +- **npm-react:** drop ctx.cause.cause for reatomComponent ([5227f3d](https://github.com/artalar/reatom/commit/5227f3ddd3eb70c777dbd3d8279300d5e1e61f1f)) ## [3.6.1](https://github.com/artalar/reatom/compare/npm-react-v3.6.0...npm-react-v3.6.1) (2023-09-15) - ### Bug Fixes -* **npm-react:** a few reatomComponent instances ([3d9d080](https://github.com/artalar/reatom/commit/3d9d080723eb207edc77e5ccd6ea1cba81a1d123)) -* **npm-react:** remove useRefSetup ([8d769fa](https://github.com/artalar/reatom/commit/8d769fa367866e53341ccab9a6201301c9d2b583)) -* **npm-react:** stale deps ([3ea9251](https://github.com/artalar/reatom/commit/3ea92518b5bb5382f0407fb4a87ae3c9c9c36ae2)) +- **npm-react:** a few reatomComponent instances ([3d9d080](https://github.com/artalar/reatom/commit/3d9d080723eb207edc77e5ccd6ea1cba81a1d123)) +- **npm-react:** remove useRefSetup ([8d769fa](https://github.com/artalar/reatom/commit/8d769fa367866e53341ccab9a6201301c9d2b583)) +- **npm-react:** stale deps ([3ea9251](https://github.com/artalar/reatom/commit/3ea92518b5bb5382f0407fb4a87ae3c9c9c36ae2)) ## [3.6.0](https://github.com/artalar/reatom/compare/npm-react-v3.5.0...npm-react-v3.6.0) (2023-09-13) - ### Features -* **npm-react:** add useAtomPromise ([f151d45](https://github.com/artalar/reatom/commit/f151d455b4e7e32134779da6a1f5bc22620b9cbf)) - +- **npm-react:** add useAtomPromise ([f151d45](https://github.com/artalar/reatom/commit/f151d455b4e7e32134779da6a1f5bc22620b9cbf)) ### Bug Fixes -* **npm-react:** reatomComponent improvements ([f2df032](https://github.com/artalar/reatom/commit/f2df032ab6c39c5e0981b7883296967c3526c457)) +- **npm-react:** reatomComponent improvements ([f2df032](https://github.com/artalar/reatom/commit/f2df032ab6c39c5e0981b7883296967c3526c457)) ## [3.5.0](https://github.com/artalar/reatom/compare/npm-react-v3.4.2...npm-react-v3.5.0) (2023-09-08) - ### Features -* **npm-react:** add reatomComponent ([e170a88](https://github.com/artalar/reatom/commit/e170a88dc594b385dd2855974594c1dd87bdde66)) +- **npm-react:** add reatomComponent ([e170a88](https://github.com/artalar/reatom/commit/e170a88dc594b385dd2855974594c1dd87bdde66)) ## [3.4.2](https://github.com/artalar/reatom/compare/npm-react-v3.4.1...npm-react-v3.4.2) (2023-07-13) - ### Bug Fixes -* **npm-react:** computed atom ([f5deadf](https://github.com/artalar/reatom/commit/f5deadf87270e47592194d5b98e6803d708686b4)) +- **npm-react:** computed atom ([f5deadf](https://github.com/artalar/reatom/commit/f5deadf87270e47592194d5b98e6803d708686b4)) ## [3.4.1](https://github.com/artalar/reatom/compare/npm-react-v3.4.0...npm-react-v3.4.1) (2023-07-12) - ### Bug Fixes -* **npm-react:** useAtom recreation ([65fb433](https://github.com/artalar/reatom/commit/65fb4338ac2b7dbcbdbcb26759ad9259dba0226c)) +- **npm-react:** useAtom recreation ([65fb433](https://github.com/artalar/reatom/commit/65fb4338ac2b7dbcbdbcb26759ad9259dba0226c)) ## [3.4.0](https://github.com/artalar/reatom/compare/npm-react-v3.3.4...npm-react-v3.4.0) (2023-07-12) - ### Features -* **npm-react:** add useCreateCtx ([fdffc19](https://github.com/artalar/reatom/commit/fdffc190be9e2deee9f24a7c8da1088c3e23252b)) -* **npm-react:** do not recreate computed atom for deps change ([a4523c2](https://github.com/artalar/reatom/commit/a4523c269812da97fd310b4246bab9779a6596dd)) +- **npm-react:** add useCreateCtx ([fdffc19](https://github.com/artalar/reatom/commit/fdffc190be9e2deee9f24a7c8da1088c3e23252b)) +- **npm-react:** do not recreate computed atom for deps change ([a4523c2](https://github.com/artalar/reatom/commit/a4523c269812da97fd310b4246bab9779a6596dd)) ## [3.3.4](https://github.com/artalar/reatom/compare/npm-react-v3.3.3...npm-react-v3.3.4) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/npm-solid-js/CHANGELOG.md b/packages/npm-solid-js/CHANGELOG.md index 248aadbf9..534525c35 100644 --- a/packages/npm-solid-js/CHANGELOG.md +++ b/packages/npm-solid-js/CHANGELOG.md @@ -2,22 +2,19 @@ ## [3.6.2](https://github.com/artalar/reatom/compare/npm-solid-js-v3.6.1...npm-solid-js-v3.6.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.6.1](https://github.com/artalar/reatom/compare/npm-solid-js-v3.6.0...npm-solid-js-v3.6.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.6.0](https://github.com/artalar/reatom/compare/npm-solid-js-v3.5.0...npm-solid-js-v3.6.0) (2023-09-26) - ### Features -* **npm-solid-js:** release! ([e0689ea](https://github.com/artalar/reatom/commit/e0689ea50570f6764eadd4d2a4b28e746c355295)) +- **npm-solid-js:** release! ([e0689ea](https://github.com/artalar/reatom/commit/e0689ea50570f6764eadd4d2a4b28e746c355295)) diff --git a/packages/npm-solid-js/src/index.ts b/packages/npm-solid-js/src/index.ts index a39cd0ffb..a3bd74d8b 100644 --- a/packages/npm-solid-js/src/index.ts +++ b/packages/npm-solid-js/src/index.ts @@ -1,15 +1,4 @@ -import { - Atom, - AtomMut, - AtomState, - Ctx, - CtxSpy, - Fn, - __count, - atom, - isAtom, - throwReatomError, -} from '@reatom/core' +import { Atom, AtomMut, AtomState, Ctx, CtxSpy, Fn, __count, atom, isAtom, throwReatomError } from '@reatom/core' import { bind } from '@reatom/lens' import { Accessor, createContext, from, getOwner, useContext } from 'solid-js' @@ -18,10 +7,7 @@ export const reatomContext = createContext() export const useCtx = (): Ctx => { let ctx = useContext(reatomContext) - throwReatomError( - !ctx, - 'ctx is not set, you probably forgot to specify the ctx provider', - ) + throwReatomError(!ctx, 'ctx is not set, you probably forgot to specify the ctx provider') return ctx! } @@ -32,31 +18,15 @@ export const useAtom: { atom: T, ): [ get: Accessor>, - updater: T extends Fn<[Ctx, ...infer Args], infer Res> - ? Fn - : undefined, + updater: T extends Fn<[Ctx, ...infer Args], infer Res> ? Fn : undefined, atom: T, ] - ( - computed: (ctx: CtxSpy) => T, - name?: string, - ): [get: Accessor, updater: undefined, atom: Atom] - ( - init: T, - name?: string, - ): [get: Accessor, updater: Fn<[T | Fn<[T, Ctx], T>], T>, atom: AtomMut] + (computed: (ctx: CtxSpy) => T, name?: string): [get: Accessor, updater: undefined, atom: Atom] + (init: T, name?: string): [get: Accessor, updater: Fn<[T | Fn<[T, Ctx], T>], T>, atom: AtomMut] } = (init, name): [any, any, Atom] => { const theAtom: Atom = isAtom(init) ? init - : atom( - init, - name ?? - __count( - `${ - getOwner()?.owner?.name?.replace('[solid-refresh]', '') ?? 'use' - }Atom`, - ), - ) + : atom(init, name ?? __count(`${getOwner()?.owner?.name?.replace('[solid-refresh]', '') ?? 'use'}Atom`)) const ctx = useCtx() return [ diff --git a/packages/npm-svelte/CHANGELOG.md b/packages/npm-svelte/CHANGELOG.md index 9cb7828ed..e5714ab65 100644 --- a/packages/npm-svelte/CHANGELOG.md +++ b/packages/npm-svelte/CHANGELOG.md @@ -2,50 +2,47 @@ ## [3.1.4](https://github.com/artalar/reatom/compare/npm-svelte-v3.1.3...npm-svelte-v3.1.4) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.3](https://github.com/artalar/reatom/compare/npm-svelte-v3.1.2...npm-svelte-v3.1.3) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.2](https://github.com/artalar/reatom/compare/npm-svelte-v3.1.1...npm-svelte-v3.1.2) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/npm-svelte/README.md b/packages/npm-svelte/README.md index 76d7ff5cf..41dde7d2c 100644 --- a/packages/npm-svelte/README.md +++ b/packages/npm-svelte/README.md @@ -67,14 +67,8 @@ Of course, you could describe atoms as a [separate module](https://www.reatom.de import { atom, action } from '@reatom/core' export const countAtom = atom(0, 'countAtom') -export const timesAtom = atom( - (ctx) => (ctx.spy(countAtom) === 1 ? 'time' : 'times'), - 'timesAtom', -) -export const increment = action( - (ctx) => countAtom(ctx, (s) => ++s), - 'increment', -) +export const timesAtom = atom((ctx) => (ctx.spy(countAtom) === 1 ? 'time' : 'times'), 'timesAtom') +export const increment = action((ctx) => countAtom(ctx, (s) => ++s), 'increment') ``` ### Data fetching and Svelte diff --git a/packages/npm-svelte/src/index.ts b/packages/npm-svelte/src/index.ts index 03b01f8fd..65670427f 100644 --- a/packages/npm-svelte/src/index.ts +++ b/packages/npm-svelte/src/index.ts @@ -1,14 +1,4 @@ -import { - Atom, - AtomState, - createCtx, - Ctx, - CtxParams, - Fn, - isAtom, - throwReatomError, - Unsubscribe, -} from '@reatom/core' +import { Atom, AtomState, createCtx, Ctx, CtxParams, Fn, isAtom, throwReatomError, Unsubscribe } from '@reatom/core' import { getContext, setContext } from 'svelte' const KEY = 'reatomCtx' diff --git a/packages/npm-vue/CHANGELOG.md b/packages/npm-vue/CHANGELOG.md index 43ff42afe..75d270c75 100644 --- a/packages/npm-vue/CHANGELOG.md +++ b/packages/npm-vue/CHANGELOG.md @@ -2,21 +2,18 @@ ## [3.6.1](https://github.com/artalar/reatom/compare/npm-vue-v3.6.0...npm-vue-v3.6.1) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.6.0](https://github.com/artalar/reatom/compare/npm-vue-v3.5.0...npm-vue-v3.6.0) (2024-06-22) - ### Features -* **npm-vue:** add useCtx, useAction, useCtxBind ([#771](https://github.com/artalar/reatom/issues/771)) ([33d11a6](https://github.com/artalar/reatom/commit/33d11a635d12c47513ee5a6519bc078089e2ce30)) -* **npm-vue:** implement vue integration ([#734](https://github.com/artalar/reatom/issues/734)) ([a281b17](https://github.com/artalar/reatom/commit/a281b17baef6d89d3165b95c8d636d3e0f855c4b)) - +- **npm-vue:** add useCtx, useAction, useCtxBind ([#771](https://github.com/artalar/reatom/issues/771)) ([33d11a6](https://github.com/artalar/reatom/commit/33d11a635d12c47513ee5a6519bc078089e2ce30)) +- **npm-vue:** implement vue integration ([#734](https://github.com/artalar/reatom/issues/734)) ([a281b17](https://github.com/artalar/reatom/commit/a281b17baef6d89d3165b95c8d636d3e0f855c4b)) ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) diff --git a/packages/npm-vue/src/index.ts b/packages/npm-vue/src/index.ts index bb69a4443..048104277 100644 --- a/packages/npm-vue/src/index.ts +++ b/packages/npm-vue/src/index.ts @@ -1,16 +1,4 @@ -import { - Atom, - AtomMut, - Ctx, - CtxSpy, - Fn, - __count, - action, - atom, - isAction, - isAtom, - throwReatomError, -} from '@reatom/core' +import { Atom, AtomMut, Ctx, CtxSpy, Fn, __count, action, atom, isAction, isAtom, throwReatomError } from '@reatom/core' import { Binded, bind } from '@reatom/lens' import { App, ref, Ref, onScopeDispose, inject } from 'vue' @@ -50,19 +38,14 @@ export const reatomRef = ((target: any, ctx = useCtx()) => { } const binder = (ctx: Ctx, fn: Fn) => bind(ctx, fn) -export const useCtxBinder = ( - ctx = useCtx(), -): ((fn: T) => Binded) => bind(ctx, binder) +export const useCtxBinder = (ctx = useCtx()): ((fn: T) => Binded) => bind(ctx, binder) interface UseActionConfig { name?: string ctx?: Ctx } -export const useAction = ]>>( - fn: T, - config: string | UseActionConfig = {}, -) => { +export const useAction = ]>>(fn: T, config: string | UseActionConfig = {}) => { if (typeof config === 'string') config = { name: config } return bind( diff --git a/packages/npm-zod/CHANGELOG.md b/packages/npm-zod/CHANGELOG.md index f9d17f7aa..373970695 100644 --- a/packages/npm-zod/CHANGELOG.md +++ b/packages/npm-zod/CHANGELOG.md @@ -2,31 +2,27 @@ ## [3.10.3](https://github.com/artalar/reatom/compare/npm-zod-v3.10.2...npm-zod-v3.10.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.10.2](https://github.com/artalar/reatom/compare/npm-zod-v3.10.1...npm-zod-v3.10.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **npm-zod:** types for ZodDiscriminatedUnion ([3c73f8f](https://github.com/artalar/reatom/commit/3c73f8feed1dd4345027a1d91032ad85d87c674e)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **npm-zod:** types for ZodDiscriminatedUnion ([3c73f8f](https://github.com/artalar/reatom/commit/3c73f8feed1dd4345027a1d91032ad85d87c674e)) ## [3.10.1](https://github.com/artalar/reatom/compare/npm-zod-v3.10.0...npm-zod-v3.10.1) (2024-06-17) - ### Bug Fixes -* **npm-zod:** deps bundling ([557a570](https://github.com/artalar/reatom/commit/557a5700fa13603221d14a563b4e9c7334f9c35e)) +- **npm-zod:** deps bundling ([557a570](https://github.com/artalar/reatom/commit/557a5700fa13603221d14a563b4e9c7334f9c35e)) ## [3.10.0](https://github.com/artalar/reatom/compare/npm-zod-v3.9.0...npm-zod-v3.10.0) (2024-06-11) - ### Features -* **npm-zod:** first release ([c664b12](https://github.com/artalar/reatom/commit/c664b123f7f28d93f530644eb476feb8d85ff3a9)) -* **npm-zod:** handle discriminatedUnion ([881f0e3](https://github.com/artalar/reatom/commit/881f0e328a7101c8faa4f2adb2239e6552745061)) +- **npm-zod:** first release ([c664b12](https://github.com/artalar/reatom/commit/c664b123f7f28d93f530644eb476feb8d85ff3a9)) +- **npm-zod:** handle discriminatedUnion ([881f0e3](https://github.com/artalar/reatom/commit/881f0e328a7101c8faa4f2adb2239e6552745061)) diff --git a/packages/npm-zod/src/index.test.ts b/packages/npm-zod/src/index.test.ts index cbf41da91..55bef6ecc 100644 --- a/packages/npm-zod/src/index.test.ts +++ b/packages/npm-zod/src/index.test.ts @@ -1,30 +1,30 @@ -import { suite } from "uvu"; -import * as assert from "uvu/assert"; +import { suite } from 'uvu' +import * as assert from 'uvu/assert' -import { createTestCtx, mockFn } from "@reatom/testing"; -import { ParseAtoms, parseAtoms } from "@reatom/lens"; +import { createTestCtx, mockFn } from '@reatom/testing' +import { ParseAtoms, parseAtoms } from '@reatom/lens' -import { z } from "zod"; +import { z } from 'zod' -import { reatomZod } from "./"; +import { reatomZod } from './' -const test = suite("reatomZod"); +const test = suite('reatomZod') -test("base API", async () => { +test('base API', async () => { const model = reatomZod(z.object({ n: z.number(), s: z.string(), readonly: z.string().readonly() }), { sync: () => { - track(parseAtoms(ctx, model)); + track(parseAtoms(ctx, model)) }, - initState: { n: 42, readonly: "foo" }, - }); - const track = mockFn<[ParseAtoms], any>(); - const ctx = createTestCtx(); + initState: { n: 42, readonly: 'foo' }, + }) + const track = mockFn<[ParseAtoms], any>() + const ctx = createTestCtx() - assert.is(model.readonly, "foo"); - assert.is(ctx.get(model.n), 42); + assert.is(model.readonly, 'foo') + assert.is(ctx.get(model.n), 42) - model.s(ctx, "bar"); - assert.equal(track.lastInput(), { n: 42, s: "bar", readonly: "foo" }); -}); + model.s(ctx, 'bar') + assert.equal(track.lastInput(), { n: 42, s: 'bar', readonly: 'foo' }) +}) -test.run(); +test.run() diff --git a/packages/npm-zod/src/index.ts b/packages/npm-zod/src/index.ts index 365ef498d..02350003f 100644 --- a/packages/npm-zod/src/index.ts +++ b/packages/npm-zod/src/index.ts @@ -26,130 +26,130 @@ import { reatomRecord, reatomMap, reatomSet, -} from "@reatom/primitives"; +} from '@reatom/primitives' -import { z } from "zod"; +import { z } from 'zod' // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ZodAtom {} export interface AtomMut extends ZodAtom, ReatomAtomMut {} export interface BooleanAtom extends ZodAtom, ReatomBooleanAtom {} export interface NumberAtom extends ZodAtom, ReatomNumberAtom {} -type EnumAtom = ZodAtom & ReatomEnumAtom; +type EnumAtom = ZodAtom & + ReatomEnumAtom export interface RecordAtom extends ZodAtom, ReatomRecordAtom {} export interface MapAtom extends ZodAtom<[Key, Element]>, ReatomMapAtom {} export interface SetAtom extends ZodAtom<[T]>, ReatomSetAtom {} -export interface LinkedListAtom extends ZodAtom>, ReatomLinkedListAtom {} +export interface LinkedListAtom + extends ZodAtom>, + ReatomLinkedListAtom {} export type ZodAtomization = T extends z.ZodAny ? AtomMut : T extends z.ZodUnknown - ? AtomMut - : T extends z.ZodNever - ? never - : T extends z.ZodReadonly - ? z.infer | Union - : T extends z.ZodUndefined - ? AtomMut - : T extends z.ZodVoid - ? undefined | Union - : T extends z.ZodNaN - ? number | Union - : T extends z.ZodNull - ? AtomMut - : T extends z.ZodLiteral - ? T | Union - : T extends z.ZodBoolean - ? never extends Union - ? BooleanAtom - : AtomMut - : T extends z.ZodNumber - ? never extends Union - ? NumberAtom - : AtomMut - : T extends z.ZodBigInt - ? AtomMut - : T extends z.ZodString - ? AtomMut - : T extends z.ZodSymbol - ? AtomMut - : T extends z.ZodDate - ? AtomMut - : T extends z.ZodArray - ? LinkedListAtom<[void | Partial>], ZodAtomization> // FIXME Union - : T extends z.ZodTuple - ? AtomMut | Union> - : T extends z.ZodObject - ? never extends Union - ? { - [K in keyof Shape]: ZodAtomization; - } - : AtomMut - : T extends z.ZodRecord - ? never extends Union - ? RecordAtom, ZodAtomization>> - : AtomMut, ZodAtomization> | Union> - : T extends z.ZodMap - ? never extends Union - ? MapAtom, ZodAtomization> - : AtomMut, ZodAtomization> | Union> - : T extends z.ZodSet - ? never extends Union - ? SetAtom> - : AtomMut> | Union> - : T extends z.ZodEnum - ? never extends Union - ? EnumAtom - : AtomMut - : T extends z.ZodNativeEnum - ? never extends Union - ? // @ts-expect-error шо? - EnumAtom - : AtomMut - : T extends z.ZodDefault - ? ZodAtomization - : T extends z.ZodOptional - ? ZodAtomization - : T extends z.ZodNullable - ? ZodAtomization - : T extends z.ZodUnion - ? AtomMut | Union> - : T extends z.ZodDiscriminatedUnion - ? never extends Union - ? T extends Array> - ? Atom<{ - [K in keyof Shape]: ZodAtomization; - }> & - (( - ctx: Ctx, - value: { - [K in keyof Shape]: z.infer; - }, - ) => void) - : unknown - : unknown - : T; - + ? AtomMut + : T extends z.ZodNever + ? never + : T extends z.ZodReadonly + ? z.infer | Union + : T extends z.ZodUndefined + ? AtomMut + : T extends z.ZodVoid + ? undefined | Union + : T extends z.ZodNaN + ? number | Union + : T extends z.ZodNull + ? AtomMut + : T extends z.ZodLiteral + ? T | Union + : T extends z.ZodBoolean + ? never extends Union + ? BooleanAtom + : AtomMut + : T extends z.ZodNumber + ? never extends Union + ? NumberAtom + : AtomMut + : T extends z.ZodBigInt + ? AtomMut + : T extends z.ZodString + ? AtomMut + : T extends z.ZodSymbol + ? AtomMut + : T extends z.ZodDate + ? AtomMut + : T extends z.ZodArray + ? LinkedListAtom<[void | Partial>], ZodAtomization> // FIXME Union + : T extends z.ZodTuple + ? AtomMut | Union> + : T extends z.ZodObject + ? never extends Union + ? { + [K in keyof Shape]: ZodAtomization + } + : AtomMut + : T extends z.ZodRecord + ? never extends Union + ? RecordAtom, ZodAtomization>> + : AtomMut, ZodAtomization> | Union> + : T extends z.ZodMap + ? never extends Union + ? MapAtom, ZodAtomization> + : AtomMut, ZodAtomization> | Union> + : T extends z.ZodSet + ? never extends Union + ? SetAtom> + : AtomMut> | Union> + : T extends z.ZodEnum + ? never extends Union + ? EnumAtom + : AtomMut + : T extends z.ZodNativeEnum + ? never extends Union + ? // @ts-expect-error шо? + EnumAtom + : AtomMut + : T extends z.ZodDefault + ? ZodAtomization + : T extends z.ZodOptional + ? ZodAtomization + : T extends z.ZodNullable + ? ZodAtomization + : T extends z.ZodUnion + ? AtomMut | Union> + : T extends z.ZodDiscriminatedUnion + ? never extends Union + ? T extends Array> + ? Atom<{ + [K in keyof Shape]: ZodAtomization + }> & + (( + ctx: Ctx, + value: { + [K in keyof Shape]: z.infer + }, + ) => void) + : unknown + : unknown + : T -type Primitive = null | undefined | string | number | boolean | symbol | bigint; -type BuiltIns = Primitive | Date | RegExp; +type Primitive = null | undefined | string | number | boolean | symbol | bigint +type BuiltIns = Primitive | Date | RegExp export type PartialDeep = T extends BuiltIns ? T | undefined : T extends object - ? T extends ReadonlyArray - ? T - : { - [K in keyof T]?: PartialDeep; - } - : unknown; + ? T extends ReadonlyArray + ? T + : { + [K in keyof T]?: PartialDeep + } + : unknown export const silentUpdate = action((ctx, cb: Fn<[Ctx]>) => { cb(ctx) }) -export const EXTENSIONS = new Array< - Fn<[AtomMut, z.ZodFirstPartyTypeKind], AtomMut> ->() +export const EXTENSIONS = new Array>() export const reatomZod = ( { _def: def }: Schema, @@ -236,8 +236,7 @@ export const reatomZod = ( // TODO @artalar generate a better name, instead of using `__count` theAtom = reatomLinkedList( { - create: (ctx, initState) => - reatomZod(def.type, { sync, initState, name: __count(name) }), + create: (ctx, initState) => reatomZod(def.type, { sync, initState, name: __count(name) }), initState: (initState as any[] | undefined)?.map((initState: any) => reatomZod(def.type, { sync, initState, name: __count(name) }), ), @@ -267,36 +266,31 @@ export const reatomZod = ( break } case z.ZodFirstPartyTypeKind.ZodUnion: { - state = - def.options.find( - (type: z.ZodDefault) => type._def.defaultValue?.(), - ) ?? initState + state = def.options.find((type: z.ZodDefault) => type._def.defaultValue?.()) ?? initState break } case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: { const getState = (initState: any) => { - const state = def.options.find( - (type: z.ZodDiscriminatedUnionOption) => { - try { - type.parse(initState); - } catch { - return undefined; - } + const state = def.options.find((type: z.ZodDiscriminatedUnionOption) => { + try { + type.parse(initState) + } catch { + return undefined + } - return type; - }, - ); + return type + }) - throwReatomError(!state, 'Missed init state for discriminated union'); + throwReatomError(!state, 'Missed init state for discriminated union') - return reatomZod(state, { sync, initState, name }); - }; + return reatomZod(state, { sync, initState, name }) + } - const originAtom = atom(getState(initState), name); + const originAtom = atom(getState(initState), name) theAtom = Object.assign((ctx: Ctx, value: any) => { - originAtom(ctx, getState(value)); - }, originAtom); - break; + originAtom(ctx, getState(value)) + }, originAtom) + break } case z.ZodFirstPartyTypeKind.ZodOptional: { // TODO @artalar allow `undefined` in innerType @@ -338,8 +332,5 @@ export const reatomZod = ( sync?.(ctx) }) - return EXTENSIONS.reduce( - (anAtom, ext) => ext(anAtom as AtomMut, def.typeName), - theAtom, - ) as ZodAtomization + return EXTENSIONS.reduce((anAtom, ext) => ext(anAtom as AtomMut, def.typeName), theAtom) as ZodAtomization } diff --git a/packages/persist-web-storage/CHANGELOG.md b/packages/persist-web-storage/CHANGELOG.md index a89e8d10e..141e85799 100644 --- a/packages/persist-web-storage/CHANGELOG.md +++ b/packages/persist-web-storage/CHANGELOG.md @@ -2,134 +2,119 @@ ## [3.4.4](https://github.com/artalar/reatom/compare/persist-web-storage-v3.4.3...persist-web-storage-v3.4.4) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.4.3](https://github.com/artalar/reatom/compare/persist-web-storage-v3.4.2...persist-web-storage-v3.4.3) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.4.2](https://github.com/artalar/reatom/compare/persist-web-storage-v3.4.1...persist-web-storage-v3.4.2) (2024-06-17) - ### Bug Fixes -* **persist-web-storage:** missing export reatomPersistIndexedDb ([#876](https://github.com/artalar/reatom/issues/876)) ([e007b7f](https://github.com/artalar/reatom/commit/e007b7f11136b358e3f6452ecd9b3f7ffb73d48d)) +- **persist-web-storage:** missing export reatomPersistIndexedDb ([#876](https://github.com/artalar/reatom/issues/876)) ([e007b7f](https://github.com/artalar/reatom/commit/e007b7f11136b358e3f6452ecd9b3f7ffb73d48d)) ## [3.4.1](https://github.com/artalar/reatom/compare/persist-web-storage-v3.4.0...persist-web-storage-v3.4.1) (2024-05-03) - ### Bug Fixes -* **persist-web-storage:** fix cookie availability check ([#836](https://github.com/artalar/reatom/issues/836)) ([8da6fe3](https://github.com/artalar/reatom/commit/8da6fe364ff61403f4e6dd6239eb7923520d5143)) +- **persist-web-storage:** fix cookie availability check ([#836](https://github.com/artalar/reatom/issues/836)) ([8da6fe3](https://github.com/artalar/reatom/commit/8da6fe364ff61403f4e6dd6239eb7923520d5143)) ## [3.4.0](https://github.com/artalar/reatom/compare/persist-web-storage-v3.3.4...persist-web-storage-v3.4.0) (2024-05-01) - ### Features -* **persist-web-storage:** add withCookie ([#830](https://github.com/artalar/reatom/issues/830)) ([25a865e](https://github.com/artalar/reatom/commit/25a865e58ca67ea230a5001a973132d3f76fc207)) +- **persist-web-storage:** add withCookie ([#830](https://github.com/artalar/reatom/issues/830)) ([25a865e](https://github.com/artalar/reatom/commit/25a865e58ca67ea230a5001a973132d3f76fc207)) ## [3.3.4](https://github.com/artalar/reatom/compare/persist-web-storage-v3.3.3...persist-web-storage-v3.3.4) (2024-04-12) - ### Bug Fixes -* **persist-web-storage:** availability of BroadcastChannel ([199d69a](https://github.com/artalar/reatom/commit/199d69a13a3b2b0eabc22bb5ffaaa8e025f40041)) +- **persist-web-storage:** availability of BroadcastChannel ([199d69a](https://github.com/artalar/reatom/commit/199d69a13a3b2b0eabc22bb5ffaaa8e025f40041)) ## [3.3.3](https://github.com/artalar/reatom/compare/persist-web-storage-v3.3.2...persist-web-storage-v3.3.3) (2024-03-24) - ### Bug Fixes -* **persist-web-storage:** isWebStorageAvailable check ([3e21b28](https://github.com/artalar/reatom/commit/3e21b28e061bea255235498e803239183eea4243)) +- **persist-web-storage:** isWebStorageAvailable check ([3e21b28](https://github.com/artalar/reatom/commit/3e21b28e061bea255235498e803239183eea4243)) ## [3.3.2](https://github.com/artalar/reatom/compare/persist-web-storage-v3.3.1...persist-web-storage-v3.3.2) (2024-02-26) - ### Bug Fixes -* **persist-web-storage:** indexedDB is not defined ([e5a527b](https://github.com/artalar/reatom/commit/e5a527b12c60dbea3383a32154a5cd2352849ce1)) +- **persist-web-storage:** indexedDB is not defined ([e5a527b](https://github.com/artalar/reatom/commit/e5a527b12c60dbea3383a32154a5cd2352849ce1)) ## [3.3.1](https://github.com/artalar/reatom/compare/persist-web-storage-v3.3.0...persist-web-storage-v3.3.1) (2024-02-20) - ### Bug Fixes -* **persist-web-storage:** types ([ab0e826](https://github.com/artalar/reatom/commit/ab0e8261f39d81998bccc6425f3dee577143da7c)) +- **persist-web-storage:** types ([ab0e826](https://github.com/artalar/reatom/commit/ab0e8261f39d81998bccc6425f3dee577143da7c)) ## [3.3.0](https://github.com/artalar/reatom/compare/persist-web-storage-v3.2.3...persist-web-storage-v3.3.0) (2024-02-05) - ### Features -* **persist-web-storage:** add withBroadcastChannel and withIndexedDb ([#740](https://github.com/artalar/reatom/issues/740)) ([fe74b4a](https://github.com/artalar/reatom/commit/fe74b4ab4e0007cb7ae417f156ef65e4d0b4ce42)) - +- **persist-web-storage:** add withBroadcastChannel and withIndexedDb ([#740](https://github.com/artalar/reatom/issues/740)) ([fe74b4a](https://github.com/artalar/reatom/commit/fe74b4ab4e0007cb7ae417f156ef65e4d0b4ce42)) ### Bug Fixes -* **persist-web-storage:** fix getting initialState for BroadcastChannel and IDB adapters ([#755](https://github.com/artalar/reatom/issues/755)) ([5302c57](https://github.com/artalar/reatom/commit/5302c575a0bb3c27d1d7935961db362b0d651f2d)) -* **persist-web-storage:** init logic, types ([6921517](https://github.com/artalar/reatom/commit/69215171b55107549a886fc05cc983a091ee2bcc)) -* **persist-web-storage:** memCache management ([72d6f89](https://github.com/artalar/reatom/commit/72d6f898244ae7818a3b7b0b01043beb851094df)) +- **persist-web-storage:** fix getting initialState for BroadcastChannel and IDB adapters ([#755](https://github.com/artalar/reatom/issues/755)) ([5302c57](https://github.com/artalar/reatom/commit/5302c575a0bb3c27d1d7935961db362b0d651f2d)) +- **persist-web-storage:** init logic, types ([6921517](https://github.com/artalar/reatom/commit/69215171b55107549a886fc05cc983a091ee2bcc)) +- **persist-web-storage:** memCache management ([72d6f89](https://github.com/artalar/reatom/commit/72d6f898244ae7818a3b7b0b01043beb851094df)) ## [3.2.3](https://github.com/artalar/reatom/compare/persist-web-storage-v3.2.2...persist-web-storage-v3.2.3) (2023-07-12) - ### Bug Fixes -* **persist-web-storage:** wrap memCache to atom ([c8ebba7](https://github.com/artalar/reatom/commit/c8ebba732ea613b01112f0abd51334299ed05f15)) +- **persist-web-storage:** wrap memCache to atom ([c8ebba7](https://github.com/artalar/reatom/commit/c8ebba732ea613b01112f0abd51334299ed05f15)) ## [3.2.2](https://github.com/artalar/reatom/compare/persist-web-storage-v3.2.1...persist-web-storage-v3.2.2) (2023-07-07) - ### Bug Fixes -* **persist-web-storage:** rm `fromState` work ([106fdca](https://github.com/artalar/reatom/commit/106fdca81fd1644d374ae70755cbcfbcbafd2583)) +- **persist-web-storage:** rm `fromState` work ([106fdca](https://github.com/artalar/reatom/commit/106fdca81fd1644d374ae70755cbcfbcbafd2583)) ## [3.2.1](https://github.com/artalar/reatom/compare/persist-web-storage-v3.2.0...persist-web-storage-v3.2.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.2.0](https://github.com/artalar/reatom/compare/persist-web-storage-v3.1.0...persist-web-storage-v3.2.0) (2023-05-10) - ### Features -* **persist-web-storage:** create (rewritten from scratch) ([1479a92](https://github.com/artalar/reatom/commit/1479a92b0dc8af716ab39f04422c990e92a8bc85)) +- **persist-web-storage:** create (rewritten from scratch) ([1479a92](https://github.com/artalar/reatom/commit/1479a92b0dc8af716ab39f04422c990e92a8bc85)) diff --git a/packages/persist-web-storage/README.md b/packages/persist-web-storage/README.md index d5d561fa5..9517b1425 100644 --- a/packages/persist-web-storage/README.md +++ b/packages/persist-web-storage/README.md @@ -38,9 +38,7 @@ The main goal of this adapter is to synchronize atom between tabs without pollut import { atom } from '@reatom/framework' import { withBroadcastChannel } from '@reatom/persist-web-storage' -export const isAuthedAtom = atom('', 'isAuthedAtom').pipe( - withBroadcastChannel('isAuthedAtom'), -) +export const isAuthedAtom = atom('', 'isAuthedAtom').pipe(withBroadcastChannel('isAuthedAtom')) ``` You can also give an instance of `BroadcastChannel` @@ -59,10 +57,7 @@ myChannel.onmessage((event) => { const withMyBroadcastChannel = reatomPersistBroadcastChannel(myChannel) - -export const isAuthedAtom = atom('', 'isAuthedAtom').pipe( - withMyBroadcastChannel('isAuthedAtom'), -) +export const isAuthedAtom = atom('', 'isAuthedAtom').pipe(withMyBroadcastChannel('isAuthedAtom')) ``` ### withIndexedDb @@ -75,10 +70,9 @@ The main goal of this adapter is to persist atom's state to IndexedDB. `withInde import { reatomResource, withCache } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withCache({ withPersist: withIndexedDb })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withCache({ withPersist: withIndexedDb }), +) ``` You can also specify a custom database name and a custom `BroadcastChannel' that will be used to synchronize the data in real time. @@ -87,10 +81,9 @@ You can also specify a custom database name and a custom `BroadcastChannel' that import { reatomResource, withCache } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel }), +) ``` If you want to avoid flickering, all you have to do is add a small delay after the atom is connected. Subscribe / spy the data atom, wait the ready atom and then use the actual data. @@ -101,10 +94,9 @@ If you want to avoid flickering, all you have to do is add a small delay after t import { reatomResource, withCache, onConnect, sleep } from '@reatom/framework' import { withIndexedDb } from '@reatom/persist-web-storage' -export const listResource = reatomResource( - async (ctx) => api.getList(ctx.spy(pageAtom)), - 'listResource', -).pipe(withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel })) +export const listResource = reatomResource(async (ctx) => api.getList(ctx.spy(pageAtom)), 'listResource').pipe( + withIndexedDb({ key: 'hugeListAtom', dbName: 'myCustomDb', channel }), +) const isListReadyAtom = atom(false, 'isListReadyAtom') onConnect(listResource, async (ctx) => { await ctx.schedule(() => new Promise((r) => requestIdleCallback(r))) @@ -115,7 +107,7 @@ onConnect(listResource, async (ctx) => { ### withCookie -Synchronizes atom state to the `document.cookie` with a given name. +Synchronizes atom state to the `document.cookie` with a given name. When using `withCookie`, the first argument it takes is an options object that allows you to configure various aspects of cookie behavior. @@ -132,9 +124,9 @@ interface CookieAttributes { sameSite?: 'strict' | 'lax' | 'none' } -export const tokenAtom = atom("", "tokenAtom").pipe( +export const tokenAtom = atom('', 'tokenAtom').pipe( withCookie({ maxAge: 3600, // 1 hour - })("token"), -); + })('token'), +) ``` diff --git a/packages/persist-web-storage/src/reatomPersistBroadcastChannel.ts b/packages/persist-web-storage/src/reatomPersistBroadcastChannel.ts index 042bc9635..8978e7070 100644 --- a/packages/persist-web-storage/src/reatomPersistBroadcastChannel.ts +++ b/packages/persist-web-storage/src/reatomPersistBroadcastChannel.ts @@ -2,9 +2,7 @@ import { atom } from '@reatom/core' import { createMemStorage, PersistRecord, reatomPersist } from '@reatom/persist' import { BroadcastMessage, WithPersistWebStorage } from './types' -export const reatomPersistBroadcastChannel = ( - channel: BroadcastChannel, -): WithPersistWebStorage => { +export const reatomPersistBroadcastChannel = (channel: BroadcastChannel): WithPersistWebStorage => { const postMessage = (msg: BroadcastMessage) => channel.postMessage(msg) const memCacheAtom = atom( @@ -75,9 +73,5 @@ try { } export const withBroadcastChannel: WithPersistWebStorage = isBroadcastChannelAvailable - ? /*#__PURE__*/ reatomPersistBroadcastChannel( - new BroadcastChannel('reatom_withBroadcastChannel_default'), - ) - : /*#__PURE__*/ reatomPersist( - createMemStorage({ name: 'withBroadcastChannel' }), - ) + ? /*#__PURE__*/ reatomPersistBroadcastChannel(new BroadcastChannel('reatom_withBroadcastChannel_default')) + : /*#__PURE__*/ reatomPersist(createMemStorage({ name: 'withBroadcastChannel' })) diff --git a/packages/persist-web-storage/src/reatomPersistCookie.ts b/packages/persist-web-storage/src/reatomPersistCookie.ts index 46bea82f9..45cea9a79 100644 --- a/packages/persist-web-storage/src/reatomPersistCookie.ts +++ b/packages/persist-web-storage/src/reatomPersistCookie.ts @@ -23,23 +23,16 @@ const stringifyAttrs = (options: CookieAttributes): string => { } const converter = { - read: (value: string): string => - value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent), + read: (value: string): string => value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent), write: (value: string): string => - encodeURIComponent(value).replace( - /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, - decodeURIComponent, - ), + encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent), } export const reatomPersistCookie = (name: string, document: Document) => (options: CookieAttributes = {}): WithPersistWebStorage => { const now = Date.now() - const memCacheAtom = atom( - (_ctx, state = new Map()) => state, - `${name}._memCacheAtom`, - ) + const memCacheAtom = atom((_ctx, state = new Map()) => state, `${name}._memCacheAtom`) return reatomPersist({ name, @@ -112,8 +105,6 @@ try { isCookieAvailable = false } -export const withCookie: (options?: CookieAttributes) => WithPersistWebStorage = - isCookieAvailable - ? /*#__PURE__*/ reatomPersistCookie('withCookie', globalThis.document) - : /*#__PURE__*/ () => - reatomPersist(createMemStorage({ name: 'withCookie' })) +export const withCookie: (options?: CookieAttributes) => WithPersistWebStorage = isCookieAvailable + ? /*#__PURE__*/ reatomPersistCookie('withCookie', globalThis.document) + : /*#__PURE__*/ () => reatomPersist(createMemStorage({ name: 'withCookie' })) diff --git a/packages/persist-web-storage/src/reatomPersistIndexedDb.ts b/packages/persist-web-storage/src/reatomPersistIndexedDb.ts index c383b483a..00e6fc2f1 100644 --- a/packages/persist-web-storage/src/reatomPersistIndexedDb.ts +++ b/packages/persist-web-storage/src/reatomPersistIndexedDb.ts @@ -5,16 +5,10 @@ import { BroadcastMessage, WithPersistWebStorage } from './types' const idb = { get, set, del, createStore } -export const reatomPersistIndexedDb = ( - dbName: string, - channel: BroadcastChannel, -): WithPersistWebStorage => { +export const reatomPersistIndexedDb = (dbName: string, channel: BroadcastChannel): WithPersistWebStorage => { const postMessage = (msg: BroadcastMessage) => channel.postMessage(msg) - const memCacheAtom = atom( - (ctx, state = new Map()) => state, - `withIndexedDb._memCacheAtom`, - ) + const memCacheAtom = atom((ctx, state = new Map()) => state, `withIndexedDb._memCacheAtom`) let store: UseStore const getStore = () => (store ??= idb.createStore(dbName, 'atoms')) diff --git a/packages/persist-web-storage/src/reatomPersistWebStorage.ts b/packages/persist-web-storage/src/reatomPersistWebStorage.ts index 8b58f14bf..5319f1db9 100644 --- a/packages/persist-web-storage/src/reatomPersistWebStorage.ts +++ b/packages/persist-web-storage/src/reatomPersistWebStorage.ts @@ -2,14 +2,8 @@ import { atom } from '@reatom/core' import { PersistRecord, createMemStorage, reatomPersist } from '@reatom/persist' import { WithPersistWebStorage } from './types' -export const reatomPersistWebStorage = ( - name: string, - storage: Storage, -): WithPersistWebStorage => { - const memCacheAtom = atom( - (ctx, state = new Map()) => state, - `${name}._memCacheAtom`, - ) +export const reatomPersistWebStorage = (name: string, storage: Storage): WithPersistWebStorage => { + const memCacheAtom = atom((ctx, state = new Map()) => state, `${name}._memCacheAtom`) return reatomPersist({ name, @@ -80,15 +74,9 @@ try { } export const withLocalStorage: WithPersistWebStorage = isWebStorageAvailable - ? /*#__PURE__*/ reatomPersistWebStorage( - 'withLocalStorage', - globalThis.localStorage, - ) + ? /*#__PURE__*/ reatomPersistWebStorage('withLocalStorage', globalThis.localStorage) : /*#__PURE__*/ reatomPersist(createMemStorage({ name: 'withLocalStorage' })) export const withSessionStorage: WithPersistWebStorage = isWebStorageAvailable - ? /*#__PURE__*/ reatomPersistWebStorage( - 'withSessionStorage', - globalThis.sessionStorage, - ) + ? /*#__PURE__*/ reatomPersistWebStorage('withSessionStorage', globalThis.sessionStorage) : /*#__PURE__*/ reatomPersist(createMemStorage({ name: 'withSessionStorage' })) diff --git a/packages/persist/CHANGELOG.md b/packages/persist/CHANGELOG.md index 12763b01b..f8cc11561 100644 --- a/packages/persist/CHANGELOG.md +++ b/packages/persist/CHANGELOG.md @@ -2,86 +2,78 @@ ## [3.3.4](https://github.com/artalar/reatom/compare/persist-v3.3.3...persist-v3.3.4) (2024-06-26) - ### Bug Fixes -* **persist:** snapshotAtom init state [#885](https://github.com/artalar/reatom/issues/885) ([209613f](https://github.com/artalar/reatom/commit/209613f425ee06e5cd81ebddb84b25accb1c5861)) +- **persist:** snapshotAtom init state [#885](https://github.com/artalar/reatom/issues/885) ([209613f](https://github.com/artalar/reatom/commit/209613f425ee06e5cd81ebddb84b25accb1c5861)) ## [3.3.3](https://github.com/artalar/reatom/compare/persist-v3.3.2...persist-v3.3.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.3.2](https://github.com/artalar/reatom/compare/persist-v3.3.1...persist-v3.3.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.3.1](https://github.com/artalar/reatom/compare/persist-v3.3.0...persist-v3.3.1) (2024-06-16) - ### Bug Fixes -* **persist:** rm extra cache for transactions ([5efcd57](https://github.com/artalar/reatom/commit/5efcd5746e9e208e46b0b6b6692b823d950bc1b6)) +- **persist:** rm extra cache for transactions ([5efcd57](https://github.com/artalar/reatom/commit/5efcd5746e9e208e46b0b6b6692b823d950bc1b6)) ## [3.3.0](https://github.com/artalar/reatom/compare/persist-v3.2.2...persist-v3.3.0) (2023-07-12) - ### Features -* **persist:** add createMemStorage ([98e23c8](https://github.com/artalar/reatom/commit/98e23c89cc56a18be7f2252b6f9e0b90abe4cbc0)) +- **persist:** add createMemStorage ([98e23c8](https://github.com/artalar/reatom/commit/98e23c89cc56a18be7f2252b6f9e0b90abe4cbc0)) ## [3.2.2](https://github.com/artalar/reatom/compare/persist-v3.2.1...persist-v3.2.2) (2023-07-07) - ### Bug Fixes -* **persist:** cyclic updates ([c1321e3](https://github.com/artalar/reatom/commit/c1321e34cb9893ed5c8041d8e2c3c51c73bb80b6)) -* **persist:** improve an update necessities track ([e4afa65](https://github.com/artalar/reatom/commit/e4afa6528becc333c97cffac5550621b4525cb81)) +- **persist:** cyclic updates ([c1321e3](https://github.com/artalar/reatom/commit/c1321e34cb9893ed5c8041d8e2c3c51c73bb80b6)) +- **persist:** improve an update necessities track ([e4afa65](https://github.com/artalar/reatom/commit/e4afa6528becc333c97cffac5550621b4525cb81)) ## [3.2.1](https://github.com/artalar/reatom/compare/persist-v3.2.0...persist-v3.2.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.2.0](https://github.com/artalar/reatom/compare/persist-v3.1.0...persist-v3.2.0) (2023-05-10) - ### Features -* **persist:** create (rewritten from scratch) ([4b6467c](https://github.com/artalar/reatom/commit/4b6467cf12c84a9c802997090b52f3ff361488e1)) +- **persist:** create (rewritten from scratch) ([4b6467c](https://github.com/artalar/reatom/commit/4b6467cf12c84a9c802997090b52f3ff361488e1)) diff --git a/packages/primitives/CHANGELOG.md b/packages/primitives/CHANGELOG.md index 694c4c3af..9fea46cca 100644 --- a/packages/primitives/CHANGELOG.md +++ b/packages/primitives/CHANGELOG.md @@ -2,91 +2,82 @@ ## [3.7.2](https://github.com/artalar/reatom/compare/primitives-v3.7.1...primitives-v3.7.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.7.1](https://github.com/artalar/reatom/compare/primitives-v3.7.0...primitives-v3.7.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.7.0](https://github.com/artalar/reatom/compare/primitives-v3.6.0...primitives-v3.7.0) (2024-06-11) - ### Features -* **primitives:** add new set methods and tests for reatomSet ([#866](https://github.com/artalar/reatom/issues/866)) ([a8ae20f](https://github.com/artalar/reatom/commit/a8ae20f9abe8adbde4e4c44247578a00b227e5e3)) - +- **primitives:** add new set methods and tests for reatomSet ([#866](https://github.com/artalar/reatom/issues/866)) ([a8ae20f](https://github.com/artalar/reatom/commit/a8ae20f9abe8adbde4e4c44247578a00b227e5e3)) ### Bug Fixes -* **primitives:** linked list batch ([1fd859c](https://github.com/artalar/reatom/commit/1fd859c7309e4e398a578a1b278fdded434c026b)) +- **primitives:** linked list batch ([1fd859c](https://github.com/artalar/reatom/commit/1fd859c7309e4e398a578a1b278fdded434c026b)) ## [3.6.0](https://github.com/artalar/reatom/compare/primitives-v3.5.0...primitives-v3.6.0) (2024-06-05) - ### Features -* **primitives:** add map atom to LinkedListAtom ([fc58024](https://github.com/artalar/reatom/commit/fc580241e3867040a4513690a6b976d39f3ac6c0)) +- **primitives:** add map atom to LinkedListAtom ([fc58024](https://github.com/artalar/reatom/commit/fc580241e3867040a4513690a6b976d39f3ac6c0)) ## [3.5.0](https://github.com/artalar/reatom/compare/primitives-v3.4.0...primitives-v3.5.0) (2024-04-28) - ### Features -* **primitives:** add reatomLinkedList ([0987d83](https://github.com/artalar/reatom/commit/0987d83df0aec36996ce16a8890012773358c7ac)) +- **primitives:** add reatomLinkedList ([0987d83](https://github.com/artalar/reatom/commit/0987d83df0aec36996ce16a8890012773358c7ac)) ## [3.4.0](https://github.com/artalar/reatom/compare/primitives-v3.3.0...primitives-v3.4.0) (2024-04-12) - ### Features -* **primitives:** add getOrCreate to reatomMap ([a6f9d48](https://github.com/artalar/reatom/commit/a6f9d4843e7e983d1a37b0c3a85214642a4d0925)) -* **primitives:** add withComputed ([055a5a7](https://github.com/artalar/reatom/commit/055a5a72524a5ce147ebbb1d7cbb4af7c9d88ca3)) +- **primitives:** add getOrCreate to reatomMap ([a6f9d48](https://github.com/artalar/reatom/commit/a6f9d4843e7e983d1a37b0c3a85214642a4d0925)) +- **primitives:** add withComputed ([055a5a7](https://github.com/artalar/reatom/commit/055a5a72524a5ce147ebbb1d7cbb4af7c9d88ca3)) ## [3.3.0](https://github.com/artalar/reatom/compare/primitives-v3.2.1...primitives-v3.3.0) (2024-04-12) - ### Features -* **primitives:** add withAssign, deprecate withReducers ([3ac66fc](https://github.com/artalar/reatom/commit/3ac66fc76fffa4ef05e9782d93c982020188196f)) +- **primitives:** add withAssign, deprecate withReducers ([3ac66fc](https://github.com/artalar/reatom/commit/3ac66fc76fffa4ef05e9782d93c982020188196f)) ## [3.2.1](https://github.com/artalar/reatom/compare/primitives-v3.2.0...primitives-v3.2.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/primitives/README.md b/packages/primitives/README.md index 80b0690b6..c868c4d10 100644 --- a/packages/primitives/README.md +++ b/packages/primitives/README.md @@ -164,20 +164,11 @@ export const pageAtom = atom(0, 'pageAtom').pipe( An operator that makes it easier to attach properties such as computed atoms, reducer actions etc. It is just a better code organization pattern to have `thingAtom`, `thingAtom.doSome`, instead of `thingAtom` and `doSomeThing`. ```ts -import { - atom, - withAssign, - action, - reatomResource, - withRetry, -} from '@reatom/framework' +import { atom, withAssign, action, reatomResource, withRetry } from '@reatom/framework' const pageAtom = atom(1).pipe( withAssign((pageAtom, name) => ({ - prev: action( - (ctx) => pageAtom(ctx, (prev) => Math.max(1, prev - 1)), - `${name}.prev`, - ), + prev: action((ctx) => pageAtom(ctx, (prev) => Math.max(1, prev - 1)), `${name}.prev`), next: action((ctx) => pageAtom(ctx, (prev) => prev + 1), `${name}.next`), })), ) @@ -190,10 +181,7 @@ const list = reatomResource(async (ctx) => { onReject: (ctx, error, retries) => 100 * Math.min(200, retries ** 3), }), withAssign((list, name) => ({ - loadingAtom: atom( - (ctx) => ctx.spy(list.pendingAtom) > 0 || ctx.spy(list.retriesAtom) > 0, - `${name}.loadingAtom`, - ), + loadingAtom: atom((ctx) => ctx.spy(list.pendingAtom) > 0 || ctx.spy(list.retriesAtom) > 0, `${name}.loadingAtom`), })), ) ``` diff --git a/packages/primitives/src/reatomArray.test.ts b/packages/primitives/src/reatomArray.test.ts index 4b4a590af..5e84cdcac 100644 --- a/packages/primitives/src/reatomArray.test.ts +++ b/packages/primitives/src/reatomArray.test.ts @@ -1,45 +1,48 @@ -import { createCtx } from '@reatom/core' -import { suite } from 'uvu' -import * as assert from 'uvu/assert' - -import { reatomArray } from './reatomArray' - -const test = suite('reatomArray') - -test(`reatomArray. init`, () => { - const ctx = createCtx() - - assert.equal(ctx.get(reatomArray([1, 2, 3])), [1, 2, 3]) -}) - -test(`reatomArray. toReversed`, () => { - const ctx = createCtx() - - assert.equal(reatomArray([1, 2, 3]).toReversed(ctx), [3, 2, 1]) -}) - -test(`reatomArray. toSorted`, () => { - const ctx = createCtx() - - assert.equal(reatomArray([3, 1, 2]).toSorted(ctx), [1, 2, 3]) -}) - -test(`reatomArray. toSorted with compareFn`, () => { - const ctx = createCtx() - - assert.equal(reatomArray([3, 1, 2]).toSorted(ctx, (a, b) => b - a), [3, 2, 1]) -}) - -test(`reatomArray. toSpliced`, () => { - const ctx = createCtx() - - assert.equal(reatomArray([3, 1, 2]).toSpliced(ctx, 1, 2, 44), [3, 44]) -}) - -test(`reatomArray. with`, () => { - const ctx = createCtx() - - assert.equal(reatomArray([3, 1, 2]).with(ctx, 1, 15), [3, 15, 2]) -}) - -test.run() +import { createCtx } from '@reatom/core' +import { suite } from 'uvu' +import * as assert from 'uvu/assert' + +import { reatomArray } from './reatomArray' + +const test = suite('reatomArray') + +test(`reatomArray. init`, () => { + const ctx = createCtx() + + assert.equal(ctx.get(reatomArray([1, 2, 3])), [1, 2, 3]) +}) + +test(`reatomArray. toReversed`, () => { + const ctx = createCtx() + + assert.equal(reatomArray([1, 2, 3]).toReversed(ctx), [3, 2, 1]) +}) + +test(`reatomArray. toSorted`, () => { + const ctx = createCtx() + + assert.equal(reatomArray([3, 1, 2]).toSorted(ctx), [1, 2, 3]) +}) + +test(`reatomArray. toSorted with compareFn`, () => { + const ctx = createCtx() + + assert.equal( + reatomArray([3, 1, 2]).toSorted(ctx, (a, b) => b - a), + [3, 2, 1], + ) +}) + +test(`reatomArray. toSpliced`, () => { + const ctx = createCtx() + + assert.equal(reatomArray([3, 1, 2]).toSpliced(ctx, 1, 2, 44), [3, 44]) +}) + +test(`reatomArray. with`, () => { + const ctx = createCtx() + + assert.equal(reatomArray([3, 1, 2]).with(ctx, 1, 15), [3, 15, 2]) +}) + +test.run() diff --git a/packages/primitives/src/reatomArray.ts b/packages/primitives/src/reatomArray.ts index be48bc43d..43188ea8a 100644 --- a/packages/primitives/src/reatomArray.ts +++ b/packages/primitives/src/reatomArray.ts @@ -1,46 +1,39 @@ -import { Action, AtomMut, action, atom } from '@reatom/core' -import { withAssign } from './withAssign' - -export interface ArrayAtom extends AtomMut> { - toReversed: Action<[], T[]> - toSorted: Action<[compareFn?: (a: T, b: T) => number], T[]> - toSpliced: Action<[start: number, deleteCount: number, ...items: T[]], T[]> - with: Action<[i: number, value: T], T[]> -} - -export const reatomArray = ( - initState = [] as T[], - name?: string, -): ArrayAtom => - atom(initState, name).pipe( - withAssign((target, name) => ({ - toReversed: action( - (ctx) => target(ctx, (prev) => prev.slice().reverse()), - `${name}.toReversed`, - ), - toSorted: action( - (ctx, compareFn?: (a: T, b: T) => number) => - target(ctx, (prev) => prev.slice().sort(compareFn)), - `${name}.toSorted`, - ), - toSpliced: action( - (ctx, start: number, deleteCount: number, ...items: T[]) => - target(ctx, (state) => { - state = state.slice() - state.splice(start, deleteCount, ...items) - return state - }), - `${name}.toSpliced`, - ), - with: action( - (ctx, i: number, value: T) => - target(ctx, (state) => { - if (Object.is(state.at(i), value)) return state - state = state.slice() - state[i] = value - return state - }), - `${name}.with`, - ), - })), - ) +import { Action, AtomMut, action, atom } from '@reatom/core' +import { withAssign } from './withAssign' + +export interface ArrayAtom extends AtomMut> { + toReversed: Action<[], T[]> + toSorted: Action<[compareFn?: (a: T, b: T) => number], T[]> + toSpliced: Action<[start: number, deleteCount: number, ...items: T[]], T[]> + with: Action<[i: number, value: T], T[]> +} + +export const reatomArray = (initState = [] as T[], name?: string): ArrayAtom => + atom(initState, name).pipe( + withAssign((target, name) => ({ + toReversed: action((ctx) => target(ctx, (prev) => prev.slice().reverse()), `${name}.toReversed`), + toSorted: action( + (ctx, compareFn?: (a: T, b: T) => number) => target(ctx, (prev) => prev.slice().sort(compareFn)), + `${name}.toSorted`, + ), + toSpliced: action( + (ctx, start: number, deleteCount: number, ...items: T[]) => + target(ctx, (state) => { + state = state.slice() + state.splice(start, deleteCount, ...items) + return state + }), + `${name}.toSpliced`, + ), + with: action( + (ctx, i: number, value: T) => + target(ctx, (state) => { + if (Object.is(state.at(i), value)) return state + state = state.slice() + state[i] = value + return state + }), + `${name}.with`, + ), + })), + ) diff --git a/packages/primitives/src/reatomBoolean.ts b/packages/primitives/src/reatomBoolean.ts index 45f9e0017..81c870786 100644 --- a/packages/primitives/src/reatomBoolean.ts +++ b/packages/primitives/src/reatomBoolean.ts @@ -13,10 +13,7 @@ export const reatomBoolean = (init = false, name?: string): BooleanAtom => withAssign((target, name) => ({ toggle: action((ctx) => target(ctx, (prev) => !prev), `${name}.toggle`), setTrue: action((ctx) => target(ctx, true) as true, `${name}.setTrue`), - setFalse: action( - (ctx) => target(ctx, false) as false, - `${name}.setFalse`, - ), + setFalse: action((ctx) => target(ctx, false) as false, `${name}.setFalse`), reset: action((ctx) => target(ctx, init), `${name}.reset`), })), ) diff --git a/packages/primitives/src/reatomEnum.test.ts b/packages/primitives/src/reatomEnum.test.ts index 54d4fc2fe..233714aca 100644 --- a/packages/primitives/src/reatomEnum.test.ts +++ b/packages/primitives/src/reatomEnum.test.ts @@ -14,12 +14,7 @@ test(`reatomEnum. static enum property`, async () => { }) test(`reatomEnum. camelCase`, async () => { - const sortFilterAtom = reatomEnum([ - 'fullName', - 'created', - 'updated', - 'pushed', - ]) + const sortFilterAtom = reatomEnum(['fullName', 'created', 'updated', 'pushed']) const ctx = createCtx() sortFilterAtom.setUpdated(ctx) diff --git a/packages/primitives/src/reatomEnum.ts b/packages/primitives/src/reatomEnum.ts index f7b4f697b..73c3b112f 100644 --- a/packages/primitives/src/reatomEnum.ts +++ b/packages/primitives/src/reatomEnum.ts @@ -2,33 +2,24 @@ import { action, Action, atom, AtomMut, Ctx, throwReatomError } from '@reatom/co export type EnumFormat = 'camelCase' | 'snake_case' -export type EnumAtom< - T extends string, - Format extends EnumFormat = 'camelCase', -> = AtomMut & { +export type EnumAtom = AtomMut & { [Variant in T as Format extends 'camelCase' - ? `set${Capitalize}` - : Format extends 'snake_case' - ? `set_${Variant}` - : never]: Action<[], Variant> + ? `set${Capitalize}` + : Format extends 'snake_case' + ? `set_${Variant}` + : never]: Action<[], Variant> } & { reset: Action<[], T> enum: { [K in T]: K } } -export type EnumAtomOptions< - T extends string, - Format extends EnumFormat = 'camelCase', -> = { +export type EnumAtomOptions = { name?: string format?: Format initState?: T } -export const reatomEnum = < - const T extends string, - Format extends 'camelCase' | 'snake_case' = 'camelCase', ->( +export const reatomEnum = ( variants: ReadonlyArray, options: string | EnumAtomOptions = {}, ) => { @@ -36,9 +27,7 @@ export const reatomEnum = < name, format = 'camelCase' as Format, initState = variants[0], - }: EnumAtomOptions = typeof options === 'string' - ? { name: options } - : options + }: EnumAtomOptions = typeof options === 'string' ? { name: options } : options const stateAtom = atom(initState, name) as EnumAtom const enumAtom: typeof stateAtom = Object.assign((ctx: Ctx, update: any) => { @@ -54,17 +43,10 @@ export const reatomEnum = < cases[variant] = variant const setterName = variant.replace( /^./, - (firstLetter) => - 'set' + - (format === 'camelCase' - ? firstLetter.toUpperCase() - : `_${firstLetter}`), + (firstLetter) => 'set' + (format === 'camelCase' ? firstLetter.toUpperCase() : `_${firstLetter}`), ) - ; (enumAtom as any)[setterName] = action( - (ctx) => enumAtom(ctx, variant)!, - `${name}.${setterName}`, - ) + ;(enumAtom as any)[setterName] = action((ctx) => enumAtom(ctx, variant)!, `${name}.${setterName}`) } return enumAtom as EnumAtom diff --git a/packages/primitives/src/reatomLinkedList.test.ts b/packages/primitives/src/reatomLinkedList.test.ts index 5d8843ceb..064cd81a9 100644 --- a/packages/primitives/src/reatomLinkedList.test.ts +++ b/packages/primitives/src/reatomLinkedList.test.ts @@ -1,170 +1,165 @@ -import { action, atom } from '@reatom/core' -import { createTestCtx, mockFn } from '@reatom/testing' -import { suite } from 'uvu' -import * as assert from 'uvu/assert' - -import { reatomLinkedList } from './reatomLinkedList' -import { parseAtoms } from '@reatom/lens' -import { isCausedBy } from '@reatom/effects' - -const test = suite('reatomLinkedList') - -test(`should respect initState, create and remove elements properly`, () => { - const ctx = createTestCtx() - const list = reatomLinkedList({ - create: (ctx, n: number) => atom(n), - initState: [atom(1), atom(2)], - }) - - const last = list.create(ctx, 3) - assert.equal( - ctx.get(list.array).map((v) => ctx.get(v)), - [1, 2, 3], - ) - - list.remove(ctx, last) - assert.equal(parseAtoms(ctx, list.array), [1, 2]) - list.remove(ctx, last) - assert.equal(parseAtoms(ctx, list.array), [1, 2]) - - list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 1)!) - assert.equal(parseAtoms(ctx, list.array), [2]) - - list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 2)!) - assert.equal(parseAtoms(ctx, list.array), []) - - try { - list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 2)!) - assert.ok(false, 'Error expected') - } catch (error: any) { - assert.is( - error?.message, - 'Reatom error: The passed data is not a linked list node.', - ) - } -}) - -test(`should swap elements`, () => { - const ctx = createTestCtx() - const list = reatomLinkedList((ctx, n: number) => ({ n })) - const { array } = list.reatomMap((ctx, { n }) => ({ n })) - const track = ctx.subscribeTrack( - atom((ctx) => ctx.spy(array).map(({ n }) => n)), - ) - const one = list.create(ctx, 1) - const two = list.create(ctx, 2) - const three = list.create(ctx, 3) - const four = list.create(ctx, 4) - - // const [one, two, three, four] = ctx.get( - // () => - // [ - // list.create(ctx, 1), - // list.create(ctx, 2), - // list.create(ctx, 3), - // list.create(ctx, 4), - // ] as const, - // ) - - assert.equal(track.lastInput(), [1, 2, 3, 4]) - - list.swap(ctx, one, two) - assert.equal(track.lastInput(), [2, 1, 3, 4]) - - list.swap(ctx, one, two) - assert.equal(track.lastInput(), [1, 2, 3, 4]) - - list.swap(ctx, three, four) - assert.equal(track.lastInput(), [1, 2, 4, 3]) - - list.swap(ctx, four, three) - assert.equal(track.lastInput(), [1, 2, 3, 4]) - - list.remove(ctx, two) - assert.equal(track.lastInput(), [1, 3, 4]) - - list.remove(ctx, three) - assert.equal(track.lastInput(), [1, 4]) - - list.swap(ctx, four, one) - assert.equal(track.lastInput(), [4, 1]) - - list.swap(ctx, four, one) - assert.equal(track.lastInput(), [1, 4]) - - list.remove(ctx, one) - assert.equal(track.lastInput(), [4]) - - // TODO - // assert.throws(() => list.swap(ctx, four, one)) - - list.clear(ctx) - assert.equal(parseAtoms(ctx, list.array), []) -}) - -test(`should move elements`, () => { - const ctx = createTestCtx() - const list = reatomLinkedList((ctx, n: number) => ({ n })) - const one = list.create(ctx, 1) - const two = list.create(ctx, 2) - const three = list.create(ctx, 3) - const four = list.create(ctx, 4) - const track = ctx.subscribeTrack(list.array) - - assert.equal( - track.lastInput().map(({ n }) => n), - [1, 2, 3, 4], - ) - - list.move(ctx, one, four) - assert.equal( - track.lastInput().map(({ n }) => n), - [2, 3, 4, 1], - ) - assert.is(track.calls.length, 2) - - list.move(ctx, one, four) - assert.equal( - track.lastInput().map(({ n }) => n), - [2, 3, 4, 1], - ) - assert.is(track.calls.length, 2) - - list.move(ctx, one, null) - assert.equal( - track.lastInput().map(({ n }) => n), - [1, 2, 3, 4], - ) -}) - -test('should respect node keys even if it is an atom', () => { - const ctx = createTestCtx() - const list = reatomLinkedList({ - create: (ctx, id: string) => ({ id: atom(id) }), - key: 'id', - initState: [{ id: atom('1') }, { id: atom('2') }], - }) - const track = ctx.subscribeTrack(atom((ctx) => [...ctx.spy(list.map).keys()])) - - assert.equal(track.lastInput(), ['1', '2']) - - ctx.get(list.map).get('1')?.id(ctx, '0') - assert.equal(track.lastInput(), ['0', '2']) -}) - -test('should correctly handle batching and cause tracking', () => { - const ctx = createTestCtx() - const list = reatomLinkedList(() => ({})) - list.onChange((ctx) => { - isCausedBy(ctx, action()) - }) - - list.create(ctx) - - list.batch(ctx, () => { - list.create(ctx) - list.create(ctx) - }) -}) - -test.run() +import { action, atom } from '@reatom/core' +import { createTestCtx, mockFn } from '@reatom/testing' +import { suite } from 'uvu' +import * as assert from 'uvu/assert' + +import { reatomLinkedList } from './reatomLinkedList' +import { parseAtoms } from '@reatom/lens' +import { isCausedBy } from '@reatom/effects' + +const test = suite('reatomLinkedList') + +test(`should respect initState, create and remove elements properly`, () => { + const ctx = createTestCtx() + const list = reatomLinkedList({ + create: (ctx, n: number) => atom(n), + initState: [atom(1), atom(2)], + }) + + const last = list.create(ctx, 3) + assert.equal( + ctx.get(list.array).map((v) => ctx.get(v)), + [1, 2, 3], + ) + + list.remove(ctx, last) + assert.equal(parseAtoms(ctx, list.array), [1, 2]) + list.remove(ctx, last) + assert.equal(parseAtoms(ctx, list.array), [1, 2]) + + list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 1)!) + assert.equal(parseAtoms(ctx, list.array), [2]) + + list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 2)!) + assert.equal(parseAtoms(ctx, list.array), []) + + try { + list.remove(ctx, list.find(ctx, (n) => ctx.get(n) === 2)!) + assert.ok(false, 'Error expected') + } catch (error: any) { + assert.is(error?.message, 'Reatom error: The passed data is not a linked list node.') + } +}) + +test(`should swap elements`, () => { + const ctx = createTestCtx() + const list = reatomLinkedList((ctx, n: number) => ({ n })) + const { array } = list.reatomMap((ctx, { n }) => ({ n })) + const track = ctx.subscribeTrack(atom((ctx) => ctx.spy(array).map(({ n }) => n))) + const one = list.create(ctx, 1) + const two = list.create(ctx, 2) + const three = list.create(ctx, 3) + const four = list.create(ctx, 4) + + // const [one, two, three, four] = ctx.get( + // () => + // [ + // list.create(ctx, 1), + // list.create(ctx, 2), + // list.create(ctx, 3), + // list.create(ctx, 4), + // ] as const, + // ) + + assert.equal(track.lastInput(), [1, 2, 3, 4]) + + list.swap(ctx, one, two) + assert.equal(track.lastInput(), [2, 1, 3, 4]) + + list.swap(ctx, one, two) + assert.equal(track.lastInput(), [1, 2, 3, 4]) + + list.swap(ctx, three, four) + assert.equal(track.lastInput(), [1, 2, 4, 3]) + + list.swap(ctx, four, three) + assert.equal(track.lastInput(), [1, 2, 3, 4]) + + list.remove(ctx, two) + assert.equal(track.lastInput(), [1, 3, 4]) + + list.remove(ctx, three) + assert.equal(track.lastInput(), [1, 4]) + + list.swap(ctx, four, one) + assert.equal(track.lastInput(), [4, 1]) + + list.swap(ctx, four, one) + assert.equal(track.lastInput(), [1, 4]) + + list.remove(ctx, one) + assert.equal(track.lastInput(), [4]) + + // TODO + // assert.throws(() => list.swap(ctx, four, one)) + + list.clear(ctx) + assert.equal(parseAtoms(ctx, list.array), []) +}) + +test(`should move elements`, () => { + const ctx = createTestCtx() + const list = reatomLinkedList((ctx, n: number) => ({ n })) + const one = list.create(ctx, 1) + const two = list.create(ctx, 2) + const three = list.create(ctx, 3) + const four = list.create(ctx, 4) + const track = ctx.subscribeTrack(list.array) + + assert.equal( + track.lastInput().map(({ n }) => n), + [1, 2, 3, 4], + ) + + list.move(ctx, one, four) + assert.equal( + track.lastInput().map(({ n }) => n), + [2, 3, 4, 1], + ) + assert.is(track.calls.length, 2) + + list.move(ctx, one, four) + assert.equal( + track.lastInput().map(({ n }) => n), + [2, 3, 4, 1], + ) + assert.is(track.calls.length, 2) + + list.move(ctx, one, null) + assert.equal( + track.lastInput().map(({ n }) => n), + [1, 2, 3, 4], + ) +}) + +test('should respect node keys even if it is an atom', () => { + const ctx = createTestCtx() + const list = reatomLinkedList({ + create: (ctx, id: string) => ({ id: atom(id) }), + key: 'id', + initState: [{ id: atom('1') }, { id: atom('2') }], + }) + const track = ctx.subscribeTrack(atom((ctx) => [...ctx.spy(list.map).keys()])) + + assert.equal(track.lastInput(), ['1', '2']) + + ctx.get(list.map).get('1')?.id(ctx, '0') + assert.equal(track.lastInput(), ['0', '2']) +}) + +test('should correctly handle batching and cause tracking', () => { + const ctx = createTestCtx() + const list = reatomLinkedList(() => ({})) + list.onChange((ctx) => { + isCausedBy(ctx, action()) + }) + + list.create(ctx) + + list.batch(ctx, () => { + list.create(ctx) + list.create(ctx) + }) +}) + +test.run() diff --git a/packages/primitives/src/reatomLinkedList.ts b/packages/primitives/src/reatomLinkedList.ts index 018e24ff9..e38cef3c9 100644 --- a/packages/primitives/src/reatomLinkedList.ts +++ b/packages/primitives/src/reatomLinkedList.ts @@ -48,18 +48,14 @@ export interface LinkedList { changes: Array> } -export interface LinkedListLikeAtom - extends Atom { +export interface LinkedListLikeAtom extends Atom { __reatomLinkedList: true array: Atom ? LLNode : never>> } -export interface LinkedListAtom< - Params extends any[] = any[], - Node extends Rec = Rec, - Key extends keyof Node = never, -> extends LinkedListLikeAtom>> { +export interface LinkedListAtom + extends LinkedListLikeAtom>> { batch: Action<[cb: Fn]> create: Action> @@ -84,10 +80,7 @@ export interface LinkedListAtom< onRemove?: (ctx: Ctx, node: LLNode, origin: LLNode) => void onSwap?: (ctx: Ctx, payload: { a: LLNode; b: LLNode }) => void onMove?: (ctx: Ctx, node: LLNode) => void - onClear?: ( - ctx: Ctx, - lastState: LinkedListDerivedState, LLNode>, - ) => void + onClear?: (ctx: Ctx, lastState: LinkedListDerivedState, LLNode>) => void }, ) => LinkedListDerivedAtom, LLNode> @@ -107,8 +100,7 @@ export interface LinkedListAtom< } // TODO rename to `DerivedLinkedList` -export interface LinkedListDerivedState - extends LinkedList { +export interface LinkedListDerivedState extends LinkedList { map: WeakMap } @@ -162,11 +154,7 @@ const removeLL = (state: LinkedList, node: Node) => { state.size-- } -const swapLL = ( - state: LinkedList, - a: Node, - b: Node, -): void => { +const swapLL = (state: LinkedList, a: Node, b: Node): void => { if (a === b) return if (state.head === b) return swapLL(state, b, a) @@ -179,11 +167,7 @@ const swapLL = ( addLL(state, b, prevA) } -const moveLL = ( - state: LinkedList, - node: Node, - after: null | Node, -) => { +const moveLL = (state: LinkedList, node: Node, after: null | Node) => { removeLL(state, node) addLL(state, node, after) } @@ -192,10 +176,7 @@ const clearLL = (state: LinkedList) => { while (state.tail) removeLL(state, state.tail) } -const toArray = ( - head: null | LLNode, - prev?: Array>, -): Array> => { +const toArray = (head: null | LLNode, prev?: Array>): Array> => { let arr: Array> = [] let i = 0 while (head) { @@ -207,11 +188,7 @@ const toArray = ( return arr.length === prev?.length ? prev : arr } -export const reatomLinkedList = < - Params extends any[], - Node extends Rec, - Key extends keyof Node = never, ->( +export const reatomLinkedList = ( options: | ((ctx: Ctx, ...params: Params) => Node) | { @@ -228,13 +205,10 @@ export const reatomLinkedList = < } = typeof options === 'function' ? { create: options } : options const _name = name - const isLL = (node: Node): node is LLNode => - !!node && LL_NEXT in node && LL_PREV in node + const isLL = (node: Node): node is LLNode => !!node && LL_NEXT in node && LL_PREV in node - const throwModel = (node: Node) => - throwReatomError(isLL(node), 'The data is already in a linked list.') - const throwNotModel = (node: Node) => - throwReatomError(!isLL(node), 'The passed data is not a linked list node.') + const throwModel = (node: Node) => throwReatomError(isLL(node), 'The data is already in a linked list.') + const throwNotModel = (node: Node) => throwReatomError(!isLL(node), 'The passed data is not a linked list node.') // for batching let STATE: null | LinkedList> = null @@ -324,18 +298,15 @@ export const reatomLinkedList = < }) }, `${name}.swap`) - const move = action( - (ctx, node: LLNode, after: null | LLNode): void => { - return batchFn(ctx, () => { - throwNotModel(node) + const move = action((ctx, node: LLNode, after: null | LLNode): void => { + return batchFn(ctx, () => { + throwNotModel(node) - moveLL(STATE!, node, after) + moveLL(STATE!, node, after) - STATE!.changes.push({ kind: 'move', node, after }) - }) - }, - `${name}.move`, - ) + STATE!.changes.push({ kind: 'move', node, after }) + }) + }, `${name}.move`) const clear = action((ctx): void => { return batchFn(ctx, () => { @@ -345,10 +316,7 @@ export const reatomLinkedList = < }) }, `${name}.clear`) - const find = ( - ctx: Ctx, - cb: (node: LLNode) => boolean, - ): null | LLNode => { + const find = (ctx: Ctx, cb: (node: LLNode) => boolean): null | LLNode => { for (let { head } = ctx.get(linkedList); head; head = head[LL_NEXT]) { if (cb(head)) return head } @@ -356,8 +324,7 @@ export const reatomLinkedList = < } const array: LinkedListAtom['array'] = atom( - (ctx, state: Array> = []) => - toArray(ctx.spy(linkedList).head, state), + (ctx, state: Array> = []) => toArray(ctx.spy(linkedList).head, state), `${name}.array`, ) @@ -368,10 +335,7 @@ export const reatomLinkedList = < // use array as it already memoized and simplifies the order tracking ctx.spy(array).map((node) => { const keyValue = node[key] - return [ - isAtom(keyValue) ? ctx.spy(keyValue) : keyValue, - node, - ] as const + return [isAtom(keyValue) ? ctx.spy(keyValue) : keyValue, node] as const }), ), ) as LinkedListAtom['map']) @@ -387,29 +351,19 @@ export const reatomLinkedList = < onRemove?: (ctx: Ctx, node: LLNode, origin: LLNode) => void onSwap?: (ctx: Ctx, payload: { a: LLNode; b: LLNode }) => void onMove?: (ctx: Ctx, node: LLNode) => void - onClear?: ( - ctx: Ctx, - lastState: LinkedListDerivedState, LLNode>, - ) => void + onClear?: (ctx: Ctx, lastState: LinkedListDerivedState, LLNode>) => void } = {}, ): LinkedListDerivedAtom, LLNode> => { - const { name = __count(`${_name}.reatomMap`), ...hooks } = - typeof options === 'string' ? { name: options } : options + const { name = __count(`${_name}.reatomMap`), ...hooks } = typeof options === 'string' ? { name: options } : options type State = LinkedListDerivedState, LLNode> const mapList = atom((ctx, mapList?: State): State => { - throwReatomError( - STATE, - `Can't compute the map of the linked list inside the batching.`, - ) + throwReatomError(STATE, `Can't compute the map of the linked list inside the batching.`) const ll = ctx.spy(linkedList) - if ( - !mapList || - /* some update was missed */ ll.version - 1 > mapList.version - ) { + if (!mapList || /* some update was missed */ ll.version - 1 > mapList.version) { if (mapList) hooks.onClear?.(ctx, mapList) mapList = { @@ -488,17 +442,13 @@ export const reatomLinkedList = < } } - throwReatomError( - mapList.size !== ll.size, - "Inconsistent linked list, is's a bug, please report an issue", - ) + throwReatomError(mapList.size !== ll.size, "Inconsistent linked list, is's a bug, please report an issue") return mapList }, name) const array: LinkedListDerivedAtom, LLNode>['array'] = atom( - (ctx, state: Array> = []) => - toArray(ctx.spy(mapList).head, state), + (ctx, state: Array> = []) => toArray(ctx.spy(mapList).head, state), `${name}.array`, ) @@ -587,5 +537,4 @@ export const reatomLinkedList = < }).pipe(readonly) } -export const isLinkedListAtom = (thing: any): thing is LinkedListLikeAtom => - thing?.__reatomLinkedList === true +export const isLinkedListAtom = (thing: any): thing is LinkedListLikeAtom => thing?.__reatomLinkedList === true diff --git a/packages/primitives/src/reatomList.ts b/packages/primitives/src/reatomList.ts index d2bc275d9..d800be963 100644 --- a/packages/primitives/src/reatomList.ts +++ b/packages/primitives/src/reatomList.ts @@ -10,41 +10,33 @@ import { throwReatomError, withInit, __count, -} from '@reatom/framework'; +} from '@reatom/framework' const readonly = ( anAtom: T, ): { - [K in keyof T]: T[K]; + [K in keyof T]: T[K] } => ({ ...anAtom, -}); +}) -interface ListMapAtom - extends Atom> { - get: (ctx: Ctx, key: Key) => Model; - has: (ctx: CtxSpy, key: Key) => boolean; - spy: (ctx: CtxSpy, key: Key) => Model; +interface ListMapAtom extends Atom> { + get: (ctx: Ctx, key: Key) => Model + has: (ctx: CtxSpy, key: Key) => boolean + spy: (ctx: CtxSpy, key: Key) => Model } -export interface ListAtom< - Params extends any[] = any[], - Model extends Rec = Rec, - Key = any, -> extends Atom> { - create: Action; - createMany: Action, Array>; - remove: Action<[Key], undefined | Model>; - removeMany: Action<[Array], Array>; - swap: Action<[a: number, b: number], Array>; - clear: Action<[], void>; - - map: ListMapAtom; - - reatomMap: ( - cb: (ctx: CtxSpy, el: Model) => T, - name?: string, - ) => Atom>>; +export interface ListAtom extends Atom> { + create: Action + createMany: Action, Array> + remove: Action<[Key], undefined | Model> + removeMany: Action<[Array], Array> + swap: Action<[a: number, b: number], Array> + clear: Action<[], void> + + map: ListMapAtom + + reatomMap: (cb: (ctx: CtxSpy, el: Model) => T, name?: string) => Atom>> } export const reatomList: { @@ -52,31 +44,31 @@ export const reatomList: { ( create: (ctx: Ctx, ...params: Params) => Model, name: string, - ): ListAtom; + ): ListAtom /** an index will be used as a key */ ( options: { - create: (ctx: Ctx, ...params: Params) => Model; - initState?: Array; + create: (ctx: Ctx, ...params: Params) => Model + initState?: Array }, name: string, - ): ListAtom; + ): ListAtom /** a model property will be used as a key if the model is an object */ ( options: { - create: (ctx: Ctx, ...params: Params) => Model; - key: Key; - initState?: Array; + create: (ctx: Ctx, ...params: Params) => Model + key: Key + initState?: Array }, name: string, - ): ListAtom; + ): ListAtom } = ( options: | Fn | { - create: Fn; - initState?: Array; - key?: string; + create: Fn + initState?: Array + key?: string }, name: string, ): ListAtom => { @@ -84,130 +76,123 @@ export const reatomList: { create: createFn, initState = [], key: KEY = undefined, - } = typeof options === 'function' ? { create: options } : options; + } = typeof options === 'function' ? { create: options } : options - const list = atom(initState, name); + const list = atom(initState, name) const create = action((ctx, ...params: any[]) => { - const model = createFn(ctx, ...params); - const listState = ctx.get(list); - const mapState = ctx.get(map); + const model = createFn(ctx, ...params) + const listState = ctx.get(list) + const mapState = ctx.get(map) if (ctx.cause.cause?.proto === createMany.__reatom) { - const idx = listState.push(model) - 1; - mapState.set(KEY ? model[KEY] : idx, model); + const idx = listState.push(model) - 1 + mapState.set(KEY ? model[KEY] : idx, model) } else { - const idx = list(ctx, [...listState, model]).length - 1; - map(ctx, new Map(mapState).set(KEY ? model[KEY] : idx, model)); + const idx = list(ctx, [...listState, model]).length - 1 + map(ctx, new Map(mapState).set(KEY ? model[KEY] : idx, model)) } - return model; - }, `${name}.create`); + return model + }, `${name}.create`) const createMany = action((ctx, paramsList: any[][]) => { - list(ctx, (listState) => [...listState]); + list(ctx, (listState) => [...listState]) - return paramsList.map((params) => create(ctx, ...params)); - }, `${name}.createMany`); + return paramsList.map((params) => create(ctx, ...params)) + }, `${name}.createMany`) const remove = action((ctx, key: any): undefined | Rec => { - const mapState = new Map(ctx.get(map)); - const model = mapState.get(key); + const mapState = new Map(ctx.get(map)) + const model = mapState.get(key) if (model && ctx.cause.cause?.proto !== removeMany.__reatom) { - list(ctx, (listState) => listState.filter((el) => el !== model)); - mapState.delete(key); - map(ctx, mapState); + list(ctx, (listState) => listState.filter((el) => el !== model)) + mapState.delete(key) + map(ctx, mapState) } - return model; - }, `${name}.remove`); + return model + }, `${name}.remove`) const removeMany = action((ctx, keys: any[]) => { - const result = keys.map((key) => remove(ctx, key)).filter(Boolean); + const result = keys.map((key) => remove(ctx, key)).filter(Boolean) if (result.length) { - list(ctx, (listState) => listState.filter((el) => !result.includes(el))); - const mapState = new Map(ctx.get(map)); - result.forEach((key) => mapState.delete(key)); - map(ctx, mapState); + list(ctx, (listState) => listState.filter((el) => !result.includes(el))) + const mapState = new Map(ctx.get(map)) + result.forEach((key) => mapState.delete(key)) + map(ctx, mapState) } - return result; - }, `${name}.removeMany`); + return result + }, `${name}.removeMany`) const swap = action( (ctx, a: number, b: number) => list(ctx, (listState) => { if (a === b) { - return listState; + return listState } if ([a, b].some((index) => index < 0 || index >= listState.length)) { - throw new RangeError('Index out of range'); + throw new RangeError('Index out of range') } - const newList = [...listState]; - const aValue = newList[a]; - newList[a] = newList[b]; - newList[b] = aValue; + const newList = [...listState] + const aValue = newList[a] + newList[a] = newList[b] + newList[b] = aValue - return newList; + return newList }), `${name}.move`, - ); + ) const clear = action((ctx) => { - list(ctx, []); - map(ctx, new Map()); - }, `${name}.clear`); + list(ctx, []) + map(ctx, new Map()) + }, `${name}.clear`) const map = Object.assign( atom(new Map(), `${name}.map`).pipe( - withInit( - (ctx) => - new Map( - ctx.get(list).map((model, i) => [KEY ? model[KEY] : i, model]), - ), - ), + withInit((ctx) => new Map(ctx.get(list).map((model, i) => [KEY ? model[KEY] : i, model]))), ), { get: (ctx: Ctx, key: any) => { - const model = ctx.get(map).get(key); - throwReatomError(!model, `Key "${key}" not found in "${name}" list`); - return model; + const model = ctx.get(map).get(key) + throwReatomError(!model, `Key "${key}" not found in "${name}" list`) + return model }, // TODO @artalar optimize it spy: (ctx: CtxSpy, key: any) => { - const model = ctx.spy(map).get(key); - throwReatomError(!model, `Key "${key}" not found in "${name}" list`); - return model; + const model = ctx.spy(map).get(key) + throwReatomError(!model, `Key "${key}" not found in "${name}" list`) + return model }, // TODO @artalar optimize it has: (ctx: CtxSpy, key: any) => ctx.spy(map).has(key), }, - ); + ) - list.onChange((ctx) => ctx.get(map)); + list.onChange((ctx) => ctx.get(map)) const reatomMap = ( cb: (ctx: CtxSpy, el: any) => T, mapName = __count(`${name}.reatomMap`), ): Atom>> => { - const atomsMap = atom(new WeakMap(), `${mapName}._atomsMap`).pipe( - withInit(() => new WeakMap()), - ); + const atomsMap = atom(new WeakMap(), `${mapName}._atomsMap`).pipe(withInit(() => new WeakMap())) return atom( (ctx, state: any[] = []) => ctx.spy(list).map((el) => { - const atoms = ctx.get(atomsMap); - let theAtom = atoms.get(el); + const atoms = ctx.get(atomsMap) + let theAtom = atoms.get(el) if (!theAtom) { - atoms.set(el, (theAtom = atom((ctx) => cb(ctx, el)))); + atoms.set(el, (theAtom = atom((ctx) => cb(ctx, el)))) } - return theAtom; + return theAtom }), mapName, - ); - }; + ) + } // TODO @artalar // @ts-expect-error generics? @@ -222,5 +207,5 @@ export const reatomList: { map: map.pipe(readonly) as ListMapAtom>, reatomMap, - }); -}; + }) +} diff --git a/packages/primitives/src/reatomMap.ts b/packages/primitives/src/reatomMap.ts index 4d2e06350..540609c55 100644 --- a/packages/primitives/src/reatomMap.ts +++ b/packages/primitives/src/reatomMap.ts @@ -11,10 +11,7 @@ export interface MapAtom extends AtomMut> { reset: Action<[], Map> } -export const reatomMap = ( - initState = new Map(), - name?: string, -): MapAtom => +export const reatomMap = (initState = new Map(), name?: string): MapAtom => atom(initState, name).pipe( withAssign((target, name) => { const getOrCreate = action((ctx, key: Key, value: Value) => { @@ -25,16 +22,13 @@ export const reatomMap = ( const actions = { get: (ctx: Ctx, key: Key) => ctx.get(target).get(key), getOrCreate: (ctx: Ctx, key: Key, creator: () => Value) => - actions.has(ctx, key) - ? actions.get(ctx, key)! - : getOrCreate(ctx, key, creator()), + actions.has(ctx, key) ? actions.get(ctx, key)! : getOrCreate(ctx, key, creator()), has: (ctx: Ctx, key: Key) => ctx.get(target).has(key), set: action( (ctx, key: Key, value: Value) => target(ctx, (prev) => { const valuePrev = prev.get(key) - return Object.is(valuePrev, value) && - (value !== undefined || prev.has(key)) + return Object.is(valuePrev, value) && (value !== undefined || prev.has(key)) ? prev : new Map(prev).set(key, value) }), diff --git a/packages/primitives/src/reatomNumber.ts b/packages/primitives/src/reatomNumber.ts index 8c4c6803a..9bd6b4ac7 100644 --- a/packages/primitives/src/reatomNumber.ts +++ b/packages/primitives/src/reatomNumber.ts @@ -11,14 +11,8 @@ export interface NumberAtom extends AtomMut { export const reatomNumber = (initState = 0, name?: string): NumberAtom => atom(initState, name).pipe( withAssign((target, name) => ({ - increment: action( - (ctx, by = 1) => target(ctx, (prev) => prev + by), - `${name}.increment`, - ), - decrement: action( - (ctx, by = 1) => target(ctx, (prev) => prev - by), - `${name}.decrement`, - ), + increment: action((ctx, by = 1) => target(ctx, (prev) => prev + by), `${name}.increment`), + decrement: action((ctx, by = 1) => target(ctx, (prev) => prev - by), `${name}.decrement`), random: action((ctx) => target(ctx, Math.random()), `${name}.decrement`), reset: action((ctx) => target(ctx, initState), `${name}.reset`), })), diff --git a/packages/primitives/src/reatomRecord.ts b/packages/primitives/src/reatomRecord.ts index d588c1869..99fd47f2a 100644 --- a/packages/primitives/src/reatomRecord.ts +++ b/packages/primitives/src/reatomRecord.ts @@ -1,62 +1,59 @@ -import { Action, action, atom, AtomMut, Rec } from '@reatom/core' -import { omit } from '@reatom/utils' -import { withAssign } from './withAssign' - -export interface RecordAtom extends AtomMut { - merge: Action<[slice: Partial], T> - omit: Action, T> - reset: Action, T> -} - -export const reatomRecord = ( - initState: T, - name?: string, -): RecordAtom => - atom(initState, name).pipe( - withAssign((target) => ({ - merge: action( - (ctx, slice: Partial) => - target(ctx, (prev) => { - for (const key in prev) { - if (!Object.is(prev[key], slice[key])) { - return { ...prev, ...slice } - } - } - return prev - }), - `${name}.merge`, - ), - - omit: action( - (ctx, ...keys: Array<(keyof T)>) => - target(ctx, (prev) => { - if (keys.some((key) => key in prev)) return omit(prev, keys) as any - return prev - }), - `${name}.omit`, - ), - - reset: action( - (ctx, ...keys: (keyof T)[]) => - target(ctx, (prev) => { - if (keys.length === 0) return initState - const next = {} as T - let changed = false - for (const key in prev) { - if (keys.includes(key)) { - if (key in initState) { - next[key] = initState[key] - changed ||= !Object.is(prev[key], initState[key]) - } else { - changed ||= key in prev - } - } else { - next[key] = prev[key] - } - } - return changed ? next : prev - }), - `${name}.reset`, - ), - })), - ) +import { Action, action, atom, AtomMut, Rec } from '@reatom/core' +import { omit } from '@reatom/utils' +import { withAssign } from './withAssign' + +export interface RecordAtom extends AtomMut { + merge: Action<[slice: Partial], T> + omit: Action, T> + reset: Action, T> +} + +export const reatomRecord = (initState: T, name?: string): RecordAtom => + atom(initState, name).pipe( + withAssign((target) => ({ + merge: action( + (ctx, slice: Partial) => + target(ctx, (prev) => { + for (const key in prev) { + if (!Object.is(prev[key], slice[key])) { + return { ...prev, ...slice } + } + } + return prev + }), + `${name}.merge`, + ), + + omit: action( + (ctx, ...keys: Array) => + target(ctx, (prev) => { + if (keys.some((key) => key in prev)) return omit(prev, keys) as any + return prev + }), + `${name}.omit`, + ), + + reset: action( + (ctx, ...keys: (keyof T)[]) => + target(ctx, (prev) => { + if (keys.length === 0) return initState + const next = {} as T + let changed = false + for (const key in prev) { + if (keys.includes(key)) { + if (key in initState) { + next[key] = initState[key] + changed ||= !Object.is(prev[key], initState[key]) + } else { + changed ||= key in prev + } + } else { + next[key] = prev[key] + } + } + return changed ? next : prev + }), + `${name}.reset`, + ), + })), + ) diff --git a/packages/primitives/src/reatomSet.test.ts b/packages/primitives/src/reatomSet.test.ts index e71cf696f..579a74587 100644 --- a/packages/primitives/src/reatomSet.test.ts +++ b/packages/primitives/src/reatomSet.test.ts @@ -51,76 +51,46 @@ test(`reatomSet. reset`, () => { test(`reatomSet. intersection`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).intersection(ctx, new Set([2, 3, 4])), - new Set([2, 3]), - ) + assert.equal(reatomSet(new Set([1, 2, 3])).intersection(ctx, new Set([2, 3, 4])), new Set([2, 3])) }) test(`reatomSet. union`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).union(ctx, new Set([2, 3, 4])), - new Set([1, 2, 3, 4]), - ) + assert.equal(reatomSet(new Set([1, 2, 3])).union(ctx, new Set([2, 3, 4])), new Set([1, 2, 3, 4])) }) test(`reatomSet. difference`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).difference(ctx, new Set([2, 3, 4])), - new Set([1]), - ) + assert.equal(reatomSet(new Set([1, 2, 3])).difference(ctx, new Set([2, 3, 4])), new Set([1])) }) test(`reatomSet. symmetricDifference`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).symmetricDifference(ctx, new Set([2, 3, 4])), - new Set([1, 4]), - ) + assert.equal(reatomSet(new Set([1, 2, 3])).symmetricDifference(ctx, new Set([2, 3, 4])), new Set([1, 4])) }) test(`reatomSet. isSubsetOf`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).isSubsetOf(ctx, new Set([2, 3, 4])), - false, - ) - assert.equal( - reatomSet(new Set([1, 2, 3])).isSubsetOf(ctx, new Set([1, 2, 3])), - true, - ) + assert.equal(reatomSet(new Set([1, 2, 3])).isSubsetOf(ctx, new Set([2, 3, 4])), false) + assert.equal(reatomSet(new Set([1, 2, 3])).isSubsetOf(ctx, new Set([1, 2, 3])), true) }) test(`reatomSet. isSupersetOf`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).isSupersetOf(ctx, new Set([2, 3, 4])), - false, - ) - assert.equal( - reatomSet(new Set([1, 2, 3])).isSupersetOf(ctx, new Set([1, 2, 3])), - true, - ) + assert.equal(reatomSet(new Set([1, 2, 3])).isSupersetOf(ctx, new Set([2, 3, 4])), false) + assert.equal(reatomSet(new Set([1, 2, 3])).isSupersetOf(ctx, new Set([1, 2, 3])), true) }) test(`reatomSet. isDisjointFrom`, () => { const ctx = createCtx() - assert.equal( - reatomSet(new Set([1, 2, 3])).isDisjointFrom(ctx, new Set([4, 5, 6])), - true, - ) - assert.equal( - reatomSet(new Set([1, 2, 3])).isDisjointFrom(ctx, new Set([3, 4, 5])), - false, - ) + assert.equal(reatomSet(new Set([1, 2, 3])).isDisjointFrom(ctx, new Set([4, 5, 6])), true) + assert.equal(reatomSet(new Set([1, 2, 3])).isDisjointFrom(ctx, new Set([3, 4, 5])), false) }) test(`reatomSet. size`, () => { diff --git a/packages/primitives/src/reatomSet.ts b/packages/primitives/src/reatomSet.ts index 194a659ff..9c663d584 100644 --- a/packages/primitives/src/reatomSet.ts +++ b/packages/primitives/src/reatomSet.ts @@ -30,23 +30,12 @@ interface ProposalSet extends Set { union(other: Set): Set } -export const reatomSet = ( - initState = new Set(), - name?: string, -): SetAtom => +export const reatomSet = (initState = new Set(), name?: string): SetAtom => atom(initState, name).pipe( withAssign((target, name) => ({ - add: action( - (ctx, el) => - target(ctx, (prev) => (prev.has(el) ? prev : new Set(prev).add(el))), - `${name}.add`, - ), + add: action((ctx, el) => target(ctx, (prev) => (prev.has(el) ? prev : new Set(prev).add(el))), `${name}.add`), /** @deprecated */ - set: action( - (ctx, el) => - target(ctx, (prev) => (prev.has(el) ? prev : new Set(prev).add(el))), - `${name}.set`, - ), + set: action((ctx, el) => target(ctx, (prev) => (prev.has(el) ? prev : new Set(prev).add(el))), `${name}.set`), delete: action((ctx, el) => { return target(ctx, (prev) => { if (!prev.has(el)) return prev @@ -63,25 +52,16 @@ export const reatomSet = ( }, `${name}.clear`), reset: action((ctx) => target(ctx, initState), `${name}.reset`), intersection: action( - (ctx, set) => - target(ctx, (prev) => (prev as ProposalSet).intersection(set)), + (ctx, set) => target(ctx, (prev) => (prev as ProposalSet).intersection(set)), `${name}.intersection`, ), - union: action( - (ctx, set) => - target(ctx, (prev) => (prev as ProposalSet).union(set)), - `${name}.union`, - ), + union: action((ctx, set) => target(ctx, (prev) => (prev as ProposalSet).union(set)), `${name}.union`), difference: action( - (ctx, set) => - target(ctx, (prev) => (prev as ProposalSet).difference(set)), + (ctx, set) => target(ctx, (prev) => (prev as ProposalSet).difference(set)), `${name}.difference`, ), symmetricDifference: action( - (ctx, set) => - target(ctx, (prev) => - (prev as ProposalSet).symmetricDifference(set), - ), + (ctx, set) => target(ctx, (prev) => (prev as ProposalSet).symmetricDifference(set)), `${name}.symmetricDifference`, ), toggle: action((ctx, el) => { @@ -93,12 +73,9 @@ export const reatomSet = ( }) }, `${name}.toggle`), has: (ctx: Ctx, el: T) => ctx.get(target).has(el), - isSubsetOf: (ctx: Ctx, set: Set) => - (ctx.get(target) as ProposalSet).isSubsetOf(set), - isSupersetOf: (ctx: Ctx, set: Set) => - (ctx.get(target) as ProposalSet).isSupersetOf(set), - isDisjointFrom: (ctx: Ctx, set: Set) => - (ctx.get(target) as ProposalSet).isDisjointFrom(set), + isSubsetOf: (ctx: Ctx, set: Set) => (ctx.get(target) as ProposalSet).isSubsetOf(set), + isSupersetOf: (ctx: Ctx, set: Set) => (ctx.get(target) as ProposalSet).isSupersetOf(set), + isDisjointFrom: (ctx: Ctx, set: Set) => (ctx.get(target) as ProposalSet).isDisjointFrom(set), size: (ctx: Ctx) => ctx.get(target).size, })), ) diff --git a/packages/primitives/src/withAssign.ts b/packages/primitives/src/withAssign.ts index d32283284..b975a69de 100644 --- a/packages/primitives/src/withAssign.ts +++ b/packages/primitives/src/withAssign.ts @@ -2,7 +2,6 @@ import { Atom, __count } from '@reatom/core' import { assign } from '@reatom/utils' export const withAssign = - ( - getProps: (target: Target, name: string) => Props, - ) => - (target: Target) => assign(target, getProps(target, target.__reatom.name!)) as Target & Props + (getProps: (target: Target, name: string) => Props) => + (target: Target) => + assign(target, getProps(target, target.__reatom.name!)) as Target & Props diff --git a/packages/primitives/src/withComputed.ts b/packages/primitives/src/withComputed.ts index a7c81518c..dbf6fde57 100644 --- a/packages/primitives/src/withComputed.ts +++ b/packages/primitives/src/withComputed.ts @@ -1,9 +1,7 @@ import { Atom, AtomState, CtxSpy } from '@reatom/core' export const withComputed = - ( - computed: (ctx: CtxSpy, state: AtomState) => AtomState, - ) => + (computed: (ctx: CtxSpy, state: AtomState) => AtomState) => (anAtom: T): T => { const prevComputed = anAtom.__reatom.computer anAtom.__reatom.computer = (ctx, state) => { diff --git a/packages/primitives/src/withReducers.ts b/packages/primitives/src/withReducers.ts index ca8ac50ab..250a3a13e 100644 --- a/packages/primitives/src/withReducers.ts +++ b/packages/primitives/src/withReducers.ts @@ -1,12 +1,9 @@ import { action, Action, AtomMut, AtomState, Fn, Rec } from '@reatom/core' -interface Reducers - extends Rec<(state: AtomState, ...args: Array) => AtomState> {} +interface Reducers extends Rec<(state: AtomState, ...args: Array) => AtomState> {} export type WithReducers> = A & { - [K in keyof R]: R[K] extends Fn<[any, ...infer Args]> - ? Action> - : never + [K in keyof R]: R[K] extends Fn<[any, ...infer Args]> ? Action> : never } /** @deprecated use withAssign instead */ diff --git a/packages/react-v1/CHANGELOG.md b/packages/react-v1/CHANGELOG.md index 4fed55b28..682b910c5 100644 --- a/packages/react-v1/CHANGELOG.md +++ b/packages/react-v1/CHANGELOG.md @@ -2,85 +2,81 @@ ## [3.1.7](https://github.com/artalar/reatom/compare/react-v1-v3.1.6...react-v1-v3.1.7) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.6](https://github.com/artalar/reatom/compare/react-v1-v3.1.5...react-v1-v3.1.6) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.1.5](https://github.com/artalar/reatom/compare/react-v1-v3.1.4...react-v1-v3.1.5) (2023-07-12) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.1.3](https://github.com/artalar/reatom/compare/react-v1-v3.1.2...react-v1-v3.1.3) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/react-v1/README.md b/packages/react-v1/README.md index 0e8e6d377..137536ce8 100644 --- a/packages/react-v1/README.md +++ b/packages/react-v1/README.md @@ -1,5 +1,3 @@ - - This is compatible package which allow you to use `@reatom/core-v1` with react. All docs is [here](https://www.reatom.dev/package/npm-react/). ## Setup batching for old React diff --git a/packages/react-v1/src/index.ts b/packages/react-v1/src/index.ts index 99ecf5075..c4e7e43c9 100644 --- a/packages/react-v1/src/index.ts +++ b/packages/react-v1/src/index.ts @@ -1,13 +1,4 @@ -import { - createContext, - useEffect, - useRef, - useReducer, - useContext, - useCallback, - Reducer, - Context, -} from 'react' +import { createContext, useEffect, useRef, useReducer, useContext, useCallback, Reducer, Context } from 'react' import { Store, Atom, Action } from '@reatom/core-v1' @@ -36,16 +27,8 @@ const defaultMapper = (atomValue: any) => atomValue */ export function createAtomHook(ctx: Context = context) { function useAtom(atom: Atom): T - function useAtom( - atom: Atom, - selector: (atomValue: TI) => TO, - deps: any[], - ): TO - function useAtom( - atom: Atom, - selector: (atomValue: TI) => TO = defaultMapper, - deps: any[] = [], - ): TO { + function useAtom(atom: Atom, selector: (atomValue: TI) => TO, deps: any[]): TO + function useAtom(atom: Atom, selector: (atomValue: TI) => TO = defaultMapper, deps: any[] = []): TO { const forceUpdate = useForceUpdate() const store = useContext(ctx) if (!store) throw new Error('[reatom] The provider is not defined') @@ -133,19 +116,10 @@ export const setupBatch = (newBatch: typeof batch) => { * @returns A `useAction` hook bound to the context. */ export function createActionHook(ctx: Context = context) { - function useAction( - cb: AC, - deps?: any[], - ): (...args: Parameters) => void + function useAction(cb: AC, deps?: any[]): (...args: Parameters) => void function useAction(cb: () => Action | void, deps?: any[]): () => void - function useAction( - cb: (a: T) => Action | void, - deps?: any[], - ): (payload: T) => void - function useAction( - cb: AnyActionCreator, - deps: any[] = [], - ): (...args: any[]) => void { + function useAction(cb: (a: T) => Action | void, deps?: any[]): (payload: T) => void + function useAction(cb: AnyActionCreator, deps: any[] = []): (...args: any[]) => void { const store = useContext(ctx) if (!store) throw new Error('[reatom] The provider is not defined') if (typeof cb !== 'function') { diff --git a/packages/react-v2/CHANGELOG.md b/packages/react-v2/CHANGELOG.md index 27e8fec3a..d2664375f 100644 --- a/packages/react-v2/CHANGELOG.md +++ b/packages/react-v2/CHANGELOG.md @@ -2,15 +2,13 @@ ## [3.1.2](https://github.com/artalar/reatom/compare/react-v2-v3.1.1...react-v2-v3.1.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.1.1](https://github.com/artalar/reatom/compare/react-v2-v3.1.0...react-v2-v3.1.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) diff --git a/packages/react-v2/README.md b/packages/react-v2/README.md index 019d6942e..2a705007d 100644 --- a/packages/react-v2/README.md +++ b/packages/react-v2/README.md @@ -53,10 +53,7 @@ const [data] = useAtom(dataAtom) #### Depended value by selector ```ts -const [propAtom] = useMemo( - () => createAtom({ dataAtom }, ({ get }) => get('dataAtom')[props.id]), - [props.id], -) +const [propAtom] = useMemo(() => createAtom({ dataAtom }, ({ get }) => get('dataAtom')[props.id]), [props.id]) const [propValue] = useAtom(propAtom) ``` @@ -73,10 +70,7 @@ const handleUpdateData = useAction(dataAtom.update) #### Prepare payload for dispatch ```ts -const handleUpdateData = useAction( - (value) => dataAtom.update({ id: props.id, value }), - [props.id], -) +const handleUpdateData = useAction((value) => dataAtom.update({ id: props.id, value }), [props.id]) ``` #### Conditional dispatch diff --git a/packages/react-v2/react-dom-batched-updates/index.d.ts b/packages/react-v2/react-dom-batched-updates/index.d.ts index 509db1866..336ce12bb 100644 --- a/packages/react-v2/react-dom-batched-updates/index.d.ts +++ b/packages/react-v2/react-dom-batched-updates/index.d.ts @@ -1 +1 @@ -export {}; +export {} diff --git a/packages/react-v2/src/index.ts b/packages/react-v2/src/index.ts index 6c7922019..049c3e488 100644 --- a/packages/react-v2/src/index.ts +++ b/packages/react-v2/src/index.ts @@ -40,16 +40,11 @@ export function useAction( ): (...args: Args) => void { const store = React.useContext(reatomContext) - return React.useCallback( - bindActionCreator(store, actionCreator), - deps.concat(store), - ) + return React.useCallback(bindActionCreator(store, actionCreator), deps.concat(store)) } type ActionCreators = { - [K in keyof T]: T[K] extends (...a: infer Args) => Action - ? (...args: Args) => void - : never + [K in keyof T]: T[K] extends (...a: infer Args) => Action ? (...args: Args) => void : never } export function useAtom( @@ -66,10 +61,7 @@ export function useAtom(atom: Atom, mapOrDeps?: Fn | any[], deps?: any[]) { if (typeof mapOrDeps === 'function') { deps ??= [] // FIXME: rewrite to useState - atom = React.useMemo( - () => createAtom({ origin }, ({ get }) => mapOrDeps(get(`origin`))), - deps.concat(origin), - ) + atom = React.useMemo(() => createAtom({ origin }, ({ get }) => mapOrDeps(get(`origin`))), deps.concat(origin)) } else { deps = mapOrDeps ?? [] } @@ -95,9 +87,7 @@ export function useAtom(atom: Atom, mapOrDeps?: Fn | any[], deps?: any[]) { React.useEffect(() => { return store.subscribe( atom, - (newState) => - Object.is(newState, lastRef.current) || - setState((lastRef.current = newState)), + (newState) => Object.is(newState, lastRef.current) || setState((lastRef.current = newState)), ) }, deps) diff --git a/packages/testing/CHANGELOG.md b/packages/testing/CHANGELOG.md index 06d48cf8c..999466b9d 100644 --- a/packages/testing/CHANGELOG.md +++ b/packages/testing/CHANGELOG.md @@ -2,86 +2,78 @@ ## [3.4.7](https://github.com/artalar/reatom/compare/testing-v3.4.6...testing-v3.4.7) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.4.6](https://github.com/artalar/reatom/compare/testing-v3.4.5...testing-v3.4.6) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.4.5](https://github.com/artalar/reatom/compare/testing-v3.4.4...testing-v3.4.5) (2024-04-12) - ### Bug Fixes -* **testing:** callSafelySilent ([d80f013](https://github.com/artalar/reatom/commit/d80f0132542b5dad732228e0c49bf807dfc916ae)) +- **testing:** callSafelySilent ([d80f013](https://github.com/artalar/reatom/commit/d80f0132542b5dad732228e0c49bf807dfc916ae)) ## [3.4.4](https://github.com/artalar/reatom/compare/testing-v3.4.3...testing-v3.4.4) (2023-09-26) - ### Bug Fixes -* **testing,jsx:** type improvements ([#639](https://github.com/artalar/reatom/issues/639)) ([d343ac4](https://github.com/artalar/reatom/commit/d343ac4f9549258851235a60e6ef01c24bc2084e)) +- **testing,jsx:** type improvements ([#639](https://github.com/artalar/reatom/issues/639)) ([d343ac4](https://github.com/artalar/reatom/commit/d343ac4f9549258851235a60e6ef01c24bc2084e)) ## [3.4.3](https://github.com/artalar/reatom/compare/testing-v3.4.2...testing-v3.4.3) (2023-09-14) - ### Bug Fixes -* **testing:** do not prevent error throwing ([0a9ac03](https://github.com/artalar/reatom/commit/0a9ac03109e508449736230520bf2a14f7553aca)) +- **testing:** do not prevent error throwing ([0a9ac03](https://github.com/artalar/reatom/commit/0a9ac03109e508449736230520bf2a14f7553aca)) ## [3.4.2](https://github.com/artalar/reatom/compare/testing-v3.4.1...testing-v3.4.2) (2023-07-12) - ### Bug Fixes -* **testing:** deprecate createMockStorage and bump deps ([da28598](https://github.com/artalar/reatom/commit/da28598f0039933db2b181c9e22cb222ae2449bf)) +- **testing:** deprecate createMockStorage and bump deps ([da28598](https://github.com/artalar/reatom/commit/da28598f0039933db2b181c9e22cb222ae2449bf)) ## [3.4.1](https://github.com/artalar/reatom/compare/testing-v3.4.0...testing-v3.4.1) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.4.0](https://github.com/artalar/reatom/compare/testing-v3.3.0...testing-v3.4.0) (2023-05-10) - ### Features -* **testing:** add createMockStorage ([0e87a88](https://github.com/artalar/reatom/commit/0e87a88f34512f6e332c279a01d3ee7a38390315)) -* **testing:** add createMockStorage for persist ([cbdcf98](https://github.com/artalar/reatom/commit/cbdcf98d6ac454ad3365d93311f90edb84dddae3)) +- **testing:** add createMockStorage ([0e87a88](https://github.com/artalar/reatom/commit/0e87a88f34512f6e332c279a01d3ee7a38390315)) +- **testing:** add createMockStorage for persist ([cbdcf98](https://github.com/artalar/reatom/commit/cbdcf98d6ac454ad3365d93311f90edb84dddae3)) diff --git a/packages/testing/src/index.ts b/packages/testing/src/index.ts index be582ebe9..c08637699 100644 --- a/packages/testing/src/index.ts +++ b/packages/testing/src/index.ts @@ -14,9 +14,7 @@ import { } from '@reatom/core' import { createMemStorage } from '@reatom/persist' -export function mockFn( - fn: (...input: I) => O = (...i: any) => void 0 as any, -) { +export function mockFn(fn: (...input: I) => O = (...i: any) => void 0 as any) { const _fn = Object.assign( function (...i: I) { try { @@ -60,10 +58,7 @@ export const getDuration = async (cb: () => void) => { export interface TestCtx extends Ctx { mock(anAtom: Atom, fallback: T): Unsubscribe - mockAction( - anAction: Action, - cb: Fn<[Ctx, ...I], O>, - ): Unsubscribe + mockAction(anAction: Action, cb: Fn<[Ctx, ...I], O>): Unsubscribe subscribeTrack>( anAtom: Atom, @@ -151,10 +146,7 @@ export const createTestCtx = (options?: CtxOptions): TestCtx => { mocks.delete(proto) } }, - mockAction( - anAction: Action, - cb: Fn<[Ctx, ...I], O>, - ) { + mockAction(anAction: Action, cb: Fn<[Ctx, ...I], O>) { const proto = anAction.__reatom throwReatomError(!proto.isAction, 'action expected') @@ -167,5 +159,4 @@ export const createTestCtx = (options?: CtxOptions): TestCtx => { } /** @deprecated use `createMemStorage` fromm `@reatom/persist` instead */ -export const createMockStorage = (snapshot?: Rec) => - createMemStorage({ name: 'test', snapshot }) +export const createMockStorage = (snapshot?: Rec) => createMemStorage({ name: 'test', snapshot }) diff --git a/packages/timer/CHANGELOG.md b/packages/timer/CHANGELOG.md index 9b3bb1365..90a47db80 100644 --- a/packages/timer/CHANGELOG.md +++ b/packages/timer/CHANGELOG.md @@ -2,76 +2,69 @@ ## [3.6.2](https://github.com/artalar/reatom/compare/timer-v3.6.1...timer-v3.6.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.6.1](https://github.com/artalar/reatom/compare/timer-v3.6.0...timer-v3.6.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.6.0](https://github.com/artalar/reatom/compare/timer-v3.5.6...timer-v3.6.0) (2024-02-13) - ### Features -* **timer:** add passed option to startTimer ([2ba31a4](https://github.com/artalar/reatom/commit/2ba31a45041ed1009d59a7628fc530dd8faa7027)) - +- **timer:** add passed option to startTimer ([2ba31a4](https://github.com/artalar/reatom/commit/2ba31a45041ed1009d59a7628fc530dd8faa7027)) ### Bug Fixes -* **timer:** do not allow the timer mutations ([05643d8](https://github.com/artalar/reatom/commit/05643d87eb0d8e8000581adfcb20deb2d2bb4169)) +- **timer:** do not allow the timer mutations ([05643d8](https://github.com/artalar/reatom/commit/05643d87eb0d8e8000581adfcb20deb2d2bb4169)) ## [3.5.6](https://github.com/artalar/reatom/compare/timer-v3.5.5...timer-v3.5.6) (2023-11-20) - ### Bug Fixes -* **timer:** use ctx.schedule for each iteration ([302cf99](https://github.com/artalar/reatom/commit/302cf9919036267c45fff4c2c865eac89248944f)) +- **timer:** use ctx.schedule for each iteration ([302cf99](https://github.com/artalar/reatom/commit/302cf9919036267c45fff4c2c865eac89248944f)) ## [3.5.5](https://github.com/artalar/reatom/compare/timer-v3.5.4...timer-v3.5.5) (2023-07-07) - ### Bug Fixes -* **timer:** delay overflow ([3113e07](https://github.com/artalar/reatom/commit/3113e07bf0a06e9f53f40a72807634e457624719)) +- **timer:** delay overflow ([3113e07](https://github.com/artalar/reatom/commit/3113e07bf0a06e9f53f40a72807634e457624719)) ## [3.5.4](https://github.com/artalar/reatom/compare/timer-v3.5.3...timer-v3.5.4) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/timer/src/index.test.ts b/packages/timer/src/index.test.ts index 7ba79b3ad..f6913dc11 100644 --- a/packages/timer/src/index.test.ts +++ b/packages/timer/src/index.test.ts @@ -12,9 +12,7 @@ test(`base API`, async () => { timerAtom.intervalAtom.setSeconds(ctx, 0.001) var target = 50 - var duration = await getDuration(() => - timerAtom.startTimer(ctx, target / 1000), - ) + var duration = await getDuration(() => timerAtom.startTimer(ctx, target / 1000)) assert.ok(duration >= target) @@ -99,7 +97,7 @@ test('allow start from passed time', async () => { const start = Date.now() const promise = timerAtom.startTimer(ctx, delay, passed) assert.is(ctx.get(timerAtom.progressAtom), passed / delay) - + await promise const duration = Date.now() - start diff --git a/packages/timer/src/index.ts b/packages/timer/src/index.ts index bd16020e7..ae710f40d 100644 --- a/packages/timer/src/index.ts +++ b/packages/timer/src/index.ts @@ -1,12 +1,4 @@ -import { - Action, - action, - Atom, - atom, - AtomMut, - throwReatomError, - __count, -} from '@reatom/core' +import { Action, action, Atom, atom, AtomMut, throwReatomError, __count } from '@reatom/core' import { withReducers } from '@reatom/primitives' import { MAX_SAFE_TIMEOUT, noop, sleep } from '@reatom/utils' import { getRootCause, onUpdate } from '@reatom/hooks' @@ -64,17 +56,11 @@ export const reatomTimer = ( } = typeof options === 'string' ? { name: options } : options const timerAtom = atom(0, `${name}Atom`) - const progressAtom /* : TimerAtom['progressAtom'] */ = atom( - 0, - `${name}.progressAtom`, - ) + const progressAtom /* : TimerAtom['progressAtom'] */ = atom(0, `${name}.progressAtom`) const pauseAtom: TimerAtom['pauseAtom'] = atom(false, `${name}.pauseAtom`) - const intervalAtom: TimerAtom['intervalAtom'] = atom( - interval, - `${name}.intervalAtom`, - ).pipe( + const intervalAtom: TimerAtom['intervalAtom'] = atom(interval, `${name}.intervalAtom`).pipe( withReducers({ setSeconds: (state, seconds: number) => seconds * 1000, }), @@ -82,88 +68,79 @@ export const reatomTimer = ( const _versionAtom = atom(0, `${name}._versionAtom`) - const startTimer: TimerAtom['startTimer'] = action( - (ctx, delay: number, passed = 0) => { - delay *= delayMultiplier - - delay = Math.min(MAX_SAFE_TIMEOUT, delay) - - throwReatomError( - delay < ctx.get(intervalAtom), - 'delay less than interval', - ) - throwReatomError(delay < passed, 'passed more than delay') - - const version = _versionAtom(ctx, (s) => s + 1) - const start = Date.now() - let target = delay + start - passed - let remains = delay - passed - let resolvePause = noop - let pauseFrom = 0 - - timerAtom(ctx, remains) - - progressAtom(ctx, passed / delay) - - pauseAtom(ctx, false) - - const cleanupPause = onUpdate(pauseAtom, (pauseCtx, pause) => { - if (getRootCause(ctx.cause) === getRootCause(pauseCtx.cause)) { - const now = Date.now() - ctx.schedule(() => { - if (pause) { - pauseFrom = now - } else { - target += now - pauseFrom - resolvePause() - resolvePause = noop - pauseFrom = 0 - } - }) - } - }) + const startTimer: TimerAtom['startTimer'] = action((ctx, delay: number, passed = 0) => { + delay *= delayMultiplier - return ctx - .schedule(async () => { - while (1) { - remains = target - Date.now() - if (remains <= 0) break + delay = Math.min(MAX_SAFE_TIMEOUT, delay) - const interval = ctx.get(intervalAtom) - const tickDelay = - remains < interval - ? remains - : // reduce perf overload shift (when the sleep resolves after expected time) - remains % interval || interval - - await ctx.schedule(() => sleep(tickDelay)) - - if (pauseFrom) { - await new Promise((r) => (resolvePause = r)) - continue - } - - if (version !== ctx.get(_versionAtom)) return - - ctx.get(() => { - remains = timerAtom(ctx, Math.max(0, target - Date.now())) - const interval = ctx.get(intervalAtom) - const steps = Math.ceil(delay / interval) - const stepsRemains = Math.ceil(remains / interval) - progressAtom( - ctx, - +(1 - stepsRemains / steps).toFixed(progressPrecision), - ) - }) + throwReatomError(delay < ctx.get(intervalAtom), 'delay less than interval') + throwReatomError(delay < passed, 'passed more than delay') + + const version = _versionAtom(ctx, (s) => s + 1) + const start = Date.now() + let target = delay + start - passed + let remains = delay - passed + let resolvePause = noop + let pauseFrom = 0 + + timerAtom(ctx, remains) + + progressAtom(ctx, passed / delay) + + pauseAtom(ctx, false) + + const cleanupPause = onUpdate(pauseAtom, (pauseCtx, pause) => { + if (getRootCause(ctx.cause) === getRootCause(pauseCtx.cause)) { + const now = Date.now() + ctx.schedule(() => { + if (pause) { + pauseFrom = now + } else { + target += now - pauseFrom + resolvePause() + resolvePause = noop + pauseFrom = 0 } }) - .finally(() => { - cleanupPause() - if (version === ctx.get(_versionAtom)) endTimer(ctx) - }) - }, - `${name}.startTimer`, - ) + } + }) + + return ctx + .schedule(async () => { + while (1) { + remains = target - Date.now() + if (remains <= 0) break + + const interval = ctx.get(intervalAtom) + const tickDelay = + remains < interval + ? remains + : // reduce perf overload shift (when the sleep resolves after expected time) + remains % interval || interval + + await ctx.schedule(() => sleep(tickDelay)) + + if (pauseFrom) { + await new Promise((r) => (resolvePause = r)) + continue + } + + if (version !== ctx.get(_versionAtom)) return + + ctx.get(() => { + remains = timerAtom(ctx, Math.max(0, target - Date.now())) + const interval = ctx.get(intervalAtom) + const steps = Math.ceil(delay / interval) + const stepsRemains = Math.ceil(remains / interval) + progressAtom(ctx, +(1 - stepsRemains / steps).toFixed(progressPrecision)) + }) + } + }) + .finally(() => { + cleanupPause() + if (version === ctx.get(_versionAtom)) endTimer(ctx) + }) + }, `${name}.startTimer`) const stopTimer: TimerAtom['stopTimer'] = action((ctx) => { _versionAtom(ctx, (s) => s + 1) @@ -175,10 +152,7 @@ export const reatomTimer = ( timerAtom(ctx, 0) }, `${name}.endTimer`) - const pause: TimerAtom['pause'] = action( - (ctx) => pauseAtom(ctx, (s) => !s), - `${name}.pause`, - ) + const pause: TimerAtom['pause'] = action((ctx) => pauseAtom(ctx, (s) => !s), `${name}.pause`) return Object.assign({}, timerAtom, { progressAtom, diff --git a/packages/undo/CHANGELOG.md b/packages/undo/CHANGELOG.md index ca767457a..1b5fc1d78 100644 --- a/packages/undo/CHANGELOG.md +++ b/packages/undo/CHANGELOG.md @@ -2,85 +2,77 @@ ## [3.4.2](https://github.com/artalar/reatom/compare/undo-v3.4.1...undo-v3.4.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.4.1](https://github.com/artalar/reatom/compare/undo-v3.4.0...undo-v3.4.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.4.0](https://github.com/artalar/reatom/compare/undo-v3.3.3...undo-v3.4.0) (2024-06-14) - ### Features -* **undo:** add withPersist option ([f76d0fc](https://github.com/artalar/reatom/commit/f76d0fc4c7c0fe655de56f79f3c3b5ca874c8640)) +- **undo:** add withPersist option ([f76d0fc](https://github.com/artalar/reatom/commit/f76d0fc4c7c0fe655de56f79f3c3b5ca874c8640)) ## [3.3.3](https://github.com/artalar/reatom/compare/undo-v3.3.2...undo-v3.3.3) (2024-06-12) - ### Bug Fixes -* **undo:** correct shouldUpdate ([f94d5b9](https://github.com/artalar/reatom/commit/f94d5b9489b0c0d0798eb258941af7dbc9b90a7c)) +- **undo:** correct shouldUpdate ([f94d5b9](https://github.com/artalar/reatom/commit/f94d5b9489b0c0d0798eb258941af7dbc9b90a7c)) ## [3.3.2](https://github.com/artalar/reatom/compare/undo-v3.3.1...undo-v3.3.2) (2024-04-28) - ### Bug Fixes -* **undo:** adding initial state in historyAtom ([#828](https://github.com/artalar/reatom/issues/828)) ([4006e44](https://github.com/artalar/reatom/commit/4006e44ad408f9daf7680cd65e86a0fc8521eade)) +- **undo:** adding initial state in historyAtom ([#828](https://github.com/artalar/reatom/issues/828)) ([4006e44](https://github.com/artalar/reatom/commit/4006e44ad408f9daf7680cd65e86a0fc8521eade)) ## [3.3.1](https://github.com/artalar/reatom/compare/undo-v3.3.0...undo-v3.3.1) (2023-08-07) - ### Bug Fixes -* **undo:** history slice ([f86f9dd](https://github.com/artalar/reatom/commit/f86f9ddaeb727eaa7c7e15f9168b007c7b185fd5)) +- **undo:** history slice ([f86f9dd](https://github.com/artalar/reatom/commit/f86f9ddaeb727eaa7c7e15f9168b007c7b185fd5)) ## [3.3.0](https://github.com/artalar/reatom/compare/undo-v3.2.4...undo-v3.3.0) (2023-08-05) - ### Features -* **undo:** add reatomDynamicUndo, more docs, refactoring ([cd21467](https://github.com/artalar/reatom/commit/cd21467eace6bb163d4bd80ff7869a5b78bc73d7)) +- **undo:** add reatomDynamicUndo, more docs, refactoring ([cd21467](https://github.com/artalar/reatom/commit/cd21467eace6bb163d4bd80ff7869a5b78bc73d7)) ## [3.2.4](https://github.com/artalar/reatom/compare/undo-v3.2.3...undo-v3.2.4) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) diff --git a/packages/undo/README.md b/packages/undo/README.md index 563f3d549..f7ce0965f 100644 --- a/packages/undo/README.md +++ b/packages/undo/README.md @@ -22,7 +22,7 @@ All methods reuse `WithUndo` interface which includes the following atoms and ac All methods accepts the optional properties: - `length` is the max amount of state records, **30** by default -- `shouldUpdate` function allows you to ignore some updates, by default it skips state updates which is equal to the last history record +- `shouldUpdate` function allows you to ignore some updates, by default it skips state updates which is equal to the last history record - `shouldReplace` function allows you define what to do with the new update, replace the last history record (`true`) or add a new record (`false` - by default) - `withPersist` - `WithPersist` instance from one of the adapter of [@reatom/persist](https://www.reatom.dev/package/persist). It will persist data from `historyAtom` and `persistAtom`, the target atom init state may be derived from the history, if it preserve. @@ -46,9 +46,7 @@ This option helps you store only important updates by replacing the last state w For example, we want to save in the history all user input, but the each record of the history should include only words, not a letters. ```ts -const inputAtom = atom('').pipe( - withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') }), -) +const inputAtom = atom('').pipe(withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') })) for (const letter of 'This is a test') { inputAtom(ctx, (s) => s + letter) diff --git a/packages/undo/src/index.test.ts b/packages/undo/src/index.test.ts index 804d13a50..6ed2a102b 100644 --- a/packages/undo/src/index.test.ts +++ b/packages/undo/src/index.test.ts @@ -151,10 +151,7 @@ test('reatomDynamicUndo', () => { const elementAtom = atom(3) listAtom.set(ctx, 3, elementAtom) assert.is(track.calls.length, 4) - assert.equal( - parseAtoms(ctx, listAtom), - new Map().set(1, 100).set(2, 200).set(3, 3), - ) + assert.equal(parseAtoms(ctx, listAtom), new Map().set(1, 100).set(2, 200).set(3, 3)) listUndoAtom.undo(ctx) assert.is(ctx.get(listAtom).size, 2) @@ -165,18 +162,13 @@ test('reatomDynamicUndo', () => { listUndoAtom.redo(ctx) listUndoAtom.redo(ctx) - assert.equal( - parseAtoms(ctx, listAtom), - new Map().set(1, 100).set(2, 200).set(3, 3), - ) + assert.equal(parseAtoms(ctx, listAtom), new Map().set(1, 100).set(2, 200).set(3, 3)) assert.is(listAtom.get(ctx, 3), elementAtom) ;('πŸ‘') //? }) test('"shouldReplace"', () => { - const inputAtom = atom('').pipe( - withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') }), - ) + const inputAtom = atom('').pipe(withUndo({ shouldReplace: (ctx, state) => !state.endsWith(' ') })) const ctx = createTestCtx() for (const letter of 'This is a test') { diff --git a/packages/undo/src/index.ts b/packages/undo/src/index.ts index d0f0fab94..f18e281df 100644 --- a/packages/undo/src/index.ts +++ b/packages/undo/src/index.ts @@ -38,19 +38,11 @@ interface AggregateHistory { export interface WithUndoOptions { length?: number - shouldUpdate?: Fn< - [ctx: Ctx, state: T, history: Array, currentPosition: number], - boolean - > - shouldReplace?: Fn< - [ctx: Ctx, state: T, history: Array, currentPosition: number], - boolean - > + shouldUpdate?: Fn<[ctx: Ctx, state: T, history: Array, currentPosition: number], boolean> + shouldReplace?: Fn<[ctx: Ctx, state: T, history: Array, currentPosition: number], boolean> withPersist?: ( options: WithPersistOptions>, - ) => ( - historyAggregateAtom: Atom>, - ) => Atom> + ) => (historyAggregateAtom: Atom>) => Atom> } const update = (ctx: Ctx, anAtom: AtomMut | Atom, state: any) => @@ -65,8 +57,7 @@ const update = (ctx: Ctx, anAtom: AtomMut | Atom, state: any) => export const withUndo = >>>({ length = 30, - shouldUpdate = (ctx, state, history) => - history[history.length - 1] !== state, + shouldUpdate = (ctx, state, history) => history[history.length - 1] !== state, shouldReplace = () => false, withPersist, }: WithUndoOptions> = {}): Fn<[T], T & WithUndo>> => @@ -84,10 +75,9 @@ export const withUndo = `${name}.Undo._aggregateHistoryAtom`, ) - const historyAtom = (anAtom.historyAtom = atom>>( - [], - `${name}.Undo._historyAtom`, - )).pipe(withInit((ctx) => ctx.get(aggregateHistoryAtom).history)) + const historyAtom = (anAtom.historyAtom = atom>>([], `${name}.Undo._historyAtom`)).pipe( + withInit((ctx) => ctx.get(aggregateHistoryAtom).history), + ) anAtom.pipe( withInit((ctx, init) => { @@ -101,16 +91,12 @@ export const withUndo = }), ) - const positionAtom = (anAtom.positionAtom = atom( - 0, - `${name}._position`, - )).pipe(withInit((ctx) => ctx.get(aggregateHistoryAtom).position)) - - anAtom.isUndoAtom = atom( - (ctx) => ctx.spy(positionAtom) > 0, - `${name}.Undo._isUndoAtom`, + const positionAtom = (anAtom.positionAtom = atom(0, `${name}._position`)).pipe( + withInit((ctx) => ctx.get(aggregateHistoryAtom).position), ) + anAtom.isUndoAtom = atom((ctx) => ctx.spy(positionAtom) > 0, `${name}.Undo._isUndoAtom`) + anAtom.isRedoAtom = atom( (ctx) => ctx.spy(positionAtom) < ctx.spy(historyAtom).length - 1, `${name}.Undo._isRedoAtom`, @@ -137,15 +123,9 @@ export const withUndo = anAtom.onChange((ctx, state) => { let position = ctx.get(positionAtom) - if ( - !isCausedBy(ctx.cause, jump.__reatom) && - shouldUpdate(ctx, state, ctx.get(historyAtom), position) - ) { + if (!isCausedBy(ctx.cause, jump.__reatom) && shouldUpdate(ctx, state, ctx.get(historyAtom), position)) { historyAtom(ctx, (history) => { - history = history.slice( - Math.max(0, position - (length - 2)), - position + 1, - ) + history = history.slice(Math.max(0, position - (length - 2)), position + 1) if (!shouldReplace(ctx, state, history, position)) { position++ } @@ -170,9 +150,7 @@ export const withUndo = const position = ctx.get(positionAtom) aggregateHistoryAtom(ctx, (state) => - state.history === history && state.position === position - ? state - : { history, position }, + state.history === history && state.position === position ? state : { history, position }, ) }) } @@ -197,9 +175,7 @@ export const reatomUndo = | Rec>( length, shouldUpdate, shouldReplace, - }: Exclude = typeof options === 'string' - ? { name: options } - : options + }: Exclude = typeof options === 'string' ? { name: options } : options const theAtom = Object.assign( (ctx: Ctx, newShape: AtomsStates): AtomsStates => { @@ -231,9 +207,7 @@ export const reatomDynamicUndo = ( length, shouldUpdate, shouldReplace, - }: Exclude = typeof options === 'string' - ? { name: options } - : options + }: Exclude = typeof options === 'string' ? { name: options } : options const theAtom = Object.assign( (ctx: Ctx, newState: AtomCache): AtomCache => { diff --git a/packages/url/CHANGELOG.md b/packages/url/CHANGELOG.md index c26330d8c..21ba686a0 100644 --- a/packages/url/CHANGELOG.md +++ b/packages/url/CHANGELOG.md @@ -2,106 +2,91 @@ ## [3.7.3](https://github.com/artalar/reatom/compare/url-v3.7.2...url-v3.7.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.7.2](https://github.com/artalar/reatom/compare/url-v3.7.1...url-v3.7.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.7.1](https://github.com/artalar/reatom/compare/url-v3.7.0...url-v3.7.1) (2024-03-23) - ### Bug Fixes -* **url:** sync links with location ([17e8b54](https://github.com/artalar/reatom/commit/17e8b54d3990745dc2498ac60eef0e0d95dd5fb2)) +- **url:** sync links with location ([17e8b54](https://github.com/artalar/reatom/commit/17e8b54d3990745dc2498ac60eef0e0d95dd5fb2)) ## [3.7.0](https://github.com/artalar/reatom/compare/url-v3.6.2...url-v3.7.0) (2024-02-16) - ### Features -* **url:** add replace option ([c6ae085](https://github.com/artalar/reatom/commit/c6ae0859fd246135e17379623e91a67ffeedbb59)) - +- **url:** add replace option ([c6ae085](https://github.com/artalar/reatom/commit/c6ae0859fd246135e17379623e91a67ffeedbb59)) ### Bug Fixes -* **url:** updateFromSource indirect check ([05b55ec](https://github.com/artalar/reatom/commit/05b55ecf973dbb43e91226390bdf102c17455f88)) +- **url:** updateFromSource indirect check ([05b55ec](https://github.com/artalar/reatom/commit/05b55ecf973dbb43e91226390bdf102c17455f88)) ## [3.6.2](https://github.com/artalar/reatom/compare/url-v3.6.1...url-v3.6.2) (2023-11-26) - ### Bug Fixes -* **url:** accept a function in urlAtom update ([5affb85](https://github.com/artalar/reatom/commit/5affb8525fe5629621e0f7fe9b999f1e2306b28e)) -* **url:** simplify path types ([6c905b5](https://github.com/artalar/reatom/commit/6c905b5424a9d85c20059db04c3c03551cef9c5b)) +- **url:** accept a function in urlAtom update ([5affb85](https://github.com/artalar/reatom/commit/5affb8525fe5629621e0f7fe9b999f1e2306b28e)) +- **url:** simplify path types ([6c905b5](https://github.com/artalar/reatom/commit/6c905b5424a9d85c20059db04c3c03551cef9c5b)) ## [3.6.1](https://github.com/artalar/reatom/compare/url-v3.6.0...url-v3.6.1) (2023-11-23) - ### Bug Fixes -* **url:** use spy in match ([0a8320c](https://github.com/artalar/reatom/commit/0a8320c02ef8036816c3361fae852eaf159b359c)) +- **url:** use spy in match ([0a8320c](https://github.com/artalar/reatom/commit/0a8320c02ef8036816c3361fae852eaf159b359c)) ## [3.6.0](https://github.com/artalar/reatom/compare/url-v3.5.0...url-v3.6.0) (2023-11-20) - ### Features -* **url:** stop abortCauseContext propagation ([b8daac9](https://github.com/artalar/reatom/commit/b8daac98b2638fbe170aebed7816afec9e73928f)) +- **url:** stop abortCauseContext propagation ([b8daac9](https://github.com/artalar/reatom/commit/b8daac98b2638fbe170aebed7816afec9e73928f)) ## [3.5.0](https://github.com/artalar/reatom/compare/url-v3.4.1...url-v3.5.0) (2023-11-05) - ### Features -* **url:** add updateFromSource ([209133f](https://github.com/artalar/reatom/commit/209133f1426583729aac394fdf5e2a484252c3eb)) - +- **url:** add updateFromSource ([209133f](https://github.com/artalar/reatom/commit/209133f1426583729aac394fdf5e2a484252c3eb)) ### Bug Fixes -* **url:** reinit qs persist state when the param was removed ([59135f8](https://github.com/artalar/reatom/commit/59135f883681bed433fb5ab36111ca9d478e05fb)) +- **url:** reinit qs persist state when the param was removed ([59135f8](https://github.com/artalar/reatom/commit/59135f883681bed433fb5ab36111ca9d478e05fb)) ## [3.4.1](https://github.com/artalar/reatom/compare/url-v3.4.0...url-v3.4.1) (2023-10-31) - ### Bug Fixes -* **url:** compatibility with different routes [#671](https://github.com/artalar/reatom/issues/671) ([b4f5761](https://github.com/artalar/reatom/commit/b4f5761a8cde3697e4a9192e88ccc42ff8378bba)) +- **url:** compatibility with different routes [#671](https://github.com/artalar/reatom/issues/671) ([b4f5761](https://github.com/artalar/reatom/commit/b4f5761a8cde3697e4a9192e88ccc42ff8378bba)) ## [3.4.0](https://github.com/artalar/reatom/compare/url-v3.3.1...url-v3.4.0) (2023-10-20) - ### Features -* **url:** add SearchParamsAtom.del and persist refactor ([af86da3](https://github.com/artalar/reatom/commit/af86da339cf0ba864eec9d074aa9f8b14c403fd2)) +- **url:** add SearchParamsAtom.del and persist refactor ([af86da3](https://github.com/artalar/reatom/commit/af86da339cf0ba864eec9d074aa9f8b14c403fd2)) ## [3.3.1](https://github.com/artalar/reatom/compare/url-v3.3.0...url-v3.3.1) (2023-10-09) - ### Bug Fixes -* **url:** parse empty QS as empty string ([3665807](https://github.com/artalar/reatom/commit/3665807cdbe131f87a414ac5907cd8a88cafe769)) +- **url:** parse empty QS as empty string ([3665807](https://github.com/artalar/reatom/commit/3665807cdbe131f87a414ac5907cd8a88cafe769)) ## [3.3.0](https://github.com/artalar/reatom/compare/url-v3.2.0...url-v3.3.0) (2023-08-05) - ### Features -* **url:** add "go" action and "match" atom creator ([b297e1a](https://github.com/artalar/reatom/commit/b297e1af737728693d5980704669377bd99b4d22)) -* **url:** added! ([7467a38](https://github.com/artalar/reatom/commit/7467a387350ef435c0594e3045da1c86bf1e235a)) - +- **url:** add "go" action and "match" atom creator ([b297e1a](https://github.com/artalar/reatom/commit/b297e1af737728693d5980704669377bd99b4d22)) +- **url:** added! ([7467a38](https://github.com/artalar/reatom/commit/7467a387350ef435c0594e3045da1c86bf1e235a)) ### Bug Fixes -* **url:** popstate and hash handling ([d7fbe55](https://github.com/artalar/reatom/commit/d7fbe5586a6707713e8981b381c8dcb5056ccad9)) -* **url:** use isCausedBy ([75c64f7](https://github.com/artalar/reatom/commit/75c64f7ae314f0d58a8c8f5e175f35b7237fcf25)) +- **url:** popstate and hash handling ([d7fbe55](https://github.com/artalar/reatom/commit/d7fbe5586a6707713e8981b381c8dcb5056ccad9)) +- **url:** use isCausedBy ([75c64f7](https://github.com/artalar/reatom/commit/75c64f7ae314f0d58a8c8f5e175f35b7237fcf25)) ## 3.2.0 diff --git a/packages/url/README.md b/packages/url/README.md index f628348ea..26615ebeb 100644 --- a/packages/url/README.md +++ b/packages/url/README.md @@ -28,9 +28,7 @@ In the code below `filterAtom` is a mutable atom which changes will be synced wi import { reatomNumber } from '@reatom/primitives' import { withSearchParamsPersist } from '@reatom/url' -export const pageAtom = reatomNumber(0, 'pageAtom').pipe( - withSearchParamsPersist('page', (page = '1') => Number(page)), -) +export const pageAtom = reatomNumber(0, 'pageAtom').pipe(withSearchParamsPersist('page', (page = '1') => Number(page))) ``` Now you have handy `increment` and `decrement` actions in `pageAtom` and synchronization with "page" search parameter. @@ -70,11 +68,7 @@ export interface SearchParamsAtom extends Atom> { set: Action<[key: string, value: string, replace?: boolean], void> del: Action<[key: string, replace?: boolean], void> /** create AtomMut which will synced with the specified query parameter */ - lens( - key: string, - parse?: (value?: string) => T, - serialize?: (value: T) => undefined | string, - ): AtomMut + lens(key: string, parse?: (value?: string) => T, serialize?: (value: T) => undefined | string): AtomMut /** create AtomMut which will synced with the specified query parameter */ lens( key: string, @@ -135,8 +129,7 @@ export const RouterSync = () => { setupRef.current = true urlAtom.settingsAtom(ctx, { init: () => new URL(location.href), - sync: (_ctx, url, replace) => - navigate(url.pathname + url.search, { replace }), + sync: (_ctx, url, replace) => navigate(url.pathname + url.search, { replace }), }) // trigger `onChange` hooks. urlAtom(ctx, new URL(location.href)) diff --git a/packages/url/src/index.test.ts b/packages/url/src/index.test.ts index 0b1121dcb..6d45ee17a 100644 --- a/packages/url/src/index.test.ts +++ b/packages/url/src/index.test.ts @@ -2,12 +2,7 @@ import { test } from 'uvu' import * as assert from 'uvu/assert' import { createTestCtx, mockFn } from '@reatom/testing' -import { - searchParamsAtom, - setupUrlAtomSettings, - updateFromSource, - urlAtom, -} from './' +import { searchParamsAtom, setupUrlAtomSettings, updateFromSource, urlAtom } from './' test('direct updateFromSource call should be ignored', async () => { const ctx = createTestCtx() diff --git a/packages/url/src/index.ts b/packages/url/src/index.ts index 28630042e..d6141c575 100644 --- a/packages/url/src/index.ts +++ b/packages/url/src/index.ts @@ -1,14 +1,4 @@ -import { - Action, - Atom, - AtomMut, - AtomState, - Ctx, - Fn, - Rec, - action, - atom, -} from '@reatom/core' +import { Action, Atom, AtomMut, AtomState, Ctx, Fn, Rec, action, atom } from '@reatom/core' import { abortCauseContext } from '@reatom/effects' import { getRootCause, withInit } from '@reatom/hooks' import { noop } from '@reatom/utils' @@ -32,11 +22,7 @@ export interface SearchParamsAtom extends Atom> { set: Action<[key: string, value: string, replace?: boolean], void> del: Action<[key: string, replace?: boolean], void> /** create AtomMut which will synced with the specified query parameter */ - lens( - key: string, - parse?: (value?: string) => T, - serialize?: (value: T) => undefined | string, - ): AtomMut + lens(key: string, parse?: (value?: string) => T, serialize?: (value: T) => undefined | string): AtomMut /** create AtomMut which will synced with the specified query parameter */ lens( key: string, @@ -59,9 +45,7 @@ const browserSync = (url: URL, replace?: boolean) => { else history.pushState({}, '', url.href) } /**Browser settings allow handling of the "popstate" event and a link click. */ -const createBrowserUrlAtomSettings = ( - shouldCatchLinkClick = true, -): AtomUrlSettings => ({ +const createBrowserUrlAtomSettings = (shouldCatchLinkClick = true): AtomUrlSettings => ({ init: (ctx: Ctx) => { // do not store causes for IO events ctx = { ...ctx, cause: getRootCause(ctx.cause) } @@ -108,9 +92,7 @@ const createBrowserUrlAtomSettings = ( } }) - globalThis.addEventListener('popstate', (event) => - updateFromSource(ctx, new URL(location.href)), - ) + globalThis.addEventListener('popstate', (event) => updateFromSource(ctx, new URL(location.href))) if (shouldCatchLinkClick) document.body.addEventListener('click', click) return new URL(location.href) @@ -120,28 +102,18 @@ const createBrowserUrlAtomSettings = ( }, }) -const settingsAtom = atom( - createBrowserUrlAtomSettings(), - 'urlAtom.settingAtom', -) +const settingsAtom = atom(createBrowserUrlAtomSettings(), 'urlAtom.settingAtom') export const setupUrlAtomSettings = action( - ( - ctx, - init: (ctx: Ctx) => URL, - sync: (ctx: Ctx, url: URL, replace?: boolean) => void = noop, - ) => { + (ctx, init: (ctx: Ctx) => URL, sync: (ctx: Ctx, url: URL, replace?: boolean) => void = noop) => { settingsAtom(ctx, { init, sync }) }, 'urlAtom.setupUrlAtomSettings', ) -export const setupUrlAtomBrowserSettings = action( - (ctx, shouldCatchLinkClick: boolean) => { - settingsAtom(ctx, createBrowserUrlAtomSettings(shouldCatchLinkClick)) - }, - 'urlAtom.setupUrlAtomBrowserSettings', -) +export const setupUrlAtomBrowserSettings = action((ctx, shouldCatchLinkClick: boolean) => { + settingsAtom(ctx, createBrowserUrlAtomSettings(shouldCatchLinkClick)) +}, 'urlAtom.setupUrlAtomBrowserSettings') const _urlAtom = atom(null as any as URL, 'urlAtom') export const urlAtom: UrlAtom = Object.assign( @@ -159,24 +131,13 @@ export const urlAtom: UrlAtom = Object.assign( _urlAtom, { settingsAtom, - go: action( - (ctx, path, replace?: boolean) => - urlAtom(ctx, (url) => new URL(path, url), replace), - 'urlAtom.go', - ), - match: (path: string) => - atom( - (ctx) => ctx.spy(urlAtom).pathname.startsWith(path), - `urlAtom.match#${path}`, - ), + go: action((ctx, path, replace?: boolean) => urlAtom(ctx, (url) => new URL(path, url), replace), 'urlAtom.go'), + match: (path: string) => atom((ctx) => ctx.spy(urlAtom).pathname.startsWith(path), `urlAtom.match#${path}`), }, ).pipe(withInit((ctx) => ctx.get(settingsAtom).init(ctx))) export const searchParamsAtom: SearchParamsAtom = Object.assign( - atom( - (ctx) => Object.fromEntries(ctx.spy(urlAtom).searchParams), - 'searchParamsAtom', - ), + atom((ctx) => Object.fromEntries(ctx.spy(urlAtom).searchParams), 'searchParamsAtom'), { set: action((ctx, key, value, replace) => { const url = ctx.get(urlAtom) @@ -191,10 +152,7 @@ export const searchParamsAtom: SearchParamsAtom = Object.assign( urlAtom(ctx, newUrl, replace) }, 'searchParamsAtom._del') satisfies SearchParamsAtom['del'], lens: ((key, ...a: Parameters) => - atom( - getSearchParamsOptions(...a).parse(), - `searchParamsAtom#${a[0]}`, - ).pipe( + atom(getSearchParamsOptions(...a).parse(), `searchParamsAtom#${a[0]}`).pipe( // TODO // @ts-expect-error withSearchParamsPersist(key, ...a), @@ -204,10 +162,7 @@ export const searchParamsAtom: SearchParamsAtom = Object.assign( const getSearchParamsOptions = ( ...a: - | [ - parse?: (value?: string) => unknown, - serialize?: (value: unknown) => undefined | string, - ] + | [parse?: (value?: string) => unknown, serialize?: (value: unknown) => undefined | string] | [ options: { parse?: (value?: string) => unknown @@ -251,10 +206,7 @@ export function withSearchParamsPersist( export function withSearchParamsPersist( key: string, ...a: - | [ - parse?: (value?: string) => unknown, - serialize?: (value: unknown) => undefined | string, - ] + | [parse?: (value?: string) => unknown, serialize?: (value: unknown) => undefined | string] | [ options: { parse?: (value?: string) => unknown diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 0cc12f80d..789c3b0fd 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -2,115 +2,103 @@ ## [3.9.2](https://github.com/artalar/reatom/compare/utils-v3.9.1...utils-v3.9.2) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.9.1](https://github.com/artalar/reatom/compare/utils-v3.9.0...utils-v3.9.1) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.9.0](https://github.com/artalar/reatom/compare/utils-v3.8.0...utils-v3.9.0) (2024-02-05) - ### Features -* **utils:** add typed keys and entries ([ed3c205](https://github.com/artalar/reatom/commit/ed3c205259d39cea018938c2b14794dbfd994c00)) +- **utils:** add typed keys and entries ([ed3c205](https://github.com/artalar/reatom/commit/ed3c205259d39cea018938c2b14794dbfd994c00)) ## [3.8.0](https://github.com/artalar/reatom/compare/utils-v3.7.0...utils-v3.8.0) (2023-11-25) - ### Features -* **utils:** add throwAbort ([a991be1](https://github.com/artalar/reatom/commit/a991be1153c8b1c3c929a7eabcc9328e022b8eff)) +- **utils:** add throwAbort ([a991be1](https://github.com/artalar/reatom/commit/a991be1153c8b1c3c929a7eabcc9328e022b8eff)) ## [3.7.0](https://github.com/artalar/reatom/compare/utils-v3.6.0...utils-v3.7.0) (2023-10-20) - ### Features -* **lens:** add match().with ([f61dddc](https://github.com/artalar/reatom/commit/f61dddc858fda9921117acd472cc4e4e71cdd47e)) -* **utils:** add isRec ([444c4f6](https://github.com/artalar/reatom/commit/444c4f6c528092a59bdd332218b3d42c88351999)) +- **lens:** add match().with ([f61dddc](https://github.com/artalar/reatom/commit/f61dddc858fda9921117acd472cc4e4e71cdd47e)) +- **utils:** add isRec ([444c4f6](https://github.com/artalar/reatom/commit/444c4f6c528092a59bdd332218b3d42c88351999)) ## [3.6.0](https://github.com/artalar/reatom/compare/utils-v3.5.0...utils-v3.6.0) (2023-10-09) - ### Features -* **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) -* **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) +- **jsx:** nested atoms, better types ([#652](https://github.com/artalar/reatom/issues/652)) ([3ceae78](https://github.com/artalar/reatom/commit/3ceae788da52ff40a561ce5b2fc5371475fb7d7c)) +- **lens:** add match ([#646](https://github.com/artalar/reatom/issues/646)) ([0f2a768](https://github.com/artalar/reatom/commit/0f2a7685dd797cac4c9fc882a8e24bc31f9503a6)) ## [3.5.0](https://github.com/artalar/reatom/compare/utils-v3.4.0...utils-v3.5.0) (2023-09-13) - ### Features -* **utils:** improve throwIfAborted signature ([d57946d](https://github.com/artalar/reatom/commit/d57946d294285f5f51714a1ffc873928ae7cf079)) +- **utils:** improve throwIfAborted signature ([d57946d](https://github.com/artalar/reatom/commit/d57946d294285f5f51714a1ffc873928ae7cf079)) ## [3.4.0](https://github.com/artalar/reatom/compare/utils-v3.3.0...utils-v3.4.0) (2023-07-07) - ### Features -* **utils:** add MAX_SAFE_TIMEOUT ([17a23d2](https://github.com/artalar/reatom/commit/17a23d2917d42855c0b2872e0c504e5d1ed83172)) +- **utils:** add MAX_SAFE_TIMEOUT ([17a23d2](https://github.com/artalar/reatom/commit/17a23d2917d42855c0b2872e0c504e5d1ed83172)) ## [3.3.0](https://github.com/artalar/reatom/compare/utils-v3.2.3...utils-v3.3.0) (2023-06-12) - ### Features -* **utils:** add merge ([7d7063a](https://github.com/artalar/reatom/commit/7d7063a5eaff2b6f2f0f1677d4d3bba34dad38a8)) +- **utils:** add merge ([7d7063a](https://github.com/artalar/reatom/commit/7d7063a5eaff2b6f2f0f1677d4d3bba34dad38a8)) ## [3.2.3](https://github.com/artalar/reatom/compare/utils-v3.2.2...utils-v3.2.3) (2023-05-21) - ### Bug Fixes -* **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) -* **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) -* **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **all-settled:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **async:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **core:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **effects:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **eslint-plugin:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form-web:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **form:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **framework:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **hooks:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **lens:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **logger:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **navigation:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- new way of hooking up typings [#560](https://github.com/artalar/reatom/issues/560) ([#568](https://github.com/artalar/reatom/issues/568)) ([99550e9](https://github.com/artalar/reatom/commit/99550e98c34df7efd8431282a868a0483bed5dc8)) +- **npm-cookie-baker:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-history:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-lit:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-react:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **npm-svelte:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist-web-storage:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **persist:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **primitives:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v1:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **react-v2:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **testing:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **timer:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **undo:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) +- **utils:** change build output ([5bedebd](https://github.com/artalar/reatom/commit/5bedebda3a1ee92850d10f767686303b8ec2ba0e)) ## [3.2.2](https://github.com/artalar/reatom/compare/utils-v3.2.1...utils-v3.2.2) (2023-05-10) - ### Bug Fixes -* **utils:** correct toAbortError ([ff1a6f0](https://github.com/artalar/reatom/commit/ff1a6f0b1cbdf04d4fa734a67c6d18cefbad2450)) +- **utils:** correct toAbortError ([ff1a6f0](https://github.com/artalar/reatom/commit/ff1a6f0b1cbdf04d4fa734a67c6d18cefbad2450)) ## [3.2.1](https://github.com/artalar/reatom/compare/utils-v3.2.0...utils-v3.2.1) (2023-04-18) - ### Bug Fixes -* **utils:** small DOMException ponyfil ([c5d0f16](https://github.com/artalar/reatom/commit/c5d0f16cd8601219031e2a219b24b712fdc03d77)) +- **utils:** small DOMException ponyfil ([c5d0f16](https://github.com/artalar/reatom/commit/c5d0f16cd8601219031e2a219b24b712fdc03d77)) diff --git a/packages/utils/src/index.test.ts b/packages/utils/src/index.test.ts index 732e3a961..8cc6c0b9d 100644 --- a/packages/utils/src/index.test.ts +++ b/packages/utils/src/index.test.ts @@ -4,12 +4,8 @@ import * as assert from 'uvu/assert' import { isDeepEqual, toAbortError } from './' test('isDeepEqual Set', () => { - assert.ok( - isDeepEqual(new Set([{ a: 1 }, { a: 2 }]), new Set([{ a: 1 }, { a: 2 }])), - ) - assert.not.ok( - isDeepEqual(new Set([{ a: 1 }, { a: 2 }]), new Set([{ a: 2 }, { a: 1 }])), - ) + assert.ok(isDeepEqual(new Set([{ a: 1 }, { a: 2 }]), new Set([{ a: 1 }, { a: 2 }]))) + assert.not.ok(isDeepEqual(new Set([{ a: 1 }, { a: 2 }]), new Set([{ a: 2 }, { a: 1 }]))) ;('πŸ‘') //? }) diff --git a/packages/web-fetch/README.md b/packages/web-fetch/README.md index e730712c0..78cf22bdf 100644 --- a/packages/web-fetch/README.md +++ b/packages/web-fetch/README.md @@ -33,12 +33,10 @@ To configure the request dynamically, pass a function that returns the configura ```ts const getUser = reatomFetch((ctx, id) => `/api/user/${id}`) -const search = reatomFetch( - (ctx, filter, sortBy) => ({ - url: '/api/search', - params: { filter, sortBy }, - }), -) +const search = reatomFetch((ctx, filter, sortBy) => ({ + url: '/api/search', + params: { filter, sortBy }, +})) ``` To specify the request method, either use the `method` option or `reatomFetch.` shorthand: diff --git a/packages/web-fetch/src/index.test.ts b/packages/web-fetch/src/index.test.ts index 8f19073db..3ad9b57f2 100644 --- a/packages/web-fetch/src/index.test.ts +++ b/packages/web-fetch/src/index.test.ts @@ -14,9 +14,7 @@ test('configuration', async () => { const reatomFetch = createReatomFetch({ transport }) - async function configure( - input: ReatomFetchConfig | (() => ReatomFetchConfig), - ) { + async function configure(input: ReatomFetchConfig | (() => ReatomFetchConfig)) { const fetcher = reatomFetch(input) await fetcher(ctx) assert.is(transport.lastInput(0), `${API}/`) @@ -49,11 +47,7 @@ test('merges URLs', async () => { test('merges headers', async () => { const ctx = createTestCtx() - async function mergeHeaders( - headersBase: HeadersInit, - headers: HeadersInit, - result: HeadersInit, - ) { + async function mergeHeaders(headersBase: HeadersInit, headers: HeadersInit, result: HeadersInit) { const fetcher = reatomFetch({ transport, url: API, @@ -62,11 +56,7 @@ test('merges headers', async () => { }) await fetcher(ctx) assert.equal( - Object.fromEntries([ - ...( - (transport.lastInput(1) as RequestInit).headers as Headers - ).entries(), - ]), + Object.fromEntries([...((transport.lastInput(1) as RequestInit).headers as Headers).entries()]), result, ) } @@ -89,12 +79,9 @@ test('content parsing', async () => { const fetcher = reatomFetch({ transport: (url, init) => { - return new Response( - JSON.stringify({ got: JSON.parse(init.body as string) }), - { - headers: { 'content-type': 'application/json' }, - }, - ) + return new Response(JSON.stringify({ got: JSON.parse(init.body as string) }), { + headers: { 'content-type': 'application/json' }, + }) }, url: API, body: 'Hello world!', diff --git a/packages/web-fetch/src/index.ts b/packages/web-fetch/src/index.ts index 1aa053ed7..e4090ae65 100644 --- a/packages/web-fetch/src/index.ts +++ b/packages/web-fetch/src/index.ts @@ -3,9 +3,7 @@ import { AsyncAction } from '@reatom/async' import { reatomAsync } from '@reatom/async' import { throwReatomError } from '@reatom/core' -export type UrlSearchParamsInit = ConstructorParameters< - typeof URLSearchParams ->[0] +export type UrlSearchParamsInit = ConstructorParameters[0] export type ReatomFetchConfig = | string @@ -13,22 +11,10 @@ export type ReatomFetchConfig = url: string urlBase?: string params?: UrlSearchParamsInit - transport?: ( - url: string, - init: RequestInit, - ) => Response | Promise + transport?: (url: string, init: RequestInit) => Response | Promise headers?: HeadersInit headersBase?: HeadersInit - method?: - | 'get' - | 'post' - | 'put' - | 'delete' - | 'patch' - | 'head' - | 'connect' - | 'trace' - | (string & {}) + method?: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'connect' | 'trace' | (string & {}) body?: unknown init?: Omit serializeBody?: (body: unknown) => BodyInit | Promise @@ -36,9 +22,7 @@ export type ReatomFetchConfig = } export type ReatomFetchQuery = ( - config: - | ReatomFetchConfig - | ((ctx: Ctx, p1: P1, p2: P2, p3: P3) => ReatomFetchConfig), + config: ReatomFetchConfig | ((ctx: Ctx, p1: P1, p2: P2, p3: P3) => ReatomFetchConfig), ) => AsyncAction<[P1, P2, P3], Result> export type ReatomFetch = ReatomFetchQuery & { @@ -49,14 +33,10 @@ export type ReatomFetch = ReatomFetchQuery & { patch: ReatomFetchQuery } -export const createReatomFetch = ( - clientConfig: Partial, object>>, -): ReatomFetch => { +export const createReatomFetch = (clientConfig: Partial, object>>): ReatomFetch => { clientConfig = { ...createReatomFetch.defaults, ...clientConfig } const query = ( - config: - | ReatomFetchConfig - | ((ctx: Ctx, p1: P1, p2: P2, p3: P3) => ReatomFetchConfig), + config: ReatomFetchConfig | ((ctx: Ctx, p1: P1, p2: P2, p3: P3) => ReatomFetchConfig), methodForce?: string, ): AsyncAction<[P1, P2, P3], Result> => reatomAsync(async (ctx, ...args: [P1, P2, P3]) => { @@ -68,30 +48,19 @@ export const createReatomFetch = ( } const { urlBase } = config - const url = new URL( - urlBase - ? urlBase.replace(/\/+$/, '') + '/' + config.url.replace(/^\/+/, '') - : config.url, - ) - if (config.params) - url.search = new URLSearchParams(config.params).toString() + const url = new URL(urlBase ? urlBase.replace(/\/+$/, '') + '/' + config.url.replace(/^\/+/, '') : config.url) + if (config.params) url.search = new URLSearchParams(config.params).toString() const headersBase = [...new Headers(config.headersBase)] const queryHeaders = new Headers(config.headers) - const headers = new Headers([ - ...new Headers(headersBase.filter((x) => !queryHeaders.has(x[0]))), - ...queryHeaders, - ]) + const headers = new Headers([...new Headers(headersBase.filter((x) => !queryHeaders.has(x[0]))), ...queryHeaders]) const response = await config.transport!(url.toString(), { ...(config.init ?? {}), signal: ctx.controller.signal, headers, method: methodForce ?? config.method, - body: - config.body === undefined - ? undefined - : await config.serializeBody!(config.body), + body: config.body === undefined ? undefined : await config.serializeBody!(config.body), }) return await config.parseResponse!(response) diff --git a/packages/web/CHANGELOG.md b/packages/web/CHANGELOG.md index a78461561..08ab797f3 100644 --- a/packages/web/CHANGELOG.md +++ b/packages/web/CHANGELOG.md @@ -2,22 +2,19 @@ ## [3.5.3](https://github.com/artalar/reatom/compare/web-v3.5.2...web-v3.5.3) (2024-06-22) - ### Bug Fixes -* **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) +- **npm-svelte:** republish without tag ([93c7f7f](https://github.com/artalar/reatom/commit/93c7f7f5ec58247b1b3aec854cd83b0a0ecd6a6c)) ## [3.5.2](https://github.com/artalar/reatom/compare/web-v3.5.1...web-v3.5.2) (2024-06-22) - ### Bug Fixes -* esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) -* **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- esm module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) +- **new-package-template:** use mjs in module export ([1011671](https://github.com/artalar/reatom/commit/10116719dd92d8102352a39e4ed772b8173d8668)) ## [3.5.1](https://github.com/artalar/reatom/compare/web-v3.5.0...web-v3.5.1) (2023-09-13) - ### Bug Fixes -* **logger:** filter changed state ([d9019a2](https://github.com/artalar/reatom/commit/d9019a2acb34bdd09d89aa0028754269d4024852)) +- **logger:** filter changed state ([d9019a2](https://github.com/artalar/reatom/commit/d9019a2acb34bdd09d89aa0028754269d4024852)) diff --git a/packages/web/README.md b/packages/web/README.md index 8c7ef1090..940038ff4 100644 --- a/packages/web/README.md +++ b/packages/web/README.md @@ -14,7 +14,7 @@ You can pass a callback as the last argument. In this case, the method will retu Please note that this API handles the abort context from the [onConnect](https://www.reatom.dev/package/hooks/#onconnect) effect and other Reatom APIs. It enables you to describe complex logic in a concise and clear manner with memory safety underneath. -### onEvent WebSocket example +### onEvent WebSocket example Here is a usage example, which was derived from [this observable example](https://github.com/domfarolino/observable/blob/c232b2e585b71a61034fd23ba4337570b537ef27/README.md?plain=1#L86): @@ -36,9 +36,7 @@ const reatomStock = (ticker) => { }) onEvent(ctx, socket, 'close', () => ctx.controller.abort()) onEvent(ctx, socket, 'error', () => ctx.controller.abort()) - onCtxAbort(ctx, () => - socket.send(JSON.stringify({ ticker, type: 'unsub' })), - ) + onCtxAbort(ctx, () => socket.send(JSON.stringify({ ticker, type: 'unsub' }))) }) return stockAtom @@ -50,6 +48,7 @@ ctx.subscribe(googStockAtom, updateView) ``` ## onEvent checkpoint example + Make sure to listen to event before you actually need it. As in [take](https://reatom.dev/package/effects/#take-checkpoints) you should use checkpoints to handle all events without skipping it. @@ -61,34 +60,34 @@ import { api } from '~/api' const heroElement = document.getElementById('#hero') -const loadPageContent = reatomAsync(async (ctx)=>{ - // Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/animate - const animation = heroElement.animate(heroAnimation) +const loadPageContent = reatomAsync(async (ctx) => { + // Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/animate + const animation = heroElement.animate(heroAnimation) - const content = await api.fetchContent() + const content = await api.fetchContent() - // ❌ Bug: - // If person's connection is not fast enough animation can finish before we load content. - // And we will be showing last frame of animation forever... - await onEvent(ctx, animation, 'finish') + // ❌ Bug: + // If person's connection is not fast enough animation can finish before we load content. + // And we will be showing last frame of animation forever... + await onEvent(ctx, animation, 'finish') - pageContent(ctx, content) + pageContent(ctx, content) }) ``` And that's how we fix this behaviour using checkpoint: ```ts -const loadPageContent = reatomAsync(async (ctx)=>{ - const animation = heroElement.animate(heroAnimation) - // βœ… We make a checkpoint before loading... - const animationFinishedCheckpoint = onEvent(ctx, animation, 'finish') - - const content = await api.fetchContent() - - // ...and we will catch that event even if content loading takes ages - await animationFinishedCheckpoint - - pageContent(ctx, content) +const loadPageContent = reatomAsync(async (ctx) => { + const animation = heroElement.animate(heroAnimation) + // βœ… We make a checkpoint before loading... + const animationFinishedCheckpoint = onEvent(ctx, animation, 'finish') + + const content = await api.fetchContent() + + // ...and we will catch that event even if content loading takes ages + await animationFinishedCheckpoint + + pageContent(ctx, content) }) ``` diff --git a/packages/web/src/event.ts b/packages/web/src/event.ts index 793dcf3f8..8df9c94a1 100644 --- a/packages/web/src/event.ts +++ b/packages/web/src/event.ts @@ -1,10 +1,10 @@ import { Ctx, Fn, Unsubscribe } from '@reatom/core' import { onCtxAbort } from '@reatom/effects' -export type EventOfTarget< - Target extends EventTarget, - Type extends string, -> = Target extends Record<`on${Type}`, infer Cb> +export type EventOfTarget = Target extends Record< + `on${Type}`, + infer Cb +> ? // @ts-expect-error `Cb extends Fn` broke the inference for some reason Parameters[0] // correct type : Target extends Record<'onEvent', (type: Type, cb: infer Cb) => any> @@ -14,30 +14,19 @@ export type EventOfTarget< // @ts-ignore export const onEvent: { - < - Target extends EventTarget, - Type extends Target extends Record<`on${infer Type}`, Fn> ? Type : string, - >( + ? Type : string>( ctx: Ctx, target: Target, type: Type, ): Promise> (ctx: Ctx, target: EventTarget, type: string): Promise - < - Target extends EventTarget, - Type extends Target extends Record<`on${infer Type}`, Fn> ? Type : string, - >( + ? Type : string>( ctx: Ctx, target: Target, type: Type, cb: (value: EventOfTarget) => any, ): Unsubscribe - ( - ctx: Ctx, - target: EventTarget, - type: string, - cb: (value: Event) => any, - ): Unsubscribe + (ctx: Ctx, target: EventTarget, type: string, cb: (value: Event) => any): Unsubscribe } = (ctx: Ctx, target: EventTarget, type: string, listener: Fn) => { let un if (!listener) { diff --git a/packages/web/src/fetch.ts b/packages/web/src/fetch.ts index 6f3cdd1f9..bbc81f18b 100644 --- a/packages/web/src/fetch.ts +++ b/packages/web/src/fetch.ts @@ -1,13 +1,8 @@ // THIS IS UNFINISHED PROTOTYPE, DO NOT TOUCH IT, DO NOT USE IT -export type UrlSearchParamsInit = ConstructorParameters< - typeof URLSearchParams ->[0] - -export interface FetchRequestInit< - Result = unknown, - Params extends any[] = any[], -> extends RequestInit { +export type UrlSearchParamsInit = ConstructorParameters[0] + +export interface FetchRequestInit extends RequestInit { url?: string | URL origin?: string transport?: typeof globalThis.fetch @@ -18,10 +13,7 @@ export interface FetchRequestInit< getResult?: (response: Response) => Result | Promise } -export class FetchRequest< - Result = unknown, - Params extends any[] = any[], -> extends Request { +export class FetchRequest extends Request { static defaults = { origin: globalThis.location?.toString(), @@ -39,9 +31,7 @@ export class FetchRequest< const ct = response.headers.get('Content-Type') if (ct !== 'application/json') { - throw new Error( - `Expected Content-Type to be "application/json", got "${ct}"`, - ) + throw new Error(`Expected Content-Type to be "application/json", got "${ct}"`) } return response.json() @@ -68,9 +58,7 @@ export class FetchRequest< return new this.__proto__.constructor(this.init) } - extends( - init: FetchRequestInit, - ): FetchRequest { + extends(init: FetchRequestInit): FetchRequest { return new FetchRequest({ ...this.init, ...init, @@ -89,10 +77,7 @@ export class FetchRequest< } init.body = - body && - typeof body === 'object' && - Reflect.getPrototypeOf(body) && - !Array.isArray(body) + body && typeof body === 'object' && Reflect.getPrototypeOf(body) && !Array.isArray(body) ? (body as BodyInit) : JSON.stringify(body) diff --git a/tools/actualize-issue-templates.ts b/tools/actualize-issue-templates.ts index fb0bf3bc1..776637329 100644 --- a/tools/actualize-issue-templates.ts +++ b/tools/actualize-issue-templates.ts @@ -28,8 +28,7 @@ function actualizePartsAvailable( packageParts: string[], ) { const block = issueTemplate?.body.find( - (bodyBlock: Record<'type' | 'id', string>) => - bodyBlock.type === 'dropdown' && bodyBlock.id === 'reatom-part', + (bodyBlock: Record<'type' | 'id', string>) => bodyBlock.type === 'dropdown' && bodyBlock.id === 'reatom-part', ) if (!block) return false @@ -42,36 +41,20 @@ const main = async () => { let packageNames = await getPackageNames() packageNames.sort() - const packageParts = packageNames.map( - (packageName) => `Package ${packageName}`, - ) + const packageParts = packageNames.map((packageName) => `Package ${packageName}`) const templateConfig: { nonPackageParts?: string[] } = - parse( - await fs.readFile( - path.join(process.cwd(), '.github', 'ISSUE_TEMPLATE', 'config.yaml'), - 'utf-8', - ), - )?.['x-reatom'] ?? {} + parse(await fs.readFile(path.join(process.cwd(), '.github', 'ISSUE_TEMPLATE', 'config.yaml'), 'utf-8'))?.[ + 'x-reatom' + ] ?? {} - const issueTemplateFiles = await fs.readdir( - path.join(process.cwd(), '.github', 'ISSUE_TEMPLATE'), - ) + const issueTemplateFiles = await fs.readdir(path.join(process.cwd(), '.github', 'ISSUE_TEMPLATE')) for (const issueTemplateFile of issueTemplateFiles) { if (issueTemplateFile === 'config.yaml') continue - const issueTemplatePath = path.join( - process.cwd(), - '.github', - 'ISSUE_TEMPLATE', - issueTemplateFile, - ) + const issueTemplatePath = path.join(process.cwd(), '.github', 'ISSUE_TEMPLATE', issueTemplateFile) const issueTemplate = parse(await fs.readFile(issueTemplatePath, 'utf-8')) - const changed = actualizePartsAvailable( - issueTemplate, - templateConfig, - packageParts, - ) + const changed = actualizePartsAvailable(issueTemplate, templateConfig, packageParts) if (changed) await fs.writeFile(issueTemplatePath, stringify(issueTemplate)) } } diff --git a/tools/new-package.ts b/tools/new-package.ts index 1565569a0..33c5596fe 100644 --- a/tools/new-package.ts +++ b/tools/new-package.ts @@ -9,19 +9,11 @@ const rl = readline.createInterface(process.stdin, process.stdout) $.verbose = false -const VERSION = JSON.parse( - fs.readFileSync( - path.join(process.cwd(), 'packages/core/package.json'), - 'utf-8', - ), -).version +const VERSION = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'packages/core/package.json'), 'utf-8')).version async function updateReleasePlease(pkgName: string) { const rpManifest = JSON.parse( - await fs.promises.readFile( - path.join(process.cwd(), '.release-please-manifest.json'), - 'utf-8', - ), + await fs.promises.readFile(path.join(process.cwd(), '.release-please-manifest.json'), 'utf-8'), ) rpManifest[`packages/${pkgName}`] = VERSION await fs.promises.writeFile( @@ -30,16 +22,10 @@ async function updateReleasePlease(pkgName: string) { ) const rpConfig = JSON.parse( - await fs.promises.readFile( - path.join(process.cwd(), 'release-please-config.json'), - 'utf-8', - ), + await fs.promises.readFile(path.join(process.cwd(), 'release-please-config.json'), 'utf-8'), ) rpConfig.packages[`packages/${pkgName}`] = {} - await fs.promises.writeFile( - path.join(process.cwd(), 'release-please-config.json'), - JSON.stringify(rpConfig, null, 2), - ) + await fs.promises.writeFile(path.join(process.cwd(), 'release-please-config.json'), JSON.stringify(rpConfig, null, 2)) } main() @@ -50,17 +36,12 @@ async function main() { } while (!pkgName) console.log(`ℹ️ The name is "${pkgName}"`) - let description = await rl.question( - '❓ How the package can be briefly described? ', - ) + let description = await rl.question('❓ How the package can be briefly described? ') description = description.trim() console.log(`ℹ️ The description is "${description}"`) - const authorNameDefault = - (await $`git config --get user.name`).stdout.trim() || 'artalar' - let authorName = await rl.question( - `❓ What is your GitHub username [${authorNameDefault}]? `, - ) + const authorNameDefault = (await $`git config --get user.name`).stdout.trim() || 'artalar' + let authorName = await rl.question(`❓ What is your GitHub username [${authorNameDefault}]? `) authorName = authorName.trim() || authorNameDefault console.log(`ℹ️ Author username is "${pkgName}"`) @@ -68,24 +49,16 @@ async function main() { await fs.promises.cp(templatePath, pkg, { recursive: true }) const pkgManifestPath = path.join(pkg, 'package.json') - const pkgManifest = JSON.parse( - await fs.promises.readFile(pkgManifestPath, 'utf-8'), - ) + const pkgManifest = JSON.parse(await fs.promises.readFile(pkgManifestPath, 'utf-8')) pkgManifest.name = `@reatom/${pkgName}` pkgManifest.version = VERSION if (description) pkgManifest.description = description pkgManifest.author = makePerson(authorName) - pkgManifest.maintainers = [ - makePerson('artalar'), - ...(authorName === 'artalar' ? [] : [makePerson(authorName)]), - ] + pkgManifest.maintainers = [makePerson('artalar'), ...(authorName === 'artalar' ? [] : [makePerson(authorName)])] pkgManifest.repository.directory = `packages/${pkgName}` pkgManifest.dependencies['@reatom/core'] = `>=${VERSION}` pkgManifest.homepage = `https://www.reatom.dev/package/${pkgName}` - await fs.promises.writeFile( - pkgManifestPath, - JSON.stringify(pkgManifest, null, '\t'), - ) + await fs.promises.writeFile(pkgManifestPath, JSON.stringify(pkgManifest, null, '\t')) const pkgReadmePath = path.join(pkg, 'README.md') let pkgReadme = await fs.promises.readFile(pkgReadmePath, 'utf8')