-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_mapping.js
44 lines (37 loc) · 914 Bytes
/
id_mapping.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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
"use strict";
import Utils from "./utils";
class IdMapping {
constructor() {
this.intIds = new Map;
}
tryIntifyId(payload) {
if (!payload.id) {
payload.id = Utils.genId();
return;
}
if (typeof payload.id !== "number") {
let newId = Utils.genId();
this.intIds.set(newId, payload.id);
payload.id = newId;
}
}
tryRestoreId(payload) {
let id = this.tryPopId(payload.id);
if (id) {
payload.id = id;
}
}
tryPopId(id) {
let originId = this.intIds.get(id);
if (originId) {
this.intIds.delete(id);
}
return originId;
}
}
module.exports = IdMapping;