Skip to content

Commit 17a85e2

Browse files
author
Pavel Petroshenko
authored
Merge pull request #32 from electricimp/develop
v3.1.1
2 parents 41b24d5 + 66f666a commit 17a85e2

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
2-
.DS_Store
1+
auth.info

ConnectionManager.lib.nut

+22-21
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const CM_DEFAULT_CALLBACK_ID = "DEFAULT_CB_ID";
3535

3636
class ConnectionManager {
3737

38-
static VERSION = "3.1.0";
38+
static VERSION = "3.1.1";
3939

4040
// Settings
4141
_connectTimeout = null;
@@ -361,9 +361,25 @@ class ConnectionManager {
361361
}
362362
}
363363

364+
function _invokeCallbacks(callbacks, arg = null) {
365+
foreach (id, clbk in callbacks) {
366+
local ctx = {
367+
"arg" : arg,
368+
"ctxClbk" : clbk
369+
};
370+
if (clbk && typeof clbk == "function") {
371+
// TODO: we assume that null is not a valid value of the argument, it's null only if it's unset.
372+
if (arg == null) {
373+
imp.wakeup(0, function() {ctxClbk();}.bindenv(ctx));
374+
} else {
375+
imp.wakeup(0, function() {ctxClbk(arg);}.bindenv(ctx));
376+
}
377+
}
378+
}
379+
}
380+
364381
// Runs whenever we connect or call connect()
365382
function _onConnectedFlow() {
366-
367383
// Set the BlinkUp State
368384
_setBlinkUpState();
369385

@@ -378,14 +394,8 @@ class ConnectionManager {
378394

379395
// Run the global onConnected Handler if it exists
380396
if (_onConnect != null) {
381-
// Invoke all the callbacks in the loop
382-
foreach (id, callback in _onConnect) {
383-
callback &&
384-
typeof callback == "function" &&
385-
imp.wakeup(0, function() {
386-
callback();
387-
}.bindenv(this));
388-
}
397+
// Invoke all the callbacks in the loop
398+
_invokeCallbacks(_onConnect);
389399
}
390400

391401
_processQueue();
@@ -399,11 +409,7 @@ class ConnectionManager {
399409

400410
// Run the global onDisconnected Handler if it exists
401411
if (_onDisconnect != null) {
402-
imp.wakeup(0, function() {
403-
foreach (id, callback in _onDisconnect) {
404-
callback && callback(expected);
405-
}
406-
}.bindenv(this));
412+
_invokeCallbacks(_onDisconnect, expected);
407413
}
408414

409415
if (_stayConnected) {
@@ -424,12 +430,7 @@ class ConnectionManager {
424430
_connecting = false;
425431
_connected = false;
426432
if (_onTimeout != null) {
427-
imp.wakeup(0, function() {
428-
// Invoke all the callbacks
429-
foreach (id, callback in _onTimeout) {
430-
callback && callback();
431-
}
432-
}.bindenv(this));
433+
_invokeCallbacks(_onTimeout);
433434
}
434435

435436
if (_retryOnTimeout) {

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# ConnectionManager 3.1.0 #
1+
# ConnectionManager 3.1.1 #
22

33
The ConnectionManager class is an Electric Imp device-side library created to simplify connect and disconnect flows.
44

55
**Note** If you are using ConnectionManager in your code, you should ensure that you *never* call [**server.connect()**](https://developer.electricimp.com/api/server/connect) or [**server.disconnect()**](https://developer.electricimp.com/api/server/disconnect). Instead you should only use ConnectionManager’s [*connect()*](#connect) and [*disconnect()*](#disconnectforce-flushtimeout) methods.
66

7-
**To include this library in your project, add** `#require "ConnectionManager.lib.nut:3.1.0"` **at the top of your device code.**
7+
**To include this library in your project, add** `#require "ConnectionManager.lib.nut:3.1.1"` **at the top of your device code.**
88

99
## Class Usage ##
1010

@@ -27,7 +27,7 @@ ConnectionManager can be instantiated with an optional table of settings that mo
2727
#### Example ####
2828

2929
```squirrel
30-
#require "ConnectionManager.lib.nut:3.1.0"
30+
#require "ConnectionManager.lib.nut:3.1.1"
3131
3232
// Instantiate ConnectionManager so BlinkUp is always enabled,
3333
// and we automatically aggressively try to reconnect on disconnect

example/ConnectionManager.example.nut

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// OTHER DEALINGS IN THE SOFTWARE.
2424

2525

26-
#require "ConnectionManager.lib.nut:3.1.0"
26+
#require "ConnectionManager.lib.nut:3.1.1"
2727

2828
// Instantiate ConnectionManager so BlinkUp is always enabled,
2929
// and starts connected.

tests/ConnectDisconnectTest.test.nut

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ConnectDisconnectTest extends CommonTest {
3030
function setUp() {
3131
_setUp();
3232
}
33-
33+
3434
/*
3535
* disconnects and connects device using CM
3636
*
@@ -139,7 +139,7 @@ class ConnectDisconnectTest extends CommonTest {
139139
assertTrue(!server.isconnected(), "should NOT be connected!");
140140

141141
_cm.onConnect(function() {
142-
assertEqual(counter, 2, "counter should be 2 now");
142+
assertEqual(counter, 2, "counter should be 2 now, while it is: " + counter);
143143
counter++;
144144
resolve();
145145
}.bindenv(this));

0 commit comments

Comments
 (0)