提交 d855853a 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

upgrade memory card for a better logic

上级 d34f62bf
......@@ -17,7 +17,6 @@
*
*/
import {
config,
Contact,
Message,
Wechaty,
......@@ -32,7 +31,7 @@ import { generate } from 'qrcode-terminal'
*
*/
const bot = new Wechaty({
name : config.default.DEFAULT_PROFILE,
name : 'ding-dong-bot',
})
/**
......
......@@ -7,7 +7,6 @@
"wechaty": {
"DEFAULT_HEAD": 0,
"DEFAULT_PORT": 8080,
"DEFAULT_PROFILE": "default",
"DEFAULT_PROTOCOL": "io|0.0.1",
"DEFAULT_TOKEN": "WECHATY_IO_TOKEN",
"DEFAULT_APIHOST": "api.chatie.io"
......@@ -82,7 +81,7 @@
"cuid": "^2.1.1",
"file-box": "^0.8.23",
"hot-import": "^0.2.1",
"memory-card": "^0.5.5",
"memory-card": "^0.6.3",
"npm-programmatic": "0.0.11",
"pkg-dir": "^3.0.0",
"promise-retry": "^1.1.1",
......
......@@ -30,7 +30,7 @@ test('important variables', async t => {
t.true('token' in config, 'should exist `token` in Config')
// t.ok(config.default.DEFAULT_PUPPET , 'should export DEFAULT_PUPPET')
t.ok(config.default.DEFAULT_PROFILE , 'should export DEFAULT_PROFILE')
// t.ok(config.default.DEFAULT_PROFILE , 'should export DEFAULT_PROFILE')
t.ok(config.default.DEFAULT_PROTOCOL , 'should export DEFAULT_PROTOCOL')
t.ok(config.default.DEFAULT_APIHOST , 'should export DEFAULT_APIHOST')
})
......
......@@ -104,7 +104,7 @@ export interface DefaultSetting {
DEFAULT_PORT : number,
// DEFAULT_PUPPET : PuppetName,
DEFAULT_APIHOST : string,
DEFAULT_PROFILE : string,
// DEFAULT_PROFILE : string,
DEFAULT_TOKEN : string,
DEFAULT_PROTOCOL : string,
}
......@@ -125,8 +125,12 @@ export class Config {
).toLowerCase() as PuppetModuleName
}
public profile = process.env.WECHATY_PROFILE || null // DO NOT set DEFAULT_PROFILE, because sometimes user do not want to save session
public token = process.env.WECHATY_TOKEN || null // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
// DEPRECATED: Use WECHATY_NAME instead
public profile = process.env.WECHATY_PROFILE
public name = process.env.WECHATY_NAME || process.env.WECHATY_PROFILE // replace WECHATY_PROFILE
public token = process.env.WECHATY_TOKEN // DO NOT set DEFAULT, because sometimes user do not want to connect to io cloud service
public debug = !!(process.env.WECHATY_DEBUG)
public httpPort = process.env.PORT || process.env.WECHATY_PORT || DEFAULT_SETTING.DEFAULT_PORT
......@@ -137,6 +141,10 @@ export class Config {
constructor () {
log.verbose('Config', 'constructor()')
this.validApiHost(this.apihost)
if (this.profile) {
log.warn('Config', 'constructor() WECHATY_PROFILE is DEPRECATED, use WECHATY_NAME instead.')
}
}
/**
......
......@@ -98,11 +98,11 @@ export type WechatyEventName = keyof typeof WECHATY_EVENT_DICT
export interface WechatyOptions {
memory? : MemoryCard,
name? : null | string, // Wechaty Name
profile? : null | string, // DEPRECATED: use name instead
puppet? : PuppetModuleName | Puppet, // Puppet name or instance
puppetOptions? : PuppetOptions, // Puppet TOKEN
ioToken? : string, // Io TOKEN
name? : string, // Wechaty Name
profile? : null | string, // DEPRECATED: use name instead
puppet? : PuppetModuleName | Puppet, // Puppet name or instance
puppetOptions? : PuppetOptions, // Puppet TOKEN
ioToken? : string, // Io TOKEN
}
const PUPPET_MEMORY_NAME = 'puppet'
......@@ -143,7 +143,7 @@ export class Wechaty extends Accessory implements Sayable {
*/
private static globalInstance: Wechaty
private readonly memory : MemoryCard
private memory?: MemoryCard
private lifeTimer? : NodeJS.Timer
private io? : Io
......@@ -239,20 +239,8 @@ export class Wechaty extends Accessory implements Sayable {
options.name = options.profile
}
options.name = options.name === null
? null
: (options.name || config.default.DEFAULT_PROFILE)
this.id = cuid()
this.memory = options.memory
? options.memory
: new MemoryCard(
options.name
? { name: options.name }
: undefined,
)
this.state = new StateSwitch('Wechaty', log)
this.readyState = new StateSwitch('WechatyReady', log)
......@@ -288,7 +276,7 @@ export class Wechaty extends Accessory implements Sayable {
'Wechaty#',
this.id,
`<${this.options && this.options.puppet || ''}>`,
`(${this.memory.name || ''})`,
`(${this.memory && this.memory.name || ''})`,
].join('')
}
......@@ -570,6 +558,10 @@ export class Wechaty extends Accessory implements Sayable {
return
}
if (!this.memory) {
throw new Error('no memory')
}
const puppet = this.options.puppet || config.systemPuppetName()
const puppetMemory = this.memory.multiplex(PUPPET_MEMORY_NAME)
......@@ -636,7 +628,6 @@ export class Wechaty extends Accessory implements Sayable {
const contact = this.ContactSelf.load(contactId)
await contact.ready()
this.emit('login', contact)
await this.memory.save()
})
break
......@@ -645,7 +636,6 @@ export class Wechaty extends Accessory implements Sayable {
const contact = this.ContactSelf.load(contactId)
await contact.ready()
this.emit('logout', contact)
await this.memory.save()
})
break
......@@ -810,8 +800,10 @@ export class Wechaty extends Accessory implements Sayable {
this.state.on('pending')
try {
await this.memory.load()
if (!this.memory) {
this.memory = new MemoryCard(this.options.name)
await this.memory.load()
}
await this.initPuppet()
await this.puppet.start()
......@@ -874,7 +866,6 @@ export class Wechaty extends Accessory implements Sayable {
this.readyState.off(true)
this.state.off('pending')
await this.memory.save()
if (this.lifeTimer) {
clearInterval(this.lifeTimer)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册