puppet.ts 2.5 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2 3 4
 * Wechat for Bot. and for human who can talk with bot/robot
 *
 * Interface for puppet
5
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
 * Class Puppet
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
7 8 9 10 11
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12

13
import { EventEmitter } from 'events'
14

15 16 17 18
import Contact  from './contact'
import Message  from './message'
import Room     from './room'
import log      from './brolog-env'
19 20

class Puppet extends EventEmitter {
21 22 23 24 25
  private _user: Contact

  private _targetState:   string
  private _currentState:  string

26 27
  constructor() {
    super()
28 29

    /*
30
     * @deprecated
31 32 33
     * connected / disconnected
     * connecting / disconnecting
     */
34 35 36 37 38 39 40
    // this._readyState = 'disconnected'

    this.targetState('dead')
    this.currentState('dead')
  }

  // targetState : 'live' | 'dead'
41
  public targetState(newState) {
42 43 44 45 46
    if (newState) {
      log.verbose('Puppet', 'targetState(%s)', newState)
      this._targetState = newState
    }
    return this._targetState
47 48
  }

49
  // currentState : 'birthing' | 'killing'
50
  public currentState(newState) {
51
    if (newState) {
52 53
      log.verbose('Puppet', 'currentState(%s)', newState)
      this._currentState = newState
54
    }
55
    return this._currentState
56 57
  }

58 59 60 61 62 63 64 65 66 67
  public self(message?: Message): boolean | Contact {
    throw new Error('pure virtual interface function')
  }

  public user(contact?: Contact) {
    if (contact) {
      this._user = contact
    }
    return this._user
  }
68

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69
  /**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70 71 72
   * let puppet send message
   *
   * @param <Message> message - the message to be sent
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73
   * @return <Promise>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74
   */
75 76
  public send(message): Promise<any>  { throw new Error('To Be Implemented') }
  public reply(message, reply): Promise<any> { throw new Error('To Be Implemented') }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77

78 79 80 81
  public reset(reason?: string)  { throw new Error('To Be Implementsd') }
  public logout(): Promise<any>  { throw new Error('To Be Implementsd') }
  public quit(): Promise<any>  { throw new Error('To Be Implementsd') }
  public ding(data?: string): Promise<any>        { throw new Error('To Be Implementsd') }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82

83
  public getContact(id): Promise<any>  { // for unit testing
84
    log.verbose('Puppet', `Interface method getContact(${id})`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
85 86
    throw new Error('Absolute Interface Method should never to be called')
    // return Promise.resolve({UserName: 'WeChaty', NickName: 'Puppet'})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
87
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
89
  // () { throw new Error('To Be Implemented')  }
90 91
}

92 93 94 95 96
// Object.assign(Puppet, {
//   Message:    require('./message')
//   , Contact:  require('./contact')
//   , Room:     require('./room')
// })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
97

98 99
// module.exports = Puppet
export default Puppet
100 101 102 103 104 105
export {
    Contact
  , Message
  , Puppet
  , Room
}