Skip to content

Instantly share code, notes, and snippets.

@imadx
Last active October 2, 2017 06:42
Show Gist options
  • Save imadx/1a80ae2899d243373f2d0ff34f0e7a60 to your computer and use it in GitHub Desktop.
Save imadx/1a80ae2899d243373f2d0ff34f0e7a60 to your computer and use it in GitHub Desktop.
Design choice makes a huge impact on the outcome. We must choose wisely.
const events = require('events');
const eventEmitter = new events.EventEmitter();
const FREQUENCY = process.env.FREQUENCY_BELOVED || 1000;
const WORK_DURATION = process.env.VARIANCE_WORK_DURATION || 10000;
const PATIENCE = process.env.LIMIT_PATIENCE || 1000;
let alive = null;
let beloved = null;
class Beloved{
constructor() {
if(!beloved){
beloved = this;
this.rejections = 0;
this.unHappy = false;
}
return beloved;
}
getsLessTimeToBeLoved() {
console.log('[BELOVED] Gets less time to be loved..');
try{
if(this.unHappy == false){
this.rejections++;
console.log('[BELOVED] didn\'t get the attention she needs', this.rejections, 'time(s), But still happy..');
let _rejections = this.rejections;
let _unhappyStatus = this.unHappy;
let requireAttention = function requireAttention() {
console.log('[BELOVED] Trying to require attention');
try{
if(beloved.theOne){
beloved.theOne.getCareAndAttention();
}
} catch (err){
console.error('[BELOVED][ERROR]', err, this.rejections >= PATIENCE, _rejections , PATIENCE);
if(_rejections >= PATIENCE) {
console.log('[BELOVED] Limit of patience exceeded ');
_unhappyStatus = true;
}
}
};
console.log('[BELOVED] Needs attention...');
eventEmitter.on('neededAttention', requireAttention(this));
setTimeout(function(){
eventEmitter.emit('neededAttention');
}, FREQUENCY);
} else {
throw 'Failed to keep her happy';
}
} catch(err) {
console.error('[BELOVED] life failed');
throw 'Failed to keep her happy';
}
}
}
class ComUniLife{
constructor(beloved) {
if(!alive){
alive = this;
this.mistakes = 0;
beloved.theOne = this;
this.beloved = beloved;
console.log('[ME] Life instance initialized.');
} else {
console.log('[ME] Life instance already exists.');
}
return alive;
}
makeProgress() {
console.log('[ME] Started making progress in work life');
try{
this.doWhatYouLoveNloveWhatYouDo(Math.floor(Math.random()*WORK_DURATION));
} catch (err){
this.mistakes++;
console.error(err);
console.log('[ME] Made', this.mistakes, 'mistake(s)');
if(this.mistakes >= PATIENCE){
throw "[ME] Made more mistakes than she can bear"
}
}
}
doWhatYouLoveNloveWhatYouDo(duration) {
console.log('[ME] Doing what I Love..');
if(this.busy == true){
console.log('[ME] Even, I\'m busy to do what I Love..');
try{
this.beloved.getsLessTimeToBeLoved();
return;
} catch(err) {
console.error(err);
throw 'Busy with work; prioritized work over life.';
}
}
this.busy = true;
try{
this.beloved.getsLessTimeToBeLoved();
return;
} catch(err) {
console.error(err);
throw 'Busy with work; prioritized work over life.';
}
setTimeout(function(){
this.busy = false;
}, duration);
}
getCareAndAttention() {
console.log('[ME] Knows that she requires attention..');
// console.log('[ME] Pay attention to her when she needs it..')
// return;
if(this.busy == true){
console.error('[ME] Failed to provide care and attention');
throw 'Busy with work; prioritized work over life.';
} else {
console.log('[ME] Paid attention and cared for her..');
}
}
}
let myBeloved = new Beloved();
let myLife = new ComUniLife(myBeloved);
let happyLifeBeats = setInterval(function(){
try{
myLife.makeProgress();
} catch(err){
console.error('made mistakes while making progress..');
console.log('Try to start over..');
myLife = new ComUniLife(null);
console.log('New life initialized..');
if(myLife.mistakes && myLife.mistakes > 0) {
console.error('Mistakes you\'ve made', myLife.mistakes);
console.error('She resisted', myLife.mistakes, 'times, you didn\'t care');
console.log('Can\'t help. Sometimes you can\'t fix your own mistakes.');
clearInterval(happyLifeBeats)
};
}
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment