Removes shared code in PushAdapter, replaces AdapterLoader.load by loadAdapter

This commit is contained in:
Florent Vilmart
2016-02-22 18:31:10 -05:00
parent 8ce8e2bbe0
commit 48dcfe37e7
9 changed files with 97 additions and 84 deletions

View File

@@ -1,39 +1,35 @@
export class AdapterLoader {
static load(options, defaultAdapter) {
let adapter;
export function loadAdapter(options, defaultAdapter) {
let adapter;
// We have options and options have adapter key
if (options) {
// Pass an adapter as a module name, a function or an instance
if (typeof options == "string" || typeof options == "function" || options.constructor != Object) {
adapter = options;
}
if (options.adapter) {
adapter = options.adapter;
}
// We have options and options have adapter key
if (options) {
// Pass an adapter as a module name, a function or an instance
if (typeof options == "string" || typeof options == "function" || options.constructor != Object) {
adapter = options;
}
if (!adapter) {
adapter = defaultAdapter;
if (options.adapter) {
adapter = options.adapter;
}
// This is a string, require the module
if (typeof adapter === "string") {
adapter = require(adapter);
// If it's define as a module, get the default
if (adapter.default) {
adapter = adapter.default;
}
}
// From there it's either a function or an object
// if it's an function, instanciate and pass the options
if (typeof adapter === "function") {
var Adapter = adapter;
adapter = new Adapter(options);
}
return adapter;
}
}
if (!adapter) {
adapter = defaultAdapter;
}
export default AdapterLoader;
// This is a string, require the module
if (typeof adapter === "string") {
adapter = require(adapter);
// If it's define as a module, get the default
if (adapter.default) {
adapter = adapter.default;
}
}
// From there it's either a function or an object
// if it's an function, instanciate and pass the options
if (typeof adapter === "function") {
var Adapter = adapter;
adapter = new Adapter(options);
}
return adapter;
}