-
Notifications
You must be signed in to change notification settings - Fork 17
/
AblyLibrary.java
110 lines (88 loc) · 3.3 KB
/
AblyLibrary.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package io.ably.flutter.plugin;
import android.util.LongSparseArray;
import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.rest.AblyRest;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.AsyncPaginatedResult;
import io.ably.lib.types.ClientOptions;
class AblyLibrary {
private static AblyLibrary _instance;
private long _nextHandle = 1;
// privatizing default constructor to enforce usage of getInstance
private AblyLibrary() {
}
static synchronized AblyLibrary getInstance() {
if (null == _instance) {
_instance = new AblyLibrary();
}
return _instance;
}
// using LongSparseArray as suggested by Studio
// and as per this answer https://stackoverflow.com/a/31413003
private final LongSparseArray<AblyRest> _restInstances = new LongSparseArray<>();
private final LongSparseArray<Object> _restTokenData = new LongSparseArray<>();
private final LongSparseArray<AblyRealtime> _realtimeInstances = new LongSparseArray<>();
private final LongSparseArray<Object> _realtimeTokenData = new LongSparseArray<>();
private final LongSparseArray<AsyncPaginatedResult<Object>> _paginatedResults = new LongSparseArray<>();
long getCurrentHandle() {
return _nextHandle;
}
long createRest(final ClientOptions clientOptions) throws AblyException {
final AblyRest rest = new AblyRest(clientOptions);
_restInstances.put(_nextHandle, rest);
return _nextHandle++;
}
AblyRest getRest(final long handle) {
return _restInstances.get(handle);
}
void setRestToken(long handle, Object tokenDetails) {
_restTokenData.put(handle, tokenDetails);
}
Object getRestToken(long handle) {
Object token = _restTokenData.get(handle);
_restTokenData.remove(handle);
return token;
}
long createRealtime(final ClientOptions clientOptions) throws AblyException {
final AblyRealtime realtime = new AblyRealtime(clientOptions);
_realtimeInstances.put(_nextHandle, realtime);
return _nextHandle++;
}
AblyRealtime getRealtime(final long handle) {
return _realtimeInstances.get(handle);
}
void setRealtimeToken(long handle, Object tokenDetails) {
_realtimeTokenData.put(handle, tokenDetails);
}
Object getRealtimeToken(long handle) {
Object token = _realtimeTokenData.get(handle);
_realtimeTokenData.remove(handle);
return token;
}
long setPaginatedResult(AsyncPaginatedResult result, Integer handle){
long longHandle;
if(handle==null){
longHandle = _nextHandle++;
}else {
longHandle = handle.longValue();
}
_paginatedResults.put(longHandle, result);
return longHandle;
}
AsyncPaginatedResult<Object> getPaginatedResult(long handle){
return _paginatedResults.get(handle);
}
void dispose() {
for (int i = 0; i < _realtimeInstances.size(); i++) {
long key = _realtimeInstances.keyAt(i);
AblyRealtime r = _realtimeInstances.get(key);
try {
r.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
_realtimeInstances.clear();
_restInstances.clear();
}
}