![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
atom.storage
Advanced tools
Readme
A Storage
(e.g. localStorage
)
implementation for Atoms that is designed for persisting non-critical data such
as selections made by the user over sessions.
You must first provide an Atom
implementation. You can use either
import Atom from "bacon.atom"
or
import Atom from "kefir.atom"
for example. See bacon.atom
and
kefir.atom
for details.
The default import
import Stored from "atom.storage"
is a function to create an atom whose contents are stored.
To create an atom whose contents are stored, you pass a key, a default value,
the desired Atom
constructor, and the desired
Storage
object to
the Stored
constructor. For example:
const stored = Stored({key: "my-unique-app-prefix:my-stored-model",
value: defaultValue,
Atom,
storage: localStorage})
The default value is used when the storage does not already contain a value for the key. Also, when a stored atom is written to with a value that is equal to the default value, the persisted value for the stored atom is removed from the storage. This avoids unnecessary use of storage space.
The value of the atom is converted to a string by calling
JSON.stringify
.
Note that when a stored atom is created, the (default) value is not stored. The
value is stored only after a modify
method call that actually results in a new
value for the stored atom.
When two (or more) stored atoms are created with the same storage and key, the implementation only actually creates an atom on the first call. This means that in
const stored1 = Stored({key: "my-unique-app-prefix:my-stored-model",
value: defaultValue,
Atom,
storage: localStorage})
const stored2 = Stored({key: "my-unique-app-prefix:my-stored-model",
value: defaultValue,
Atom,
storage: localStorage})
the objects stored1
and stored2
are the one and same object and stored1 === stored2
is true
.
The full argument object to Stored
can be described as follows:
{key: String,
value: JSON,
Atom: JSON => AbstractMutable,
storage: Storage,
time: Maybe Milliseconds,
schema: Maybe JSON,
debounce: Maybe Milliseconds}
The time
, if specified, is the number of milliseconds after which the value is
considered to have expired. If not specified, the value never expires.
The schema
, if specified, is stored with the value, and checked when a stored
atom is created. If the stored schema
is not
equal to the given schema, then the
stored value is removed and the given default is used instead.
The debounce
, if specified, is the debounce period, in milliseconds, to use
for storing values. If not specified, values are stored immediately. Note that
debounce: 0
is different from no debounce.
When a value is persisted to storage, the expiration time is set to time + Date.now()
. Also, when a stored atom with a particular key is first created
(e.g. when the application is started), the expiration time is updated to
time + Date.now()
. This way a value is kept alive as long as it is being
used.
The named export
import {expireNow} from "atom.storage"
is a function that takes a {storage, regex}
argument object. expireNow
goes
through items in the storage
, whose keys match the given regex
, and removes
items that have expired. You typically call expireNow
once immediately or
shortly after your app starts. For example:
expireNow({storage: localStorage, regex: /^my-unique-app-prefix:/})
Note the use of ^
in the above regex.
Even though Stored
shares atoms with the same storage and key,
Stored
directly returns the shared object it constructs with Atom
. This
means that you can combine Stored
with more complex ways to create atoms. In
particular, you can combine Stored
with Undo
from
atom.undo
. You can decide whether
you create an Undo
atom with Stored
:
const storesFullHistory =
Undo({value, Atom: value => Stored({key, value, storage, Atom})})
Or a stored Atom
with Undo
:
const storesLatestValue =
Stored({key, value, storage, Atom: value => Undo({value, Atom})})
Both of these combinations return an undoable atom and can be useful.
FAQs
Reactive variables with Storage
The npm package atom.storage receives a total of 133 weekly downloads. As such, atom.storage popularity was classified as not popular.
We found that atom.storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.