本文整理汇总了TypeScript中state-switch.StateSwitch类的典型用法代码示例。如果您正苦于以下问题:TypeScript StateSwitch类的具体用法?TypeScript StateSwitch怎么用?TypeScript StateSwitch使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StateSwitch类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: start
public async start (): Promise<void> {
log.verbose('IoClient', 'init()')
if (this.state.pending()) {
log.warn('IoClient', 'start() with a pending state, not the time')
const e = new Error('state.pending() when start()')
throw e
}
this.state.on('pending')
try {
await this.startIo()
await this.hookWechaty(this.options.wechaty)
this.state.on(true)
} catch (e) {
log.error('IoClient', 'init() exception: %s', e.message)
this.state.off(true)
throw e
}
return
}
示例2: quit
public async quit (): Promise<void> {
log.verbose('IoClient', 'quit()')
if (this.state.off() === 'pending') {
log.warn('IoClient', 'quit() with state.off() = `pending`, skipped')
throw new Error('quit() with state.off() = `pending`')
}
this.state.off('pending')
try {
if (this.options.wechaty) {
await this.options.wechaty.stop()
// this.wechaty = null
} else { log.warn('IoClient', 'quit() no this.wechaty') }
if (this.io) {
await this.io.stop()
// this.io = null
} else { log.warn('IoClient', 'quit() no this.io') }
} catch (e) {
log.error('IoClient', 'exception: %s', e.message)
throw e
} finally {
this.state.off(true)
}
return
}
示例3: start
public async start(): Promise<void> {
log.verbose('PuppetPadchatManager', `start()`)
if (this.userId) {
throw new Error('userId exist')
}
this.state.on('pending')
if (this.delayQueueExecutorSubscription) {
throw new Error('this.delayExecutorSubscription exist')
} else {
this.delayQueueExecutorSubscription = this.delayQueueExecutor.subscribe(unit => {
log.verbose('PuppetPadchatManager', 'startQueues() delayQueueExecutor.subscribe(%s) executed', unit.name)
})
}
this.memorySlot = {
...this.memorySlot,
...await this.options.memory.get<PadchatMemorySlot>(MEMORY_SLOT_NAME),
}
/**
* Sometimes the RPC WebSocket will failure on connect in super.start(),
* if that's true then a Error will be throw out.
* Try again in the following while loop until the state is not on('pending')
*/
while (this.state.on() === 'pending') {
try {
await super.start()
break
} catch (e) {
log.warn('PuppetPadchatManager', 'start() super.start() exception: %s', e)
await super.stop()
await new Promise(r => setTimeout(r, 1000))
log.warn('PuppetPadchatManager', 'start() super.start() retry now ...')
}
}
await this.tryLoad62Data()
const succeed = await this.tryAutoLogin(this.memorySlot)
if (!succeed) {
await this.startCheckScan()
}
this.state.on(true)
}
示例4: stop
public async stop (): Promise<void> {
log.verbose('IoClient', 'stop()')
this.state.off('pending')
// XXX
if (!this.io) {
log.warn('IoClient', 'stop() without this.io')
this.state.off(true)
return
}
await this.io.stop()
this.state.off(true)
// XXX 20161026
// this.io = null
return
}
示例5: stop
public async stop(): Promise<void> {
log.verbose('PuppetPadchatManager', `stop()`)
this.state.off('pending')
if (this.delayQueueExecutorSubscription) {
this.delayQueueExecutorSubscription.unsubscribe()
this.delayQueueExecutorSubscription = undefined
} else {
log.warn('PuppetPadchatManager', 'stop() subscript not exist')
}
await this.stopCheckScan()
await super.stop()
await this.releaseCache()
this.userId = undefined
this.loginScanQrcode = undefined
this.loginScanStatus = undefined
this.state.off(true)
}
示例6: hookWechaty
private async hookWechaty (wechaty: Wechaty): Promise<void> {
log.verbose('IoClient', 'initWechaty()')
if (this.state.off()) {
const e = new Error('state.off() is true, skipped')
log.warn('IoClient', 'initWechaty() %s', e.message)
throw e
}
wechaty
.on('login' , user => log.info('IoClient', `${user.name()} logined`))
.on('logout' , user => log.info('IoClient', `${user.name()} logouted`))
.on('scan', (url, code) => log.info('IoClient', `[${code}] ${url}`))
.on('message' , msg => this.onMessage(msg))
return
}
示例7: startIo
private async startIo (): Promise<void> {
log.verbose('IoClient', 'startIo() with token %s', this.options.token)
if (this.state.off()) {
const e = new Error('startIo() state.off() is true, skipped')
log.warn('IoClient', e.message)
throw e
}
try {
await this.io.start()
} catch (e) {
log.verbose('IoClient', 'startIo() init fail: %s', e.message)
throw e
}
return
}
示例8: syncContactsAndRooms
public async syncContactsAndRooms(): Promise<void> {
log.verbose('PuppetPadchatManager', `syncContactsAndRooms()`)
let cont = true
while (cont && this.state.on() && this.userId) {
log.silly('PuppetPadchatManager', `syncContactsAndRooms() while() syncing WXSyncContact ...`)
const syncContactList = await this.WXSyncContact()
await new Promise(r => setTimeout(r, 10 * 1000))
if (!Array.isArray(syncContactList) || syncContactList.length <= 0) {
log.warn('PuppetPadchatManager', 'syncContactsAndRooms() cannot get array result: %s', JSON.stringify(syncContactList))
continue
}
if ( !this.cacheContactRawPayload
|| !this.cacheRoomRawPayload
) {
throw new Error('no cache')
}
log.silly('PuppetPadchatManager', 'syncContactsAndRooms() syncing %d out of Contact(%d) & Room(%d) ...',
syncContactList.length,
this.cacheContactRawPayload.size,
this.cacheRoomRawPayload.size,
)
for (const syncContact of syncContactList) {
if (syncContact.continue !== PadchatContinue.Go) {
log.verbose('PuppetPadchatManager', 'syncContactsAndRooms() sync contact done!')
cont = false
break
}
if (syncContact.msg_type === PadchatContactMsgType.Contact) {
if (isRoomId(syncContact.user_name)) {
/**
* Room
*/
log.silly('PuppetPadchatManager', 'syncContactsAndRooms() updating Room %s(%s)',
syncContact.nick_name,
syncContact.user_name,
)
const roomId = syncContact.user_name
const roomPayload = syncContact as PadchatRoomPayload
this.cacheRoomRawPayload.set(roomId, roomPayload)
/**
* Use delay queue executor to sync room:
* add syncRoomMember task to the queue
*/
await this.delayQueueExecutor.execute(
() => this.syncRoomMember(roomId),
`syncRoomMember(${roomId})`,
)
log.silly('PuppetPadchatManager', 'syncContactsAndRooms() added sync room(%s) task to delayQueueExecutor', roomId)
} else if (isContactId(syncContact.user_name)) {
/**
* Contact
*/
log.silly('PuppetPadchatManager', 'syncContactsAndRooms() updating Contact %s(%s)',
syncContact.nick_name,
syncContact.user_name,
)
const contactPayload = syncContact as PadchatContactPayload
const contactId = contactPayload.user_name
this.cacheContactRawPayload.set(contactId, contactPayload)
} else {
throw new Error('id is neither room nor contact')
}
} else {
// {"continue":1,"msg_type":2048,"status":1,"uin":4763975}
if ( syncContact.continue === PadchatContinue.Go
&& syncContact.msg_type === PadchatContactMsgType.N11_2048
&& typeof syncContact.uin !== 'undefined'
) {
// HeartBeat??? discard it in silent
} else {
log.silly('PuppetPadchatManager', `syncContactsAndRooms() skip for syncContact.msg_type=%s(%s) %s`,
syncContact.msg_type && PadchatContactMsgType[syncContact.msg_type],
syncContact.msg_type,
JSON.stringify(syncContact),
)
}
}
}
}
}