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

angular-cyanez-local-storage

v1.0.1

Published

Efficient local storage module for Angular : simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.

Downloads

4

Readme

Async local storage for Angular

Efficient local storage module for Angular : simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular asynchronous modules.

Previously named angular2-async-local-storage.

Why this module ?

For now, Angular does not provide a local storage module, and almost every app needs some local storage. There is 2 native JavaScript APIs available :

The localStorage API is simple to use but synchronous, so if you use it too often, your app will soon begin to freeze.

The IndexedDB API is asychronous and efficient, but it's a mess to use : you'll soon be caught by the callbacks hell, as it does not support Promises yet.

Mozilla has done a very great job with the localForage library : a simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance. But it is written in ES5 and then it's a mess to include into Angular.

This module is based on the same idea as localForage, but in ES6/ES2015 and additionnaly wrapped into RxJS Observables to be homogeneous with other Angular modules.

Getting started

Install via npm :

npm install angular-async-local-storage --save

Then include the AsyncLocalStorage module in your app root module. It works like the HttpModule, providing a global service to the whole app, so do NOT re-import it in your own sub modules.

import { AsyncLocalStorageModule } from 'angular-async-local-storage';

@NgModule({
  imports: [
    BrowserModule,
    AsyncLocalStorageModule,
    ...
  ]
  ...
})
export class AppModule {}

Now you just have to inject the service where you need it :

import { AsyncLocalStorage } from 'angular-async-local-storage';

@Injectable()
export class YourService {

  public constructor(protected storage: AsyncLocalStorage) {}

  public ngOnInit() {

    this.storage.setItem('lang', 'fr').subscribe(() => {
      // Done
    });

  }

}

If you use System.js loading in developpement, configure the module path like for other Angular modules :

System.config({
    ...
    map: {
      '@angular/core': 'node_modules/@angular/core/bundles/core.umd.js',
      ...
      'angular-async-local-storage': 'node_modules/angular-async-local-storage/bundles/async-local-storage.umd.js'
    }
    ...
});

API

The API follows the native localStorage API, except it's asynchronous via RxJS Observables.

Writing data

Errors are unlikely to happen, but in an app you should always catch all potential errors.

You do NOT need to unsubscribe : the observable autocompletes (like in the Http service).

this.storage.setItem('color', 'red').subscribe(() => {
  // Done
}, () => {
  // Error
});

You can store any value, without worrying about stringifying.

this.storage.setItem('user', { firstName: 'Henri', lastName: 'Bergson' })
  .subscribe(() => {}, () => {});

To delete one item :

this.storage.removeItem('user').subscribe(() => {}, () => {});

To delete all items :

this.storage.clear().subscribe(() => {}, () => {});

Reading data

Not finding an item is not an error, it succeeds but returns null.

this.storage.getItem('notexisting').subscribe((data) => {
  data; // null
}, () => {
  // Not called
});

So always check the data as it may have been removed from local storage.

this.storage.getItem('user').subscribe((user) => {
  if (user != null) {
    user.firstName; // 'Henri'
  }
}, () => {});

As any data can be stored, you need to type your data manually :

this.storage.getItem('color').subscribe((color: string) => {
  color; // 'red'
}, () => {});

Browser support

All browsers supporting IndexedDB, ie. all current browsers : Firefox, Chrome, Opera, Safari, Edge and IE10+.

IE8/9 are supported but use native localStorage as a fallback, so internal operations are synchronous (the public API remains asynchronous-like).

Older or special browsers (like Opera Mini) not supporting IndexedDB and localStorage use a fake storage, so the data won't be persistent but the module won't crash.

This module is not impacted by IE/Edge missing IndexedDB features.

This module has not been tested against Safari 8/9 buggy IndexedDB implementation, but it uses very basic features of IndexedDB so it may be fine. Otherwise, use the IndexedDBshim polyfill.

AoT and Universal support

This module supports AoT pre-compiling and Universal server-side rendering.

Changelog

Changelog available here.

Roadmap

  • Unit tests.
  • Cache / preload ?

License

MIT