carvajalconsultants / apollo-link-offline

An Apollo Link to queue mutations when offline or network errors exist.
MIT License
71 stars 15 forks source link

this.storage.getItem(...).then is not a function #2

Closed Hasan-git closed 5 years ago

Hasan-git commented 5 years ago

setting storage to window.localStorage leads to error, note that I'm using angular 7

 const offlineLink = new OfflineLink({
       storage: window.localStorage
});

and getting this error :

this.storage.getItem(...).then is not a function

miguelocarvajal commented 5 years ago

This is probably because window.localStorage is synchronous and the link expects an asynchronous API.

Try using a wrapper like this:

const asyncLocalStorage = {
    setItem: function (key, value) {
        return Promise.resolve().then(function () {
            localStorage.setItem(key, value);
        });
    },
    getItem: function (key) {
        return Promise.resolve().then(function () {
            return localStorage.getItem(key);
        });
    }
};

Then use the wrapper as follows:

const offlineLink = new OfflineLink({
    storage: asyncLocalStorage
});

Have not tested this myself, we've also not used this link on a browser. We're using it with react-native. Let us know how it works out for you!

Hasan-git commented 5 years ago

Thank you @miguelocarvajal ! I found a workaround , I'm using localforage instead and working as well

npm i localforage

in module

import * as localforage from "localforage";

...

const offlineLink = new OfflineLink({
            storage: localforage
        });