Skip to content

Commit f2520ea

Browse files
comcastmikeMichael Fiess
authored andcommitted
suspend after ready and remoteReady and returns promise (#2001)
1 parent c754542 commit f2520ea

File tree

1 file changed

+90
-17
lines changed

1 file changed

+90
-17
lines changed

examples/pxScene2d/src/rcvrcore/optimus.js

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function Application(props) {
6969

7070
// Public variables
7171
this.id = undefined;
72-
this.priority = 1;
72+
this.priority = 5;
7373
this.name = "";
7474
this.createTime = 0;
7575
this.uri = "";
@@ -190,41 +190,111 @@ function Application(props) {
190190
var w = 0;
191191
var h = 0;
192192
var uri = "";
193+
var hasApi = true;
193194
var serviceContext = {};
194195
var launchParams;
195196
var _externalApp;
196197
var _browser;
197198
var _state = ApplicationState.RUNNING;
198199

199-
// Public functions that use _externalApp
200-
// Suspends the application. Returns true if the application was suspended, or false otherwise
201-
this.suspend = function(o) {
202-
var ret;
200+
// Internal function needed for suspend
201+
var do_suspend_internal = function(o)
202+
{
203+
//basic failure conditions
203204
if (_state === ApplicationState.DESTROYED){
204-
this.log("suspend on already destroyed app");
205+
_this.log("suspend on already destroyed app");
205206
return false;
206207
}
207208
if (_state === ApplicationState.SUSPENDED){
208-
this.log("suspend on already suspended app");
209+
_this.log("suspend on already suspended app");
209210
return false;
210211
}
211212
if (!_externalApp || !_externalApp.suspend){
212-
this.log("suspend api not available on app");
213+
_this.log("suspend api not available on app");
213214
_state = ApplicationState.SUSPENDED;
214-
this.applicationSuspended();
215+
_this.applicationSuspended();
215216
return false;
216217
}
217-
ret = _externalApp.suspend(o);
218+
219+
var ret = _externalApp.suspend(o);
220+
218221
if (ret === true) {
219222
_state = ApplicationState.SUSPENDED;
220-
this.applicationSuspended();
221-
this.logTelemetry("suspend", true);
222-
} else {
223-
this.log("suspend returned:", ret);
224-
this.logTelemetry("suspend", false);
223+
_this.applicationSuspended();
225224
}
225+
226+
_this.log("suspend returned:", ret);
227+
226228
return ret;
229+
}
230+
231+
// Public functions that use _externalApp
232+
// Suspends the application. Returns promise if the application was suspended or not.
233+
this.suspend = function(o) {
234+
235+
//setup return promise
236+
var ret_promise_resolve;
237+
var ret_promise_reject;
238+
var ret_promise = new Promise(function (resolve, reject) {
239+
ret_promise_resolve = resolve;
240+
ret_promise_reject = reject;
241+
});
242+
243+
//telemetry
244+
ret_promise.then(
245+
function()
246+
{
247+
_this.logTelemetry("suspend", true);
248+
},
249+
function()
250+
{
251+
_this.logTelemetry("suspend", false);
252+
});
253+
254+
this.readyBase.then(
255+
function()
256+
{
257+
//needs also remoteReady?
258+
if(_externalApp && _externalApp.hasApi)
259+
{
260+
_externalApp.remoteReady.then(
261+
function()
262+
{
263+
//readyBase and remoteReady succeeded so suspend
264+
var suspend_ret = do_suspend_internal(o);
265+
266+
if(suspend_ret)
267+
ret_promise_resolve();
268+
else
269+
ret_promise_reject();
270+
},
271+
function()
272+
{
273+
_this.log("suspend remoteReady failed");
274+
ret_promise_reject();
275+
});
276+
}
277+
else
278+
{
279+
//otherwise attempt suspend now
280+
var suspend_ret = do_suspend_internal(o);
281+
282+
if(suspend_ret)
283+
ret_promise_resolve();
284+
else
285+
ret_promise_reject();
286+
}
287+
},
288+
function()
289+
{
290+
_this.log("suspend readyBase failed");
291+
ret_promise_reject();
292+
}
293+
);
294+
295+
return ret_promise;
227296
};
297+
228298
// Resumes a suspended application. Returns true if the application was resumed, or false otherwise
229299
this.resume = function(o) {
230300
var ret;
@@ -409,6 +479,9 @@ function Application(props) {
409479
if ("h" in props){
410480
h = props.h;
411481
}
482+
if ("hasApi" in props){
483+
hasApi = props.hasApi;
484+
}
412485
if (cmd === "wpe" && uri){
413486
cmd = cmd + " " + uri;
414487
}
@@ -419,7 +492,7 @@ function Application(props) {
419492
this.expectedMemoryUsage = props.expectedMemoryUsage;
420493
}
421494

422-
this.log("cmd:",cmd,"uri:",uri,"w:",w,"h:",h);
495+
this.log("cmd:",cmd,"uri:",uri,"w:",w,"h:",h,"hasApi:",hasApi);
423496

424497
if (!cmd) {
425498
this.log('cannot create app because cmd is not set');
@@ -587,7 +660,7 @@ function Application(props) {
587660
}
588661
else{
589662
this.type = ApplicationType.NATIVE;
590-
_externalApp = scene.create( {t:"external", parent:root, cmd:cmd, w:w, h:h, hasApi:true} );
663+
_externalApp = scene.create( {t:"external", parent:root, cmd:cmd, w:w, h:h, hasApi:hasApi} );
591664
_externalApp.on("onReady", function () { _this.log("onReady"); }); // is never called
592665
_externalApp.on("onClientStarted", function () { _this.log("onClientStarted"); });
593666
_externalApp.on("onClientConnected", function () { _this.log("onClientConnected"); });

0 commit comments

Comments
 (0)