Skip to content
Snippets Groups Projects
Select Git revision
  • 61a1232e9dc34bf2fecf8b0db0151c829a9a3b28
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

database-1_test.go

Blame
  • deadmansswitch.mjs 2.66 KiB
    
    
    /**
     * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    
    import {internalSymbol} from "../constants.mjs";
    
    import {Base} from "../types/base.mjs";
    import {isInteger} from "../types/is.mjs";
    import {validateFunction, validateInteger} from "../types/validate.mjs";
    
    export {DeadMansSwitch}
    
    /**
     * The dead man's switch allows to set a timer which can be reset again and again within a defined period of time.
     *
     * ```
     * <script type="module">
     * import {DeadMansSwitch} from '@schukai/monster/source/util/deadmansswitch.mjs';
     * new DeadMansSwitch();
     * </script>
     * ```
     *
     * @example
     * import {DeadMansSwitch} from '@schukai/monster/source/util/deadmansswitch.mjs';
     *
     * const deadmansswitch = new DeadMansSwitch(100, ()=>{
     *   console.log('yeah!')
     *   // ↦ "yeah!"
     * })
     *
     * deadmansswitch.touch(); // from here wait again 100 ms
     * deadmansswitch.touch(200); // from here wait 200 ms
     *
     * @copyright schukai GmbH
     * @license AGPLv3
     * @since 1.29.0
     * @memberOf Monster.Util
     * @summary Class to be able to execute function chains
     */
     class DeadMansSwitch extends Base {
    
        /**
         * Create new dead man's switch
         *
         * @param {Integer} delay
         * @param {function} callback
         * @throw {TypeError} the arguments must be either integer or functions
         * @throws {TypeError} value is not an integer
         */
        constructor(delay, callback) {
            super();
    
            init.call(this, validateInteger(delay), validateFunction(callback));
        }
    
        /**
         *
         * @param {Integer|undefined} [delay]
         */
        touch(delay) {
    
            if (this[internalSymbol]['isAlreadyRun'] === true) {
                throw new Error('has already run')
            }
    
            if (isInteger(delay)) {
                this[internalSymbol]['delay'] = delay
            } else if (delay !== undefined) {
                throw new Error('unsupported argument')
            }
    
            clearTimeout(this[internalSymbol]['timer']);
    
            initCallback.call(this);
    
            return this;
        }
    }
    
    /**
     * @private
     */
    function initCallback() {
    
        const self = this;
    
        self[internalSymbol]['timer'] = setTimeout(() => {
            self[internalSymbol]['isAlreadyRun'] = true;
            self[internalSymbol]['callback']();
        }, self[internalSymbol]['delay'])
    }
    
    /**
     * @private
     * @param {integer} delay
     * @param {function} callback
     */
    function init(delay, callback) {
        const self = this;
    
        self[internalSymbol] = {
            callback,
            delay,
            isAlreadyRun: false,
            timer: undefined
        };
    
        initCallback.call(self);
    
    }