UNPKG

nanostores

Version:

A tiny (340 bytes) state manager for React/Preact/Vue/Svelte with many atomic tree-shakable stores

24 lines (20 loc) 536 B
import { atom } from '../atom/index.js' /* @__NO_SIDE_EFFECTS__ */ export const map = (initial = {}) => { let $map = atom(initial) $map.setKey = function (key, value) { let oldMap = $map.value if (typeof value === 'undefined' && key in $map.value) { $map.value = { ...$map.value } delete $map.value[key] $map.notify(oldMap, key) } else if ($map.value[key] !== value) { $map.value = { ...$map.value, [key]: value } $map.notify(oldMap, key) } } return $map }