Comparing version 0.5.2 to 0.6.0
@@ -244,3 +244,3 @@ 'use strict'; | ||
var writeAtom = function writeAtom(updatingAtom, update, setState, dependentsMap, writeCache) { | ||
var writeAtom = function writeAtom(updatingAtom, update, dependentsMap, addWriteThunk) { | ||
var updateDependentsState = function updateDependentsState(prevState, atom) { | ||
@@ -263,3 +263,3 @@ var partialState = new Map(); | ||
}; | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
var nextState = new Map(prev).set(dependent, nextAtomState); | ||
@@ -270,3 +270,3 @@ var nextPartialState = updateDependentsState(nextState, dependent); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(dependent, { | ||
@@ -292,10 +292,3 @@ value: getAtomStateValue(prev, dependent), | ||
var updateAtomState = function updateAtomState(writeId, prevState, atom, update) { | ||
if (!writeCache.has(prevState)) { | ||
writeCache.set(prevState, new Map()); | ||
} | ||
var cache = writeCache.get(prevState); | ||
var hit = cache.get(writeId); | ||
if (hit) return hit; | ||
var updateAtomState = function updateAtomState(prevState, atom, update) { | ||
var partialState = new Map(); | ||
@@ -317,3 +310,3 @@ var isSync = true; | ||
} else { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
@@ -325,10 +318,8 @@ var nextPartialState = updateDependentsState(nextState, a); | ||
} else { | ||
var _newWriteId = Symbol(); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
var nextPartialState = updateAtomState(prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
addWriteThunk(function (prev) { | ||
var nextPartialState = updateAtomState(prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
@@ -344,3 +335,3 @@ }); | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(atom, { | ||
@@ -352,3 +343,3 @@ value: getAtomStateValue(prev, atom), | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(atom, { | ||
@@ -372,8 +363,6 @@ value: getAtomStateValue(prev, atom), | ||
isSync = false; | ||
cache.set(writeId, partialState); | ||
return partialState; | ||
}; | ||
var newWriteId = Symbol(); | ||
setState(function (prevState) { | ||
addWriteThunk(function (prevState) { | ||
var updatingAtomState = prevState.get(updatingAtom); | ||
@@ -384,4 +373,4 @@ | ||
var promise = updatingAtomState.promise.then(function () { | ||
var updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
setState(function (prev) { | ||
var updateState = updateAtomState(prevState, updatingAtom, update); | ||
addWriteThunk(function (prev) { | ||
return appendMap(new Map(prev), updateState); | ||
@@ -394,3 +383,3 @@ }); | ||
} else { | ||
var updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
var updateState = updateAtomState(prevState, updatingAtom, update); | ||
return appendMap(new Map(prevState), updateState); | ||
@@ -401,2 +390,23 @@ } | ||
var runWriteThunk = function runWriteThunk(lastStateRef, setState, writeThunkQueue) { | ||
while (true) { | ||
if (lastStateRef.current === null) { | ||
return; | ||
} | ||
if (writeThunkQueue.length === 0) { | ||
return; | ||
} | ||
var thunk = writeThunkQueue.shift(); | ||
var lastState = lastStateRef.current; | ||
var nextState = thunk(lastState); | ||
if (nextState !== lastState) { | ||
setState(nextState); | ||
return; | ||
} | ||
} | ||
}; | ||
var ActionsContext = useContextSelector.createContext(warningObject); | ||
@@ -409,4 +419,15 @@ var StateContext = useContextSelector.createContext(warningObject); | ||
state = _useState[0], | ||
setState = _useState[1]; | ||
setStateOrig = _useState[1]; | ||
var setState = function setState(setStateAction) { | ||
lastStateRef.current = null; | ||
setStateOrig(setStateAction); | ||
}; | ||
var lastStateRef = react.useRef(null); | ||
var writeThunkQueueRef = react.useRef([]); | ||
react.useEffect(function () { | ||
lastStateRef.current = state; | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}, [state]); | ||
var dependentsMapRef = react.useRef(); | ||
@@ -418,8 +439,2 @@ | ||
var writeCacheRef = react.useRef(); | ||
if (!writeCacheRef.current) { | ||
writeCacheRef.current = new WeakMap(); | ||
} | ||
var gcRequiredRef = react.useRef(false); | ||
@@ -446,3 +461,6 @@ react.useEffect(function () { | ||
write: function write(atom, update) { | ||
return writeAtom(atom, update, setState, dependentsMapRef.current, writeCacheRef.current); | ||
writeAtom(atom, update, dependentsMapRef.current, function (thunk) { | ||
writeThunkQueueRef.current.push(thunk); | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}); | ||
} | ||
@@ -449,0 +467,0 @@ }; |
@@ -240,3 +240,3 @@ var jotai = (function (exports, react, useContextSelector) { | ||
var writeAtom = function writeAtom(updatingAtom, update, setState, dependentsMap, writeCache) { | ||
var writeAtom = function writeAtom(updatingAtom, update, dependentsMap, addWriteThunk) { | ||
var updateDependentsState = function updateDependentsState(prevState, atom) { | ||
@@ -259,3 +259,3 @@ var partialState = new Map(); | ||
}; | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
var nextState = new Map(prev).set(dependent, nextAtomState); | ||
@@ -266,3 +266,3 @@ var nextPartialState = updateDependentsState(nextState, dependent); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(dependent, { | ||
@@ -288,10 +288,3 @@ value: getAtomStateValue(prev, dependent), | ||
var updateAtomState = function updateAtomState(writeId, prevState, atom, update) { | ||
if (!writeCache.has(prevState)) { | ||
writeCache.set(prevState, new Map()); | ||
} | ||
var cache = writeCache.get(prevState); | ||
var hit = cache.get(writeId); | ||
if (hit) return hit; | ||
var updateAtomState = function updateAtomState(prevState, atom, update) { | ||
var partialState = new Map(); | ||
@@ -313,3 +306,3 @@ var isSync = true; | ||
} else { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
@@ -321,10 +314,8 @@ var nextPartialState = updateDependentsState(nextState, a); | ||
} else { | ||
var _newWriteId = Symbol(); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
var nextPartialState = updateAtomState(prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
addWriteThunk(function (prev) { | ||
var nextPartialState = updateAtomState(prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
@@ -340,3 +331,3 @@ }); | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(atom, { | ||
@@ -348,3 +339,3 @@ value: getAtomStateValue(prev, atom), | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
addWriteThunk(function (prev) { | ||
return new Map(prev).set(atom, { | ||
@@ -368,8 +359,6 @@ value: getAtomStateValue(prev, atom), | ||
isSync = false; | ||
cache.set(writeId, partialState); | ||
return partialState; | ||
}; | ||
var newWriteId = Symbol(); | ||
setState(function (prevState) { | ||
addWriteThunk(function (prevState) { | ||
var updatingAtomState = prevState.get(updatingAtom); | ||
@@ -380,4 +369,4 @@ | ||
var promise = updatingAtomState.promise.then(function () { | ||
var updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
setState(function (prev) { | ||
var updateState = updateAtomState(prevState, updatingAtom, update); | ||
addWriteThunk(function (prev) { | ||
return appendMap(new Map(prev), updateState); | ||
@@ -390,3 +379,3 @@ }); | ||
} else { | ||
var updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
var updateState = updateAtomState(prevState, updatingAtom, update); | ||
return appendMap(new Map(prevState), updateState); | ||
@@ -397,2 +386,23 @@ } | ||
var runWriteThunk = function runWriteThunk(lastStateRef, setState, writeThunkQueue) { | ||
while (true) { | ||
if (lastStateRef.current === null) { | ||
return; | ||
} | ||
if (writeThunkQueue.length === 0) { | ||
return; | ||
} | ||
var thunk = writeThunkQueue.shift(); | ||
var lastState = lastStateRef.current; | ||
var nextState = thunk(lastState); | ||
if (nextState !== lastState) { | ||
setState(nextState); | ||
return; | ||
} | ||
} | ||
}; | ||
var ActionsContext = useContextSelector.createContext(warningObject); | ||
@@ -405,4 +415,15 @@ var StateContext = useContextSelector.createContext(warningObject); | ||
state = _useState[0], | ||
setState = _useState[1]; | ||
setStateOrig = _useState[1]; | ||
var setState = function setState(setStateAction) { | ||
lastStateRef.current = null; | ||
setStateOrig(setStateAction); | ||
}; | ||
var lastStateRef = react.useRef(null); | ||
var writeThunkQueueRef = react.useRef([]); | ||
react.useEffect(function () { | ||
lastStateRef.current = state; | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}, [state]); | ||
var dependentsMapRef = react.useRef(); | ||
@@ -414,8 +435,2 @@ | ||
var writeCacheRef = react.useRef(); | ||
if (!writeCacheRef.current) { | ||
writeCacheRef.current = new WeakMap(); | ||
} | ||
var gcRequiredRef = react.useRef(false); | ||
@@ -442,3 +457,6 @@ react.useEffect(function () { | ||
write: function write(atom, update) { | ||
return writeAtom(atom, update, setState, dependentsMapRef.current, writeCacheRef.current); | ||
writeAtom(atom, update, dependentsMapRef.current, function (thunk) { | ||
writeThunkQueueRef.current.push(thunk); | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}); | ||
} | ||
@@ -445,0 +463,0 @@ }; |
87
index.js
@@ -230,3 +230,3 @@ import { useState, useRef, useEffect, useMemo, createElement, useCallback, useDebugValue, useLayoutEffect } from 'react'; | ||
const writeAtom = (updatingAtom, update, setState, dependentsMap, writeCache) => { | ||
const writeAtom = (updatingAtom, update, dependentsMap, addWriteThunk) => { | ||
const updateDependentsState = (prevState, atom) => { | ||
@@ -249,3 +249,3 @@ const partialState = new Map(); | ||
}; | ||
setState(prev => { | ||
addWriteThunk(prev => { | ||
const nextState = new Map(prev).set(dependent, nextAtomState); | ||
@@ -256,3 +256,3 @@ const nextPartialState = updateDependentsState(nextState, dependent); | ||
}).catch(e => { | ||
setState(prev => new Map(prev).set(dependent, { | ||
addWriteThunk(prev => new Map(prev).set(dependent, { | ||
value: getAtomStateValue(prev, dependent), | ||
@@ -276,10 +276,3 @@ error: e instanceof Error ? e : new Error(e) | ||
const updateAtomState = (writeId, prevState, atom, update) => { | ||
if (!writeCache.has(prevState)) { | ||
writeCache.set(prevState, new Map()); | ||
} | ||
const cache = writeCache.get(prevState); | ||
const hit = cache.get(writeId); | ||
if (hit) return hit; | ||
const updateAtomState = (prevState, atom, update) => { | ||
const partialState = new Map(); | ||
@@ -299,3 +292,3 @@ let isSync = true; | ||
} else { | ||
setState(prev => { | ||
addWriteThunk(prev => { | ||
const nextState = new Map(prev).set(a, nextAtomState); | ||
@@ -307,10 +300,8 @@ const nextPartialState = updateDependentsState(nextState, a); | ||
} else { | ||
const newWriteId = Symbol(); | ||
if (isSync) { | ||
const nextPartialState = updateAtomState(newWriteId, prevState, a, v); | ||
const nextPartialState = updateAtomState(prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(prev => { | ||
const nextPartialState = updateAtomState(newWriteId, prev, a, v); | ||
addWriteThunk(prev => { | ||
const nextPartialState = updateAtomState(prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
@@ -326,3 +317,3 @@ }); | ||
promise: promise.then(() => { | ||
setState(prev => new Map(prev).set(atom, { | ||
addWriteThunk(prev => new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
@@ -332,3 +323,3 @@ promise: undefined | ||
}).catch(e => { | ||
setState(prev => new Map(prev).set(atom, { | ||
addWriteThunk(prev => new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
@@ -350,8 +341,6 @@ error: e instanceof Error ? e : new Error(e) | ||
isSync = false; | ||
cache.set(writeId, partialState); | ||
return partialState; | ||
}; | ||
const newWriteId = Symbol(); | ||
setState(prevState => { | ||
addWriteThunk(prevState => { | ||
const updatingAtomState = prevState.get(updatingAtom); | ||
@@ -362,4 +351,4 @@ | ||
const promise = updatingAtomState.promise.then(() => { | ||
const updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
setState(prev => appendMap(new Map(prev), updateState)); | ||
const updateState = updateAtomState(prevState, updatingAtom, update); | ||
addWriteThunk(prev => appendMap(new Map(prev), updateState)); | ||
}); | ||
@@ -370,3 +359,3 @@ return new Map(prevState).set(updatingAtom, _extends({}, updatingAtomState, { | ||
} else { | ||
const updateState = updateAtomState(newWriteId, prevState, updatingAtom, update); | ||
const updateState = updateAtomState(prevState, updatingAtom, update); | ||
return appendMap(new Map(prevState), updateState); | ||
@@ -377,2 +366,23 @@ } | ||
const runWriteThunk = (lastStateRef, setState, writeThunkQueue) => { | ||
while (true) { | ||
if (lastStateRef.current === null) { | ||
return; | ||
} | ||
if (writeThunkQueue.length === 0) { | ||
return; | ||
} | ||
const thunk = writeThunkQueue.shift(); | ||
const lastState = lastStateRef.current; | ||
const nextState = thunk(lastState); | ||
if (nextState !== lastState) { | ||
setState(nextState); | ||
return; | ||
} | ||
} | ||
}; | ||
const ActionsContext = createContext(warningObject); | ||
@@ -383,3 +393,15 @@ const StateContext = createContext(warningObject); | ||
}) => { | ||
const [state, setState] = useState(initialState); | ||
const [state, setStateOrig] = useState(initialState); | ||
const setState = setStateAction => { | ||
lastStateRef.current = null; | ||
setStateOrig(setStateAction); | ||
}; | ||
const lastStateRef = useRef(null); | ||
const writeThunkQueueRef = useRef([]); | ||
useEffect(() => { | ||
lastStateRef.current = state; | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}, [state]); | ||
const dependentsMapRef = useRef(); | ||
@@ -391,8 +413,2 @@ | ||
const writeCacheRef = useRef(); | ||
if (!writeCacheRef.current) { | ||
writeCacheRef.current = new WeakMap(); | ||
} | ||
const gcRequiredRef = useRef(false); | ||
@@ -411,3 +427,8 @@ useEffect(() => { | ||
read: (state, atom) => readAtom(state, atom, setState, dependentsMapRef.current), | ||
write: (atom, update) => writeAtom(atom, update, setState, dependentsMapRef.current, writeCacheRef.current) | ||
write: (atom, update) => { | ||
writeAtom(atom, update, dependentsMapRef.current, thunk => { | ||
writeThunkQueueRef.current.push(thunk); | ||
runWriteThunk(lastStateRef, setState, writeThunkQueueRef.current); | ||
}); | ||
} | ||
}), []); | ||
@@ -414,0 +435,0 @@ return createElement(ActionsContext.Provider, { |
{ | ||
"name": "jotai", | ||
"private": false, | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "👻 Next gen state management that will spook you", | ||
@@ -6,0 +6,0 @@ "main": "index.cjs.js", |
import { Atom, WritableAtom, NonPromise } from './types'; | ||
export declare function useAtom<Value, Update>(atom: WritableAtom<Value, Update>): [NonPromise<Value>, (update: Update) => void]; | ||
declare type SetAtom<Update> = [Update] extends [never] ? () => void : (update: Update) => void; | ||
export declare function useAtom<Value, Update>(atom: WritableAtom<Value, Update>): [NonPromise<Value>, SetAtom<Update>]; | ||
export declare function useAtom<Value>(atom: Atom<Value>): [NonPromise<Value>, never]; | ||
export {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59308
1470