@ 260528.1

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/7-events/. All files

src/7-events/emit_event_from_cpp/node-addon-api/binding.gyp

new file mode 100644@@ -0,0 +1,30 @@+{+  "targets": [+    {+      "target_name": "emit_from_cpp",+      "sources": [+        "src/emit-from-cpp.cc"+      ],+      'cflags!': [ '-fno-exceptions' ],+      'cflags_cc!': [ '-fno-exceptions' ],+      'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],+      'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],+      'conditions': [+        ['OS=="win"', {+          "msvs_settings": {+            "VCCLCompilerTool": {+              "ExceptionHandling": 1+            }+          }+        }],+        ['OS=="mac"', {+          "xcode_settings": {+            "CLANG_CXX_LIBRARY": "libc++",+            'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',+            'MACOSX_DEPLOYMENT_TARGET': '10.7'+          }+        }]+      ]+    }+  ]+}

src/7-events/emit_event_from_cpp/node-addon-api/index.js

new file mode 100644@@ -0,0 +1,19 @@+'use strict'++const EventEmitter = require('events').EventEmitter+const addon = require('bindings')('emit_from_cpp')++const emitter = new EventEmitter()++emitter.on('start', () => {+    console.log('### START ...')+})+emitter.on('data', (evt) => {+    console.log(evt);+})++emitter.on('end', () => {+    console.log('### END ###')+})++addon.callEmit(emitter.emit.bind(emitter))

src/7-events/emit_event_from_cpp/node-addon-api/package.json

new file mode 100644@@ -0,0 +1,15 @@+{+  "name": "emit-event-from-cpp-example",+  "version": "0.0.0",+  "description": "Node.js Addons - emit event from C++ to JS",+  "main": "index.js",+  "private": true,+  "gypfile": true,+  "scripts": {+    "start": "node index.js"+  },+  "dependencies": {+    "node-addon-api": "*",+    "bindings": "*"+  }+}

src/7-events/emit_event_from_cpp/node-addon-api/src/emit-from-cpp.cc

new file mode 100644@@ -0,0 +1,27 @@+#include <napi.h>++#include <chrono>+#include <iostream>+#include <thread>++Napi::Value CallEmit(const Napi::CallbackInfo& info) {+  Napi::Env env = info.Env();+  Napi::Function emit = info[0].As<Napi::Function>();+  emit.Call({Napi::String::New(env, "start")});+  for (int i = 0; i < 3; i++) {+    std::this_thread::sleep_for(std::chrono::seconds(3));+    emit.Call(+        {Napi::String::New(env, "data"), Napi::String::New(env, "data ...")});+  }+  emit.Call({Napi::String::New(env, "end")});+  return Napi::String::New(env, "OK");+}++// Init+Napi::Object Init(Napi::Env env, Napi::Object exports) {+  exports.Set(Napi::String::New(env, "callEmit"),+              Napi::Function::New(env, CallEmit));+  return exports;+}++NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);

src/7-events/inherits_from_event_emitter/node-addon-api/binding.gyp

new file mode 100644@@ -0,0 +1,31 @@+{+  "targets": [+    {+      "target_name": "native_emitter",+      "sources": [+        "src/binding.cc",+        "src/native-emitter.cc"+      ],+      'cflags!': [ '-fno-exceptions' ],+      'cflags_cc!': [ '-fno-exceptions' ],+      'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],+      'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],+      'conditions': [+        ['OS=="win"', {+          "msvs_settings": {+            "VCCLCompilerTool": {+              "ExceptionHandling": 1+            }+          }+        }],+        ['OS=="mac"', {+          "xcode_settings": {+            "CLANG_CXX_LIBRARY": "libc++",+            'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',+            'MACOSX_DEPLOYMENT_TARGET': '10.7'+          }+        }]+      ]+    }+  ]+}

src/7-events/inherits_from_event_emitter/node-addon-api/index.js

new file mode 100644@@ -0,0 +1,23 @@+'use strict'++const EventEmitter = require('events').EventEmitter+const NativeEmitter = require('bindings')('native_emitter').NativeEmitter+const inherits = require('util').inherits++inherits(NativeEmitter, EventEmitter)++const emitter = new NativeEmitter()++emitter.on('start', () => {+    console.log('### START ...')+})++emitter.on('data', (evt) => {+    console.log(evt)+})++emitter.on('end', () => {+    console.log('### END ###')+})++emitter.callAndEmit()

src/7-events/inherits_from_event_emitter/node-addon-api/package.json

new file mode 100644@@ -0,0 +1,15 @@+{+  "name": "inherits-from-event-emitter-example",+  "version": "0.0.0",+  "description": "Node.js Addons - inherits from event emitter",+  "main": "index.js",+  "private": true,+  "gypfile": true,+  "scripts": {+    "start": "node index.js"+  },+  "dependencies": {+    "node-addon-api": "*",+    "bindings": "*"+  }+}

src/7-events/inherits_from_event_emitter/node-addon-api/src/binding.cc

new file mode 100644@@ -0,0 +1,9 @@+#include <napi.h>+#include "native-emitter.h"++Napi::Object Init(Napi::Env env, Napi::Object exports) {+  NativeEmitter::Init(env, exports);+  return exports;+}++NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

src/7-events/inherits_from_event_emitter/node-addon-api/src/native-emitter.cc

new file mode 100644@@ -0,0 +1,40 @@+#include <chrono>+#include <iostream>+#include <thread>++#include "native-emitter.h"++Napi::FunctionReference NativeEmitter::constructor;++Napi::Object NativeEmitter::Init(Napi::Env env, Napi::Object exports) {+  Napi::Function func =+      DefineClass(env,+                  "NativeEmitter",+                  {InstanceMethod("callAndEmit", &NativeEmitter::CallAndEmit)});++  constructor = Napi::Persistent(func);+  constructor.SuppressDestruct();++  exports.Set("NativeEmitter", func);+  return exports;+}++NativeEmitter::NativeEmitter(const Napi::CallbackInfo& info)+    : Napi::ObjectWrap<NativeEmitter>(info) {+  // NOOP+}++Napi::Value NativeEmitter::CallAndEmit(const Napi::CallbackInfo& info) {+  Napi::Env env = info.Env();+  Napi::Function emit =+      info.This().As<Napi::Object>().Get("emit").As<Napi::Function>();+  emit.Call(info.This(), {Napi::String::New(env, "start")});+  for (int i = 0; i < 3; i++) {+    std::this_thread::sleep_for(std::chrono::seconds(1));+    emit.Call(+        info.This(),+        {Napi::String::New(env, "data"), Napi::String::New(env, "data ...")});+  }+  emit.Call(info.This(), {Napi::String::New(env, "end")});+  return Napi::String::New(env, "OK");+}

src/7-events/inherits_from_event_emitter/node-addon-api/src/native-emitter.h

new file mode 100644@@ -0,0 +1,12 @@+#include <napi.h>++class NativeEmitter : public Napi::ObjectWrap<NativeEmitter> {+ public:+  static Napi::Object Init(Napi::Env env, Napi::Object exports);+  NativeEmitter(const Napi::CallbackInfo& info);++ private:+  static Napi::FunctionReference constructor;++  Napi::Value CallAndEmit(const Napi::CallbackInfo& info);+};