Skip to content

Commit 52095e5

Browse files
committed
perf: add debounce and sample on subjects for performance
1 parent 476a918 commit 52095e5

6 files changed

Lines changed: 38 additions & 13 deletions

File tree

packages/neuron-ui/src/services/subjects.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const FallbackSubject = {
1515
},
1616
}
1717
export const SystemScript = window.remote
18-
? (window.remote.require(`${SUBJECT_PATH}/system-script`).default as NeuronWalletSubject<{ codeHash: string }>)
18+
? (window.remote.require(`${SUBJECT_PATH}/system-script`).DebouncedSystemScriptSubject as NeuronWalletSubject<{
19+
codeHash: string
20+
}>)
1921
: FallbackSubject
2022

2123
export const DataUpdate = window.remote
@@ -27,23 +29,23 @@ export const DataUpdate = window.remote
2729
: FallbackSubject
2830

2931
export const NetworkList = window.remote
30-
? (window.remote.require(`${SUBJECT_PATH}/networks`).NetworkListSubject as NeuronWalletSubject<{
32+
? (window.remote.require(`${SUBJECT_PATH}/networks`).DebouncedNetworkListSubject as NeuronWalletSubject<{
3133
currentNetworkList: State.Network[]
3234
}>)
3335
: FallbackSubject
3436

3537
export const CurrentNetworkID = window.remote
36-
? (window.remote.require(`${SUBJECT_PATH}/networks`).CurrentNetworkIDSubject as NeuronWalletSubject<{
38+
? (window.remote.require(`${SUBJECT_PATH}/networks`).DebouncedCurrentNetworkIDSubject as NeuronWalletSubject<{
3739
currentNetworkID: string
3840
}>)
3941
: FallbackSubject
4042

4143
export const ConnectionStatus = window.remote
42-
? (window.remote.require(`${SUBJECT_PATH}/node`).ConnectionStatusSubject as NeuronWalletSubject<boolean>)
44+
? (window.remote.require(`${SUBJECT_PATH}/node`).DebouncedConnectionStatusSubject as NeuronWalletSubject<boolean>)
4345
: FallbackSubject
4446

4547
export const SyncedBlockNumber = window.remote
46-
? (window.remote.require(`${SUBJECT_PATH}/node`).SyncedBlockNumberSubject as NeuronWalletSubject<string>)
48+
? (window.remote.require(`${SUBJECT_PATH}/node`).SampledSyncedBlockNumberSubject as NeuronWalletSubject<string>)
4749
: FallbackSubject
4850

4951
export const Command = window.remote

packages/neuron-wallet/src/models/lock-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import NodeService from '../services/node'
22
import { OutPoint, Script } from '../types/cell-types'
33
import env from '../env'
4-
import systemScriptSubject from './subjects/system-script'
4+
import { SystemScriptSubject } from './subjects/system-script'
55

66
const { core } = NodeService.getInstance()
77

@@ -15,7 +15,7 @@ const subscribed = (target: any, propertyName: string) => {
1515
Object.defineProperty(target, propertyName, {
1616
get: () => value,
1717
set: (info: { codeHash: string }) => {
18-
systemScriptSubject.next({ codeHash: info.codeHash })
18+
SystemScriptSubject.next({ codeHash: info.codeHash })
1919
value = info
2020
},
2121
})
@@ -67,7 +67,7 @@ export default class LockUtils {
6767

6868
static setSystemScript(info: SystemScript) {
6969
LockUtils.systemScriptInfo = info
70-
systemScriptSubject.next({ codeHash: info.codeHash })
70+
SystemScriptSubject.next({ codeHash: info.codeHash })
7171
}
7272

7373
// use SDK lockScriptToHash

packages/neuron-wallet/src/models/subjects/current-block-subject.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ReplaySubject } from 'rxjs'
2-
import { sampleTime } from 'rxjs/operators'
32
import { SyncedBlockNumberSubject } from './node'
43

54
export interface CurrentBlockInfo {
@@ -15,7 +14,7 @@ export class CurrentBlockSubject {
1514
}
1615

1716
static subscribe() {
18-
CurrentBlockSubject.subject.pipe(sampleTime(500)).subscribe(({ blockNumber }) => {
17+
CurrentBlockSubject.subject.subscribe(({ blockNumber }) => {
1918
SyncedBlockNumberSubject.next(blockNumber)
2019
})
2120
}

packages/neuron-wallet/src/models/subjects/networks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { BehaviorSubject } from 'rxjs'
2+
import { debounceTime } from 'rxjs/operators'
3+
4+
const DEBOUNCE_TIME = 50
25

36
export const NetworkListSubject = new BehaviorSubject<{
47
currentNetworkList: Controller.Network[]
@@ -7,7 +10,12 @@ export const CurrentNetworkIDSubject = new BehaviorSubject<{ currentNetworkID: C
710
currentNetworkID: '',
811
})
912

13+
export const DebouncedNetworkListSubject = NetworkListSubject.pipe(debounceTime(DEBOUNCE_TIME))
14+
export const DebouncedCurrentNetworkIDSubject = CurrentNetworkIDSubject.pipe(debounceTime(DEBOUNCE_TIME))
15+
1016
export default {
1117
NetworkListSubject,
1218
CurrentNetworkIDSubject,
19+
DebouncedNetworkListSubject,
20+
DebouncedCurrentNetworkIDSubject,
1321
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { BehaviorSubject } from 'rxjs'
2+
import { debounceTime, sampleTime } from 'rxjs/operators'
3+
4+
const DEBOUNCE_TIME = 50
5+
const SAMPLE_TIME = 500
26

37
export const ConnectionStatusSubject = new BehaviorSubject<boolean>(false)
48
export const SyncedBlockNumberSubject = new BehaviorSubject<string>('0')
59

6-
export default { ConnectionStatusSubject, SyncedBlockNumberSubject }
10+
export const DebouncedConnectionStatusSubject = ConnectionStatusSubject.pipe(debounceTime(DEBOUNCE_TIME))
11+
export const SampledSyncedBlockNumberSubject = SyncedBlockNumberSubject.pipe(sampleTime(SAMPLE_TIME))
12+
13+
export default {
14+
ConnectionStatusSubject,
15+
SyncedBlockNumberSubject,
16+
DebouncedConnectionStatusSubject,
17+
SampledSyncedBlockNumberSubject,
18+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { BehaviorSubject } from 'rxjs'
2+
import { debounceTime } from 'rxjs/operators'
23

3-
const systemScriptSubject = new BehaviorSubject<{ codeHash: string }>({ codeHash: '' })
4+
const DEBOUNCE_TIME = 50
45

5-
export default systemScriptSubject
6+
export const SystemScriptSubject = new BehaviorSubject<{ codeHash: string }>({ codeHash: '' })
7+
export const DebouncedSystemScriptSubject = SystemScriptSubject.pipe(debounceTime(DEBOUNCE_TIME))
8+
9+
export default { SystemScriptSubject, DebouncedSystemScriptSubject }

0 commit comments

Comments
 (0)