Skip to content

Commit 3595368

Browse files
committed
feat: using simple queue
1 parent fd34cb0 commit 3595368

4 files changed

Lines changed: 66 additions & 18 deletions

File tree

packages/neuron-wallet/src/services/sync/block-listener.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ export default class BlockListener {
6060
await this.queue.kill()
6161
}
6262

63-
public drain = async () => {
64-
if (this.queue) {
65-
return this.queue.drain()
66-
}
67-
return undefined
68-
}
69-
7063
public regenerate = async (): Promise<void> => {
7164
if (this.queue && this.queue.length() > 0) {
7265
return

packages/neuron-wallet/src/services/sync/queue.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { Block, BlockHeader } from '../../types/cell-types'
33
import RangeForCheck from './range-for-check'
44
import BlockNumber from './block-number'
55
import Utils from './utils'
6-
import QueueAdapter from './queue-adapter'
6+
import SimpleQueue from './simple-queue'
77
import { TransactionPersistor } from '../tx'
88

99
export default class Queue {
10-
private q: QueueAdapter
11-
private concurrent: number = 1
10+
private q: SimpleQueue
1211
private lockHashes: string[]
1312
private getBlocksService: GetBlocks
1413
private startBlockNumber: bigint
@@ -26,7 +25,7 @@ export default class Queue {
2625
currentBlockNumber: BlockNumber = new BlockNumber(),
2726
rangeForCheck: RangeForCheck = new RangeForCheck()
2827
) {
29-
this.q = new QueueAdapter(this.getWorker(), this.concurrent)
28+
this.q = new SimpleQueue(this.getWorker())
3029
this.lockHashes = lockHashes
3130
this.getBlocksService = new GetBlocks()
3231
this.startBlockNumber = BigInt(startBlockNumber)
@@ -40,12 +39,11 @@ export default class Queue {
4039
}
4140

4241
private getWorker = () => {
43-
const worker = async (task: any, callback: any) => {
42+
const worker = async (task: any) => {
4443
try {
4544
await Utils.retry(this.retryTime, 0, async () => {
4645
await this.pipeline(task.blockNumbers)
4746
})
48-
await callback()
4947
} catch {
5048
this.clear()
5149
}
@@ -69,10 +67,6 @@ export default class Queue {
6967
this.q.kill()
7068
}
7169

72-
public drain = async () => {
73-
return this.q.drain()
74-
}
75-
7670
public pipeline = async (blockNumbers: string[]) => {
7771
// 1. get blocks
7872
const blocks: Block[] = await this.getBlocksService.getRangeBlocks(blockNumbers)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Utils from './utils'
2+
3+
export default class SimpleQueue {
4+
private q: any[] = []
5+
private worker: any
6+
private stopped = false
7+
8+
constructor(worker: any, start: boolean = true) {
9+
this.worker = worker
10+
if (start) {
11+
this.start()
12+
}
13+
}
14+
15+
/* eslint no-await-in-loop: "off" */
16+
public start = async () => {
17+
while (!this.stopped) {
18+
const nextValue = this.shift()
19+
if (nextValue) {
20+
await this.worker(nextValue)
21+
await this.yield()
22+
} else {
23+
await this.yield(50)
24+
}
25+
}
26+
}
27+
28+
private shift = () => {
29+
return this.q.shift()
30+
}
31+
32+
public yield = async (millisecond: number = 1) => {
33+
return Utils.sleep(millisecond)
34+
}
35+
36+
public push = (value: any) => {
37+
this.q.push(value)
38+
}
39+
40+
public stop = () => {
41+
this.stopped = true
42+
43+
this.push = (value: any) => value
44+
this.clear()
45+
}
46+
47+
public kill = () => {
48+
this.stop()
49+
}
50+
51+
public clear = () => {
52+
while (this.q.length) {
53+
this.q.pop()
54+
}
55+
}
56+
57+
public length = (): number => {
58+
return this.q.length
59+
}
60+
}

packages/neuron-wallet/src/startup/sync-block-task/task.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AddressesUsedSubject from '../../models/subjects/addresses-used-subject'
77
import BlockListener from '../../services/sync/block-listener'
88
import { NetworkWithID } from '../../services/networks'
99
import { initDatabase } from './init-database'
10+
import Utils from '../../services/sync/utils'
1011
import { register as registerTxStatusListener } from '../../listeners/tx-status'
1112

1213
import { register as registerAddressListener } from '../../listeners/address'
@@ -58,7 +59,7 @@ export const switchNetwork = async () => {
5859
const regenerateListener = async () => {
5960
await blockListener.stop()
6061
// wait former queue to be drained
61-
await blockListener.drain()
62+
await Utils.sleep(3000)
6263
const hashes: string[] = await loadAddressesAndConvert()
6364
blockListener = new BlockListener(hashes, nodeService.tipNumberSubject)
6465
await blockListener.start(true)

0 commit comments

Comments
 (0)