Giter Club home page Giter Club logo

angular-activity-monitor's Introduction

Angular Activity Monitor

This is a simple service that will emit a couple of events based on the users' DOM activity. It also allows you to "keep items alive" in the background so long as the user is considered "active".

Installation:

$ [npm|bower] install --save angular-activity-monitor

Usage:

// with bower (or without packaging)
angular.module('myModule', ['ActivityMonitor']);

// with npm (via webpack or Browserify)
angular.module('myModule', [require('angular-activity-monitor')]);

MyController.$inject = ['ActivityMonitor'];
function MyController(ActivityMonitor) {
  ActivityMonitor.on('inactive', function() {
    alert("y0, you're inactive!");
  });
}
ActivityMonitor.options (configuration):
  • enabled: whether to regularly check for inactivity (default: false) [bool]
  • keepAlive: background execution frequency (default: 800) [seconds]
  • inactive: how long until user is considered inactive (default: 900) [seconds]
  • warning: when user is nearing inactive state (deducted from inactive) (default: 60) [seconds]
  • disableOnInactive: Once user is inactive, all event listeners are detached and activity monitoring is discontinued (default: true) [bool]
  • DOMevents: array of events on the DOM that count as user activity (default: ['mousemove', 'mousedown', 'mouseup', 'keypress', 'wheel', 'touchstart', 'scroll'])
ActivityMonitor.user (information about the user):
  • action: timestamp of the users' last action (default: Date.now()) [milliseconds]
  • active: is the user considered active? (default: true) [bool]
  • warning: is the user nearing inactivity? (default: false) [bool]
ActivityMonitor Methods:
  • on(event, callback) (alias bind): subsribe to a particular event
  • off(event[, callback]) (alias unbind): unsubscribe to a particular event. If no callback or namespace provided, all subscribers for the given event will be cleared.
  • activity(): manually invoke user activity (this updates the User object above)
ActivityMonitor Events:
  • keepAlive: anything to execute (at the Options.keepAlive interval) so long as the user is active.
  • warning: when user is approaching inactive state
  • inactive: when user is officially considered inactive

How long until user is inactive?

This can be configured by setting the ActivityMonitor.options.inactive property to the desired timeout (in seconds).

When is the user considered active?

Everytime one of the follow DOM events occur, the action and active properties on the User object is updated accordingly.

var DOMevents = ['mousemove', 'mousedown', 'keypress', 'wheel', 'touchstart', 'scroll'];

(Un)subscribing and Event namespacing

If you've ever used jQuery.unbind(), you're in luck. This subscription model works almost exactly like that. Subscribing is pretty straight forward using .on() or .bind() as described above but, unsubscribing gets a little weird. You essentially have two options:

  • Pass the same callback argument to .unbind() or .off()
  • Subscribe and unsubscribe using event namespacing.

same callback example

var foo = function() {
  alert("y0, you're inactive!");
};
ActivityMonitor.on('inactive', foo); /* subscribe */
ActivityMonitor.off('inactive', foo); /* unsubscribe */

event namespace example

Instead of maintaining references to callbacks in order to unbind them, we can namespace the events and use this capability to easily unbind our actions. Namespaces are defined by using a period (.) character when binding to an event:

ActivityMonitor.on('inactive.myEvent', function foo() {
  alert("y0, you're inactive!");
});
ActivityMonitor.off('inactive.myEvent');

If there's something missing or some quirk you've found. FIX OR UPDATE ME!!!

angular-activity-monitor's People

Contributors

ackerapple avatar chander avatar sean3z avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-activity-monitor's Issues

Version tags

Please add version tags such that specific versions can be identified in bower.json files. We need to be able to reference specific versions instead of just head (which would potentially be unpredictable unless you reference a specific commit hash).

Fails on DevKit, undeclared dependency on Angular

The npmjs page has a "Try on RunKit" link bringing to a DevKit playground. The run button unfortunately fails with:
Error: Cannot find module 'angular'

Require stack:

  • /app/available_modules/1628189980000/angular-activity-monitor/activity-monitor.js
  • /app/index.js

This is presumably because angular-activity-monitor does not declare its dependency on Angular. Indeed, its package.json mentions an outdated Angular version in devDependencies, but the dependencies array is empty.

Not working after window reload

without reloading the browser page angular-activity-monitor is working fine . But after reload keepalive and inactive are not working.

Tag does not match package.jsom

The version number on npmjs.org says "1.0.0" like the package.json - but there is no matching tag in the git repository. Please add tag to match version.

Changelog

Please add a CHANGELOG.md file listing changes for each update

Provider for angular.config()

It would be nice if I can configure this service in the angular.config() section, where other service providers got configured (for example $logProvider, $translateProvider, etc.)

Does not work

angularjs version: AngularJS v1.2.25

var module = angular.module('derp.module', [
    'ActivityMonitor'
  ])
  .controller('derpController', function (ActivityMonitor) {
    ActivityMonitor.options.enabled = true;
    ActivityMonitor.options.inactive = 10;
    ActivityMonitor.options.warning = 5;
    ActivityMonitor.on('inactive', function() {
      alert('user is inactive');
    });
    ActivityMonitor.on('keepAlive', function() {
    });
    ActivityMonitor.on('warning', function() {
      alert('warning session is timing out');
    });

inactive event is does not firing at all.

following is my code. let me know where i am doing wrong. because as far user perform activity it work fine and logs numbers as i did. but once it reached 0 it should print message but it is not behaving good in that. looking like inactive event is not being triggered. please help!

ActivityMonitor.off('inactive.userStatus');
            ActivityMonitor.off('activity.userStatus');

            // interval = $interval(function() {
            //   scope.state.countdown--;
            //   // console.log(scope.state.countdown);
            //   if (!scope.state.countdown) {
            //     $interval.cancel(interval);
            //   }

            // }, 1000);

            ActivityMonitor.on('inactive.userStatus', function() {
                console.log(scope.state.countdown);
                console.log('You\'re inactive!');
            });

            ActivityMonitor.on('activity.userStatus', function() {
              scope.state.countdown = timeout;
              console.log(scope.state.countdown);
            });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.