diff --git a/README.md b/README.md index c32c34d..59701f7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # assignment_async_nodejs Async Node.js sprint + + +Steven Zarrella \ No newline at end of file diff --git a/data/testy.txt b/data/testy.txt new file mode 100644 index 0000000..2d33477 --- /dev/null +++ b/data/testy.txt @@ -0,0 +1 @@ +The mouse is a big blue rabbit fox hound nugget!!!! MEOWWWWW!!! \ No newline at end of file diff --git a/data/text.txt b/data/text.txt new file mode 100644 index 0000000..dcb4c5a --- /dev/null +++ b/data/text.txt @@ -0,0 +1 @@ +Organic twee quinoa slow-carb biodiesel. Ethical kinfolk disrupt tousled, direct trade slow-carb salvia. Banh mi intelligentsia thundercats sartorial VHS schlitz hoodie. Bitters chia ennui bushwick, tilde hexagon cornhole paleo chambray tumblr etsy. XOXO locavore gochujang, occupy meh literally air plant. Cardigan bicycle rights letterpress, trust fund plaid blog mustache vexillologist bushwick slow-carb normcore gastropub tacos hammock. Gastropub bicycle rights ramps direct trade VHS. Copper mug 90's iceland, biodiesel enamel pin hot chicken art party occupy bitters scenester. Vexillologist master cleanse umami green juice. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..f9292be --- /dev/null +++ b/index.js @@ -0,0 +1,182 @@ +"use strict"; + +const fs = require('fs'), + //Emitter = require('events'); + Emitter = require('./modules/emitter'); + +// WARMUP 1 +// let promise1 = new Promise(function(resolve, reject) { +// if (1===1) { +// resolve("Hello Promise!"); +// } else { +// reject("UNIVERSE BROKEN."); +// } +// }) +// .then(function(message) { +// setTimeout(function() { +// return; +// }, 1000); +// console.log(message); +// }); + + +// // WARMUP 2 +// let delay = function(milliseconds) { +// return new Promise(function(resolve, reject) { +// setTimeout(function() { +// resolve(milliseconds); +// }, milliseconds); +// }); +// } + +// let countDown = function(value) { +// let newValue = value - 100; +// if (newValue < 100) { +// newValue = 0; +// } +// console.log(newValue); +// return new Promise(function(resolve, reject) { +// resolve(delay(newValue)); +// }); +// } + +// delay(1000) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .then(countDown) +// .catch(function(error) { +// console.log(error); +// }); + + +// // WARMUP 3 +// let squaredPromise = function(number) { +// return new Promise(function(resolve, reject) { +// if (typeof number === 'number') { +// var numSquared = number * number; +// resolve(numSquared); +// } else { +// reject("NOT A NUMBER"); +// } +// }); +// } + +// let numbers = [2, 3, 4, 5, 6]; + +// numbers = numbers.map(squaredPromise); + +// Promise.all(numbers) +// .then(function(results) { +// console.log(results); +// }) +// .catch(function(error) { +// console.log(error); +// }); + + +// // WARMUP 4 +// let doBadThing = function(forRealz) { +// return new Promise(function(resolve, reject) { +// if(forRealz == true) { +// resolve("Yay!"); +// } else { +// reject("BOO."); +// } +// }); +// } + +// doBadThing(true) +// .then(function(result) { +// console.log(result); +// }) +// .catch(function(error) { +// console.log(error); +// }); + + +// // FILE OPERATIONS +// let fsp = { +// readFile: function(path) { +// return new Promise(function(resolve, reject) { +// var text = fs.readFile(path, 'utf8', (error, data) => { +// if (error) throw error; +// resolve(data); +// }); +// }); +// }, + +// writeFile: function(path, input) { +// return new Promise(function(resolve, reject) { +// var text = fs.writeFile(path, input, 'utf8', (error) => { +// if (error) reject(error); +// resolve(fsp.readFile(path)); +// }); +// }); +// }, + +// appendFile: function(path, input) { +// return new Promise(function(resolve, reject) { +// var text = fs.appendFile(path, input, 'utf8', (error) => { +// if (error) reject(error); +// resolve(fsp.readFile(path)); +// }); +// }); +// }, +// } + + +// fsp.readFile('./data/text.txt') +// .then(function(data) { +// console.log(data); +// }) +// .catch(function(error) { +// console.log(error); +// }); + +// fsp.writeFile('./data/testy.txt', "The mouse is a big blue rabbit fox hound nugget!!!!") +// .then(function(data) { +// console.log("File saved! CONTENTS: " + data); +// }) +// .catch(function(error) { +// console.log(error); +// }); + +// fsp.appendFile('./data/testy.txt', " MEOWWWWW!!!") +// .then(function(data) { +// console.log("File saved! CONTENTS: " + data); +// }) +// .catch(function(error) { +// console.log(error); +// }); + +// CREATE AN EVENT EMITTER FROM SCRATCH +let emitter = new Emitter(); + +emitter.on("meow", function(){console.log("foo meow");}); +emitter.on("woof", function(){console.log("foo woof");}); +emitter.on("click", function(){console.log("foo click");}); +emitter.on("woof", function(){setTimeout(function() {console.log("WOOF woof")} + , 3000)}); +emitter.on("woof", function(){console.log("KITTYCAT woof");}); + + +emitter.emit("woof"); + +//emitter.removeListener("woof", woofWoof); + +emitter.removeAllListeners("woof"); + + + + + + + + diff --git a/modules/emitter.js b/modules/emitter.js new file mode 100644 index 0000000..d6100c0 --- /dev/null +++ b/modules/emitter.js @@ -0,0 +1,108 @@ +"use strict"; + +//create Emitter constructor +function Emitter(eventType, callback) { + + this.listeners = []; + + this.executeCallback = function(data) { + if (data.length !== 0) { + data.forEach(function(item) { + return new Promise(function(resolve, reject) { + if (item) { + resolve(item.callback()); + } else { + reject("No callback found!"); + } + }); + }); + } else { + throw new Error("Event not found!"); + } + }; + + this.on = function(eventType, callback) { + this.listeners.push({ + "eventType": eventType, + "callback": callback + }); + }; //this.on + + this.emit = function(event) { + + let listeners = this.listeners; + let matchArray = []; + + //create the promise to execute each matching callback asynchronously + let executeCallback = function(data) { + if (data.length !== 0) { + return new Promise(function(resolve, reject) { + if (data) { + resolve(data.callback()); + } else { + reject("No callback found!"); + } + }); + } else { + throw new Error("Event not found!"); + } + }; + + //cycle through each listener to look for a matching event + listeners.forEach(function(item) { + if (item.eventType === event) { + matchArray.push(item); + }; + }); + + //execute each matching event's callback function + if (matchArray.length === 0) { + throw new Error("Event not found."); + } else { + matchArray.forEach(function(item) { + executeCallback(item); + }); + console.log("Success! Event(s) triggered."); + } + + }; //this.emit + + this.removeListener = function(event, callFunc) { + + let eventString = event.toString() + callFunc.toString(); + + this.listeners = this.listeners.filter(function(item) { + let currentString = item.eventType.toString() + item.callback.toString(); + console.log(currentString); + if (currentString !== eventString) { + return item; + } + }); + console.log("Current listeners: " + this.listeners); + }; //this.removelistener + + this.removeAllListeners = function(event) { + this.listeners = this.listeners.filter(function(item) { + if (item.eventType !== event) { + return item; + } + }); + + }; //this.removeAllListeners +} + +module.exports = Emitter; + + + + + + + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..8fc06db --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "assignment_async_nodejs", + "version": "1.0.0", + "description": "Async Node.js sprint", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/stevenvz/assignment_async_nodejs.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/stevenvz/assignment_async_nodejs/issues" + }, + "homepage": "https://github.com/stevenvz/assignment_async_nodejs#readme" +}