sunnylqm / react-native-storage

local storage wrapper for both react-native and browser. Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
MIT License
3.02k stars 268 forks source link

extraFetchOptions When parameters change, is the data returned or cached? #235

Closed lixiasandy closed 5 years ago

lixiasandy commented 5 years ago

// load storage .load({ key: 'tuimusic',

// autoSync (default: true) means if data is not found or has expired,
// then invoke the corresponding sync method
autoSync: true,

// syncInBackground (default: true) means if data expired,
// return the outdated data first while invoking the sync method.
// If syncInBackground is set to false, and there is expired data,
// it will wait for the new data and return only after the sync completed.
// (This, of course, is slower)
syncInBackground: true,

// you can pass extra params to the sync method
// see sync example below
syncParams: {
  extraFetchOptions: {
    page:1, //
  },
  someFlag: true
}

}) .then(ret => { // found data go to then()

}) .catch(err => { // any exception including data not found // goes to catch() console.warn(err.message); switch (err.name) { case 'NotFoundError': // TODO; break; case 'ExpiredError': // TODO break; } });

//sync.js async tuimusic(params) {

let {resolve, reject, syncParams:{extraFetchOptions, someFlag}} = params;
await fetch(TUIMUSIC, {
  method: 'POST',
  headers: {'Accept': 'application/json','Content-Type': 'application/json'},
  ...extraFetchOptions,
}).then(response => {
  return response.json();
}).then(json => {

  if (json && json.data) {

    storage.save('tuimusic',json);

    if (someFlag) {

    }
    resolve && resolve(json)
  }else{
    reject && reject(new Error('data parse error'));
  }
}).catch(err => {
  console.warn(err);
  reject && reject(err)
});

},

sunnylqm commented 5 years ago

Parameters just pass to sync methods directly. Nothing special.

lixiasandy commented 5 years ago

Thank you very much for your reply. My parameters pass pages, and the data returned is different for different pages. For example, the first page returns {data:{name:'zs'} and I hope the second page returns {data:{name:'ls'}, but in sync it returns the cached information {data:{name:'zs'} of the first page.

sunnylqm commented 5 years ago

Post your code here

lixiasandy commented 5 years ago
//storage.js
save(key, obj) {
    storage.save({
      key: key,  
      data: obj,
      expires: defaultExpires
    })
  },
  load(key,extraFetchOptions, callBack) {
    storage.load({
        key: key,
        autoSync: true,
        syncInBackground: true,
        syncParams: {
          extraFetchOptions: extraFetchOptions,
          someFlag: true,
        }
      }).then(ret => {
        callBack && callBack(ret)
        return ret
      }).catch(err => {
        console.warn(err.message);
      })
  }

//sync.js
async tuimusic(params) {
    let {resolve, reject, syncParams:{extraFetchOptions, someFlag}} = params;
    await fetch(TUIMUSIC, {
      method: 'POST',
      headers: {'Accept': 'application/json','Content-Type': 'application/json'},
      ...extraFetchOptions,
    }).then(response => {
      return response.json();
    }).then(json => {
      if (json && json.data) {
        storage.save('tuimusic',json);
        resolve && resolve(json)
      }else{
        reject && reject(new Error('data parse error'));
      }
    }).catch(err => {
      console.warn(err);
      reject && reject(err)
    });
  }

//index.js
let { page, data } = this.state
const params = {page:page};
storage.load('tuimusic', params, (json) => {
    if (json == undefined) {
        storage.load('tuimusic', params, (json) => {

            this.setState({
                data: data.concat(json.data.music)
            })
        });
    }else {
        this.setState({
            data: data.concat(json.data.music)
        })
    }
}); 
sunnylqm commented 5 years ago

What version are you using now? The latest version does not have resolve and reject. From you snippet the parameter depends on how you invoke your load method.