Skip to content

Commit caa20db

Browse files
author
Pavel Petroshenko
authored
Merge pull request #31 from electricimp/develop
3.0.1
2 parents 75ab5d9 + df8b14e commit caa20db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+985
-152
lines changed

.imptest

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"agentFile": "promise.class.nut",
77
"deviceFile": false,
88
"stopOnFailure": false,
9-
"timeout": 30,
9+
"timeout": 60,
1010
"tests": [
1111
"*.test.nut",
1212
"tests/**/*.test.nut"

LICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
MIT License
2+
3+
Copyright 2016-2017 Electric Imp
4+
Copyright 2015 SMS Diagnostics Pty Ltd
5+
6+
SPDX-License-Identifier: MIT
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
21+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22+
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24+
OTHER DEALINGS IN THE SOFTWARE.

LICENSE.txt

-22
This file was deleted.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ For more information on the concept of promises, see the following references fo
1010
- [JavaScript Promises: There and back again](http://www.html5rocks.com/en/tutorials/es6/promises/)
1111
- [Promise - Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
1212

13-
**To add this library to your project, add** `#require "promise.class.nut:3.0.0"` **to the top of your agent and/or device code.**
13+
**To add this library to your project, add** `#require "promise.class.nut:3.0.1"` **to the top of your agent and/or device code.**
1414

1515
[![Build Status](https://travis-ci.org/electricimp/Promise.svg?branch=master)](https://travis-ci.org/electricimp/Promise)
1616

@@ -24,7 +24,7 @@ The constructor should receive a single function, which will be executed to dete
2424
- `function reject([reason])` — calling *reject()* sets Promise state as rejected and calls *.fail()* handlers
2525

2626
```squirrel
27-
#require "promise.class.nut:3.0.0"
27+
#require "promise.class.nut:3.0.1"
2828
2929
myPromise <- Promise(myActionFunction);
3030
```

examples/example-a.nut

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
#require "Promise.class.nut:3.0.0"
1+
// MIT License
2+
//
3+
// Copyright 2017 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
#require "Promise.class.nut:3.0.1"
226

327
// -----------------------------------------------------------------------------
428
// This class exposed a single method fire() which waits a second and then randomly fails or succeeds.

examples/example-b.nut

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
#require "Promise.class.nut:3.0.0"
1+
// MIT License
2+
//
3+
// Copyright 2017 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
#require "Promise.class.nut:3.0.1"
226

327
/**
428
* Example of resolving Promise after 1 second timeout

examples/example-c.nut

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
#require "Promise.class.nut:3.0.0"
1+
// MIT License
2+
//
3+
// Copyright 2017 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
#require "Promise.class.nut:3.0.1"
226

327
/**
428
* Uses a chain of promises to call an API and make some transformations on the
@@ -21,7 +45,7 @@
2145
* traditional, callback style function in a promise.
2246
*/
2347
class MorseCode {
24-
static URL_TEMPLATE = "http://www.morsecode-api.de/%s/%s";
48+
static URL_TEMPLATE = "http://www.morsecode-api.de/%s?string=%s";
2549

2650
_message = null;
2751

@@ -91,14 +115,14 @@ class MorseCode {
91115
*/
92116
static function PromisedSendAsync(request) {
93117
return Promise(function(resolve, reject) {
94-
118+
95119
request.sendasync(function(response) {
96120
if (response.statuscode < 200 || response.statuscode > 299) {
97121
return reject(response);
98122
}
99123
return resolve(response); // Resolve promise on response
100124
}.bindenv(this));
101-
125+
102126
}.bindenv(this));
103127
}
104128

promise.class.nut

+51-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1+
// MIT License
2+
//
3+
// Copyright 2016-2017 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
125
/**
226
* Promises for Electric Imp/Squirrel
327
*
428
* @author Mikhail Yurasov <mikhail@electricimp.com>
529
* @author Aron Steg <aron@electricimp.com>
630
* @author Jaye Heffernan <jaye@mysticpants.com>
7-
* @version 3.0.0
31+
* @version 3.0.1
832
*/
33+
34+
// Error messages
35+
const PROMISE_ERR_UNHANDLED_REJ = "Unhandled promise rejection: ";
36+
937
class Promise {
10-
static version = [3, 0, 0];
38+
static VERSION = "3.0.1";
1139

1240
static STATE_PENDING = 0;
1341
static STATE_FULFILLED = 1;
1442
static STATE_REJECTED = 2;
1543

1644
_state = null;
1745
_value = null;
46+
_last = null;
1847

1948
/* @var {{resolve, reject}[]} _handlers */
2049
_handlers = null;
@@ -24,6 +53,7 @@ class Promise {
2453
*/
2554
constructor(action) {
2655
this._state = this.STATE_PENDING;
56+
this._last = true;
2757
this._handlers = [];
2858

2959
try {
@@ -42,6 +72,10 @@ class Promise {
4272
function _callHandlers() {
4373
if (this.STATE_PENDING != this._state) {
4474
imp.wakeup(0, function() {
75+
if (this._last && this._handlers.len() == 0
76+
&& this.STATE_REJECTED == this._state) {
77+
server.log(PROMISE_ERR_UNHANDLED_REJ + this._value);
78+
}
4579
foreach (handler in this._handlers) {
4680
(/* create closure and bind handler to it */ function (handler) {
4781
if (this._state == this.STATE_FULFILLED) {
@@ -127,6 +161,7 @@ class Promise {
127161
onRejected = (typeof onRejected == "function") ? onRejected : Promise._onRejected;
128162

129163
local self = this;
164+
this._last = false;
130165
local result = Promise(function(resolve, reject) {
131166
self._handlers.push({
132167
"resolve": resolve.bindenv(this),
@@ -209,19 +244,27 @@ class Promise {
209244
/**
210245
* Returns Promise that resolves when
211246
* all promises in chain resolve:
212-
* one after each other
247+
* one after each other.
248+
* Make every promise in array of promises not last
249+
* for suppressing Unhadled Exeptions Warnings
213250
*
214251
* @param {{Promise|function}[]} promises - array of Promises/functions that return Promises
215252
* @return {Promise} Promise that is resolved/rejected with the last value that come from looped promise
216253
*/
217254
static function serial(promises) {
218255
local i = 0;
256+
local onlyPromises = [];
257+
for (local t = 0; t < promises.len(); t++) {
258+
local pr = "function" == type(promises[t])
259+
? promises[t]()
260+
: promises[t];
261+
pr._last = false;
262+
onlyPromises.push(pr);
263+
}
219264
return this.loop(
220-
@() i < promises.len(),
265+
@() i < onlyPromises.len(),
221266
function () {
222-
return "function" == type(promises[i])
223-
? promises[i++]()
224-
: promises[i++];
267+
return onlyPromises[i++];
225268
}
226269
)
227270
}
@@ -254,7 +297,7 @@ class Promise {
254297
result[index] = value;
255298
resolved += 1;
256299
if (resolved == len) {
257-
resolve(result)
300+
resolve(result);
258301
}
259302
};
260303

tests/All.agent.test.nut

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
// Copyright (c) 2016 Electric Imp
2-
// This file is licensed under the MIT License
3-
// http://opensource.org/licenses/MIT
1+
// MIT License
2+
//
3+
// Copyright 2016 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
424

525
// "Promise" symbol is injected dependency from ImpUnit_Promise module,
626
// while class being tested can be accessed from global scope as "::Promise".

tests/Basic.agent.test.nut

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1-
// Copyright (c) 2016 Electric Imp
2-
// This file is licensed under the MIT License
3-
// http://opensource.org/licenses/MIT
4-
1+
// MIT License
2+
//
3+
// Copyright 2016 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
524
// "Promise" symbol is injected dependency from ImpUnit_Promise module,
625
// while class being tested can be accessed from global scope as "::Promise".
726

0 commit comments

Comments
 (0)