updated by GasGit automation
This commit is contained in:
parent
8974faf1bc
commit
e4c56d30d8
1 changed files with 107 additions and 97 deletions
|
@ -1,14 +1,14 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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_;
|
||||||
|
|
||||||
// now clean it
|
// now clean it
|
||||||
function cleanTheCamel_ (cleanThis) {
|
function cleanTheCamel_ (cleanThis) {
|
||||||
return typeof cleanThis === "string" ? cleanThis.slice(0,1).toUpperCase() + cleanThis.slice(1) : cleanThis;
|
return typeof cleanThis === "string" ? cleanThis.slice(0,1).toUpperCase() + cleanThis.slice(1) : cleanThis;
|
||||||
|
@ -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 || {}]);
|
||||||
|
|
||||||
|
@ -62,16 +64,16 @@ var ClientWatcher = (function (ns) {
|
||||||
watch.domain[k] = cleanTheCamel_ (watch.domain[k]);
|
watch.domain[k] = cleanTheCamel_ (watch.domain[k]);
|
||||||
});
|
});
|
||||||
watch.id = watch.id || ('w' + Object.keys(watchers_).length);
|
watch.id = watch.id || ('w' + Object.keys(watchers_).length);
|
||||||
|
|
||||||
// add to the registry
|
// add to the registry
|
||||||
return (watchers_[watch.id] = ns.newWatcher(watch));
|
return (watchers_[watch.id] = ns.newWatcher(watch));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 () {
|
||||||
|
@ -202,7 +205,7 @@ var ClientWatcher = (function (ns) {
|
||||||
}
|
}
|
||||||
self.start()
|
self.start()
|
||||||
.then (function(pack) {
|
.then (function(pack) {
|
||||||
|
|
||||||
|
|
||||||
if (pack.changed.active || pack.changed.data || pack.changed.sheets) {
|
if (pack.changed.active || pack.changed.data || pack.changed.sheets) {
|
||||||
callback(current_, pack, self);
|
callback(current_, pack, self);
|
||||||
|
@ -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();
|
||||||
// save this for interest
|
resolve(pack_);
|
||||||
pack_ = pack;
|
}
|
||||||
current_.dataSource = pack_.dataSource;
|
else {
|
||||||
|
|
||||||
// if there's been some changes to data then store it
|
Provoke.run ("ServerWatcher", "poll", watch_)
|
||||||
if (pack.data) {
|
.then (
|
||||||
|
function (pack) {
|
||||||
|
// save this for interest
|
||||||
|
pack_ = pack;
|
||||||
|
current_.dataSource = pack_.dataSource;
|
||||||
|
|
||||||
if (watch_.domain.fiddler && watch_.domain.property === "Values") {
|
// if there's been some changes to data then store it
|
||||||
// it may fail because data is in midde of being updated
|
if (pack.data) {
|
||||||
// but that's - it'll get it next time.
|
|
||||||
try {
|
if (watch_.domain.fiddler && watch_.domain.property === "Values") {
|
||||||
current_.fiddler = new Fiddler().setValues(pack.data);
|
// it may fail because data is in midde of being updated
|
||||||
}
|
// but that's - it'll get it next time.
|
||||||
catch (err) {
|
try {
|
||||||
// dont want to count this as a valid piece of data yet
|
current_.fiddler = new Fiddler().setValues(pack.data);
|
||||||
// so we'll pass on this poll result and treat it as a reject
|
}
|
||||||
|
catch (err) {
|
||||||
return rejectActions(reject,err);
|
// 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
|
||||||
|
|
||||||
|
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(pack.active) {
|
||||||
|
current_.active = pack.active;
|
||||||
// if there's been some changes to active positions
|
watch_.checksum.active = pack.checksum.active;
|
||||||
if(pack.active) {
|
}
|
||||||
current_.active = pack.active;
|
|
||||||
watch_.checksum.active = pack.checksum.active;
|
// if there's been some changes to sheets then store it
|
||||||
}
|
if (pack.sheets) {
|
||||||
|
current_.sheets = pack.sheets;
|
||||||
// if there's been some changes to sheets then store it
|
watch_.checksum.sheets = pack.checksum.sheets;
|
||||||
if (pack.sheets) {
|
}
|
||||||
current_.sheets = pack.sheets;
|
|
||||||
watch_.checksum.sheets = pack.checksum.sheets;
|
if (pack.data || pack.active || pack.sheets) {
|
||||||
}
|
status_.hits++;
|
||||||
|
}
|
||||||
if (pack.data || pack.active || pack.sheets) {
|
finallyActions();
|
||||||
status_.hits++;
|
resolve (pack);
|
||||||
}
|
})
|
||||||
finallyActions();
|
['catch'](function (err) {
|
||||||
resolve (pack);
|
// sometimes there will be network errors which can generally be ignored..
|
||||||
})
|
rejectActions (reject, err);
|
||||||
['catch'](function (err) {
|
});
|
||||||
// sometimes there will be network errors which can generally be ignored..
|
}
|
||||||
rejectActions (reject, err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return ns;
|
return ns;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue