-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
766aeeb
commit 324760a
Showing
12 changed files
with
127 additions
and
82 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,83 @@ | ||
import { exampleReducer } from '$lib/redux/example'; | ||
import { feedReducer, postReducer, postRepliesReducer } from '$lib/redux/posts/slice'; | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
|
||
/** | ||
* The base store. | ||
* | ||
* This is a low level API and should not be used directly. | ||
* @private | ||
*/ | ||
export const _store = configureStore({ | ||
reducer: { | ||
example: exampleReducer, | ||
post: postReducer, | ||
postReplies: postRepliesReducer, | ||
feed: feedReducer | ||
import { configureStore, createSelector } from '@reduxjs/toolkit'; | ||
import { derived, readable, type Readable } from 'svelte/store'; | ||
|
||
// Individual interfaces to be used when consuming in other servies. | ||
// By specifying only the interfaces you need, IE: | ||
// `appState: AppPostState & AppExampleState`, it means there is less mocking | ||
// needed when testing. | ||
export interface AppExampleState { | ||
readonly example: Readable<ReturnType<typeof exampleReducer>>; | ||
} | ||
|
||
export interface AppPostState { | ||
readonly post: Readable<ReturnType<typeof postReducer>>; | ||
} | ||
|
||
export interface AppPostRepliesState { | ||
readonly postReplies: Readable<ReturnType<typeof postRepliesReducer>>; | ||
} | ||
|
||
export interface AppFeedState { | ||
readonly feed: Readable<ReturnType<typeof feedReducer>>; | ||
} | ||
|
||
export class AppDispatch { | ||
constructor(readonly dispatch: Dispatch) {} | ||
} | ||
|
||
export class AppState implements AppExampleState, AppPostState, AppPostRepliesState, AppFeedState { | ||
/** | ||
* The base store. | ||
* | ||
* This is a low level API and should not be used directly. | ||
* @private | ||
*/ | ||
readonly _store = configureStore({ | ||
reducer: { | ||
example: exampleReducer, | ||
post: postReducer, | ||
postReplies: postRepliesReducer, | ||
feed: feedReducer | ||
} | ||
}); | ||
|
||
readonly appDispatch = new AppDispatch(this._store.dispatch); | ||
|
||
/** | ||
* Used to access the store directly. It is recommended to access state via | ||
* selectors as they are more efficient. | ||
*/ | ||
readonly rootState: Readable<RootState> = readable(this._store.getState(), (set) => { | ||
const unsubscribe = this._store.subscribe(() => { | ||
set(this._store.getState()); | ||
}); | ||
return unsubscribe; | ||
}); | ||
|
||
private selectSelf(state: RootState) { | ||
return state; | ||
} | ||
}); | ||
|
||
export type RootState = ReturnType<typeof _store.getState>; | ||
export type AppDispatch = typeof _store.dispatch; | ||
private readonly selectExample = createSelector( | ||
[this.selectSelf], | ||
(rootState) => rootState.example | ||
); | ||
readonly example = derived(this.rootState, this.selectExample); | ||
|
||
private readonly selectPost = createSelector([this.selectSelf], (rootState) => rootState.post); | ||
readonly post = derived(this.rootState, this.selectPost); | ||
|
||
private readonly selectPostReplies = createSelector( | ||
[this.selectSelf], | ||
(rootState) => rootState.postReplies | ||
); | ||
readonly postReplies = derived(this.rootState, this.selectPostReplies); | ||
|
||
private readonly selectFeed = createSelector([this.selectSelf], (rootState) => rootState.feed); | ||
readonly feed = derived(this.rootState, this.selectFeed); | ||
} | ||
|
||
export type RootState = ReturnType<typeof AppState.prototype._store.getState>; | ||
export type Dispatch = typeof AppState.prototype._store.dispatch; |
Oops, something went wrong.