Skip to content

Commit

Permalink
- Added ability to create a new instance of the base class
Browse files Browse the repository at this point in the history
-  Registration to object pool will now use <any> keyword due to generics
- GlobalObjectPool can now instantiate objects using the object key
  • Loading branch information
DavidArayan committed Nov 22, 2023
1 parent 7faccfc commit a0e1c79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions sdk-core/src/core/core-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export abstract class CoreObject<Attributes extends CoreObjectAttributes> {
throw new Error('CoreObject.type is not implemented, contact admin');
}

public static newInstance<T extends CoreObject<CoreObjectAttributes>>(): T {
return <T>(new (<any>this)());
}

public get type(): string {
return (<any>this.constructor).type;
}
Expand Down
11 changes: 10 additions & 1 deletion sdk-core/src/core/global-object-pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CoreObject } from './core-object';
import { CoreObjectAttributes, type CoreObject } from './core-object';

/**
* This object provides runtime functionality to access object types for construction
Expand All @@ -24,4 +24,13 @@ export class GlobalObjectPool {

return obj ? obj : null;
}

/**
* Generates a new instance of the object provided a key, otherwise returns null
*/
public static newInstance<T extends CoreObject<CoreObjectAttributes>>(key: string): T | null {
const obj: typeof CoreObject | null = this.get(key);

return obj ? obj.newInstance<T>() : null;
}
}
2 changes: 1 addition & 1 deletion sdk-core/src/generator/generators/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Schema {
output += `\t\treturn new ${queryName}Dynamic(this, service);\n`;
output += '\t}\n';

output += `}\nGlobalObjectPool.register(${className});\n`;
output += `}\nGlobalObjectPool.register(<any>${className});\n`;

return {
name: schemaInstance.type.replaceAll('_', '-'),
Expand Down

0 comments on commit a0e1c79

Please sign in to comment.