Changeset View
Changeset View
Standalone View
Standalone View
lib/app/chip-manager.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const Errors = locreq("lib/response/error.js"); | const Errors = locreq("lib/response/error.js"); | ||||
const ChipManager = function(app) { | const ChipManager = function(app) { | ||||
this.app = app; | this.app = app; | ||||
this.chips = {}; | this.chips = {}; | ||||
}; | }; | ||||
ChipManager.chip_type_start_order = [ | ChipManager.chip_type_start_order = [ | ||||
"access_strategy_type", | "access_strategy_type", | ||||
"field_type", | "field_type", | ||||
"collection", | "collection", | ||||
"channel", | "channel", | ||||
]; | ]; | ||||
ChipManager.pure = { | ChipManager.pure = { | ||||
start_chips: function(app, chips) { | start_chips(app, chips) { | ||||
const promises = []; | const promises = []; | ||||
const datastore = ChipManager.pure.get_datastore_chip(app, chips); | const datastore = ChipManager.pure.get_datastore_chip(app, chips); | ||||
app.Logger.info(`\u2713 Started ${datastore.name}`); | app.Logger.info(`\u2713 Started ${datastore.name}`); | ||||
let promise; | let promise; | ||||
for (const i in ChipManager.chip_type_start_order) { | for (const i in ChipManager.chip_type_start_order) { | ||||
const type = ChipManager.chip_type_start_order[i]; | const type = ChipManager.chip_type_start_order[i]; | ||||
for (const name in chips[type]) { | for (const name in chips[type]) { | ||||
const chip = chips[type][name]; | const chip = chips[type][name]; | ||||
try { | try { | ||||
if (chip.start) { | if (chip.start) { | ||||
promise = chip.start(); | promise = chip.start(); | ||||
promises.push(promise); | promises.push(promise); | ||||
} | } | ||||
} catch (error) { | } catch (error) { | ||||
app.Logger.error(`\t " + "couldn't start "${name}"`); | app.Logger.error(`\t " + "couldn't start "${name}"`); | ||||
return Promise.reject(error); | return Promise.reject(error); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return Promise.all(promises); | return Promise.all(promises); | ||||
}, | }, | ||||
add_chip: function(chips, type, name, chip) { | add_chip(chips, type, name, chip) { | ||||
if (chips[type] === undefined) { | if (chips[type] === undefined) { | ||||
chips[type] = []; | chips[type] = []; | ||||
} | } | ||||
if (chips[type][name]) | if (chips[type][name]) | ||||
throw Error(`Chip '${type}.${name}' already exists!`); | throw Error(`Chip '${type}.${name}' already exists!`); | ||||
chips[type][name] = chip; | chips[type][name] = chip; | ||||
}, | }, | ||||
get_all_collections: function(chips) { | get_all_collections(chips) { | ||||
const names = []; | const names = []; | ||||
for (const collection in chips.collection) { | for (const collection in chips.collection) { | ||||
names.push(collection); | names.push(collection); | ||||
} | } | ||||
return names; | return names; | ||||
}, | }, | ||||
get_chip: function(chips, type, name) { | get_chip(chips, type, name) { | ||||
try { | try { | ||||
const ret = chips[type][name]; | const ret = chips[type][name]; | ||||
if (ret === undefined) { | if (ret === undefined) { | ||||
throw new Error( | throw new Error( | ||||
`Chip of type ${type} and name ${name} has not yet been registered` | `Chip of type ${type} and name ${name} has not yet been registered` | ||||
); | ); | ||||
} | } | ||||
return ret; | return ret; | ||||
} catch (e) { | } catch (e) { | ||||
throw new Errors.ValidationError( | throw new Errors.ValidationError( | ||||
`ChipManager was asked to return a chip of type "${type}" and name "${name}", but it was not found`, | `ChipManager was asked to return a chip of type "${type}" and name "${name}", but it was not found`, | ||||
{}, | {}, | ||||
{ short_message: "chip_not_found" } | { short_message: "chip_not_found" } | ||||
); | ); | ||||
} | } | ||||
}, | }, | ||||
get_chip_amount_by_type: function(chips, type) { | get_chip_amount_by_type(chips, type) { | ||||
if (chips[type]) { | if (chips[type]) { | ||||
return Object.keys(chips[type]).length; | return Object.keys(chips[type]).length; | ||||
} else { | |||||
return 0; | |||||
} | } | ||||
return 0; | |||||
}, | }, | ||||
get_datastore_chip: function(app, chips) { | get_datastore_chip(app, chips) { | ||||
return app.Datastore; | return app.Datastore; | ||||
}, | }, | ||||
get_chips_by_type: function(chips, chip_type) { | get_chips_by_type(chips, chip_type) { | ||||
return chips[chip_type]; | return chips[chip_type]; | ||||
}, | }, | ||||
}; | }; | ||||
ChipManager.prototype = { | ChipManager.prototype = { | ||||
start_chips() { | start_chips() { | ||||
return ChipManager.pure.start_chips(this.app, this.chips); | return ChipManager.pure.start_chips(this.app, this.chips); | ||||
}, | }, | ||||
Show All 21 Lines |