Local storage polyfill with promise based API

229 views
Skip to first unread message

Matt Friedman

unread,
Feb 2, 2014, 10:38:54 PM2/2/14
to angula...@googlegroups.com
My db layer uses deferreds / promises. I'm attempting to write a local storage polyfill for use with angular-cache.

With db.get for instance I return a promise. BTW, I'm using pouchdb as my db 

Will angular-cache detect if this is a promise and handle it appropriately?


angular.module('listoutfitterApp').factory('localStoragePolyfill',['db', function (db) {

    return {
        getItem: function(key) {
            return db.get(key);// returns a promise.
        },

        setItem: function(key, value) {
             // todo
        },

        removeItem: function(key) {
           // todo
        }
    };

}]);

Jason Dobry

unread,
Feb 2, 2014, 11:19:11 PM2/2/14
to angula...@googlegroups.com
Angular-cache does not support asynchronous operations, and to do so would require considerable work. A feature like this would introduce breaking changes, and I'm not sure if Angular-cache could maintain its API compatibility with $cacheFactory with these changes. For example, cache instantiation would go from synchronous to asynchronous, because the cache won't synchronously be filled with data from localStorage anymore. Loading data from localStorage might have to be moved from the constructor to its own method, so the developer can safely execute code when the cache is "ready". This could break things for a lot of people. Possibly the code could be written to stay synchronous when promises are not returned, but that would be messy.

In lieu of re-writing Angular-cache's internals, might I suggest that you take a look at my new project Angular-data. Angular-cache is intended solely to be used as a cache, not an interface for real data persistence, though many have tried to adapt it for such use, including myself. That is why I am writing Angular-data, which fully supports asynchronous operations and pluggable adapters so you can retrieve data from any data source you want. Angular-data is intended to provide the abstraction layer necessary for robust and DRY data management. It's still a work in progress, so I would love some feedback.

Matt Friedman

unread,
Feb 2, 2014, 11:51:36 PM2/2/14
to angula...@googlegroups.com
Hi Jason,

Thanks for your response. I did look at angular-data. Looks great but I don't know if I'm ready to switch at this point. 

Perhaps there is still a way I can use your cache in a sync manner inside of some of my success functions. But I'll have to experiment. 

Cheers,
Matt
Reply all
Reply to author
Forward
0 new messages