fix exercise 2 and add exercise 3 in chapter 8 #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thanks for the great book.
I fixed 2 problems in the answers of the exercises of chapter 8.
1. Fix
onmessage
compilation errorThe current
onmessage
implementation in the answer of the exercise 2 does not compile.I implemented
handle
method in whichswitch
is used for type refinement and createdData
type which takes advantage of distributive conditional type to declare more appropriate type than{command: C; args: MatrixProtocol[C]['in']}
fordata
.2. Add an answer for the exercise 3
Added code similar to the worker thread example.
By the way, though it is described that
child_process
is the way to do multithreading in Node.js, it is actually for multi processing.Node.js provides
worker_threads
module for multithreading.Other thing to note
This is not a problem in exercises, but the
SafeEmitter
implementation in chapter 8 does not compile.What about using interface instead of class?
Not only does this compile, but also this is more lightweight as this is a type-level wrapper and does not emit any JavaScript code.