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

@open-pioneer/local-storage

v2.3.0

Published

Provides access to the browser's local storage.

Downloads

165

Readme

@open-pioneer/local-storage

This package provides access to the browser's local storage.

A single local storage key (configurable, see Configuration) is used to keep track of the application's persistent data. Packages using the LocalStorageService can work with arbitrary values (including nested data structures) through a convenient API.

NOTE: The LocalStorageService reads the persistent data from the browser's local storage on application startup. Changes to that data made via the LocalStorageService are reflected in the browser's local storage immediately. Concurrent changes made to the browser's local storage are not reflected by the LocalStorageService. In other words, there is no two-way synchronization between the two systems while the application is running.

You should not attempt to modify the local storage value managed by the LocalStorageService (see storageId in Configuration) through the "raw" Browser APIs while the application is running. Other keys are safe to use.

Usage

Reference the interface name local-storage.LocalStorageService to inject an instance of LocalStorageService.

Checking for local storage support

Not all browsers implement or enable support for local storage. Use the .isSupported property to check whether local storage can be used at all:

const storageService = ...; // injected
console.log(storageService.isSupported);

If local storage is not supported, other methods (such as get() and set()) throw an error.

Reading and writing values

In its most basic form, you can use the LocalStorageService similar to a map. In the background, changes to the service are always persisted into local storage.

All keys and values used in the LocalStorageService are serialized to JSON via JSON.stringify(). Thus, only values supported by JSON can be used.

Example:

const storageService = ...; // injected
storageService.set("foo", "bar");
storageService.set("foo", ["array"]);
storageService.set("foo", {
    nested: {
        object: "hello world"
    }
});
storageService.get("foo"); // returns (copy of) previous value
storageService.remove("foo");
storageService.clear();

Namespaces

You can use the LocalStorageService to manage hierarchical data, including objects and arrays (see above). Namespaces can help you treat an object as a group of (nested) properties. Getting or setting entries in the namespace update an object behind the scenes.

To use a namespace, call getNamespace(key) on either the LocalStorageService or another LocalStorageNamespace object. The key used in getNamespace(key) should either already be associated with an object or it should not be set to a value at all. If key is not yet associated with an existing object, a new empty object is created.

Example:

const storageService = ...; // injected
const namespace = storageService.getNamespace("my-key");
namespace.set("foo", "bar"); // actually sets `"my-key" -> "foo"`

getNamespace("my-key") returns a LocalStorageNamespace instance that manipulates the object at "my-key".

Namespaces provide a convenient way to scope your component's persistent values, avoiding conflicts with other packages. For example, you can use your package name as the namespace key:

const storageService = ...; // injected
const namespace = storageService.getNamespace("my-package-name");
namespace.set("my-state", "some-value-to-save");

NOTE: Multiple namespace instances using the same key manipulate the same object and see each other's effects.

Configuration

| Name | Type | Description | | ----------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | storageId | String | The key under which the persistent data is saved. This value should be configured to a reasonably unique value to avoid clashes with other applications at the same origin. Defaults to trails-state (with a warning). |

Implementation notes

The LocalStorageService manages the persistent data as a single, hierarchical JSON object. This JSON object is loaded from and saved to the browser's local storage using the storageId key.

The top level value is always an object; its properties are manipulated when calling get, set etc. on the LocalStorageService. Nested values can be arbitrary (JSON-compatible) values.

License

Apache-2.0 (see LICENSE file)