Created
April 8, 2016 01:31
-
-
Save tlvenn/9c468a5b50e0497bd319bfa0ab8b6c7f to your computer and use it in GitHub Desktop.
This file contains 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 {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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice cache reset implementation