Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {Environment} from 'react-relay';
import invariant from 'invariant';
class RelayStore {
constructor() {
this._env = new Environment();
this._networkLayer = null;
this._taskScheduler = null;
}
reset(networkLayer) {
invariant(
!this._env.getStoreData().getChangeEmitter().hasActiveListeners() &&
!this._env.getStoreData().getMutationQueue().hasPendingMutations() &&
!this._env.getStoreData().getPendingQueryTracker().hasPendingQueries(),
'RelayStore.reset(): Cannot reset the store while there are active ' +
'Relay Containers or pending mutations/queries.'
);
if (networkLayer !== undefined) {
this._networkLayer = networkLayer;
}
this._env = new Environment();
if (this._networkLayer !== null) {
this._env.injectNetworkLayer(this._networkLayer);
}
if (this._taskScheduler !== null) {
this._env.injectTaskScheduler(this._taskScheduler);
}
}
// Map existing RelayEnvironment methods
getStoreData() {
return this._env.getStoreData();
}
injectNetworkLayer(networkLayer) {
this._networkLayer = networkLayer;
this._env.injectNetworkLayer(networkLayer);
}
injectTaskScheduler(taskScheduler) {
this._taskScheduler = taskScheduler;
this._env.injectTaskScheduler(taskScheduler);
}
primeCache(...args) {
return this._env.primeCache(...args);
}
forceFetch(...args) {
return this._env.forceFetch(...args);
}
read(...args) {
return this._env.read(...args);
}
readAll(...args) {
return this._env.readAll(...args);
}
readQuery(...args) {
return this._env.readQuery(...args);
}
observe(...args) {
return this._env.observe(...args);
}
getFragmentResolver(...args) {
return this._env.getFragmentResolver(...args);
}
applyUpdate(...args) {
return this._env.applyUpdate(...args);
}
commitUpdate(...args) {
return this._env.commitUpdate(...args);
}
/**
* @deprecated
*
* Method renamed to commitUpdate
*/
update(...args) {
return this._env.update(...args);
}
}
export default new RelayStore();
@redcom
Copy link

redcom commented Apr 8, 2016

Nice cache reset implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment