all files / dist/ pause.js

88.89% Statements 16/18
62.5% Branches 5/8
100% Functions 5/5
88.89% Lines 16/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                          25×         25× 25× 25×     25× 24×     25×     25× 23×            
'use strict';
 
/**
 *  Break text up with a separate messages pausing before each one
 *  ```xml
 *  <pause wait=2000 />
 *  ```
 *  evaluated in series
 *  after evaluating all text / xml before removed
 *  controller sends text before and then waits before allowing rest of text/xml to be evaluated
 *  if the bot implements typing a typing status is sent between pauses.
 *  @param wait {String} how long to wait in ms between each defaults to 1000
 *  @module pause
 */
 
var R = require('ramda');
 
var DEFAULT_WAIT = 1000;
var lensImplementsTyping = R.lensPath(['implements', 'typing']);
var lensId = R.lensPath(['sender', 'id']);
 
var spec = {
    series: true,
    evaluate: 'step',
    replace: 'before',
    controller: function controller(_ref, cb) {
        var before = _ref.before,
            bot = _ref.bot,
            attributes = _ref.attributes,
            update = _ref.update;
 
        var wait = R.isNil(attributes.wait) ? DEFAULT_WAIT : Number(attributes.wait);
        var sendNext = function sendNext() {
            Iif (R.view(lensImplementsTyping, update) && R.view(lensId, bot)) {
                bot.sendIsTypingMessageTo(R.view(lensId, update), { ignoreMiddleware: true });
            }
            setTimeout(function () {
                return cb(null, '');
            }, wait);
        };
        Iif (before === '') {
            sendNext();
        } else {
            bot.reply(update, before, { __update: update }).then(function () {
                sendNext();
            }).catch(function () {
                sendNext();
            });
        }
    }
};
 
module.exports = spec;