adopted from rainbows' fork for adding support for this.

check:
https://github.com/godfat/rainbows/tree/em-thread