├── .gitignore ├── example.js ├── index.js ├── README.md ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var awake = require('on-wake-up') 2 | 3 | awake(function () { 4 | console.log('computer woke from sleep') 5 | }) 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var CLOCK = 1000 2 | 3 | module.exports = function awake (fn) { 4 | var then = Date.now() 5 | var interval = setInterval(tick, CLOCK) 6 | return clear 7 | 8 | function clear () { 9 | clearInterval(interval) 10 | } 11 | 12 | function tick () { 13 | var now = Date.now() 14 | if (now - then > 2 * CLOCK) fn() 15 | then = now 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # on-wake-up 2 | 3 | Small module that calls a function when your computer wakes up from sleep. 4 | Useful for resetting retry timeouts and stuff. 5 | 6 | ``` 7 | npm install on-wake-up 8 | ``` 9 | 10 | ## Usage 11 | 12 | ``` js 13 | var awake = require('on-wake-up') 14 | 15 | awake(function () { 16 | console.log('computer (probably) woke from sleep') 17 | }) 18 | ``` 19 | 20 | ## Credits 21 | 22 | [@juliangruber](https://github.com/juliangruber) came up with the algorithm 23 | 24 | [@pfraze](https://github.com/pfraze) for writing [almost the same module 6 months before me](https://github.com/pfraze/on-wakeup) 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "on-wake-up", 3 | "version": "1.0.2", 4 | "description": "Small module that calls a function when your computer wakes up from sleep", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "standard": "^7.1.2" 9 | }, 10 | "scripts": { 11 | "test": "standard" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/mafintosh/on-wake-up.git" 16 | }, 17 | "author": "Mathias Buus (@mafintosh)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/mafintosh/on-wake-up/issues" 21 | }, 22 | "homepage": "https://github.com/mafintosh/on-wake-up" 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | --------------------------------------------------------------------------------