Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created June 17, 2019 13:38
Show Gist options
  • Save lukemelia/ec51e029e33530f0260daed48f70587b to your computer and use it in GitHub Desktop.
Save lukemelia/ec51e029e33530f0260daed48f70587b to your computer and use it in GitHub Desktop.
Orbit strategy with filter based on whether in cache or not
import {
RequestStrategy,
} from '@orbit/coordinator';
export default {
create() {
return new RequestStrategy({
name: 'store-yapp-api-query-blocking',
source: 'store',
on: 'beforeQuery',
target: 'yapp-api',
action: 'pull',
blocking: true,
filter: onlySingleRecordLookupsThatAreNotAlreadyCached,
catch(e) {
console.log('error performing yapp-api.pull', e); // eslint-disable-line
this.source.requestQueue.skip();
this.target.requestQueue.skip();
throw e;
}
});
}
};
function onlySingleRecordLookupsThatAreNotAlreadyCached(query) {
if (query.expression.op !== 'findRecord') {
return false;
}
return !this.source.cache.getRecordSync(query.expression.record);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment