2016-02-21 12:02:18 -05:00
|
|
|
/*
|
|
|
|
|
AdaptableController.js
|
|
|
|
|
|
|
|
|
|
AdaptableController is the base class for all controllers
|
|
|
|
|
that support adapter,
|
|
|
|
|
The super class takes care of creating the right instance for the adapter
|
|
|
|
|
based on the parameters passed
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-22 14:12:51 -05:00
|
|
|
// _adapter is private, use Symbol
|
|
|
|
|
var _adapter = Symbol();
|
2016-02-25 19:04:27 -05:00
|
|
|
import cache from '../cache';
|
2016-02-22 14:12:51 -05:00
|
|
|
|
2016-02-21 12:02:18 -05:00
|
|
|
export class AdaptableController {
|
2016-02-22 14:12:51 -05:00
|
|
|
|
2016-02-25 19:04:27 -05:00
|
|
|
constructor(adapter, appId) {
|
2016-02-22 14:12:51 -05:00
|
|
|
this.adapter = adapter;
|
2016-02-25 19:04:27 -05:00
|
|
|
this.appId = appId;
|
2016-02-21 23:47:07 -05:00
|
|
|
}
|
2016-02-22 14:12:51 -05:00
|
|
|
|
|
|
|
|
set adapter(adapter) {
|
2016-02-21 23:47:07 -05:00
|
|
|
this.validateAdapter(adapter);
|
2016-02-22 14:12:51 -05:00
|
|
|
this[_adapter] = adapter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get adapter() {
|
|
|
|
|
return this[_adapter];
|
2016-02-21 12:02:18 -05:00
|
|
|
}
|
2016-02-21 16:54:30 -05:00
|
|
|
|
2016-02-25 19:04:27 -05:00
|
|
|
get config() {
|
|
|
|
|
return cache.apps[this.appId];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 23:47:07 -05:00
|
|
|
expectedAdapterType() {
|
|
|
|
|
throw new Error("Subclasses should implement expectedAdapterType()");
|
2016-02-21 16:54:30 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-21 23:47:07 -05:00
|
|
|
validateAdapter(adapter) {
|
|
|
|
|
if (!adapter) {
|
|
|
|
|
throw new Error(this.constructor.name+" requires an adapter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Type = this.expectedAdapterType();
|
|
|
|
|
// Allow skipping for testing
|
|
|
|
|
if (!Type) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Makes sure the prototype matches
|
|
|
|
|
let mismatches = Object.getOwnPropertyNames(Type.prototype).reduce( (obj, key) => {
|
|
|
|
|
const adapterType = typeof adapter[key];
|
|
|
|
|
const expectedType = typeof Type.prototype[key];
|
|
|
|
|
if (adapterType !== expectedType) {
|
|
|
|
|
obj[key] = {
|
|
|
|
|
expected: expectedType,
|
|
|
|
|
actual: adapterType
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
if (Object.keys(mismatches).length > 0) {
|
|
|
|
|
console.error(adapter, mismatches);
|
|
|
|
|
throw new Error("Adapter prototype don't match expected prototype");
|
|
|
|
|
}
|
2016-02-21 16:54:30 -05:00
|
|
|
}
|
2016-02-21 12:02:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AdaptableController;
|