Created
June 17, 2019 13:38
-
-
Save lukemelia/ec51e029e33530f0260daed48f70587b to your computer and use it in GitHub Desktop.
Orbit strategy with filter based on whether in cache or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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