Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enable features like hot reload during development process? #134

Closed
imWildCat opened this issue Dec 12, 2016 · 3 comments
Closed

Comments

@imWildCat
Copy link

imWildCat commented Dec 12, 2016

Dear maintainers,

Durning my development process of this project, I have to restart the programme if the codes are changed. Is there any way to implement features such as hot load?

It is found that there is a dev.ts in package.json. Would you please show the contents of it?

Or, is there any way to save sessions so that it will not require QR code scanning while restarting?

@huan huan added the question label Dec 13, 2016
@huan
Copy link
Member

huan commented Dec 13, 2016

Dear @imWildCat ,

  1. "Hot Reload" is a very good idea to make develop fast because the code will go live right after you edit it.

There's many ways to "Hot Reload" javascript source code, one of them is import the function from your code file, and re-import it if it updated.

An example should like this: (just write from scratch, not tested)

let hotOnMessage = require('./hotOnMessage')

fs.watch('./hotOnMessage.js', (eventType, filename) => {
  if (eventType === 'change') {
    hotReload(filename)
  }
})

function hotReload(filename) {
  // http://stackoverflow.com/a/20790238/1123955
  delete require.cache[filename]
  hotOnMessage = require('./hotOnMessage')
}

Wechaty.instance()
.on('message', msg => hotOnMessage(msg))

If you could make it work, I'd like to invite you to write an example/hot-reload-bot.ts for Wechaty, because that's also what I want, and will help others as well.

  1. How to save sessions so that it will not require scan QR Code each time

Wechaty already support that: use profile to define your bot profile name in Wechaty.instance().

An example should like this:

Wechaty.instance({ profile: 'my-bot-name-which-will-save-session' })

You can have a look inside example directory, almost all bots support that already. There's also a known bug which will cause the session reload fail: sometimes you will be prompted to scan the QR Code again even you specified profile.

Please let me know if you have any other questions, hope you can make hot reloading work soon!

@imWildCat
Copy link
Author

@zixia , many thanks for your detailed response! Have a nice day!

@huan
Copy link
Member

huan commented Mar 9, 2017

Thanks for the implemention PR from @Gcaufy !

Example is at here: https://github.com/wechaty/wechaty/tree/master/example/hot-reload-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants