Skip to content

NodeJS #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Nodejs/Factory/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function MyClass (options) {
this.options = options;
}

function create(options) {
// modify the options here if you want
return new MyClass(options);
}

module.exports.create = create;
38 changes: 38 additions & 0 deletions Nodejs/observers/MyFancyObservable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// MyFancyObservable.js

// Reference from: https://blog.risingstack.com/fundamental-node-js-design-patterns/

// var util = require('util');
// var EventEmitter = require('events').EventEmitter;

// function MyFancyObservable() {
// EventEmitter.call(this);
// }

// util.inherits(MyFancyObservable, EventEmitter);

// MyFancyObservable.prototype.hello = function (name) {
// this.emit('hello', name);
// };

// ---------------

// Code improved
var EventEmitter = require('events').EventEmitter;

class MyFancyObservable extends EventEmitter {
constructor() {
super();
EventEmitter.call(this);
}

hello(name) {
this.emit('hello', name);
}

writeInAgenda() {
this.emit('write-firestore-agents-agenda', { visit: { code: '989' }});
}
}

module.exports = MyFancyObservable;
26 changes: 26 additions & 0 deletions Nodejs/observers/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var MyFancyObservable = require('./MyFancyObservable');
var observable = new MyFancyObservable();

observable.on('hello', (name) => {
setTimeout(() => {
console.log(name, new Date().toString())
}, 1000)

process.nextTick();
});


observable.on('write-firestore-agents-agenda', (newVisit) => {
// do some stuff
console.log('write-firestore-agents-agenda', new Date().toString());
});

// observable.hello('john');
// observable.writeInAgenda();

const SayHello = () => Promise.resolve(observable.hello('john'))
const WriteAgenda = () => Promise.resolve(observable.writeInAgenda())


const promises = Promise.all([SayHello(), WriteAgenda()])