-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd34cb0
commit 3595368
Showing
4 changed files
with
66 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Utils from './utils' | ||
|
||
export default class SimpleQueue { | ||
private q: any[] = [] | ||
private worker: any | ||
private stopped = false | ||
|
||
constructor(worker: any, start: boolean = true) { | ||
this.worker = worker | ||
if (start) { | ||
this.start() | ||
} | ||
} | ||
|
||
/* eslint no-await-in-loop: "off" */ | ||
public start = async () => { | ||
while (!this.stopped) { | ||
const nextValue = this.shift() | ||
if (nextValue) { | ||
await this.worker(nextValue) | ||
await this.yield() | ||
} else { | ||
await this.yield(50) | ||
} | ||
} | ||
} | ||
|
||
private shift = () => { | ||
return this.q.shift() | ||
} | ||
|
||
public yield = async (millisecond: number = 1) => { | ||
return Utils.sleep(millisecond) | ||
} | ||
|
||
public push = (value: any) => { | ||
this.q.push(value) | ||
} | ||
|
||
public stop = () => { | ||
this.stopped = true | ||
|
||
this.push = (value: any) => value | ||
this.clear() | ||
} | ||
|
||
public kill = () => { | ||
this.stop() | ||
} | ||
|
||
public clear = () => { | ||
while (this.q.length) { | ||
this.q.pop() | ||
} | ||
} | ||
|
||
public length = (): number => { | ||
return this.q.length | ||
} | ||
} |
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