SystemTickFeed.sys.mjs (1819B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { actionTypes as at } from "resource://newtab/common/Actions.mjs"; 6 7 const lazy = {}; 8 9 ChromeUtils.defineESModuleGetters(lazy, { 10 clearInterval: "resource://gre/modules/Timer.sys.mjs", 11 setInterval: "resource://gre/modules/Timer.sys.mjs", 12 }); 13 14 // Frequency at which SYSTEM_TICK events are fired 15 export const SYSTEM_TICK_INTERVAL = 5 * 60 * 1000; 16 17 export class SystemTickFeed { 18 init() { 19 this._idleService = Cc["@mozilla.org/widget/useridleservice;1"].getService( 20 Ci.nsIUserIdleService 21 ); 22 this._hasObserver = false; 23 this.setTimer(); 24 } 25 26 setTimer() { 27 this.intervalId = lazy.setInterval(() => { 28 if (this._idleService.idleTime > SYSTEM_TICK_INTERVAL) { 29 this.cancelTimer(); 30 Services.obs.addObserver(this, "user-interaction-active"); 31 this._hasObserver = true; 32 return; 33 } 34 this.dispatchTick(); 35 }, SYSTEM_TICK_INTERVAL); 36 } 37 38 cancelTimer() { 39 lazy.clearInterval(this.intervalId); 40 this.intervalId = null; 41 } 42 43 observe() { 44 this.dispatchTick(); 45 Services.obs.removeObserver(this, "user-interaction-active"); 46 this._hasObserver = false; 47 this.setTimer(); 48 } 49 50 dispatchTick() { 51 ChromeUtils.idleDispatch(() => 52 this.store.dispatch({ type: at.SYSTEM_TICK }) 53 ); 54 } 55 56 onAction(action) { 57 switch (action.type) { 58 case at.INIT: 59 this.init(); 60 break; 61 case at.UNINIT: 62 this.cancelTimer(); 63 if (this._hasObserver) { 64 Services.obs.removeObserver(this, "user-interaction-active"); 65 this._hasObserver = false; 66 } 67 break; 68 } 69 } 70 }