Storeable
Updated on August 22, 2024Source codeTests
Storeable
is a class that enriches a storage key (String), allowing it to:
- Store state in
localStorage
orsessionStorage
- Remove itself from
localStorage
orsessionStorage
- Store a status (
ready
,stored
, orremoved
) inlocalStorage
orsessionStorage
- Remove its status from
localStorage
orsessionStorage
Construct a Storeable
instance
The Storeable
constructor accepts two parameters:
key
options
Storeable
instance. See the Storeable
constructor options section for more guidance.Storeable
constructor options
kind
local
Storeable
instance should use. Valid values are local
and session
.statusKeySuffix
status
Indicates the suffix your Storeable
instance should add to your key
when generating the key used to store status
.
See the Access state and methods table to learn more about status
.
State and methods
key
The key
(String) passed to the constructor.
If you assign a value directly to key
, a setter will pass the new value to setKey
.
status
The status (String) of the Storeable
instance.
status
is constructing
while the instance is constructing and ready
after it's constructed. It changes to stored
after a value has been stored for your key
, and it changes to removed
after your key/value pair has been removed from storage.
status
also gets stored in localStorage
or sessionStorage
. See the How to use persistent status section for more guidance on this.
storage
localStorage
or sessionStorage
, depending on what options were passed to the Storeable
constructor.string
key
in localStorage
or sessionStorage
.error
undefined
before any storage has been attempted The value (String) stored under your key
in localStorage
or sessionStorage
.setKey(newKey)
key
and updates trie
.key
(Array)Storeable
instancestore(string)
key
in localStorage
or sessionStorage
, along with any String you want to store as the value for your key
.Storeable
will store undefined
as the value for your key
.Storeable
instanceremove()
key
from localStorage
or sessionStorage
.Storeable
instanceremoveStatus()
status
from localStorage
or sessionStorage
.Storeable
instanceHow to use persistent status
As mentioned in the table above, each Storeable
instance stores its status
in localStorage
or sessionStorage
. After the instance is constructed, status
will be ready
, and if the DOM is available, status
will be stored.
The key used for your Storeable
instance's status
is your key
suffixed with the statusKeySuffix
option. For example, if the key
is baleada
, and the statusKeySuffix
option is left as the default, status
will be stored under the baleada status
key.
Persistent status isn't quite as useful when using sessionStorage
, but makes it particularly easy to write explicit, readable code when using localStorage
.
Here's an example of how you could use status
to make a decision about whether or not to apply "theme 1" or "theme 2" to a page:
const theme = new Storeable('theme')
switch (theme.status) {
case 'ready':
theme.store('theme 1') // Set "theme 1" by default
break
case 'stored':
// do nothing - respect the stored theme choice
break
}
// Add the theme to the body so that other elements can read it
// and change their styles.
document.body.dataset.theme = theme.string
Using with TypeScript
Nothing special to know about using Storeable
with TypeScript 🚀
API design compliance
options
object.key
setKey
set<Property>
methodsstatus
, storage
, string
, error
store
, remove
, removeStatus
stop
methodable