updated by GasGit automation

This commit is contained in:
Bruce McPherson 2016-06-24 18:10:59 +01:00
parent 8974faf1bc
commit e4c56d30d8

View file

@ -1,10 +1,10 @@
/** /**
* simulate Watcher with apps script * simulate Watcher with apps script
* various changes server side can be watched for server side * various changes server side can be watched for server side
* and resolved client side * and resolved client side
* @constructor ClientWatcher * @constructor ClientWatcher
*/ */
var ClientWatcher = (function (ns) { var ClientWatcher = (function (ns) {
var watchers_ = {},startTime_=0, pack_; var watchers_ = {},startTime_=0, pack_;
@ -16,27 +16,28 @@ var ClientWatcher = (function (ns) {
/** /**
* return {object} all current Watchers, the id is the key * return {object} all current Watchers, the id is the key
*/ */
ns.getWatchers = function () { ns.getWatchers = function () {
return watchers_; return watchers_;
}; };
/** /**
* add a Watcher * add a Watcher
* @param {object} options what to watch * @param {object} options what to watch
* @param {string} [sheet] the sheet to watch if missing, watch the active sheet * @param {string} [sheet] the sheet to watch if missing, watch the active sheet
* @param {string} [range] the range to watch - if missing, watch the whole sheet * @param {string} [range] the range to watch - if missing, watch the whole sheet
* @param {string} [property=Data] matches getData, getBackground * @param {string} [property=Data] matches getData, getBackground
* @param {TYPES} [type=SHEET] the type of Watcher * @param {TYPES} [type=SHEET] the type of Watcher
* @param {number} pollFrequency in ms, how often to poll * @param {number} pollFrequency in ms, how often to poll
* @return {ClientWatcher.Watcher} the Watcher * @return {ClientWatcher.Watcher} the Watcher
*/ */
ns.addWatcher = function (options) { ns.addWatcher = function (options) {
// default settings for a Watcher request // default settings for a Watcher request
var watch = Utils.vanMerge ([{ var watch = Utils.vanMerge ([{
pollFrequency:2500, // if this is 0, then polling is not done, and it needs self.poke() pollFrequency:2500, // if this is 0, then polling is not done, and it needs self.poke()
id: '' , // Watcher id id: '' , // Watcher id
pollVisibleOnly:true, // just poll if the page is actually visible
watch: { watch: {
active: true, // whether to watch for changes to active active: true, // whether to watch for changes to active
data: true, // whether to watch for data content changes data: true, // whether to watch for data content changes
@ -53,7 +54,8 @@ var ClientWatcher = (function (ns) {
range: "", // if range, specifiy a range to watch range: "", // if range, specifiy a range to watch
sheet: "", // a sheet name - if not given, the active sheet will be used sheet: "", // a sheet name - if not given, the active sheet will be used
property:"Values", // Values,Backgrounds etc... property:"Values", // Values,Backgrounds etc...
fiddler:true // whether to create a fiddler to mnipulate data (ignored for nondata property) fiddler:true, // whether to create a fiddler to mnipulate data (ignored for nondata property)
applyFilters:false // whether to apply filters
} }
},options || {}]); },options || {}]);
@ -68,10 +70,10 @@ var ClientWatcher = (function (ns) {
}; };
/** /**
* remove a Watcher * remove a Watcher
* @param {string||object} id the id or object * @param {string||object} id the id or object
* @return {ClientWatcher} self * @return {ClientWatcher} self
*/ */
ns.removeWatcher = function (watcher) { ns.removeWatcher = function (watcher) {
var id = Utils.isVanObject(watcher) ? watcher.id : watcher; var id = Utils.isVanObject(watcher) ? watcher.id : watcher;
if (!id || watchers_[id]) { if (!id || watchers_[id]) {
@ -82,26 +84,26 @@ var ClientWatcher = (function (ns) {
return ns; return ns;
}; };
/** /**
* return a specifc Watcher * return a specifc Watcher
* @param {string} id the Watcher * @param {string} id the Watcher
* @return {ClientWatcher.watcher} the Watcher * @return {ClientWatcher.watcher} the Watcher
*/ */
ns.getWatcher = function (id) { ns.getWatcher = function (id) {
return watchers_[id]; return watchers_[id];
}; };
/** /**
* used to create a new Watcher object * used to create a new Watcher object
* @return {ClientWatcher.Watcher} * @return {ClientWatcher.Watcher}
*/ */
ns.newWatcher = function (watch) { ns.newWatcher = function (watch) {
return new ns.Watcher(watch); return new ns.Watcher(watch);
} }
/** /**
* this is a Watcher object * this is a Watcher object
* @param {object} watch the Watcher resource * @param {object} watch the Watcher resource
* @return {ClientWatcher.Watcher} * @return {ClientWatcher.Watcher}
*/ */
ns.Watcher = function (watch) { ns.Watcher = function (watch) {
var self = this; var self = this;
@ -119,7 +121,8 @@ var ClientWatcher = (function (ns) {
responded:0, // time responded responded:0, // time responded
errors:0 , // number of errors errors:0 , // number of errors
hits:0, // how many times a change was detected hits:0, // how many times a change was detected
totalWaiting:0 // time spent waiting for server response totalWaiting:0, // time spent waiting for server response
idle:0 // no of times we didnt bother polling
}; };
self.start = function () { self.start = function () {
@ -155,17 +158,17 @@ var ClientWatcher = (function (ns) {
}; };
/** /**
* if you want the latest status * if you want the latest status
* @return {object} status * @return {object} status
*/ */
self.getStatus = function () { self.getStatus = function () {
return status_; return status_;
}; };
/** /**
* do the next polling after waiting some time * do the next polling after waiting some time
* @return {Promise} * @return {Promise}
*/ */
function nextPolling_ (immediate) { function nextPolling_ (immediate) {
return new Promise(function (resolve,reject) { return new Promise(function (resolve,reject) {
setTimeout ( function () { setTimeout ( function () {
@ -222,11 +225,11 @@ var ClientWatcher = (function (ns) {
}; };
/** /**
* this returns a promise * this returns a promise
* which will be resolved when the server sends back changed data * which will be resolved when the server sends back changed data
* and rejected when there is no change * and rejected when there is no change
* @return {Promise} * @return {Promise}
*/ */
self.poll = function () { self.poll = function () {
status_.requested = new Date().getTime(); status_.requested = new Date().getTime();
@ -252,63 +255,70 @@ var ClientWatcher = (function (ns) {
function pollWork () { function pollWork () {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
Provoke.run ("ServerWatcher", "poll", watch_) // check for visibility.. if not visible, then don't bother polling
.then ( if (pack_ && watch_.pollVisibleOnly && !ifvisible.now() ) {
function (pack) { status_.idle++;
finallyActions();
resolve(pack_);
}
else {
// save this for interest Provoke.run ("ServerWatcher", "poll", watch_)
pack_ = pack; .then (
current_.dataSource = pack_.dataSource; function (pack) {
// save this for interest
pack_ = pack;
current_.dataSource = pack_.dataSource;
// if there's been some changes to data then store it // if there's been some changes to data then store it
if (pack.data) { if (pack.data) {
if (watch_.domain.fiddler && watch_.domain.property === "Values") { if (watch_.domain.fiddler && watch_.domain.property === "Values") {
// it may fail because data is in midde of being updated // it may fail because data is in midde of being updated
// but that's - it'll get it next time. // but that's - it'll get it next time.
try { try {
current_.fiddler = new Fiddler().setValues(pack.data); current_.fiddler = new Fiddler().setValues(pack.data);
} }
catch (err) { catch (err) {
// dont want to count this as a valid piece of data yet // dont want to count this as a valid piece of data yet
// so we'll pass on this poll result and treat it as a reject // so we'll pass on this poll result and treat it as a reject
return rejectActions(reject,err); return rejectActions(reject,err);
}
} }
watch_.checksum.data = pack.checksum.data;
current_.data = pack.data;
} }
watch_.checksum.data = pack.checksum.data;
current_.data = pack.data;
}
// if there's been some changes to active positions // if there's been some changes to active positions
if(pack.active) { if(pack.active) {
current_.active = pack.active; current_.active = pack.active;
watch_.checksum.active = pack.checksum.active; watch_.checksum.active = pack.checksum.active;
} }
// if there's been some changes to sheets then store it // if there's been some changes to sheets then store it
if (pack.sheets) { if (pack.sheets) {
current_.sheets = pack.sheets; current_.sheets = pack.sheets;
watch_.checksum.sheets = pack.checksum.sheets; watch_.checksum.sheets = pack.checksum.sheets;
} }
if (pack.data || pack.active || pack.sheets) { if (pack.data || pack.active || pack.sheets) {
status_.hits++; status_.hits++;
} }
finallyActions(); finallyActions();
resolve (pack); resolve (pack);
}) })
['catch'](function (err) { ['catch'](function (err) {
// sometimes there will be network errors which can generally be ignored.. // sometimes there will be network errors which can generally be ignored..
rejectActions (reject, err); rejectActions (reject, err);
}); });
}
}); });
} }
}; };
}; };
return ns; return ns;