Skip to content
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

Bump QR display timeouts (adjust after looping) #237

Merged
merged 3 commits into from
Nov 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/react-qr/src/Display.tsx
Original file line number Diff line number Diff line change
@@ -20,11 +20,13 @@ interface State {
frames: Uint8Array[];
frameIdx: number;
image: string | null;
timerDelay: number;
timerId: number | null;
valueHash: string | null;
}

const FRAME_DELAY = 2100;
const FRAME_DELAY = 2500;
const TIMER_INC = 500;

function getDataUrl (value: Uint8Array): string {
const qr = qrcode(0, 'M');
@@ -43,6 +45,7 @@ class Display extends React.PureComponent<Props, State> {
frames: [],
frameIdx: 0,
image: null,
timerDelay: FRAME_DELAY,
timerId: null,
valueHash: null
};
@@ -69,15 +72,15 @@ class Display extends React.PureComponent<Props, State> {

public componentDidMount (): void {
this.setState({
timerId: window.setInterval(this.nextFrame, FRAME_DELAY)
timerId: window.setTimeout(this.nextFrame, FRAME_DELAY)
});
}

public componentWillUnmount (): void {
const { timerId } = this.state;

if (timerId) {
clearInterval(timerId);
clearTimeout(timerId);
}
}

@@ -105,7 +108,7 @@ class Display extends React.PureComponent<Props, State> {
}

private nextFrame = (): void => {
const { frames, frameIdx } = this.state;
const { frames, frameIdx, timerDelay } = this.state;

if (!frames || frames.length <= 1) {
return;
@@ -114,13 +117,17 @@ class Display extends React.PureComponent<Props, State> {
const nextIdx = frameIdx === frames.length - 1
? 0
: frameIdx + 1;
const nextDelay = timerDelay + ((nextIdx === 0) ? TIMER_INC : 0);
const timerId = setTimeout(this.nextFrame, nextDelay);

// only encode the frames on demand, not above as part of the
// state derivation - in the case of large payloads, this should
// be slightly more responsive on initial load
this.setState({
frameIdx: nextIdx,
image: getDataUrl(frames[nextIdx])
image: getDataUrl(frames[nextIdx]),
timerId,
timerDelay: nextDelay
});
}
}