Adds setDefaultAdapter

This commit is contained in:
Florent Vilmart
2016-02-21 16:54:30 -05:00
parent d504681589
commit 33fa5a7b2a
4 changed files with 29 additions and 11 deletions

View File

@@ -47,14 +47,16 @@ describe("AdaptableController", ()=>{
}); });
it("should instantiate the default adapter from Class", (done) => { it("should instantiate the default adapter from Class", (done) => {
var controller = new AdaptableController(null, FilesAdapter); AdaptableController.setDefaultAdapter(FilesAdapter);
var controller = new AdaptableController();
expect(controller.adapter instanceof FilesAdapter).toBe(true); expect(controller.adapter instanceof FilesAdapter).toBe(true);
done(); done();
}); });
it("should use the default adapter", (done) => { it("should use the default adapter", (done) => {
var adapter = new FilesAdapter(); var adapter = new FilesAdapter();
var controller = new AdaptableController(null, adapter); AdaptableController.setDefaultAdapter(adapter);
var controller = new AdaptableController();
expect(controller.adapter).toBe(adapter); expect(controller.adapter).toBe(adapter);
done(); done();
}); });

View File

@@ -76,12 +76,13 @@ describe('LoggerController', () => {
}); });
it('should throw without an adapter', (done) => { it('should throw without an adapter', (done) => {
LoggerController.setDefaultAdapter(undefined);
var loggerController = new LoggerController(); var loggerController = new LoggerController();
expect(() => { expect(() => {
loggerController.getLogs(); loggerController.getLogs();
}).toThrow(); }).toThrow();
LoggerController.setDefaultAdapter(FileLoggerAdapter);
done(); done();
}); });
}); });

View File

@@ -8,6 +8,8 @@ based on the parameters passed
*/ */
const DefaultAdapters = {};
export class AdaptableController { export class AdaptableController {
/** /**
* Check whether the api call has master key or not. * Check whether the api call has master key or not.
@@ -17,12 +19,11 @@ export class AdaptableController {
* Supported options types: * Supported options types:
* - string: the options will be loaded with required, when loaded, if default * - string: the options will be loaded with required, when loaded, if default
* is set on the returned object, we'll use that one to support modules * is set on the returned object, we'll use that one to support modules
* - object: a plain javascript object (options.constructor === Object), if options.adapter is set, we'll try to load it with the same mechanics * - object: a plain javascript object (options.constructor === Object), if options.adapter is set, we'll try to load it with the same mechanics.
* - function: we'll create a new instance from that function, and pass the options object * - function: we'll create a new instance from that function, and pass the options object
*/ */
constructor(options, defaultAdapter) { constructor(options) {
// Use the default by default
let adapter; let adapter;
// We have options and options have adapter key // We have options and options have adapter key
@@ -37,7 +38,7 @@ export class AdaptableController {
} }
if (!adapter) { if (!adapter) {
adapter = defaultAdapter; adapter = this.defaultAdapter();
} }
// This is a string, require the module // This is a string, require the module
@@ -58,6 +59,15 @@ export class AdaptableController {
this.adapter = adapter; this.adapter = adapter;
this.options = options; this.options = options;
} }
defaultAdapter() {
return DefaultAdapters[this.constructor.name];
}
// Sets the default adapter for that Class
static setDefaultAdapter(defaultAdapter) {
DefaultAdapters[this.name] = defaultAdapter;
}
} }
export default AdaptableController; export default AdaptableController;

View File

@@ -36,6 +36,11 @@ import { LogsRouter } from './Routers/LogsRouter';
import { FileLoggerAdapter } from './Adapters/Logger/FileLoggerAdapter'; import { FileLoggerAdapter } from './Adapters/Logger/FileLoggerAdapter';
import { LoggerController } from './Controllers/LoggerController'; import { LoggerController } from './Controllers/LoggerController';
FilesController.setDefaultAdapter(GridStoreAdapter);
PushController.setDefaultAdapter(ParsePushAdapter);
LoggerController.setDefaultAdapter(FileLoggerAdapter);
// Mutate the Parse object to add the Cloud Code handlers // Mutate the Parse object to add the Cloud Code handlers
addParseCloud(); addParseCloud();
@@ -107,9 +112,9 @@ function ParseServer({
// We pass the options and the base class for the adatper, // We pass the options and the base class for the adatper,
// Note that passing an instance would work too // Note that passing an instance would work too
const filesController = new FilesController(filesAdapter, GridStoreAdapter); const filesController = new FilesController(filesAdapter);
const pushController = new PushController(push, new ParsePushAdapter(push)); const pushController = new PushController(push);
const loggerController = new LoggerController(loggerAdapter, FileLoggerAdapter); const loggerController = new LoggerController(loggerAdapter);
cache.apps[appId] = { cache.apps[appId] = {
masterKey: masterKey, masterKey: masterKey,