Initial import
This commit is contained in:
56
src/lifecycle/AppClass.ts
Normal file
56
src/lifecycle/AppClass.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {Application} from './Application';
|
||||
import {Container} from "@extollo/di";
|
||||
|
||||
/**
|
||||
* Base type for a class that supports binding methods by string.
|
||||
*/
|
||||
export interface Bindable {
|
||||
getBoundMethod(methodName: string): (...args: any[]) => any
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given object is bindable.
|
||||
* @param what
|
||||
* @return boolean
|
||||
*/
|
||||
export function isBindable(what: any): what is Bindable {
|
||||
return (
|
||||
what
|
||||
&& typeof what.getBoundMethod === 'function'
|
||||
&& what.getBoundMethod.length === 1
|
||||
&& typeof what.getBoundMethod('getBoundMethod') === 'function'
|
||||
)
|
||||
}
|
||||
|
||||
export class AppClass {
|
||||
private readonly appClassApplication!: Application;
|
||||
|
||||
constructor() {
|
||||
this.appClassApplication = Application.getApplication();
|
||||
}
|
||||
|
||||
protected app(): Application {
|
||||
return this.appClassApplication;
|
||||
}
|
||||
|
||||
protected container(): Container {
|
||||
return this.appClassApplication;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the method with the given name from this class, bound to this class.
|
||||
* @param {string} methodName
|
||||
* @return function
|
||||
*/
|
||||
public getBoundMethod(methodName: string): (...args: any[]) => any {
|
||||
// @ts-ignore
|
||||
if ( typeof this[methodName] !== 'function' ) {
|
||||
throw new TypeError(`Attempt to get bound method for non-function type: ${methodName}`)
|
||||
}
|
||||
|
||||
return (...args: any[]): any => {
|
||||
// @ts-ignore
|
||||
return this[methodName](...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user