Skip to content

Commit 6e88288

Browse files
author
engg@coditation
committed
Updated queue based message sending
1 parent 9acc9b3 commit 6e88288

File tree

3 files changed

+267
-52
lines changed

3 files changed

+267
-52
lines changed

lib/AsyncStorageBackedQueue.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {InteractionManager, Text} from 'react-native';
2+
import {AsyncStorage} from 'react-native';
3+
4+
5+
AsyncStorageBackedQueue = function(config) {
6+
if('xmpp_client' in config) {
7+
this.xmpp_client = config.xmpp_client;
8+
}
9+
if('retry_interval_sec' in config) {
10+
this.retry_interval_sec = config.retry_interval_sec;
11+
}
12+
if('queue_name' in config) {
13+
this.queue_name = config.queue_name;
14+
AsyncStorage.getItem(this.queue_name).then((data) => {
15+
if(data) {
16+
this.queue = JSON.parse(data);
17+
}
18+
});
19+
} else {
20+
throw {
21+
"messgae": "Queue name missing"
22+
}
23+
}
24+
}
25+
26+
AsyncStorageBackedQueue.prototype.queue = []
27+
AsyncStorageBackedQueue.prototype.queue_name = undefined
28+
AsyncStorageBackedQueue.prototype.is_dirty = false
29+
AsyncStorageBackedQueue.prototype.retry_interval_sec = 15;
30+
AsyncStorageBackedQueue.prototype.xmpp_client = undefined;
31+
AsyncStorageBackedQueue.prototype.interval = undefined;
32+
33+
34+
AsyncStorageBackedQueue.prototype.dequeue_element = function(id, id_field) {
35+
var index = this.queue.map(function(el) {
36+
return el[id_field];
37+
}).indexOf(id);
38+
if(index != -1) {
39+
var dequeued_element = this.queue.splice(index, 1);
40+
this._persist_queue();
41+
return dequeued_element;
42+
}
43+
return undefined;
44+
}
45+
46+
AsyncStorageBackedQueue.prototype.queue_element = function(element) {
47+
this.queue.push(element);
48+
this.is_dirty = true;
49+
this._persist_queue();
50+
}
51+
52+
53+
AsyncStorageBackedQueue.prototype.dequeue = function () {
54+
var element = this.queue.pop();
55+
this._persist_queue();
56+
return element;
57+
}
58+
59+
AsyncStorageBackedQueue.prototype.filter = function(filter_fn) {
60+
var res = this.queue.filter(filter_fn);
61+
if(res && res.length > 0) {
62+
return res[0];
63+
}
64+
}
65+
66+
AsyncStorageBackedQueue.prototype._persist_queue = function() {
67+
var that = this;
68+
AsyncStorage.setItem(that.queue_name, JSON.stringify(that.queue),
69+
() => {
70+
is_dirty = false;
71+
},
72+
(error) => {
73+
console.log("Error while inserting data");
74+
});
75+
}
76+
77+
AsyncStorageBackedQueue.prototype._retry_send = function() {
78+
var that = this;
79+
this.interval = setInterval(function() {
80+
InteractionManager.runAfterInteractions(() => {
81+
that.queue.map(function(el) {
82+
if((new Date().getTime() - el.send_timestamp) >= that.retry_interval_sec*1000) {
83+
if(that.xmpp_client) {
84+
try {
85+
that.xmpp_client.push(el.to, el.str, el.type, [el.message], true);
86+
}
87+
catch(e) {
88+
89+
}
90+
}
91+
}
92+
});
93+
});
94+
}, that.retry_interval_sec*1000);
95+
}
96+
97+
AsyncStorageBackedQueue.prototype.stop_retry_send = function() {
98+
if (this.interval !== undefined) {
99+
clearInterval(this.interval)
100+
}
101+
}
102+
103+
AsyncStorageBackedQueue.prototype.start_retry = function() {
104+
this._retry_send();
105+
}
106+
107+
exports.AsyncStorageBackedQueue = AsyncStorageBackedQueue;

0 commit comments

Comments
 (0)