Object.defineProperty(rootStore.state, store.id, {
get: () => store.state,
- set: state => store.replaceState(state),
+ set: state => (store.state = state),
})
// Vue.set(rootStore.state, store.name, store.state)
rootStore._modulesNamespaceMap[store.id + '/'] = true
devtoolHook.on('vuex:travel-to-state', targetState => {
- store.replaceState(targetState[store.id])
+ store.state = targetState[store.id]
})
store.subscribe((mutation, state) => {
>(
id: Id,
buildState: () => S,
- // @ts-ignore an empty object is valid for Record
- getters: G = {}
+ getters: G = {} as G
// methods: Record<string | symbol, StoreMethod>
): CombinedStore<Id, S, G> {
const state: Ref<S> = ref(buildState())
+ // TODO: do we need this?
function replaceState(newState: S) {
state.value = newState
}
let isListening = true
- const subscriptions: SubscriptionCallback<S>[] = []
+ let subscriptions: SubscriptionCallback<S>[] = []
watch(
() => state.value,
// TODO: return function to remove subscription
}
+ function reset() {
+ subscriptions = []
+ state.value = buildState()
+ }
+
const storeWithState: Store<Id, S> = {
id,
// it is replaced below by a getter
patch,
subscribe,
- replaceState: (newState: S) => {
- isListening = false
- replaceState(newState)
- isListening = true
- },
+ reset,
}
// @ts-ignore we have to build it
// make state access invisible
Object.defineProperty(store, 'state', {
get: () => state.value,
+ set: (newState: S) => {
+ isListening = false
+ state.value = newState
+ isListening = true
+ },
})
// Devtools injection hue hue
Id extends string,
S extends StateTree,
G extends Record<string, StoreGetter<S>>
->(
- id: Id,
- buildState: () => S,
- // @ts-ignore
- getters: G = {}
-) {
+>(id: Id, buildState: () => S, getters: G = {} as G) {
let store: CombinedStore<Id, S, G> | undefined
function useStore(): CombinedStore<Id, S, G> {
patch(partialState: DeepPartial<S>): void
/**
- * Replaces current state with a completely new version.
- * @param newState state object to replace current state
+ * Resets the store to its initial state by removing all subscriptions and
+ * building a new state object
*/
- replaceState(newState: S): void
+ reset(): void
+
/**
* Setups a callback to be called whenever the state changes.
* @param callback callback that is called whenever the state changes