-
Notifications
You must be signed in to change notification settings - Fork 2
Network optimizations #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markotron
wants to merge
1
commit into
demo-base
Choose a base branch
from
demo-base-1
base: demo-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
import {User, UserId} from "../model/Model"; | ||
import Axios from "axios-observable"; | ||
import {map, publishReplay, refCount, retry, share} from "rxjs/operators"; | ||
import {Observable} from "rxjs"; | ||
import {map, publishReplay, refCount, retry, share, tap} from "rxjs/operators"; | ||
import {Observable, of} from "rxjs"; | ||
import {unsupported} from "../Common"; | ||
import LRU from "lru-cache"; | ||
|
||
interface ValueAndRequest<Value> { | ||
value?: Value | ||
request: Observable<Value> | ||
} | ||
|
||
interface UserService { | ||
getUserWithId: (id: UserId) => Observable<User> | ||
} | ||
|
||
class UserServiceImpl implements UserService { | ||
|
||
private readonly baseUrl = "http://localhost:5000/"; | ||
private readonly userRequests: Map<UserId, Observable<User>> = new Map(); | ||
private static readonly baseUrl = "http://localhost:5000/"; | ||
private static readonly cacheOptions = { | ||
max: 100, | ||
maxAge: 1000 * 60 * 60 | ||
}; | ||
|
||
private readonly userCache = new LRU<UserId, ValueAndRequest<User>>(UserServiceImpl.cacheOptions); | ||
|
||
getUserWithId(id: UserId): Observable<User> { | ||
|
||
if(this.userRequests.has(id)) | ||
return this.userRequests.get(id) ?? unsupported("Can't happen!"); | ||
const valueAndRequest = this.userCache.get(id); | ||
if(valueAndRequest && valueAndRequest.value) return of(valueAndRequest.value); | ||
if(valueAndRequest) return valueAndRequest.request; | ||
|
||
const path = `users/?userId=${id}`; | ||
const userRequest = Axios | ||
.get(this.baseUrl + path) | ||
.get(UserServiceImpl.baseUrl + path) | ||
.pipe( | ||
map(response => response.data), | ||
tap(user => this.cacheUser(id, user)), | ||
publishReplay(1), | ||
refCount() | ||
); | ||
this.userRequests.set(id, userRequest); | ||
this.userCache.set(id, { | ||
request: userRequest | ||
}); | ||
return userRequest; | ||
} | ||
|
||
private cacheUser(id: UserId, user: User) { | ||
const valueAndRequest = this.userCache.get(id); | ||
if(!valueAndRequest) unsupported("There must be a request cached at this point!"); | ||
if(valueAndRequest.value) unsupported("Value must be undefined at this point!"); | ||
valueAndRequest.value = user; | ||
} | ||
} | ||
|
||
export default new UserServiceImpl(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, {Dispatch, Reducer, useReducer} from "react"; | ||
import {User} from "../model/Model"; | ||
import {feedbackFactory, noop, unsupported} from "../Common"; | ||
import userService from "../service/UserService"; | ||
import Container from "@material-ui/core/Container"; | ||
import {TableContainer} from "@material-ui/core"; | ||
import Paper from "@material-ui/core/Paper"; | ||
import Table from "@material-ui/core/Table"; | ||
import TableHead from "@material-ui/core/TableHead"; | ||
import TableRow from "@material-ui/core/TableRow"; | ||
import TableCell from "@material-ui/core/TableCell"; | ||
import TableBody from "@material-ui/core/TableBody"; | ||
|
||
const randomUserIds = Array.from({length: 30}, () => Math.floor(Math.random() * 5)); | ||
|
||
interface State { | ||
users: Array<User> | ||
} | ||
|
||
const initialState: State = { | ||
users: randomUserIds.map(id => ({id, name: "-"})) | ||
}; | ||
|
||
export const UserLoader: React.FC = () => { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
useFeedbacks(state, dispatch); | ||
return ( | ||
<Container fixed maxWidth="sm"> | ||
<TableContainer component={Paper}> | ||
<Table aria-label="simple table"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>User Id</TableCell> | ||
<TableCell>User name</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{state.users.map(u => ( | ||
<TableRow> | ||
<TableCell component="th" scope="row"> | ||
{u.id} | ||
</TableCell> | ||
<TableCell component="th" scope="row"> | ||
{u.name} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
</Container> | ||
); | ||
}; | ||
|
||
|
||
type Action = { user: User } | ||
|
||
const reducer: Reducer<State, Action> = (state, action) => { | ||
const index = state.users.findIndex(u => u.id === action.user.id && u.name ==="-"); | ||
if (index === -1) unsupported("The user must be in the array!"); | ||
state.users[index] = action.user; | ||
return {users: state.users}; | ||
}; | ||
|
||
const useFeedbacks = (state: State, dispatch: Dispatch<Action>) => { | ||
const useFeedback = feedbackFactory(state); | ||
useFeedback( | ||
s => s.users.map(u => u.id), | ||
ids => { | ||
ids.forEach(id => { | ||
setTimeout(_ => { | ||
userService | ||
.getUserWithId(id) | ||
.subscribe(user => dispatch({user})) | ||
}, Math.random() * 10000); | ||
}); | ||
return noop; | ||
} | ||
) | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically this is a possible case so calling
unsupported
is not a correct approach. Instead, just return ifvalueAndRequest === undefined
.