Comparing version 0.15.1 to 0.15.2
13
index.js
@@ -71,2 +71,11 @@ 'use strict'; | ||
var concatMap = function concatMap(m1, m2) { | ||
// Map([...m1, ...m2]) alternative | ||
var newMap = new Map(m1); | ||
m2.forEach(function (value, key) { | ||
newMap.set(key, value); | ||
}); | ||
return newMap; | ||
}; | ||
var createState = function createState(initialValues, newAtomReceiver) { | ||
@@ -286,3 +295,3 @@ var currWip = new Map(); | ||
state.u(function (prev) { | ||
return setAtomValue(state, new Map([].concat(prev, wip)), atom, value, dependencies, promise); | ||
return setAtomValue(state, concatMap(prev, wip), atom, value, dependencies, promise); | ||
}); | ||
@@ -296,3 +305,3 @@ }).catch(function (e) { | ||
state.u(function (prev) { | ||
return setAtomReadError(state, new Map([].concat(prev, wip)), atom, e instanceof Error ? e : new Error(e), dependencies, promise); | ||
return setAtomReadError(state, concatMap(prev, wip), atom, e instanceof Error ? e : new Error(e), dependencies, promise); | ||
}); | ||
@@ -299,0 +308,0 @@ }); |
@@ -23,2 +23,11 @@ import { useRef, useState, useEffect, createContext, createElement, useCallback, useDebugValue, useContext } from 'react'; | ||
const concatMap = (m1, m2) => { | ||
// Map([...m1, ...m2]) alternative | ||
const newMap = new Map(m1); | ||
m2.forEach((value, key) => { | ||
newMap.set(key, value); | ||
}); | ||
return newMap; | ||
}; | ||
const createState = (initialValues, newAtomReceiver) => { | ||
@@ -222,3 +231,3 @@ let currWip = new Map(); | ||
promise = promiseOrValue.then(value => { | ||
state.u(prev => setAtomValue(state, new Map([...prev, ...wip]), atom, value, dependencies, promise)); | ||
state.u(prev => setAtomValue(state, concatMap(prev, wip), atom, value, dependencies, promise)); | ||
}).catch(e => { | ||
@@ -230,3 +239,3 @@ if (e instanceof Promise) { | ||
state.u(prev => setAtomReadError(state, new Map([...prev, ...wip]), atom, e instanceof Error ? e : new Error(e), dependencies, promise)); | ||
state.u(prev => setAtomReadError(state, concatMap(prev, wip), atom, e instanceof Error ? e : new Error(e), dependencies, promise)); | ||
}); | ||
@@ -233,0 +242,0 @@ } else { |
{ | ||
"name": "jotai", | ||
"private": false, | ||
"version": "0.15.1", | ||
"version": "0.15.2", | ||
"description": "👻 Next gen state management that will spook you", | ||
@@ -6,0 +6,0 @@ "module": "index.module.js", |
import { WritableAtom } from 'jotai'; | ||
import { RESET } from './atomWithReset'; | ||
export declare function useResetAtom<Value>(anAtom: WritableAtom<Value, typeof RESET>): (update?: unknown) => void | Promise<void>; | ||
export declare function useResetAtom<Value>(anAtom: WritableAtom<Value, typeof RESET>): () => void | Promise<void>; |
import { WritableAtom } from 'jotai'; | ||
export declare function useUpdateAtom<Value, Update>(anAtom: WritableAtom<Value, Update>): (update: Update) => void | Promise<void>; | ||
import { SetAtom } from '../core/types'; | ||
export declare function useUpdateAtom<Value, Update>(anAtom: WritableAtom<Value, Update>): SetAtom<Update>; |
16
utils.js
@@ -37,9 +37,11 @@ 'use strict'; | ||
function useResetAtom(anAtom) { | ||
var writeOnlyAtom = react.useMemo(function () { | ||
return jotai.atom(null, function (_get, set, _update) { | ||
return set(anAtom, RESET); | ||
}); | ||
}, [anAtom]); | ||
writeOnlyAtom.scope = anAtom.scope; | ||
return jotai.useAtom(writeOnlyAtom)[1]; | ||
var StoreContext = jotai.SECRET_INTERNAL_getStoreContext(anAtom.scope); | ||
var _useContext = react.useContext(StoreContext), | ||
updateAtom = _useContext[1]; | ||
var setAtom = react.useCallback(function () { | ||
return updateAtom(anAtom, RESET); | ||
}, [updateAtom, anAtom]); | ||
return setAtom; | ||
} | ||
@@ -46,0 +48,0 @@ |
@@ -28,5 +28,6 @@ import { useContext, useCallback, useMemo } from 'react'; | ||
function useResetAtom(anAtom) { | ||
const writeOnlyAtom = useMemo(() => atom(null, (_get, set, _update) => set(anAtom, RESET)), [anAtom]); | ||
writeOnlyAtom.scope = anAtom.scope; | ||
return useAtom(writeOnlyAtom)[1]; | ||
const StoreContext = SECRET_INTERNAL_getStoreContext(anAtom.scope); | ||
const [, updateAtom] = useContext(StoreContext); | ||
const setAtom = useCallback(() => updateAtom(anAtom, RESET), [updateAtom, anAtom]); | ||
return setAtom; | ||
} | ||
@@ -33,0 +34,0 @@ |
@@ -1,3 +0,3 @@ | ||
import { WritableAtom } from 'jotai'; | ||
import type { WritableAtom } from 'jotai'; | ||
import { RESET } from './atomWithReset'; | ||
export declare function useResetAtom<Value>(anAtom: WritableAtom<Value, typeof RESET>): (update?: unknown) => void | Promise<void>; | ||
export declare function useResetAtom<Value>(anAtom: WritableAtom<Value, typeof RESET>): () => void | Promise<void>; |
import type { WritableAtom } from 'jotai'; | ||
export declare function useUpdateAtom<Value, Update>(anAtom: WritableAtom<Value, Update>): (update: Update) => void | Promise<void>; | ||
import type { SetAtom } from '../core/types'; | ||
export declare function useUpdateAtom<Value, Update>(anAtom: WritableAtom<Value, Update>): SetAtom<Update>; |
124731
3094