-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectActionHandler.js
52 lines (44 loc) · 1.52 KB
/
ObjectActionHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { AbstractActionHandler } = require("demux")
// Initial state
let state = {
volumeBySymbol: {},
totalTransfers: 0,
indexState: {
blockNumber: 0,
blockHash: "",
isReplay: false,
handlerVersionName: "v1",
},
}
const stateHistory = {}
const stateHistoryMaxLength = 300
class ObjectActionHandler extends AbstractActionHandler {
async handleWithState(handle) {
await handle(state)
const { blockNumber } = state.indexState
stateHistory[blockNumber] = JSON.parse(JSON.stringify(state))
if (blockNumber > stateHistoryMaxLength && stateHistory[blockNumber - stateHistoryMaxLength]) {
delete stateHistory[blockNumber - stateHistoryMaxLength]
}
}
async loadIndexState() {
return state.indexState
}
async updateIndexState(stateObj, block, isReplay, handlerVersionName) {
stateObj.indexState.blockNumber = block.blockInfo.blockNumber
stateObj.indexState.blockHash = block.blockInfo.blockHash
stateObj.indexState.isReplay = isReplay
stateObj.indexState.handlerVersionName = handlerVersionName
}
async rollbackTo(blockNumber) {
const latestBlockNumber = state.indexState.blockNumber
const toDelete = [...Array(latestBlockNumber - (blockNumber)).keys()].map(n => n + blockNumber + 1)
for (const n of toDelete) {
delete stateHistory[n]
}
state = stateHistory[blockNumber]
}
async setup() {
}
}
module.exports = ObjectActionHandler