How do I check if a promise or deferred object is completed? It looks like the state management is done by the resolver which is hidden via closure. Am I missing another way to access the state?
--
Deft JS home page: http://deftjs.org/
Wiki: https://github.com/deftjs/DeftJS/wiki
For rules and tips on asking questions on the Deft JS mailing list: https://github.com/deftjs/DeftJS/wiki/Asking-Questions-on-the-Mailing-List
---
You received this message because you are subscribed to the Google Groups "Deft JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deftjs+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/deftjs/bdbc4f8f-c747-4d1b-bc63-65234190b88d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
//
// IdleMonitor is responsible for monitoring the system for non-idle activity and providing API
// hooks for clients to observe idleness.
//
Ext.define('Fc.tc.service.IdleMonitor', {
config: {
documentEvents: ['touchstart', 'touchmove', 'touchend', 'keydown', 'keyup']
},
constructor: function (config) {
var me = this;
me.lastActivity = null;
me.initConfig(config);
// defer start, so we don't hold up ui
Ext.defer(me.start, 100, me);
},
start: function () {
var me = this,
onActivity = Ext.bind(me.onActivity, me);
// listen to activity events
Ext.each(me.getDocumentEvents(), function (event) {
document.addEventListener(event, onActivity, false);
});
},
onActivity: function () {
this.lastActivity = new Date();
},
idleSince: function () {
return Ext.Date.clone(this.lastActivity); // pass by value, not by reference
},
startIdleTimer: function (args, millis) {
window.setTimeout(Ext.bind(me.idleTimeout, me, args), millis);
},
idleTimeout: function (deferred, millis) {
var me = this;
if(deferred.canceled) {
// ignore
return;
}
if (me.lastActivity < Ext.Date.add(new Date(), Ext.Date.MILLI, -millis)) {
deferred.resolve();
}
else {
// restart timer, adjust delay for next possible idle state
me.startIdleTimer(
[deferred, millis],
Ext.diff(me.lastActivity, new Date(), Ext.Date.MILLIS) + millis
);
}
},
whenIdleForSeconds: function (seconds) {
var deferred = Ext.create('Deft.Deferred'),
millis = seconds*1000;
me.startIdleTimer([deferred, millis], millis);
return deferred.promise.then({
failure: function () {
// client canceled promise, so signal to idleTimeout not to restart timer
deferred.canceled = true;
}
});
}
});
To view this discussion on the web visit https://groups.google.com/d/msgid/deftjs/79c10193-48ea-42c0-9107-d9c2786f0ff0%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/deftjs/027987e1-2fcd-498d-9fbc-28cd1cc2f55b%40googlegroups.com.
//
// IdleMonitor is responsible for monitoring the system for non-idle activity and providing API
// hooks for clients to observe idleness. Unlike the usual way of services not being available
// globally, the IdleMonitor is injected via Deft because it needs to expose methods to clients.
//
Ext.define('Fc.tc.service.IdleMonitor', {
config: {
documentEvents: ['touchstart', 'touchmove', 'touchend', 'keydown', 'keyup']
},
constructor: function (config) {
var me = this;
me.lastActivity = null;
me.initConfig(config);
// defer start, so we don't hold up ui
Ext.defer(me.start, 100, me);
},
// @private
start: function () {
var me = this,
onActivity = Ext.bind(me.onActivity, me);
// listen to activity events
Ext.each(me.getDocumentEvents(), function (event) {
document.addEventListener(event, onActivity, false);
});
},
// @private
onActivity: function () {
this.lastActivity = new Date();
},
idleSince: function () {
return Ext.Date.clone(this.lastActivity); // pass by value, not by reference
},
// @private
startIdleTimer: function (options, millis) {
window.setTimeout(Ext.bind(me.idleTimeout, me, [options]), millis);
},
// @private
idleTimeout: function (options, millis) {
var me = this;
if(options.canceled) {
// ignore
return;
}
if (me.lastActivity < Ext.Date.add(new Date(), Ext.Date.MILLI, -options.millis)) {
options.deferred.resolve();
}
else {
// There was activity during the waiting period, so restart timer,
// while adjusting delay for next possible idle state.
me.startIdleTimer(
options,
Ext.diff(me.lastActivity, new Date(), Ext.Date.MILLI) + options.millis
);
}
},
whenIdleForSeconds: function (seconds) {
var deferred = Ext.create('Deft.Deferred'),
millis = seconds*1000,
options = {
deferred: deferred,
millis: millis,
canceled: false
};
me.startIdleTimer(options, millis);
return deferred.promise.then({
failure: function () {
// client canceled promise, so signal to idleTimeout not to restart timer
options.canceled = true;
}
});
}
});