diff --git a/package.json b/package.json index dd4608e818153b0aede78315084ee6cd21104367..f7e656a3bed742b1f92243f4e9b2fab32544f11c 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "wechaty": { "DEFAULT_HEAD": "chrome", "DEFAULT_PUPPET": "web", - "DEFAULT_PROFILE": "profile", + "DEFAULT_PROFILE": "demo", "DEFAULT_PORT": 8788, "DEFAULT_PROTOCOL": "io|0.0.1", "DEFAULT_TOKEN": "WECHATY_IO_TOKEN", diff --git a/src/config.ts b/src/config.ts index 6115bb69f461695647c9719eae2a9115e27d9b6b..d5cc32e07c0b75ee8ce251959319d740749b87aa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -135,12 +135,14 @@ Object.assign(Config, { puppetInstance }) +export type WatchdogFoodName = 'HEARTBEAT' + | 'POISON' + | 'SCAN' + export type WatchdogFood = { data: any , timeout?: number // millisecond - , type?: 'HEARTBEAT' - | 'POISON' - | 'SCAN' + , type?: WatchdogFoodName } export type ScanInfo = { diff --git a/src/puppet-web/browser.ts b/src/puppet-web/browser.ts index 4105f9c8c1af1b740aa6edf903606e5138642ad0..a68f14b1be1f2e56467b416496d136c6f8aaa860 100644 --- a/src/puppet-web/browser.ts +++ b/src/puppet-web/browser.ts @@ -534,7 +534,11 @@ class Browser extends EventEmitter { } public dead(forceReason?: string): boolean { - log.verbose('PuppetWebBrowser', 'dead(%s)', forceReason ? forceReason : '') + if (forceReason) { + log.verbose('PuppetWebBrowser', 'dead(%s)', forceReason) + } else { + log.silly('PuppetWebBrowser', 'dead()') + } let msg let dead = false diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index e6a3ee34f00aa79c122022b5d8486db72b1e08c1..9093857590eb73e7c77897fb533b360f72e1eee0 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -49,7 +49,10 @@ export class PuppetWeb extends Puppet { private port: number - constructor(private setting: PuppetWebSetting = {}) { + public lastScanEventTime: number + public watchDogLastSaveSession: number + + constructor(public setting: PuppetWebSetting = {}) { super() this.on('watchdog', Watchdog.onFeed.bind(this)) diff --git a/src/puppet-web/watchdog.ts b/src/puppet-web/watchdog.ts index 922d8cce61f83094ececf22543145130ed5bb0d4..c8f8434b365d2175baf4b6b0b3b01df9834cc2f2 100644 --- a/src/puppet-web/watchdog.ts +++ b/src/puppet-web/watchdog.ts @@ -15,12 +15,13 @@ */ // const co = require('co') -import log from '../brolog-env' import { - WatchdogFood -} from '../config' - -import Event from './event' + WatchdogFood + , WatchdogFoodName + , log +} from '../config' +import PuppetWeb from './puppet-web' +import Event from './event' /* tslint:disable:variable-name */ const Watchdog = { @@ -122,11 +123,16 @@ function watchDogReset(timeout, lastFeed) { * save every 5 mins * */ -function autoSaveSession() { +function autoSaveSession(this: PuppetWeb, force = false) { + log.silly('PuppetWebWatchdog', 'autoSaveSession()') + + if (force) { + this.watchDogLastSaveSession = 0 // 0 will cause save session right now + } const SAVE_SESSION_INTERVAL = 5 * 60 * 1000 // 5 mins if (Date.now() - this.watchDogLastSaveSession > SAVE_SESSION_INTERVAL) { - log.verbose('PuppetWebWatchdog', 'watchDog() saveSession(%s) after %d minutes' - , this.profile + log.verbose('PuppetWebWatchdog', 'autoSaveSession() profile(%s) after %d minutes' + , this.setting.profile , Math.floor(SAVE_SESSION_INTERVAL / 1000 / 60) ) this.browser.saveSession() @@ -143,16 +149,17 @@ function autoSaveSession() { * so we need to refresh the page after a while * */ -function monitorScan(type) { +function monitorScan(this: PuppetWeb, type: WatchdogFoodName) { log.silly('PuppetWebWatchdog', 'monitorScan(%s)', type) const scanTimeout = 10 * 60 * 1000 // 10 mins if (type === 'SCAN') { // watchDog was feed a 'scan' data this.lastScanEventTime = Date.now() + // autoSaveSession.call(this, true) } if (this.logined()) { // XXX: login status right? - this.lastScanEventTime = null + this.lastScanEventTime = 0 } else if (this.lastScanEventTime && Date.now() - this.lastScanEventTime > scanTimeout) { log.warn('PuppetWebWatchdog', 'monirotScan() refresh browser for no food of type scan after %s mins' diff --git a/src/room.ts b/src/room.ts index d523a35c134e046e69e874284b440c54cbca8ac7..58211b1d037f0d015e31a6ab9799278a93b3f139 100644 --- a/src/room.ts +++ b/src/room.ts @@ -353,7 +353,7 @@ export class Room extends EventEmitter implements Sayable { public static create(contactList: Contact[], topic?: string): Promise { log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic) - if (!contactList || !(typeof contactList === 'array')) { + if (!contactList || !Array.isArray(contactList)) { throw new Error('contactList not found') }