tymondesigns / angular-locker

🗄️ A simple & configurable abstraction for local/session storage in angular js projects

Home Page:https://npm.im/angular-locker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: locker.getDriverName()

andreaslarssen opened this issue · comments

Hi.

I just added my own custom driver that uses cookies for storage, and works fine for the most part. Some functionality in my app needs just a bit more space than cookies can handle, so I want to disable that functionality when user's using a browser that does not support localStorage, like Safari in private mode.

To keep from having to test everywhere if localStorage is available and / or manually set the proper driver, I chucked it all in my Locker extension:

app.config(['CookieStorageProvider', 'lockerProvider',
  function (CookieStorageProvider, lockerProvider) {
    var defaults = {
      driver: 'local',
      namespace: 'my.namespace',
      separator: '_',
      eventsEnabled: true,
      extend: {}
    };

    try {
      localStorage.setItem('localStorageTest', 'localStorageTest');
      localStorage.removeItem('localStorageTest');
      lockerProvider.defaults(defaults);
    } catch(e) {
      defaults.extend = {
        cookieStorage: CookieStorageProvider.get()
      };

      defaults.driver = 'cookieStorage';
      lockerProvider.defaults(defaults);
    }
  }
]);

I haven't quite figured out if Locker is meant to use whatever driver(s) are available, and switch between them, leaving it unnecessary and undesirable for developers to even know what happens behind the curtains other than setting desired driver (default). If so, this might not be the right approach, and the feature request redundant. If Locker, on the other hand, is supposed to use one driver at the time (unless explicitly switched), then it would be nice to be able to call something like locker.getDriverName(), that would return 'cookieStorage' in the case of Safari private mode etc. in my case.

I'm using the "private" locker._options.driver attribute to achieve this right now, but that's hardly the intention.

👍 Good or 👎 garbage?