Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4186 from EdgeApp/sam/fiat-plugin-refactors
- Refactor Fiat Plugin `enterAmount` API
- Loading branch information
Showing
7 changed files
with
181 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useRef } from 'react' | ||
|
||
import { useHandler } from '../../../hooks/useHandler' | ||
import { useState } from '../../../types/reactHooks' | ||
|
||
export class StateManager<State extends object> { | ||
private _state: State | ||
private readonly onUpdate: (state: State) => void | ||
|
||
constructor(state: State, onUpdate: (state: State) => void) { | ||
this._state = state | ||
this.onUpdate = onUpdate | ||
} | ||
|
||
get state(): State { | ||
return this._state | ||
} | ||
|
||
update(state: Partial<State>): void { | ||
this._state = { ...this._state, ...state } | ||
this.onUpdate(this._state) | ||
} | ||
} | ||
|
||
export const useStateManager = <T extends object>(defaultState: T): StateManager<T> => { | ||
const [state, setState] = useState<T>(defaultState) | ||
const handleUpdate: StateManager<T>['update'] = useHandler((state: Partial<T>) => setState({ ...stateManagerRef.current.state, ...state })) | ||
const stateManagerRef = useRef<StateManager<T>>(new StateManager(state, handleUpdate)) | ||
|
||
return stateManagerRef.current | ||
} |
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
Oops, something went wrong.