Skip to content

Commit

Permalink
resolves analyzer settings back bug
Browse files Browse the repository at this point in the history
  • Loading branch information
efc committed Feb 24, 2019
1 parent 7bd951b commit 62927db
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
23 changes: 18 additions & 5 deletions src/Analyzer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Chart } from 'primereact/chart';
import * as _u from './Utilities'
import { Snapshot } from './Snapshot'
import { ValueCard } from './ValueCard';
import { ShowJSON } from './ShowJSON';

/**
* Properties for the snapshot loader.
Expand Down Expand Up @@ -86,9 +87,9 @@ export class Analyzer extends React.Component<Props, State> {
* Called when an instance of a component is being created and inserted into the DOM.
*/
componentDidMount = () => {
_u.debug("Analyzer did mount with history state: ", history.state)
_u.debug("Analyzer did mount with history state: ", history.state, history.length)
_u.setHistory("Analyzer")
_u.debug(`Analyzer pushed, now history state: `, history.state)
_u.debug(`Analyzer pushed, now history state: `, history.state, history.length)
this.priorBackButtonHandler = window.onpopstate
window.onpopstate = this.handleBackButton("Analyzer")
}
Expand All @@ -99,26 +100,37 @@ export class Analyzer extends React.Component<Props, State> {
* Called when a component is being removed from the DOM.
*/
componentWillUnmount = () => {
_u.debug("Analyzer will unmount with history state: ", history.state)
_u.debug("Analyzer will unmount with history state: ", history.state, history.length)
if (_u.isHistory("Analyzer")) {
history.back()
}
window.onpopstate = this.priorBackButtonHandler
}

/**
* Lets us know that the settings card was shown,
* tracked outside the component state because we need
* to consult it during the `handleBackButton()` function
* which appears to be called asyncronously.
*/
showingSettings = false

/**
* Make sure we properly exit when the back button is pressed.
*/
handleBackButton = (from: string) => (event: PopStateEvent) => {
_u.debug(`Analyzer handling back button with history state ${history.state} from ${from}`)
_u.debug(`Analyzer handling back button from ${from} with history state`, history.state, history.length)
_u.debug(`Current showingSettings`, this.showingSettings)
// only handle the exit case if this is the history.state we expect to encounter
if (from === "Analyzer") {
if (from === "Analyzer" && !this.showingSettings) {
this.props.onExit()
} else {
// just in case we get a late report from another component
// sending a programatic history.back() from its unmount
_u.setHistory("Analyzer")
}
// we always return this to fals so it only blocks once
this.showingSettings = false
}

/**
Expand Down Expand Up @@ -309,6 +321,7 @@ export class Analyzer extends React.Component<Props, State> {
*/
renderSettings = (): JSX.Element => {
if (!this.state.showSettings) return <></>
this.showingSettings = true
// change substitutions object into plain text for editing
let text = Object.keys(this.substitutions).reduce((sofar: string, term: string): string => {
const substitute = this.substitutions[term]
Expand Down
8 changes: 4 additions & 4 deletions src/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class Loader extends React.Component<Props, State> {
* Called when an instance of a component is being created and inserted into the DOM.
*/
componentDidMount = () => {
_u.debug("Loader did mount with history state: ", history.state)
_u.debug("Loader did mount with history state: ", history.state, history.length)
_u.setHistory("Loader")
_u.debug(`Loader pushed, now history state: `, history.state)
_u.debug(`Loader pushed, now history state: `, history.state, history.length)
this.priorBackButtonHandler = window.onpopstate
window.onpopstate = this.handleBackButton("Loader")
}
Expand All @@ -79,7 +79,7 @@ export class Loader extends React.Component<Props, State> {
* Called when a component is being removed from the DOM.
*/
componentWillUnmount = () => {
_u.debug("Loader will unmount with history state: ", history.state)
_u.debug("Loader will unmount with history state: ", history.state, history.length)
if (_u.isHistory("Loader")) {
history.back()
}
Expand All @@ -90,7 +90,7 @@ export class Loader extends React.Component<Props, State> {
* Make sure we properly exit when the back button is pressed.
*/
handleBackButton = (from: string) => (event: PopStateEvent) => {
_u.debug(`Loader handling back button with history state ${history.state} from ${from}`)
_u.debug(`Loader handling back button from ${from} with history state`, history.state, history.length)
// only handle the exit case if this is the history.state we expect to encounter
if (from === "Loader") {
this.props.onLoad()
Expand Down
2 changes: 2 additions & 0 deletions src/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,10 @@ export function now(): TimestampString {
export function setHistory(value: string, key: string = 'tenseg') {
let state = {}
state[key] = value
state["index"] = history.length
if (history.state) {
if (history.state[key]) {
state["index"] = history.state["index"]
history.replaceState(state, '')
return
}
Expand Down
8 changes: 4 additions & 4 deletions src/ValueCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export class ValueCard extends React.Component<Props, State> {
*/
componentDidMount = () => {
// register for back button
_u.debug(`${this.props.id} did mount with history state: `, history.state)
_u.debug(`${this.props.id} did mount with history state: `, history.state, history.length)
_u.setHistory(this.props.id, this.props.historyKey)
_u.debug(`${this.props.id} pushed, now history state: `, history.state)
_u.debug(`${this.props.id} pushed, now history state: `, history.state, history.length)
this.priorBackButtonHandler = window.onpopstate
window.onpopstate = this.handleBackButton(this.props.id)

Expand All @@ -126,7 +126,7 @@ export class ValueCard extends React.Component<Props, State> {
*/
componentWillUnmount = () => {
// let go of the back button
_u.debug(`${this.props.id} will unmount with history state: `, history.state)
_u.debug(`${this.props.id} will unmount with history state: `, history.state, history.length)
window.onpopstate = this.priorBackButtonHandler
if (_u.isHistory(this.props.id, this.props.historyKey)) {
history.back()
Expand All @@ -140,7 +140,7 @@ export class ValueCard extends React.Component<Props, State> {
* Make sure we properly exit when the back button is pressed.
*/
handleBackButton = (from: string) => (event: PopStateEvent) => {
_u.debug(`${this.props.id} handling back button with history state ${history.state} from ${from}`)
_u.debug(`${this.props.id} handling back button from ${from} with history state`, history.state, history.length)
// only handle the exit case if this is the history.state we expect to encounter
if (from === this.props.id) {
this.props.onSave()
Expand Down

0 comments on commit 62927db

Please sign in to comment.