wechaty-puppet-service
Version:
Puppet Service for Wechaty
41 lines • 3.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.epicRecoverDing$ = exports.epicRecoverReset$ = exports.monitorHeartbeat$ = void 0;
/**
* Wechaty Open Source Software - https://github.com/wechaty
*
* @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
* Wechaty Contributors <https://github.com/wechaty>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const typesafe_actions_1 = require("typesafe-actions");
const wechaty_redux_1 = require("wechaty-redux");
const wechaty_puppet_1 = require("wechaty-puppet");
const stateActive$ = (action$) => action$.pipe((0, operators_1.filter)((0, typesafe_actions_1.isActionOf)(wechaty_redux_1.Duck.actions.activeState)), (0, operators_1.filter)(action => action.payload.status === true));
const stateInactive$ = (action$) => action$.pipe((0, operators_1.filter)((0, typesafe_actions_1.isActionOf)(wechaty_redux_1.Duck.actions.inactiveState)));
const heartbeat$ = (action$) => action$.pipe((0, operators_1.filter)((0, typesafe_actions_1.isActionOf)([
wechaty_redux_1.Duck.actions.heartbeatEvent,
wechaty_redux_1.Duck.actions.dongEvent,
])));
// Emit once when an active puppet lost heartbeat after a timeout period
const monitorHeartbeat$ = (timeoutMilliseconds) => (action$) => stateActive$(action$).pipe((0, operators_1.switchMap)(action => heartbeat$(action$).pipe((0, operators_1.debounce)(() => (0, rxjs_1.interval)(timeoutMilliseconds)), (0, operators_1.tap)(() => wechaty_puppet_1.log.verbose('PuppetService', 'monitorHeartbeat$() %d seconds TIMEOUT', Math.floor(timeoutMilliseconds / 1000))), (0, operators_1.mapTo)(wechaty_redux_1.Duck.actions.errorEvent(action.payload.puppetId, { gerror: `monitorHeartbeat$() TIMEOUT(${timeoutMilliseconds})` })))));
exports.monitorHeartbeat$ = monitorHeartbeat$;
const epicRecoverDing$ = (timeoutMilliseconds) => (action$) => monitorHeartbeat$(timeoutMilliseconds)(action$).pipe((0, operators_1.switchMap)(action => (0, rxjs_1.timer)(0, Math.floor(timeoutMilliseconds)).pipe((0, operators_1.tap)(n => wechaty_puppet_1.log.verbose('PuppetService', 'epicRecoverDing$() actions.ding() emitted #%d', n)), (0, operators_1.mapTo)(wechaty_redux_1.Duck.actions.ding(action.payload.puppetId, 'epicRecoverDing$')), (0, operators_1.takeUntil)(heartbeat$(action$)), (0, operators_1.takeUntil)(stateInactive$(action$)))));
exports.epicRecoverDing$ = epicRecoverDing$;
const epicRecoverReset$ = (timeoutMilliseconds) => (action$) => monitorHeartbeat$(timeoutMilliseconds)(action$).pipe((0, operators_1.switchMap)(action => (0, rxjs_1.timer)(0, timeoutMilliseconds * 2).pipe((0, operators_1.tap)(n => wechaty_puppet_1.log.verbose('PuppetService', 'epicRecoverReset$() actions.reset() emitted #%d', n)), (0, operators_1.mapTo)(wechaty_redux_1.Duck.actions.reset(action.payload.puppetId, 'epicRecoverReset$')), (0, operators_1.takeUntil)(heartbeat$(action$)), (0, operators_1.takeUntil)(stateInactive$(action$)))));
exports.epicRecoverReset$ = epicRecoverReset$;
//# sourceMappingURL=epic-recover.js.map
;