summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/models/utils/timer.js
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2018-12-05 14:35:26 +0000
committerAlexandre Kühn <aku@odoo.com>2018-12-06 16:18:09 +0000
commit15bbadb69b1b26404a614736c3cde7980d3c1dfd (patch)
tree81dd1957900c612d5a06830f172b685b1326f11e /addons/mail/static/src/js/models/utils/timer.js
parent50dfa865ea32c88d55e7eb0fce6a6ae978c9c3b4 (diff)
[REF] im_livechat, mail, website_livechat: clean mail model folder
The utility classes `Timer`, `Timers` and `CCThrottleFunction` were only used by the feature 'is typing...', thus all those files were in the same folder. This commit moves the utility classes in a dedicated folder, and introduces a folder for thread mixins, in preparation for future features on threads that will be implemented as mixins.
Diffstat (limited to 'addons/mail/static/src/js/models/utils/timer.js')
-rw-r--r--addons/mail/static/src/js/models/utils/timer.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/addons/mail/static/src/js/models/utils/timer.js b/addons/mail/static/src/js/models/utils/timer.js
new file mode 100644
index 00000000000..a3236fd1a16
--- /dev/null
+++ b/addons/mail/static/src/js/models/utils/timer.js
@@ -0,0 +1,69 @@
+odoo.define('mail.model.Timer', function (require) {
+"use strict";
+
+var Class = require('web.Class');
+
+/**
+ * This class creates a timer which, when times out, calls a function.
+ */
+var Timer = Class.extend({
+
+ /**
+ * Instantiate a new timer. Note that the timer is not started on
+ * initialization (@see start method).
+ *
+ * @param {Object} params
+ * @param {integer} params.duration duration of timer before timeout in
+ * milli-seconds.
+ * @param {function} params.onTimeout function that is called when the
+ * timer times out.
+ */
+ init: function (params) {
+ this._duration = params.duration;
+ this._timeout = undefined;
+ this._timeoutCallback = params.onTimeout;
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Clears the countdown of the timer.
+ */
+ clear: function () {
+ clearTimeout(this._timeout);
+ },
+ /**
+ * Resets the timer, i.e. resets its duration.
+ */
+ reset: function () {
+ this.clear();
+ this.start();
+ },
+ /**
+ * Starts the timer, i.e. after a certain duration, it times out and calls
+ * a function back.
+ */
+ start: function () {
+ this._timeout = setTimeout(this._onTimeout.bind(this), this._duration);
+ },
+
+ //--------------------------------------------------------------------------
+ // Handler
+ //--------------------------------------------------------------------------
+
+ /**
+ * Called when the timer times out, calls back a function on timeout.
+ *
+ * @private
+ */
+ _onTimeout: function () {
+ this._timeoutCallback();
+ },
+
+});
+
+return Timer;
+
+});