-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrors.java
More file actions
68 lines (53 loc) · 1.52 KB
/
Errors.java
File metadata and controls
68 lines (53 loc) · 1.52 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package wish;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by jeppe on 3/2/17.
*/
public class Errors {
private static Errors instance;
static Errors getInstance() {
if (instance == null) {
instance = new Errors();
}
return instance;
}
private int id;
private HashMap<Integer, ListenCb> wishCbHashMap;
private Errors() {
id = 1;
wishCbHashMap = new HashMap<>();
WishApp.registerRpcErrorHandler(new WishApp.Error() {
@Override
public void cb(int code, String msg) {
wishError(code, msg);
}
});
}
private int registerCb(ListenCb cb) {
wishCbHashMap.put(id, cb);
return id++;
}
public static int listen(ListenCb callback) {
return getInstance().registerCb(callback);
}
public interface ListenCb {
public void cb(int code, String msg);
}
public static void cancel(int id) {
if (getInstance().wishCbHashMap.containsKey(id)) {
getInstance().wishCbHashMap.remove(id);
return;
}
}
static void wishError(int code, String msg) {
Iterator iterator = getInstance().wishCbHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
ListenCb listenCb = (ListenCb) pair.getValue();
listenCb.cb(code, msg);
iterator.remove();
}
}
}