231103.1 · 7f4a9ed91bbecb84e39f4bc8030247ce5116ddf8
Reorganize examples into categorized folders (#341)
* refactor: create directory structure to organize exmaples
* test: update test_all to work with new directory structure
* fix: bug in test_all if no package.json scripts
* test: cleanup output of test_all
* fix: remove default test script that throws from thread_safe_function_with_object_wrap
* fix: remove build_with_cmake from .gitignore
* fix: swith to ' from " and remove subdir from getAllTests call
* test: add extra test cases for npm start and just running the pkgJson.main with node
* refactor: move all examples to src directory
* refactor: add numbers to categories so they are ordered in the src folder
* refactor: simplify getAllExamples in test_all.js
* docs: add directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure example in readme
* add back build_with_cmake
---------
Co-authored-by: legendecas <legendecas@gmail.com>
reading declarations
Changes to src/3-context-awareness/napi/index.js. All files
src/3-context-awareness/napi/index.js
@@ -0,0 +1,40 @@+// Example illustrating the case where a native addon is loaded multiple times.+// This entire file is executed twice, concurrently - once on the main thread,+// and once on a thread launched from the main thread.++// We load the worker threads module, which allows us to launch multiple Node.js+// environments, each in its own thread.+const {+ Worker, isMainThread+} = require('worker_threads');++// We load the native addon.+const addon = require('bindings')('multiple_load');++// The iteration count can be tweaked to ensure that the output from the two+// threads is interleaved. Too few iterations and the output of one thread+// follows the output of the other, not really illustrating the concurrency.+const iterations = 1000;++// This function is an idle loop that performs a random walk from 0 by calling+// into the native addon to either increment or decrement the initial value.+function useAddon(addon, prefix, iterations) {+ if (iterations >= 0) {+ if (Math.random() < 0.5) {+ console.log(prefix + ': new value (decremented): ' + addon.decrement());+ } else {+ console.log(prefix + ': new value (incremented): ' + addon.increment());+ }+ setImmediate(() => useAddon(addon, prefix, --iterations));+ }+}++if (isMainThread) {+ // On the main thread, we launch a worker and wait for it to come online. Then+ // we start the loop.+ (new Worker(__filename)).on('online',+ () => useAddon(addon, "Main thread", iterations));+} else {+ // On the secondary thread we immediately start the loop.+ useAddon(addon, "Worker thread", iterations);+}