Here is a case I faced using the great ngrx/store & effects libraries to take care of my data.
Working only with streams to manage all side effects is hard at first but once you got the declic and start to tame more and more rxjs operators, you are the king of the world!
➜ Simple need to start:
Imagine you have these needs:
When ACTION_X –> callApiX() –> ACTION_X_SUCCESS / ACTION_X_FAIL
When ACTION_Y –> callApiY() –> ACTION_Y_SUCCESS / ACTION_Y_FAIL
Nothing special here:
An action > an HTTP request > another action depending on success or not.
In case of success, results are stored inside state using reducers.
This corresponds to these @effects code:
➜ Let’s complexify the need:
Now imagine that, before calling callApiY(), you need to be sure that callApiX() has been called successfully.
And if it hasn’t, call it first, then call callApiY() and finish with ACTION_Y_SUCCESS if both calls succeed (else ACTION_Y_FAIL).
Here is a simple workaround to solve it elegantly:
I’m using Typescript daily with angular since 6 months and see the positive impact on my workflow and the code I produce.
So I decided to use it on the Node side too, but it wasn’t an easy task to have the setup I wanted!
I didn’t find a place with all explanations up to date and using latest tools so I post it there to hopefully help some of you. ;)
Scroll down to find full JSON files details.
A. Produce code compatible with Node:
NodeJS needs compatible JS files in input to work, so configure your tsconfig.json with these properties:
"target": "es6" Or "es5" if you are using a Node version older than 6.
"module": "commonjs" It tells compiler to convert all ES6 modules (import/export) to CommonJS modules (require/module.exports).
B. Always lunch the app with transpiled JS files from latest TS files:
➜ VSCode automatically displays your TS error.
You don’t need to run the Typescript compiler in watch mode.
Inside tsconfig.json, add property "watch": false.
➜ Configure VSCode debugging.
Use lunch.json config file for debugging session start and tasks.json to create a task which run Typescript compiler.
Inside lunch.json, add property "preLaunchTask": "tsc" to force Typescript compilation before app start.
Inside tasks.json, add properties "command": "tsc" and "isShellCommand": true to make a task named tsc running Typescript compiler.
Inside tsconfig.json, add property "noEmitOnError": true to make compiler throw error when something wrong.
Now when starting your app with F5 (Or Debug panel > Start Debugging), if your TS files are not valid you will see the following message letting you stop debugging to correct it before lunch it again:
Build error has been detected during preLaunchTask 'tsc'. DEBUG ANYWAY / CLOSE