Comparing version 0.4.1 to 0.5.0
225
index.cjs.js
@@ -26,4 +26,2 @@ 'use strict'; | ||
var isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
var useIsoLayoutEffect = isClient ? react.useLayoutEffect : react.useEffect; | ||
var appendMap = function appendMap(dst, src) { | ||
@@ -34,3 +32,5 @@ src.forEach(function (v, k) { | ||
return dst; | ||
}; | ||
}; // create new map from two maps | ||
var concatMap = function concatMap(src1, src2) { | ||
@@ -85,12 +85,2 @@ var dst = new Map(); | ||
var getAtomState = function getAtomState(state, atom) { | ||
var atomState = state.get(atom); | ||
if (!atomState) { | ||
throw new Error('atom is not initialized'); | ||
} | ||
return atomState; | ||
}; | ||
var getAtomStateValue = function getAtomStateValue(state, atom) { | ||
@@ -111,6 +101,7 @@ var atomState = state.get(atom); | ||
if (atomState) { | ||
return [atomState.promise, atomState.value, partialState]; | ||
return [atomState, partialState]; | ||
} | ||
var promise = null; | ||
var error = undefined; | ||
var promise = undefined; | ||
var value = null; | ||
@@ -123,5 +114,4 @@ var isSync = true; | ||
var _readAtomValue = readAtomValue(prevState, a, atom), | ||
nextPromise = _readAtomValue[0], | ||
nextValue = _readAtomValue[1], | ||
nextPartialState = _readAtomValue[2]; | ||
_nextAtomState = _readAtomValue[0], | ||
nextPartialState = _readAtomValue[1]; | ||
@@ -136,7 +126,11 @@ if (isSync) { | ||
if (nextPromise) { | ||
throw nextPromise; | ||
if (_nextAtomState.error) { | ||
throw _nextAtomState.error; | ||
} | ||
return nextValue; | ||
if (_nextAtomState.promise) { | ||
throw _nextAtomState.promise; | ||
} | ||
return _nextAtomState.value; | ||
} // primitive atom | ||
@@ -162,6 +156,12 @@ | ||
return new Map(prev).set(atom, { | ||
promise: null, | ||
value: value | ||
}); | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}); | ||
@@ -174,16 +174,17 @@ } else { | ||
promise = errorOrPromise; | ||
} else if (errorOrPromise instanceof Error) { | ||
error = errorOrPromise; | ||
} else { | ||
throw errorOrPromise; | ||
error = new Error(errorOrPromise); | ||
} | ||
} | ||
partialState.set(atom, promise ? { | ||
var nextAtomState = { | ||
error: error, | ||
promise: promise, | ||
value: atom.init | ||
} : { | ||
promise: null, | ||
value: value | ||
}); | ||
value: promise ? atom.init : value | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
isSync = false; | ||
return [promise, value, partialState]; | ||
return [nextAtomState, partialState]; | ||
}; | ||
@@ -267,3 +268,2 @@ | ||
var nextAtomState = { | ||
promise: null, | ||
value: vv | ||
@@ -276,9 +276,16 @@ }; | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(dependent, { | ||
value: getAtomStateValue(prev, dependent), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}); | ||
partialState.set(dependent, _extends({}, getAtomState(prevState, dependent), { | ||
partialState.set(dependent, { | ||
value: getAtomStateValue(prevState, dependent), | ||
promise: promise | ||
})); | ||
}); | ||
} else { | ||
partialState.set(dependent, { | ||
promise: null, | ||
value: v | ||
@@ -302,48 +309,64 @@ }); | ||
var isSync = true; | ||
var promise = atom.write(function (a) { | ||
return getAtomStateValue(concatMap(prevState, partialState), a); | ||
}, function (a, v) { | ||
if (a === atom) { | ||
var nextAtomState = { | ||
promise: null, | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
try { | ||
var promise = atom.write(function (a) { | ||
return getAtomStateValue(concatMap(prevState, partialState), a); | ||
}, function (a, v) { | ||
if (a === atom) { | ||
var nextAtomState = { | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
} else { | ||
setState(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
var nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
} | ||
} else { | ||
setState(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
var nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
var _newWriteId = Symbol(); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
} | ||
} else { | ||
var _newWriteId = Symbol(); | ||
}, update); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
if (promise instanceof Promise) { | ||
var nextAtomState = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
promise: undefined | ||
}); | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}) | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
} | ||
}, update); | ||
if (promise instanceof Promise) { | ||
var nextAtomState = _extends({}, getAtomState(prevState, atom), { | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, _extends({}, getAtomState(prev, atom), { | ||
promise: null | ||
})); | ||
}); | ||
}) | ||
}); | ||
partialState.set(atom, nextAtomState); | ||
} catch (e) { | ||
var _nextAtomState2 = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}; | ||
partialState.set(atom, _nextAtomState2); | ||
} | ||
@@ -455,2 +478,5 @@ | ||
var isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
var useIsoLayoutEffect = isClient ? react.useLayoutEffect : react.useEffect; | ||
var isWritable = function isWritable(atom) { | ||
@@ -462,31 +488,46 @@ return !!atom.write; | ||
var actions = useContextSelector.useContext(ActionsContext); | ||
var pendingPartialStateRef = react.useRef(); | ||
pendingPartialStateRef.current = undefined; | ||
var pendingListRef = react.useRef([]); | ||
var value = useContextSelector.useContextSelector(StateContext, react.useCallback(function (state) { | ||
var atomState = state.get(atom); | ||
if (atomState) { | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
if (!atomState) { | ||
var _actions$read = actions.read(state, atom), | ||
initialAtomState = _actions$read[0], | ||
pendingPartialState = _actions$read[1]; | ||
atomState = initialAtomState; | ||
if (!atomState.error && !atomState.promise && pendingPartialState.size) { | ||
pendingListRef.current.unshift({ | ||
v: initialAtomState.value, | ||
p: pendingPartialState | ||
}); | ||
} | ||
return atomState.value; | ||
} | ||
var _actions$read = actions.read(state, atom), | ||
initialPromise = _actions$read[0], | ||
initialValue = _actions$read[1], | ||
pendingPartialState = _actions$read[2]; | ||
if (initialPromise) { | ||
throw initialPromise; | ||
if (atomState.error) { | ||
throw atomState.error; | ||
} | ||
if (pendingPartialState.size) { | ||
pendingPartialStateRef.current = pendingPartialState; | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
} | ||
return initialValue; | ||
return atomState.value; | ||
}, [atom, actions])); | ||
var pendingPartialStateRef = react.useRef(); | ||
useIsoLayoutEffect(function () { | ||
var pendingList = pendingListRef.current; | ||
var found = pendingList.find(function (_ref) { | ||
var v = _ref.v; | ||
return v === value; | ||
}); | ||
if (found) { | ||
pendingPartialStateRef.current = found.p; | ||
} | ||
pendingList.splice(0, pendingList.length); | ||
}); | ||
useIsoLayoutEffect(function () { | ||
var id = Symbol(); | ||
@@ -493,0 +534,0 @@ actions.add(id, atom, pendingPartialStateRef.current); |
@@ -22,4 +22,2 @@ var jotai = (function (exports, react, useContextSelector) { | ||
var isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
var useIsoLayoutEffect = isClient ? react.useLayoutEffect : react.useEffect; | ||
var appendMap = function appendMap(dst, src) { | ||
@@ -30,3 +28,5 @@ src.forEach(function (v, k) { | ||
return dst; | ||
}; | ||
}; // create new map from two maps | ||
var concatMap = function concatMap(src1, src2) { | ||
@@ -81,12 +81,2 @@ var dst = new Map(); | ||
var getAtomState = function getAtomState(state, atom) { | ||
var atomState = state.get(atom); | ||
if (!atomState) { | ||
throw new Error('atom is not initialized'); | ||
} | ||
return atomState; | ||
}; | ||
var getAtomStateValue = function getAtomStateValue(state, atom) { | ||
@@ -107,6 +97,7 @@ var atomState = state.get(atom); | ||
if (atomState) { | ||
return [atomState.promise, atomState.value, partialState]; | ||
return [atomState, partialState]; | ||
} | ||
var promise = null; | ||
var error = undefined; | ||
var promise = undefined; | ||
var value = null; | ||
@@ -119,5 +110,4 @@ var isSync = true; | ||
var _readAtomValue = readAtomValue(prevState, a, atom), | ||
nextPromise = _readAtomValue[0], | ||
nextValue = _readAtomValue[1], | ||
nextPartialState = _readAtomValue[2]; | ||
_nextAtomState = _readAtomValue[0], | ||
nextPartialState = _readAtomValue[1]; | ||
@@ -132,7 +122,11 @@ if (isSync) { | ||
if (nextPromise) { | ||
throw nextPromise; | ||
if (_nextAtomState.error) { | ||
throw _nextAtomState.error; | ||
} | ||
return nextValue; | ||
if (_nextAtomState.promise) { | ||
throw _nextAtomState.promise; | ||
} | ||
return _nextAtomState.value; | ||
} // primitive atom | ||
@@ -158,6 +152,12 @@ | ||
return new Map(prev).set(atom, { | ||
promise: null, | ||
value: value | ||
}); | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}); | ||
@@ -170,16 +170,17 @@ } else { | ||
promise = errorOrPromise; | ||
} else if (errorOrPromise instanceof Error) { | ||
error = errorOrPromise; | ||
} else { | ||
throw errorOrPromise; | ||
error = new Error(errorOrPromise); | ||
} | ||
} | ||
partialState.set(atom, promise ? { | ||
var nextAtomState = { | ||
error: error, | ||
promise: promise, | ||
value: atom.init | ||
} : { | ||
promise: null, | ||
value: value | ||
}); | ||
value: promise ? atom.init : value | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
isSync = false; | ||
return [promise, value, partialState]; | ||
return [nextAtomState, partialState]; | ||
}; | ||
@@ -263,3 +264,2 @@ | ||
var nextAtomState = { | ||
promise: null, | ||
value: vv | ||
@@ -272,9 +272,16 @@ }; | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(dependent, { | ||
value: getAtomStateValue(prev, dependent), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}); | ||
partialState.set(dependent, _extends({}, getAtomState(prevState, dependent), { | ||
partialState.set(dependent, { | ||
value: getAtomStateValue(prevState, dependent), | ||
promise: promise | ||
})); | ||
}); | ||
} else { | ||
partialState.set(dependent, { | ||
promise: null, | ||
value: v | ||
@@ -298,48 +305,64 @@ }); | ||
var isSync = true; | ||
var promise = atom.write(function (a) { | ||
return getAtomStateValue(concatMap(prevState, partialState), a); | ||
}, function (a, v) { | ||
if (a === atom) { | ||
var nextAtomState = { | ||
promise: null, | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
try { | ||
var promise = atom.write(function (a) { | ||
return getAtomStateValue(concatMap(prevState, partialState), a); | ||
}, function (a, v) { | ||
if (a === atom) { | ||
var nextAtomState = { | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
} else { | ||
setState(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
var nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
} | ||
} else { | ||
setState(function (prev) { | ||
var nextState = new Map(prev).set(a, nextAtomState); | ||
var nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
var _newWriteId = Symbol(); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
} | ||
} else { | ||
var _newWriteId = Symbol(); | ||
}, update); | ||
if (isSync) { | ||
var nextPartialState = updateAtomState(_newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(function (prev) { | ||
var nextPartialState = updateAtomState(_newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
if (promise instanceof Promise) { | ||
var nextAtomState = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
promise: undefined | ||
}); | ||
}); | ||
}).catch(function (e) { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}); | ||
}); | ||
}) | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
} | ||
}, update); | ||
if (promise instanceof Promise) { | ||
var nextAtomState = _extends({}, getAtomState(prevState, atom), { | ||
promise: promise.then(function () { | ||
setState(function (prev) { | ||
return new Map(prev).set(atom, _extends({}, getAtomState(prev, atom), { | ||
promise: null | ||
})); | ||
}); | ||
}) | ||
}); | ||
partialState.set(atom, nextAtomState); | ||
} catch (e) { | ||
var _nextAtomState2 = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}; | ||
partialState.set(atom, _nextAtomState2); | ||
} | ||
@@ -451,2 +474,5 @@ | ||
var isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
var useIsoLayoutEffect = isClient ? react.useLayoutEffect : react.useEffect; | ||
var isWritable = function isWritable(atom) { | ||
@@ -458,31 +484,46 @@ return !!atom.write; | ||
var actions = useContextSelector.useContext(ActionsContext); | ||
var pendingPartialStateRef = react.useRef(); | ||
pendingPartialStateRef.current = undefined; | ||
var pendingListRef = react.useRef([]); | ||
var value = useContextSelector.useContextSelector(StateContext, react.useCallback(function (state) { | ||
var atomState = state.get(atom); | ||
if (atomState) { | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
if (!atomState) { | ||
var _actions$read = actions.read(state, atom), | ||
initialAtomState = _actions$read[0], | ||
pendingPartialState = _actions$read[1]; | ||
atomState = initialAtomState; | ||
if (!atomState.error && !atomState.promise && pendingPartialState.size) { | ||
pendingListRef.current.unshift({ | ||
v: initialAtomState.value, | ||
p: pendingPartialState | ||
}); | ||
} | ||
return atomState.value; | ||
} | ||
var _actions$read = actions.read(state, atom), | ||
initialPromise = _actions$read[0], | ||
initialValue = _actions$read[1], | ||
pendingPartialState = _actions$read[2]; | ||
if (initialPromise) { | ||
throw initialPromise; | ||
if (atomState.error) { | ||
throw atomState.error; | ||
} | ||
if (pendingPartialState.size) { | ||
pendingPartialStateRef.current = pendingPartialState; | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
} | ||
return initialValue; | ||
return atomState.value; | ||
}, [atom, actions])); | ||
var pendingPartialStateRef = react.useRef(); | ||
useIsoLayoutEffect(function () { | ||
var pendingList = pendingListRef.current; | ||
var found = pendingList.find(function (_ref) { | ||
var v = _ref.v; | ||
return v === value; | ||
}); | ||
if (found) { | ||
pendingPartialStateRef.current = found.p; | ||
} | ||
pendingList.splice(0, pendingList.length); | ||
}); | ||
useIsoLayoutEffect(function () { | ||
var id = Symbol(); | ||
@@ -489,0 +530,0 @@ actions.add(id, atom, pendingPartialStateRef.current); |
203
index.js
@@ -1,2 +0,2 @@ | ||
import { useLayoutEffect, useEffect, useState, useRef, useMemo, createElement, useCallback, useDebugValue } from 'react'; | ||
import { useState, useRef, useEffect, useMemo, createElement, useCallback, useDebugValue, useLayoutEffect } from 'react'; | ||
import { createContext, useContext, useContextSelector } from 'use-context-selector'; | ||
@@ -22,4 +22,2 @@ | ||
const isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
const useIsoLayoutEffect = isClient ? useLayoutEffect : useEffect; | ||
const appendMap = (dst, src) => { | ||
@@ -30,3 +28,5 @@ src.forEach((v, k) => { | ||
return dst; | ||
}; | ||
}; // create new map from two maps | ||
const concatMap = (src1, src2) => { | ||
@@ -83,12 +83,2 @@ const dst = new Map(); | ||
const getAtomState = (state, atom) => { | ||
const atomState = state.get(atom); | ||
if (!atomState) { | ||
throw new Error('atom is not initialized'); | ||
} | ||
return atomState; | ||
}; | ||
const getAtomStateValue = (state, atom) => { | ||
@@ -109,6 +99,7 @@ const atomState = state.get(atom); | ||
if (atomState) { | ||
return [atomState.promise, atomState.value, partialState]; | ||
return [atomState, partialState]; | ||
} | ||
let promise = null; | ||
let error = undefined; | ||
let promise = undefined; | ||
let value = null; | ||
@@ -120,3 +111,3 @@ let isSync = true; | ||
if (a !== atom) { | ||
const [nextPromise, nextValue, nextPartialState] = readAtomValue(prevState, a, atom); | ||
const [nextAtomState, nextPartialState] = readAtomValue(prevState, a, atom); | ||
@@ -129,7 +120,11 @@ if (isSync) { | ||
if (nextPromise) { | ||
throw nextPromise; | ||
if (nextAtomState.error) { | ||
throw nextAtomState.error; | ||
} | ||
return nextValue; | ||
if (nextAtomState.promise) { | ||
throw nextAtomState.promise; | ||
} | ||
return nextAtomState.value; | ||
} // primitive atom | ||
@@ -154,5 +149,9 @@ | ||
setState(prev => new Map(prev).set(atom, { | ||
promise: null, | ||
value | ||
})); | ||
}).catch(e => { | ||
setState(prev => new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
})); | ||
}); | ||
@@ -165,16 +164,17 @@ } else { | ||
promise = errorOrPromise; | ||
} else if (errorOrPromise instanceof Error) { | ||
error = errorOrPromise; | ||
} else { | ||
throw errorOrPromise; | ||
error = new Error(errorOrPromise); | ||
} | ||
} | ||
partialState.set(atom, promise ? { | ||
const nextAtomState = { | ||
error, | ||
promise, | ||
value: atom.init | ||
} : { | ||
promise: null, | ||
value | ||
}); | ||
value: promise ? atom.init : value | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
isSync = false; | ||
return [promise, value, partialState]; | ||
return [nextAtomState, partialState]; | ||
}; | ||
@@ -254,3 +254,2 @@ | ||
const nextAtomState = { | ||
promise: null, | ||
value: vv | ||
@@ -263,9 +262,14 @@ }; | ||
}); | ||
}).catch(e => { | ||
setState(prev => new Map(prev).set(dependent, { | ||
value: getAtomStateValue(prev, dependent), | ||
error: e instanceof Error ? e : new Error(e) | ||
})); | ||
}); | ||
partialState.set(dependent, _extends({}, getAtomState(prevState, dependent), { | ||
partialState.set(dependent, { | ||
value: getAtomStateValue(prevState, dependent), | ||
promise | ||
})); | ||
}); | ||
} else { | ||
partialState.set(dependent, { | ||
promise: null, | ||
value: v | ||
@@ -289,43 +293,57 @@ }); | ||
let isSync = true; | ||
const promise = atom.write(a => getAtomStateValue(concatMap(prevState, partialState), a), (a, v) => { | ||
if (a === atom) { | ||
const nextAtomState = { | ||
promise: null, | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
try { | ||
const promise = atom.write(a => getAtomStateValue(concatMap(prevState, partialState), a), (a, v) => { | ||
if (a === atom) { | ||
const nextAtomState = { | ||
value: v | ||
}; | ||
if (isSync) { | ||
partialState.set(a, nextAtomState); | ||
appendMap(partialState, updateDependentsState(concatMap(prevState, partialState), a)); | ||
} else { | ||
setState(prev => { | ||
const nextState = new Map(prev).set(a, nextAtomState); | ||
const nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
} | ||
} else { | ||
setState(prev => { | ||
const nextState = new Map(prev).set(a, nextAtomState); | ||
const nextPartialState = updateDependentsState(nextState, a); | ||
return appendMap(nextState, nextPartialState); | ||
}); | ||
const newWriteId = Symbol(); | ||
if (isSync) { | ||
const nextPartialState = updateAtomState(newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(prev => { | ||
const nextPartialState = updateAtomState(newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
} | ||
} else { | ||
const newWriteId = Symbol(); | ||
}, update); | ||
if (isSync) { | ||
const nextPartialState = updateAtomState(newWriteId, prevState, a, v); | ||
appendMap(partialState, nextPartialState); | ||
} else { | ||
setState(prev => { | ||
const nextPartialState = updateAtomState(newWriteId, prev, a, v); | ||
return appendMap(new Map(prev), nextPartialState); | ||
}); | ||
} | ||
if (promise instanceof Promise) { | ||
const nextAtomState = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
promise: promise.then(() => { | ||
setState(prev => new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
promise: undefined | ||
})); | ||
}).catch(e => { | ||
setState(prev => new Map(prev).set(atom, { | ||
value: getAtomStateValue(prev, atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
})); | ||
}) | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
} | ||
}, update); | ||
if (promise instanceof Promise) { | ||
const nextAtomState = _extends({}, getAtomState(prevState, atom), { | ||
promise: promise.then(() => { | ||
setState(prev => new Map(prev).set(atom, _extends({}, getAtomState(prev, atom), { | ||
promise: null | ||
}))); | ||
}) | ||
}); | ||
} catch (e) { | ||
const nextAtomState = { | ||
value: getAtomStateValue(concatMap(prevState, partialState), atom), | ||
error: e instanceof Error ? e : new Error(e) | ||
}; | ||
partialState.set(atom, nextAtomState); | ||
@@ -421,2 +439,5 @@ } | ||
const isClient = typeof window !== 'undefined' && !/ServerSideRendering/.test(window.navigator && window.navigator.userAgent); | ||
const useIsoLayoutEffect = isClient ? useLayoutEffect : useEffect; | ||
const isWritable = atom => !!atom.write; | ||
@@ -426,28 +447,42 @@ | ||
const actions = useContext(ActionsContext); | ||
const pendingPartialStateRef = useRef(); | ||
pendingPartialStateRef.current = undefined; | ||
const pendingListRef = useRef([]); | ||
const value = useContextSelector(StateContext, useCallback(state => { | ||
const atomState = state.get(atom); | ||
let atomState = state.get(atom); | ||
if (atomState) { | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
if (!atomState) { | ||
const [initialAtomState, pendingPartialState] = actions.read(state, atom); | ||
atomState = initialAtomState; | ||
if (!atomState.error && !atomState.promise && pendingPartialState.size) { | ||
pendingListRef.current.unshift({ | ||
v: initialAtomState.value, | ||
p: pendingPartialState | ||
}); | ||
} | ||
return atomState.value; | ||
} | ||
const [initialPromise, initialValue, pendingPartialState] = actions.read(state, atom); | ||
if (initialPromise) { | ||
throw initialPromise; | ||
if (atomState.error) { | ||
throw atomState.error; | ||
} | ||
if (pendingPartialState.size) { | ||
pendingPartialStateRef.current = pendingPartialState; | ||
if (atomState.promise) { | ||
throw atomState.promise; | ||
} | ||
return initialValue; | ||
return atomState.value; | ||
}, [atom, actions])); | ||
const pendingPartialStateRef = useRef(); | ||
useIsoLayoutEffect(() => { | ||
const pendingList = pendingListRef.current; | ||
const found = pendingList.find(({ | ||
v | ||
}) => v === value); | ||
if (found) { | ||
pendingPartialStateRef.current = found.p; | ||
} | ||
pendingList.splice(0, pendingList.length); | ||
}); | ||
useIsoLayoutEffect(() => { | ||
const id = Symbol(); | ||
@@ -454,0 +489,0 @@ actions.add(id, atom, pendingPartialStateRef.current); |
{ | ||
"name": "jotai", | ||
"private": false, | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "👻 Next gen state management that will spook you", | ||
"main": "index.cjs.js", | ||
"module": "index.js", | ||
"types": "src/index.d.ts", | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -10,0 +10,0 @@ "**" |
@@ -15,3 +15,4 @@ <p align="center"> | ||
You can try a live demo [here](https://codesandbox.io/s/jotai-demo-47wvh). | ||
You can try a live demo [here](https://codesandbox.io/s/jotai-demo-47wvh) | ||
and [there](https://codesandbox.io/s/jotai-demo-forked-x2g5d). | ||
@@ -18,0 +19,0 @@ #### How does Jotai differ from Recoil? |
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
55658
1372
172
13