UNPKG

jotai

Version:

👻 Next gen state management that will spook you

1,040 lines (853 loc) • 26.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var jotai = require('jotai'); var react = require('react'); var RESET = Symbol(); function atomWithReset(initialValue) { var anAtom = jotai.atom(initialValue, function (get, set, update) { if (update === RESET) { set(anAtom, initialValue); } else { set(anAtom, typeof update === 'function' ? update(get(anAtom)) : update); } }); return anAtom; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var WRITE_ATOM = 'w'; var RESTORE_ATOMS = 'h'; function useResetAtom(anAtom, scope) { var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope); var store = react.useContext(ScopeContext).s; var setAtom = react.useCallback(function () { return store[WRITE_ATOM](anAtom, RESET); }, [store, anAtom]); return setAtom; } function useReducerAtom(anAtom, reducer, scope) { var _useAtom = jotai.useAtom(anAtom, scope), state = _useAtom[0], setState = _useAtom[1]; var dispatch = react.useCallback(function (action) { setState(function (prev) { return reducer(prev, action); }); }, [setState, reducer]); return [state, dispatch]; } function atomWithReducer(initialValue, reducer) { var anAtom = jotai.atom(initialValue, function (get, set, action) { return set(anAtom, reducer(get(anAtom), action)); }); return anAtom; } function atomFamily(initializeAtom, areEqual) { var shouldRemove = null; var atoms = new Map(); var createAtom = function createAtom(param) { var item; if (areEqual === undefined) { item = atoms.get(param); } else { for (var _iterator = _createForOfIteratorHelperLoose(atoms), _step; !(_step = _iterator()).done;) { var _step$value = _step.value, key = _step$value[0], value = _step$value[1]; if (areEqual(key, param)) { item = value; break; } } } if (item !== undefined) { if (shouldRemove != null && shouldRemove(item[1], param)) { atoms.delete(param); } else { return item[0]; } } var newAtom = initializeAtom(param); atoms.set(param, [newAtom, Date.now()]); return newAtom; }; createAtom.remove = function (param) { if (areEqual === undefined) { atoms.delete(param); } else { for (var _iterator2 = _createForOfIteratorHelperLoose(atoms), _step2; !(_step2 = _iterator2()).done;) { var _step2$value = _step2.value, key = _step2$value[0]; if (areEqual(key, param)) { atoms.delete(key); break; } } } }; createAtom.setShouldRemove = function (fn) { shouldRemove = fn; if (!shouldRemove) return; for (var _iterator3 = _createForOfIteratorHelperLoose(atoms), _step3; !(_step3 = _iterator3()).done;) { var _step3$value = _step3.value, key = _step3$value[0], value = _step3$value[1]; if (shouldRemove(value[1], key)) { atoms.delete(key); } } }; return createAtom; } var getWeakCacheItem = function getWeakCacheItem(cache, deps) { do { var _deps = deps, dep = _deps[0], rest = _deps.slice(1); var entry = cache.get(dep); if (!entry) { return; } if (!rest.length) { return entry[1]; } cache = entry[0]; deps = rest; } while (deps.length); }; var setWeakCacheItem = function setWeakCacheItem(cache, deps, item) { do { var _deps2 = deps, dep = _deps2[0], rest = _deps2.slice(1); var entry = cache.get(dep); if (!entry) { entry = [new WeakMap()]; cache.set(dep, entry); } if (!rest.length) { entry[1] = item; return; } cache = entry[0]; deps = rest; } while (deps.length); }; var createMemoizeAtom = function createMemoizeAtom() { var cache = new WeakMap(); var memoizeAtom = function memoizeAtom(createAtom, deps) { var cachedAtom = getWeakCacheItem(cache, deps); if (cachedAtom) { return cachedAtom; } var createdAtom = createAtom(); setWeakCacheItem(cache, deps, createdAtom); return createdAtom; }; return memoizeAtom; }; var memoizeAtom$4 = createMemoizeAtom(); function selectAtom(anAtom, selector, equalityFn) { if (equalityFn === void 0) { equalityFn = Object.is; } return memoizeAtom$4(function () { var refAtom = jotai.atom(function () { return {}; }); var derivedAtom = jotai.atom(function (get) { var slice = selector(get(anAtom)); var ref = get(refAtom); if ('prev' in ref && equalityFn(ref.prev, slice)) { return ref.prev; } ref.prev = slice; return slice; }); return derivedAtom; }, [anAtom, selector, equalityFn]); } function useAtomCallback(callback, scope) { var anAtom = react.useMemo(function () { return jotai.atom(null, function (get, set, _ref) { var arg = _ref[0], resolve = _ref[1], reject = _ref[2]; try { resolve(callback(get, set, arg)); } catch (e) { reject(e); } }); }, [callback]); var invoke = jotai.useSetAtom(anAtom, scope); return react.useCallback(function (arg) { var isSync = true; var settled = {}; var promise = new Promise(function (resolve, reject) { invoke([arg, function (v) { if (isSync) { settled = { v: v }; } else { resolve(v); } }, function (e) { if (isSync) { settled = { e: e }; } else { reject(e); } }]); }); isSync = false; if ('e' in settled) { throw settled.e; } if ('v' in settled) { return settled.v; } return promise; }, [invoke]); } var memoizeAtom$3 = createMemoizeAtom(); var deepFreeze = function deepFreeze(obj) { if (typeof obj !== 'object' || obj === null) return; Object.freeze(obj); var propNames = Object.getOwnPropertyNames(obj); for (var _iterator = _createForOfIteratorHelperLoose(propNames), _step; !(_step = _iterator()).done;) { var name = _step.value; var value = obj[name]; deepFreeze(value); } return obj; }; function freezeAtom(anAtom) { return memoizeAtom$3(function () { var frozenAtom = jotai.atom(function (get) { return deepFreeze(get(anAtom)); }, function (_get, set, arg) { return set(anAtom, arg); }); return frozenAtom; }, [anAtom]); } function freezeAtomCreator(createAtom) { return function () { var anAtom = createAtom.apply(void 0, arguments); var origRead = anAtom.read; anAtom.read = function (get) { return deepFreeze(origRead(get)); }; return anAtom; }; } var memoizeAtom$2 = createMemoizeAtom(); var isWritable = function isWritable(atom) { return !!atom.write; }; var isFunction = function isFunction(x) { return typeof x === 'function'; }; function splitAtom(arrAtom, keyExtractor) { return memoizeAtom$2(function () { var mappingCache = new WeakMap(); var getMapping = function getMapping(arr, prev) { var mapping = mappingCache.get(arr); if (mapping) { return mapping; } var prevMapping = prev && mappingCache.get(prev); var atomList = []; var keyList = []; arr.forEach(function (item, index) { var key = keyExtractor ? keyExtractor(item) : index; keyList[index] = key; var cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)]; if (cachedAtom) { atomList[index] = cachedAtom; return; } var read = function read(get) { var ref = get(refAtom); var currArr = get(arrAtom); var mapping = getMapping(currArr, ref.prev); var index = mapping.keyList.indexOf(key); if (index < 0 || index >= currArr.length) { var prevItem = arr[getMapping(arr).keyList.indexOf(key)]; if (prevItem) { return prevItem; } throw new Error('splitAtom: index out of bounds for read'); } return currArr[index]; }; var write = function write(get, set, update) { var ref = get(refAtom); var arr = get(arrAtom); var mapping = getMapping(arr, ref.prev); var index = mapping.keyList.indexOf(key); if (index < 0 || index >= arr.length) { throw new Error('splitAtom: index out of bounds for write'); } var nextItem = isFunction(update) ? update(arr[index]) : update; set(arrAtom, [].concat(arr.slice(0, index), [nextItem], arr.slice(index + 1))); }; atomList[index] = isWritable(arrAtom) ? jotai.atom(read, write) : jotai.atom(read); }); if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every(function (x, i) { return x === keyList[i]; })) { mapping = prevMapping; } else { mapping = { atomList: atomList, keyList: keyList }; } mappingCache.set(arr, mapping); return mapping; }; var refAtom = jotai.atom(function () { return {}; }); var read = function read(get) { var ref = get(refAtom); var arr = get(arrAtom); var mapping = getMapping(arr, ref.prev); ref.prev = arr; return mapping.atomList; }; var write = function write(get, set, action) { if ('read' in action) { console.warn('atomToRemove is deprecated. use action with type'); action = { type: 'remove', atom: action }; } switch (action.type) { case 'remove': { var index = get(splittedAtom).indexOf(action.atom); if (index >= 0) { var arr = get(arrAtom); set(arrAtom, [].concat(arr.slice(0, index), arr.slice(index + 1))); } break; } case 'insert': { var _index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length; if (_index >= 0) { var _arr = get(arrAtom); set(arrAtom, [].concat(_arr.slice(0, _index), [action.value], _arr.slice(_index))); } break; } case 'move': { var index1 = get(splittedAtom).indexOf(action.atom); var index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length; if (index1 >= 0 && index2 >= 0) { var _arr2 = get(arrAtom); if (index1 < index2) { set(arrAtom, [].concat(_arr2.slice(0, index1), _arr2.slice(index1 + 1, index2), [_arr2[index1]], _arr2.slice(index2))); } else { set(arrAtom, [].concat(_arr2.slice(0, index2), [_arr2[index1]], _arr2.slice(index2, index1), _arr2.slice(index1 + 1))); } } break; } } }; var splittedAtom = isWritable(arrAtom) ? jotai.atom(read, write) : jotai.atom(read); return splittedAtom; }, keyExtractor ? [arrAtom, keyExtractor] : [arrAtom]); } function atomWithDefault(getDefault) { var EMPTY = Symbol(); var overwrittenAtom = jotai.atom(EMPTY); var anAtom = jotai.atom(function (get) { var overwritten = get(overwrittenAtom); if (overwritten !== EMPTY) { return overwritten; } return getDefault(get); }, function (get, set, update) { if (update === RESET) { return set(overwrittenAtom, EMPTY); } return set(overwrittenAtom, typeof update === 'function' ? update(get(anAtom)) : update); }); return anAtom; } var memoizeAtom$1 = createMemoizeAtom(); function waitForAll(atoms) { var createAtom = function createAtom() { var unwrappedAtoms = unwrapAtoms(atoms); var derivedAtom = jotai.atom(function (get) { var promises = []; var values = unwrappedAtoms.map(function (anAtom, index) { try { return get(anAtom); } catch (e) { if (e instanceof Promise) { promises[index] = e; } else { throw e; } } }); if (promises.length) { throw Promise.all(promises); } return wrapResults(atoms, values); }); return derivedAtom; }; if (Array.isArray(atoms)) { return memoizeAtom$1(createAtom, atoms); } return createAtom(); } var unwrapAtoms = function unwrapAtoms(atoms) { return Array.isArray(atoms) ? atoms : Object.getOwnPropertyNames(atoms).map(function (key) { return atoms[key]; }); }; var wrapResults = function wrapResults(atoms, results) { return Array.isArray(atoms) ? results : Object.getOwnPropertyNames(atoms).reduce(function (out, key, idx) { var _extends2; return _extends({}, out, (_extends2 = {}, _extends2[key] = results[idx], _extends2)); }, {}); }; function createJSONStorage(getStringStorage) { var lastStr; var lastValue; return { getItem: function getItem(key) { var parse = function parse(str) { str = str || ''; if (lastStr !== str) { lastValue = JSON.parse(str); lastStr = str; } return lastValue; }; var str = getStringStorage().getItem(key); if (str instanceof Promise) { return str.then(parse); } return parse(str); }, setItem: function setItem(key, newValue) { return getStringStorage().setItem(key, JSON.stringify(newValue)); }, removeItem: function removeItem(key) { return getStringStorage().removeItem(key); } }; } var defaultStorage = createJSONStorage(function () { return localStorage; }); defaultStorage.subscribe = function (key, callback) { var storageEventCallback = function storageEventCallback(e) { if (e.key === key && e.newValue) { callback(JSON.parse(e.newValue)); } }; window.addEventListener('storage', storageEventCallback); return function () { window.removeEventListener('storage', storageEventCallback); }; }; function atomWithStorage(key, initialValue, storage) { if (storage === void 0) { storage = defaultStorage; } var getInitialValue = function getInitialValue() { try { var _value = storage.getItem(key); if (_value instanceof Promise) { return _value.catch(function () { return initialValue; }); } return _value; } catch (_unused) { return initialValue; } }; var baseAtom = jotai.atom(storage.delayInit ? initialValue : getInitialValue()); baseAtom.onMount = function (setAtom) { var unsub; if (storage.subscribe) { unsub = storage.subscribe(key, setAtom); setAtom(getInitialValue()); } if (storage.delayInit) { var _value2 = getInitialValue(); if (_value2 instanceof Promise) { _value2.then(setAtom); } else { setAtom(_value2); } } return unsub; }; var anAtom = jotai.atom(function (get) { return get(baseAtom); }, function (get, set, update) { if (update === RESET) { set(baseAtom, initialValue); return storage.removeItem(key); } var newValue = typeof update === 'function' ? update(get(baseAtom)) : update; set(baseAtom, newValue); return storage.setItem(key, newValue); }); return anAtom; } function atomWithHash(key, initialValue, options) { var serialize = (options == null ? void 0 : options.serialize) || JSON.stringify; var deserialize = (options == null ? void 0 : options.deserialize) || JSON.parse; var _subscribe = (options == null ? void 0 : options.subscribe) || function (callback) { window.addEventListener('hashchange', callback); return function () { window.removeEventListener('hashchange', callback); }; }; var hashStorage = _extends({ getItem: function getItem(key) { var searchParams = new URLSearchParams(location.hash.slice(1)); var storedValue = searchParams.get(key); if (storedValue === null) { throw new Error('no value stored'); } return deserialize(storedValue); }, setItem: function setItem(key, newValue) { var searchParams = new URLSearchParams(location.hash.slice(1)); searchParams.set(key, serialize(newValue)); if (options != null && options.replaceState) { history.replaceState(null, '', '#' + searchParams.toString()); } else { location.hash = searchParams.toString(); } }, removeItem: function removeItem(key) { var searchParams = new URLSearchParams(location.hash.slice(1)); searchParams.delete(key); if (options != null && options.replaceState) { history.replaceState(null, '', '#' + searchParams.toString()); } else { location.hash = searchParams.toString(); } } }, (options == null ? void 0 : options.delayInit) && { delayInit: true }, { subscribe: function subscribe(key, setValue) { var callback = function callback() { var searchParams = new URLSearchParams(location.hash.slice(1)); var str = searchParams.get(key); if (str !== null) { setValue(deserialize(str)); } else { setValue(initialValue); } }; return _subscribe(callback); } }); return atomWithStorage(key, initialValue, hashStorage); } function atomWithObservable(createObservable, options) { var observableResultAtom = jotai.atom(function (get) { var _observable$Symbol$ob, _observable; var observable = createObservable(get); var itself = (_observable$Symbol$ob = (_observable = observable)[Symbol.observable]) == null ? void 0 : _observable$Symbol$ob.call(_observable); if (itself) { observable = itself; } var dataAtom = jotai.atom(options != null && options.initialValue ? getInitialValue(options) : firstValueFrom(observable)); var setData = function setData() { throw new Error('setting data without mount'); }; var dataListener = function dataListener(data) { setData(data); }; var errorListener = function errorListener(error) { setData(Promise.reject(error)); }; var subscription = null; dataAtom.onMount = function (update) { setData = update; if (!subscription) { subscription = observable.subscribe(dataListener, errorListener); } return function () { var _subscription; (_subscription = subscription) == null ? void 0 : _subscription.unsubscribe(); subscription = null; }; }; return { dataAtom: dataAtom, observable: observable }; }); var observableAtom = jotai.atom(function (get) { var _get = get(observableResultAtom), dataAtom = _get.dataAtom; return get(dataAtom); }, function (get, _set, data) { var _get2 = get(observableResultAtom), observable = _get2.observable; if ('next' in observable) { observable.next(data); } else { throw new Error('observable is not subject'); } }); return observableAtom; } function getInitialValue(options) { var initialValue = options.initialValue; return initialValue instanceof Function ? initialValue() : initialValue; } function firstValueFrom(source) { return new Promise(function (resolve, reject) { var resolved = false; var subscription = source.subscribe({ next: function next(value) { resolve(value); resolved = true; if (subscription) { subscription.unsubscribe(); } }, error: reject, complete: function complete() { reject(); } }); if (resolved) { subscription.unsubscribe(); } }); } var hydratedMap = new WeakMap(); function useHydrateAtoms(values, scope) { var ScopeContext = jotai.SECRET_INTERNAL_getScopeContext(scope); var scopeContainer = react.useContext(ScopeContext); var store = scopeContainer.s; var hydratedSet = getHydratedSet(scopeContainer); var tuplesToRestore = []; for (var _iterator = _createForOfIteratorHelperLoose(values), _step; !(_step = _iterator()).done;) { var tuple = _step.value; var atom = tuple[0]; if (!hydratedSet.has(atom)) { hydratedSet.add(atom); tuplesToRestore.push(tuple); } } if (tuplesToRestore.length) { store[RESTORE_ATOMS](tuplesToRestore); } } function getHydratedSet(scopeContainer) { var hydratedSet = hydratedMap.get(scopeContainer); if (!hydratedSet) { hydratedSet = new WeakSet(); hydratedMap.set(scopeContainer, hydratedSet); } return hydratedSet; } var memoizeAtom = createMemoizeAtom(); var LOADING = { state: 'loading' }; function loadable(anAtom) { return memoizeAtom(function () { var loadableAtomCache = new WeakMap(); var catchAtom = jotai.atom(function (get) { var promise; try { var data = get(anAtom); var _loadableAtom = jotai.atom({ state: 'hasData', data: data }); return _loadableAtom; } catch (error) { if (error instanceof Promise) { promise = error; } else { var _loadableAtom2 = jotai.atom({ state: 'hasError', error: error }); return _loadableAtom2; } } var cached = loadableAtomCache.get(promise); if (cached) { return cached; } var loadableAtom = jotai.atom(LOADING, function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(get, set) { var _data; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return get(anAtom, { unstable_promise: true }); case 3: _data = _context.sent; set(loadableAtom, { state: 'hasData', data: _data }); _context.next = 10; break; case 7: _context.prev = 7; _context.t0 = _context["catch"](0); set(loadableAtom, { state: 'hasError', error: _context.t0 }); case 10: case "end": return _context.stop(); } } }, _callee, null, [[0, 7]]); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); loadableAtom.onMount = function (init) { init(); }; loadableAtomCache.set(promise, loadableAtom); return loadableAtom; }); var derivedAtom = jotai.atom(function (get) { var loadableAtom = get(catchAtom); return get(loadableAtom); }); return derivedAtom; }, [anAtom]); } Object.defineProperty(exports, 'useAtomValue', { enumerable: true, get: function () { return jotai.useAtomValue; } }); Object.defineProperty(exports, 'useUpdateAtom', { enumerable: true, get: function () { return jotai.useSetAtom; } }); exports.RESET = RESET; exports.atomFamily = atomFamily; exports.atomWithDefault = atomWithDefault; exports.atomWithHash = atomWithHash; exports.atomWithObservable = atomWithObservable; exports.atomWithReducer = atomWithReducer; exports.atomWithReset = atomWithReset; exports.atomWithStorage = atomWithStorage; exports.createJSONStorage = createJSONStorage; exports.freezeAtom = freezeAtom; exports.freezeAtomCreator = freezeAtomCreator; exports.loadable = loadable; exports.selectAtom = selectAtom; exports.splitAtom = splitAtom; exports.useAtomCallback = useAtomCallback; exports.useHydrateAtoms = useHydrateAtoms; exports.useReducerAtom = useReducerAtom; exports.useResetAtom = useResetAtom; exports.waitForAll = waitForAll;