You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.5 KiB
57 lines
1.5 KiB
3 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|