-
Notifications
You must be signed in to change notification settings - Fork 27
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
Fix babel build error, convert Poll class into a function #1594
Draft
JSReds
wants to merge
1
commit into
master
Choose a base branch
from
fix-babel-error-build
base: master
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.
+55
−59
Draft
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
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,93 +1,89 @@ | ||
const DEFAULT_INTERVAL = 15000; | ||
const DEFAULT_GRACE_RATIO = 0.33; | ||
|
||
export class Poll { | ||
private _cancelled = true; | ||
|
||
// This flag is true when the poll is not running. You may | ||
// use it in your code to determine if you should proceed. | ||
public get cancelled(): boolean { | ||
return this._cancelled; | ||
} | ||
|
||
private _counter = 0; | ||
public get counter(): number { | ||
return this._counter; | ||
} | ||
|
||
private graceInterval: number; | ||
private pollInterval: NodeJS.Timer | null = null; | ||
private lastCompleteTime = 0; | ||
private blocked = false; | ||
private isTabActive: boolean; | ||
export interface Poll { | ||
poll: (opts?: { forced?: boolean }) => void; | ||
start: () => void; | ||
stop: () => void; | ||
destroy: () => void; | ||
} | ||
|
||
constructor( | ||
private fn: (poll: Poll) => Promise<any>, | ||
private interval: number, | ||
graceRatio: number, | ||
isTabActive: boolean, | ||
) { | ||
this.poll = this.poll.bind(this); | ||
this.graceInterval = this.interval * graceRatio; | ||
this.isTabActive = isTabActive; | ||
} | ||
const pollFactory = ( | ||
fn: () => Promise<any>, | ||
interval: number, | ||
graceRatio: number, | ||
isTabActive: boolean, | ||
) => { | ||
let cancelled = true; | ||
const graceInterval: number = interval * graceRatio; | ||
let pollInterval: NodeJS.Timer | null = null; | ||
let lastCompleteTime = 0; | ||
let counter = 0; | ||
let blocked = false; | ||
|
||
public async poll(opts?: { forced?: boolean }) { | ||
const poll = async (opts?: { forced?: boolean }) => { | ||
// Only run poll if the previous call has finished and the poll | ||
// is not cancelled | ||
if (this.cancelled || this.blocked) { | ||
if (cancelled || blocked) { | ||
return; | ||
} | ||
|
||
const forced = opts && opts.forced; | ||
// Return if we're invisible, unless it's first run | ||
if (!forced && !this.isTabActive && this.lastCompleteTime !== 0) { | ||
if (!forced && !isTabActive && lastCompleteTime !== 0) { | ||
return; | ||
} | ||
if (!forced && Date.now() - this.lastCompleteTime < this.graceInterval) { | ||
if (!forced && Date.now() - lastCompleteTime < graceInterval) { | ||
return; | ||
} | ||
this.blocked = true; | ||
blocked = true; | ||
|
||
try { | ||
await this.fn(this); | ||
this.lastCompleteTime = Date.now(); | ||
await fn(); | ||
lastCompleteTime = Date.now(); | ||
} finally { | ||
this._counter++; | ||
this.blocked = false; | ||
counter++; | ||
blocked = false; | ||
} | ||
} | ||
}; | ||
|
||
public start() { | ||
const start = () => { | ||
// Only start polling if it is currently stopped. | ||
if (this.pollInterval != null) { | ||
if (pollInterval != null) { | ||
return; | ||
} | ||
this.lastCompleteTime = 0; | ||
this._cancelled = false; | ||
this.pollInterval = setInterval(this.poll, this.interval); | ||
lastCompleteTime = 0; | ||
cancelled = false; | ||
pollInterval = setInterval(poll, interval); | ||
// Also run a poll instantly, as we wanted to start it *now*, | ||
// not X time in the future. | ||
return this.poll(); | ||
} | ||
return poll(); | ||
}; | ||
|
||
public stop() { | ||
if (this.pollInterval) { | ||
clearInterval(this.pollInterval); | ||
const stop = () => { | ||
if (pollInterval) { | ||
clearInterval(pollInterval); | ||
} | ||
this._cancelled = true; | ||
this.pollInterval = null; | ||
} | ||
cancelled = true; | ||
pollInterval = null; | ||
}; | ||
|
||
public destroy() { | ||
this.stop(); | ||
this.fn = async () => undefined; // break possible cyclic reference | ||
} | ||
} | ||
const destroy = () => { | ||
stop(); | ||
fn = async () => undefined; // break possible cyclic reference | ||
}; | ||
|
||
return { | ||
poll, | ||
start, | ||
stop, | ||
destroy, | ||
}; | ||
}; | ||
|
||
export const createPoll = ( | ||
fn: (poll: Poll) => Promise<any>, | ||
fn: () => Promise<any>, | ||
interval: number = DEFAULT_INTERVAL, | ||
isTabActive: boolean = true, | ||
graceRatio: number = DEFAULT_GRACE_RATIO, | ||
) => new Poll(fn, interval, graceRatio, isTabActive); | ||
) => pollFactory(fn, interval, graceRatio, isTabActive); |
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.
I think we do use this in the UI to check whether the poll is cancelled.
Or maybe we only do that for useRequest 🤔 ?