i looking elegant way lazy download , cache items collection on client.
let's request batch 0-50 174-223 etc... familiar library or algorithm handles such problem?
let's imagine simple function fetching data.
function fetch(from: number, to: number): promise<object>
this function takes range , returns promise
resolve data want (what data is, not important). let's function calls server every time data. if know data on server won't change, can add caching in simple way. need create function use fetch
internally , save results. basic approach this.
function makecachedfetch() { var cache = {}; return function cachedfetch(from, to) { var key = + '-' + to; if (!cache[key]) { // keep promises in cache cache[key] = fetch(from, to); } return cache[key]; }; } // make instance of our cached fetch function var cachedfetch = makecachedfetch(); // call server cachedfetch(0, 50); // bu won't cachedfetch(0, 50);
obviously doesn't work if don't ask same ranges on , over, if data cached, should starting point you.
Comments
Post a Comment