npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loopmode/persistence

v0.1.10

Published

A scoped wrapper for web storage APIs.

Downloads

25

Readme

@loopmode/persistence

A scoped wrapper for web storage APIs.

  • Allows simplified and more performant usage of window.localStorage and window.sessionStorage.
  • Instead of using complex keys to avoid naming collisions, create scoped persistence objects and use simple keys
  • Instead of serializing/deserializing object values on each access, do it only once and operate on a plain object in memory

Resources

Installation

yarn add @loopmode/persistence

Usage

Supports an API similar to the web storage API (getItem, setItem) with additional get and set aliases, but under the scope of a specific name. You can use logical and short/similar keys across scopes. Additionally, you can store object values.

// PageOne.js
import Persistence from '@loopmode/persistence';
const storage = new Persistence('PageOne');

storage.set('viewMode', 'list');

// PageTwo.js
import Persistence from '@loopmode/persistence';
const storage = new Persistence('PageTwo');

storage.set('viewMode', 'grid');
storage.set('foo', {bar: {baz: 'boo'}});
console.log(storage.get('foo')); // {bar: {baz: 'boo'}}

// you can also pass objects to set all values at once
storage.set({foo: 'foo', bar: {baz: 'boo'}});
console.log(storage.get('foo')); // 'foo'
console.log(storage.get('bar')); // {baz: 'boo'}

Serialization and performance

When using the get/set methods, you are not operating on the actual web storage yet, because that would involve serialization/deserialization (e.g. JSON.encode, JSON.stringify).
Instead, you work on a simple in-memory object and no serialization is taking place until before the page is unloaded or you call instance.save() manually.

NOTE: Most mutating methods (set/setItem, remove/removeItem, setItemValues) support an optional autoSave flag. Passing true will cause the changes to be immediatly persisted to the web storage backend. The clear and clearAll methods are an exception to that rule as they are always immediatly persisted.

// PageTwo.js
storage.set('foo', {bar: {baz: 'boo'}});
// value is immediatly available for reading, even if it's not persisted to the web storage yet
console.log(storage.get('foo').bar); // {baz: 'boo'}
window.localStorage.getItem("PageTwo"); // null

storage.save();
window.localStorage.getItem("PageTwo"); // "{\"viewMode\": \"grid\", \"foo\": {\"bar\": {\"baz\": \"boo\"}}"} 

Effectively, you are free to set ridiculous amounts of data without worrying about performance impacts, for example you could set complex data object values from inside a mousemove event handler or set values from inside a requestAnimationFrame loop without the penalties of serialization.

Shared instances

In case you need to access values from different components, e.g. in a react application, when you don't know the order of instantiation, use the the static connect method instead of creating an instance.
The first call will create and return an instance, all consecutive calls (e.g. in other parts of your code) will simply receive the existing instance.

var a = Persistence.connect('foo');
var b = Persistence.connect('foo');
console.log(a === b); // true

// also:
var a = new Persistence('foo');
var b = Persistence.connect('foo');
var c = Persistence.connect('foo');
console.log(a === b === c); // true

Storage: backend

Pass the backend option to specify where to actually persist the data. The value should be an object that supports the web storage API (setItem, getItem, removeItem).
The default value is window.localStorage.

const storage = new Persistence('options', {backend: window.sessionStorage});