├── .gitignore ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # active-app-watcher 2 | 3 | > Event emitting active window/app watcher 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install active-app-watcher --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import ActiveAppWatcher from 'active-app-watcher' 15 | 16 | const appWatch = new ActiveAppWatcher() 17 | 18 | appWatch.on('error', (err) => { 19 | console.error(err) 20 | }) 21 | 22 | appWatch.on('change', (app) => { 23 | console.log('change', app) 24 | }) 25 | 26 | appWatch.on('check', (app) => { 27 | console.log('check', app) 28 | }) 29 | 30 | appWatch.start() 31 | ``` 32 | 33 | ## Related 34 | 35 | * [active-app](https://github.com/bencevans/active-app) - Detect the active window/application 36 | 37 | ## Licence 38 | 39 | MIT © [Ben Evans](http://bensbit.co.uk) 40 | 41 | ## 42 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const ActiveAppWatcher = require('./') 2 | 3 | const appWatch = new ActiveAppWatcher() 4 | 5 | appWatch.on('error', (err) => { 6 | console.error(err) 7 | }) 8 | 9 | appWatch.on('change', (app) => { 10 | console.log('change', app) 11 | }) 12 | 13 | appWatch.on('check', (app) => { 14 | console.log('check', app) 15 | }) 16 | 17 | appWatch.start() 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const activeApp = require('active-app') 3 | const EventEmitter = require('events').EventEmitter 4 | const equal = require('deep-equal') 5 | 6 | class ActiveAppWatcher extends EventEmitter { 7 | constructor(interval) { 8 | super() 9 | this.interval = interval || 1000 10 | this.lastApp = null 11 | this.targetState = 'start' 12 | this.timeout = null 13 | } 14 | 15 | 16 | tick() { 17 | let self = this 18 | activeApp((err, app) => { 19 | if (err) { 20 | return self.emit('error', err) 21 | } 22 | 23 | self.emit('check', app) 24 | 25 | if (!equal(app, self.lastApp)) { 26 | self.emit('change', app) 27 | self.lastApp = app 28 | } 29 | }) 30 | } 31 | 32 | start() { 33 | let self = this 34 | this.tick() 35 | this.once('check', () => { 36 | if (self.targetState === 'start') { 37 | self.timeout = setTimeout(() => { 38 | self.start() 39 | }, self.interval) 40 | } 41 | }) 42 | } 43 | 44 | stop() { 45 | this.targetState = 'stop' 46 | cancelTimeout(this.timeout) 47 | } 48 | 49 | } 50 | 51 | module.exports = ActiveAppWatcher 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "active-app-watcher", 3 | "version": "1.1.1", 4 | "description": "Event emitting active window/app watcher", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "ava" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/bencevans/active-app-watcher.git" 12 | }, 13 | "keywords": [ 14 | "active", 15 | "app", 16 | "application", 17 | "window", 18 | "watch", 19 | "monitor", 20 | "event", 21 | "eventemitter", 22 | "x11" 23 | ], 24 | "author": "Ben Evans (http://bensbit.co.uk)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/bencevans/active-app-watcher/issues" 28 | }, 29 | "homepage": "https://github.com/bencevans/active-app-watcher#readme", 30 | "dependencies": { 31 | "active-app": "^1.0.0", 32 | "deep-equal": "^1.0.1" 33 | } 34 | } 35 | --------------------------------------------------------------------------------