TheCocoaProject / cordova-plugin-nativestorage

Cordova plugin: Native storage of variables in Android, iOS and Windows
http://thecocoaproject.github.io/
Apache License 2.0
292 stars 106 forks source link

Implement Web fallback when cordova is not available #66

Closed NoNameProvided closed 7 years ago

NoNameProvided commented 7 years ago

Now if cordova is not available the plugin throws the plugin_not_installed error.

Since this plugin has a small API, it would be easy to add a proper web fallback for browsers:

  getPromisifiedLocalStorage() {
    return {
        setItem: (key, value) => {
          return new Promise( (resolve, reject) => {
            try {
              window.localStorage.setItem(key, this.encodePayload(value));
              resolve();
            } catch (error) {
              reject(error);
            }
          });
        },
        getItem: (key) => {
          return new Promise( (resolve, reject) => {
            try {
              const result = window.localStorage.getItem(key);
              resolve(this.decodePayload(result));
            } catch (error) {
              reject(error);
            }
          });
        },
        remove: (key) => {
          return new Promise( (resolve, reject) => {
            try {
              window.localStorage.removeItem(key);
              resolve();
            } catch (error) {
              reject(error);
            }
          });
        },
        clear: () => {
          return new Promise( (resolve, reject) => {
            try {
              window.localStorage.clear();
              resolve();
            } catch (error) {
              reject(error);
            }
          });
        },
      }

Note: This implementation is Promise based as I use Ionic-Native, but its pretty straight forward to make rewrite it into callbacks.

GillesC commented 7 years ago

I'm not familiar with promises in native JS. Is this native JS? If so, please make u pull request and I'l be happy to merge it.

Thanks

NoNameProvided commented 7 years ago

Here is an implementation with callbacks:

  getCallbackifiedLocalStorage() {
    return {
        setItem: function setItem(key, value, success, error) {
          try {
            window.localStorage.setItem(key, this.encodePayload(value));
            succes();
          } catch (error) {
            error(error);
          }
        },
        getItem: function getItem(key, success, error) {
          try {
            var result = window.localStorage.getItem(key);
            success(this.decodePayload(result));
          } catch (error) {
            error(error);
          }
        },
        remove: function remove(key, success, error) {
          try {
            window.localStorage.removeItem(key);
            success();
          } catch (error) {
            error(error);
          }
        },
        clear: function clear(success, error) {
          try {
            window.localStorage.clear();
            success();
          } catch (error) {
            error(error);
          }
        },
       encodePayload: function encodePayload(payload) {
         try {
           payload = JSON.stringify(payload);
         } catch(error) { }
           return payload.toString();
       },
       decodePayload: function decodePayload(payload) {
         try {
           payload = JSON.parse(payload);
         } catch(error) { }
           return payload;
       }
     }

If so, please make a pull request and I'l be happy to merge it.

Okay, I will test it and make one

NoNameProvided commented 7 years ago

Hmm, just did a quick look on the source code and it seems it already has local storage as a fallback. So I guess the Ionic Native wrapper messes up something, I will give it a proper look later (probably on the weekend)

alokrajiv commented 7 years ago

@NoNameProvided @GillesC Sorry, for the late reply.

What I had done was there is a delegate that falls back to localstorage based on the storageSupportAnalyse function's response to StorageHandle at line 27.

If even localstorage isn't available we fail gracefully with a console.log (my comment :) at the code). No detailed error message with trace that's all.

alokrajiv commented 7 years ago

Let us reopen, if there is something we can work on here though. I'm closing for now. Thanks!