Code Monkey home page Code Monkey logo

Comments (6)

alexmingoia avatar alexmingoia commented on September 26, 2024

It depends on what storage mechanism you're referring to. Each browser implements them differently.

localStorage

localStorage has no "I'm full" event, and no way to determine remaining space cross-browser. When Sticky attempts to set an item in localStorage that lacks the capacity, Chrome and Firefox will throw an exception, Sticky will catch it and log it to the console. It looks like this:

Chrome:

DOMException:
    code: 22
    message: "QUOTA_EXCEEDED_ERR: DOM Exception 22"
    name: "QUOTA_EXCEEDED_ERR"

Firefox:

DOMException:
    code: 1014
    message: "Persistent storage maximum size reached"
    name: "NS_ERROR_DOM_QUOTA_REACHED"

Older versions of safari will crash, newer ones throw the chrome exception. IE9 should also throw an exception. Not sure about older version of IE though.

indexedDB / webDB

IndexedDB or webDB will fail to open a connection to the database if there is no capacity, and Sticky will write an error about it to the console. In addition, Sticky will return false as the argument to a callback passed to Sticky.set() if the request fails. There will also be an error message in the console.

store.get('foo', function(item) {
    console.log(item); // outputs `false` if indexedDB request fails
});

from sticky.

aronwoost avatar aronwoost commented on September 26, 2024

Thanks for the explanations for now. I'll do some testing soon and might get back to that issue.

from sticky.

alexmingoia avatar alexmingoia commented on September 26, 2024

I hope my reply wasn't confusing. I'll answer your question directly: There are no events in Sticky, only callbacks: The callback for when the store is repopulated and ready, and the callback for each store method.

It might be a good idea to add events for errors instead of logging them to the console. I'd be open to that if you want to do it. I would prefer it to work as such:

var store = new StickyStore();

store.on('error', function(msg) {
    console.log(msg);
});

If we're going to adopt that style of event registration, it would be good to replace the store ready callback with an event as well:

store.on('ready', function(store) {
    console.log(store);
});

from sticky.

alexmingoia avatar alexmingoia commented on September 26, 2024

I couldn't help myself and added events tonight. I've updated the documentation, check out "events" towards the bottom. Thanks for the suggestion :)

I improved error handling and replaced the console messages with error events. There's also events ready, get, set, and remove.

Use them like this:

var store = new StickyStore();

store.on('error', function(err, item) {
    console.log(error); // Returns the error message
    console.log(item); // Returns the item, if applicable
});

from sticky.

aronwoost avatar aronwoost commented on September 26, 2024

Thanks for getting back to that issue.

I always prefere callbacks (function(err[, item])) over events, since I want to have the error corresponding to the actual API call I'm doing.

Also I'm wondering if there is the need to more granular errors. I.e. I would be very interested if the error was a QUOTA or a general one (I havent worked with indexDB and webSQL so far, is there really no quota error callback?).

I'm somewhat sorry, for opening this ticket without playing around with sticky first. I'll do exactly that in the next days. Maybe I can provide a pull request then... :)

from sticky.

alexmingoia avatar alexmingoia commented on September 26, 2024

Neither localStorage, indexedDB, or webSQL include the space remaining in error messages, or methods to check how much storage is remaining. It's pathetic and frustrating.

Except for IE8, which implements a window.localStorage.remainingSpace method to get the space remaining, but it's not implemented in other browsers.

The only way to keep track of space used would be to calculate it when StickyStore.set() is called. I doubt there's a way to do it accurately.

from sticky.

Related Issues (14)

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.