Skip to content

Instantly share code, notes, and snippets.

@fluffybunnies
Created September 28, 2018 19:15
Show Gist options
  • Save fluffybunnies/7a879a10d69dd9f6a808b7ef6e54d4e9 to your computer and use it in GitHub Desktop.
Save fluffybunnies/7a879a10d69dd9f6a808b7ef6e54d4e9 to your computer and use it in GitHub Desktop.
Factory + Inheritance
'use strict';
class ImportDaoInterface {
/**
This methods contract is x, y, z
*/
getOrders(dateRange, cb) {
throw 'Extend Me!'
}
}
class GrubhubImportDao extends ImportDaoInterface {
getOrders(dateRange, cb) {
process.nextTick(function(){
cb(false, [])
})
}
}
function ImportDaoFactory(whichOne){
//try {
// var importDao = new require('../dao/'+whichOne+'Dao')
//} catch (e) {
// throw new Error('Not Implemented')
//}
// the above for production, but for this all-in-one test case:
var importDao = new GrubhubImportDao
// can do factory things here if want to, like:
var getOrdersWrapper = importDao.getOrders
importDao.getOrders = function(dateRange,cb){
getOrdersWrapper(dateRange,function(err,orders){
if (err) {
console.log('report error to sentry, etc')
}
cb(err,orders)
})
}
return importDao
}
// test...
ImportDaoFactory('GrubhubImport').getOrders('last 3 hours', function(err,orders){
console.log('orders=',orders)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment