Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created June 13, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfpiccolo/9b5bb2c976056147eac5 to your computer and use it in GitHub Desktop.
Save mfpiccolo/9b5bb2c976056147eac5 to your computer and use it in GitHub Desktop.
import Ember from 'ember';
var Pollable = Ember.Mixin.create({
afterModel: function (model, transition) {
var self = this;
this._super(model, transition);
var interval_info = this.get('interval_info');
if (!interval_info) {
interval_info = Ember.Object.create({
repititions_per_itteration: 5,
muliplier: 2,
max_interval_time: 60000,
current_interval_delay: 1000,
current_run_count: 0,
});
}
this.set('interval_info', interval_info);
var reset = function () {
self.set('interval_info.current_run_count', 1);
self.set('interval_info.current_interval_delay', 1000);
Ember.run.cancel(self.get('current_poll'));
transition.send('poll', model.contact, transition);
};
Ember.$(document).idle({
onIdle: function(){
Ember.run.cancel(self.get('current_poll'));
},
onActive: function(){
Ember.run.throttle(this, reset, 1000);
},
idle: 10000
});
// Ember.$(window).on("mousemove", function () {
// Ember.run.throttle(this, reset, 1000);
// });
transition.send('poll', model.contact, transition);
},
reloadable: function (record) {
return (
record.get('isLoaded') &&
! record.get('isDirty') &&
! record.get('isSaving') &&
! record.get('isDeleted') &&
! record.get('isError') &&
! record.get('isNew') &&
record.get('isValid')
);
},
actions: {
poll: function (record, transition) {
var self = this;
// TODO use jquery-idletimer/away
var interval_info = this.get('interval_info');
var current_run_count = interval_info.current_run_count % (interval_info.repititions_per_itteration + 1);
var current_interval_delay = interval_info.current_interval_delay;
var current_model = this.modelFor(this.routeName).contact;
var id = current_model.id;
if (this.reloadable(record) && id === record.id) {
if (current_run_count >= interval_info.repititions_per_itteration) {
current_interval_delay = interval_info.current_interval_delay * interval_info.muliplier;
this.set('interval_info.current_interval_delay', current_interval_delay);
this.set('interval_info.current_run_count', 1);
}
this.set('interval_info.current_run_count', current_run_count + 1);
var poll = Ember.run.later( function() {
record.reload();
transition.send('poll', record, transition);
}, current_interval_delay);
this.set('current_poll', poll);
}
},
}
});
Ember.$.fn.idle = function (options) {
var defaults = {
idle: 60000, //idle time in ms
events: 'mousemove keypress mousedown touchstart', //events that will trigger the idle resetter
onIdle: function () {}, //callback function to be executed after idle time
onActive: function () {}, //callback function to be executed after back from idleness
onHide: function () {}, //callback function to be executed when window is hidden
onShow: function () {}, //callback function to be executed when window is visible
keepTracking: false //if you want to keep tracking user even after the first time, set this to true
},
idle = false,
visible = true,
settings = Ember.$.extend({}, defaults, options),
resetTimeout,
timeout;
resetTimeout = function (id, settings) {
if (idle) {
settings.onActive.call();
idle = false;
}
(settings.keepTracking ? clearInterval : clearTimeout)(id);
return timeout(settings);
};
timeout = function (settings) {
var timer = (settings.keepTracking ? setInterval : setTimeout),
id;
id = timer(function () {
idle = true;
settings.onIdle.call();
}, settings.idle);
return id;
};
return this.each(function () {
var id = timeout(settings);
Ember.$(this).on(settings.events, function (e) {
id = resetTimeout(id, settings);
});
if (options.onShow || options.onHide) {
Ember.$(document).on("visibilitychange webkitvisibilitychange mozvisibilitychange msvisibilitychange", function () {
if (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
if (visible) {
visible = false;
settings.onHide.call();
}
} else {
if (!visible) {
visible = true;
settings.onShow.call();
}
}
});
}
});
};
export default Pollable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment