Git Product home page Git Product logo

async-local-storage's Introduction

async-local-storage

This is a p(r)ollyfill for a fully-async object-storage API that's simpler than IndexedDB, but also async, removing many of the performance hazards of localStorage.

The API is roughly mirrors the ES6 Map type, but all methods return Future instances to enable async operation.

To get started quickly, use one of the pre-built versions from the bin/ directory. To work from source, make sure you git submodule update --init after cloning.

API

The API lives at navigator.storage to distinguish it from window.localStorage and to prevent introducing a new global symbol,

Methods:

var storage = navigator.storage ||          // New API, new object
              navigator.alsPolyfillStorage; // Where the polyfill lives

storage.has(/*string*/ key)
    .then(function(bool) {});

storage.get(/*string*/ key)
    .then(function(value) {});

storage.set(/*string*/ key, /*cloneable*/ value)
    .then(function() {});

storage.delete(/*string*/ key)
    .then(function() {});

storage.clear()
    .then(function() {});

storage.count()
    .then(function(integer) {});

storage.forEach(/*function*/ callback, /*any*/ scope)
    .then(function() {});

Examples

var storage = navigator.storage ||
              navigator.alsPolyfillStorage;

// Testing for a value
storage.has("thinger").then(function(doesHaveThinger) {
  if (doesHaveThinger) {
    // ...
  }
});

// Getting a value
storage.get("thinger").then(
  function(value) {
    // ...
  },
  function(e) { console.log("get failed with error:", e); }
);

// Setting a value without error handling is simple:
storage.set("thinger", "blarg");

// But setting is also async, so to read related values, it's best to wait
storage.set("thinger", "othervalue").then(function() {
  storage.get("...").then(function(value) {
    // ...
  });
});

// Iteration is also async. The returned Future resolves when the passed
// callback has been invoked for each item (or when one fails, in case of error)
var itemCount = 0;
storage.forEach(function(value, key) {
  itemCount++;
}).then(function() { console.log(itemCount, "items in storage"); });

// The above is equivalent to using .count():
storage.count().then(function(c) { console.log(c, "items in storage"); });

Browser Support

This code is only meant to work on browsers with modern IndexedDB support and has been tested in stable-channel Chrome and Firefox as of Spring '13.

License

Apache v2 License. See the LICENSE file.

async-local-storage's People

Contributors

slightlyoff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.