getting-started icon indicating copy to clipboard operation
getting-started copied to clipboard

how to hot-import config?

Open suntong opened this issue 7 years ago • 11 comments

I'm trying to hot-import config:

const { hotImport } = require('hot-import')

async function onMessage (message) {
  const cfg = await hotImport('./config')
  console.log(`Received message: ${message} from ${cfg.db.host}`)
}

It works for the first received message. However on receiving the 2nd message, the program errored out with:

(node:80803) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'db' of undefined
    at Wechaty.onMessage (/path/to/wx/hot-import-bot/on-message.js:25:56)
(node:80803) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

How can I make it works please?

suntong avatar Sep 03 '18 02:09 suntong

Made another attempt to fix it myself, still nok

suntong avatar Sep 03 '18 21:09 suntong

Hi, I did not use a constant in hotMod before, however, I had just checked the document and it seems should support it. (at https://github.com/zixia/hot-import )

Can you try to run the example at https://github.com/zixia/hot-import#full-example ? the source is at https://github.com/zixia/hot-import/blob/master/examples/demo-js.ts

I'll also have a look into your code(which use a constant in module like cfg.db.host) later.

huan avatar Sep 04 '18 01:09 huan

Yeah, the demo-js.ts works fine for me, I've tested it as

both work fine.

suntong avatar Sep 04 '18 02:09 suntong

I think the problem is not constant in hotMod, but the **await** hotImport in **async** function onMessage that is causing the problem.

Once again, It works for the first received message but not the second.

suntong avatar Sep 04 '18 02:09 suntong

Did you find any way to resolve this issue and could you share it for us? Thanks.

huan avatar Dec 23 '18 10:12 huan

I think ... the await hotImport in async function onMessage that is causing the problem.

Moving the await hotImport out of the async onMessage event-handling function solved the problem, because the redefinition was indeed the problem.

suntong avatar Dec 23 '18 15:12 suntong

Yes, we should only use hotImport once for a module from the beginning of the code.

Glad to hear from you that this issue had been fixed. Cheers and happy Christmas!

huan avatar Dec 24 '18 07:12 huan

Oh, actually it is not working -- It seems working because I have a let cfg = require('./config') at the top of the script, but I was not able to make it hotImported.

Reopening it, and hope someone can put hot-import config into the demo of https://github.com/wechaty/wechaty-getting-started/tree/master/examples/professional/hot-import-bot

It isn't hard for me if I know nodejs well.

thx

suntong avatar Dec 25 '18 15:12 suntong

Really? I will have a look into it when I have time.

On the other hand, I remembered that the hotImport feature is working inside Docker container too.

huan avatar Dec 26 '18 08:12 huan

The same problem happens to me too, if it's imported twice there's a problem. The error repros on docker

mkliu avatar Apr 16 '19 07:04 mkliu

I changed to this

if(!config)
{
  config = await hotImport('./config.js')
}

mkliu avatar Apr 16 '19 07:04 mkliu