-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add way to get all instances of a type of model from dashboard #649
base: main
Are you sure you want to change the base?
Changes from 3 commits
c96c32a
f9d1599
dbf3cb5
da87244
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import { ModelOnDestroy, ModelOnInit } from './model-lifecycle-hooks'; | |
* models. | ||
*/ | ||
export class ModelManager { | ||
private readonly modelInstanceMap: WeakMap<object, ModelInstanceData> = new WeakMap(); | ||
private readonly modelInstanceMap: Map<object, ModelInstanceData> = new Map(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Highlighting this change so others see it in the review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching this to a strongly referenced map means we'd need to be more careful on memory leaks and manually manage this. |
||
private readonly apiBuilders: ModelApiBuilder<ModelApi>[] = []; | ||
private readonly decorators: ModelDecorator[] = []; | ||
|
||
|
@@ -24,6 +24,15 @@ export class ModelManager { | |
private readonly beforeModelDestroyedEvent: BeforeModelDestroyedEvent | ||
) {} | ||
|
||
/** | ||
* Returns a shallow copy array of model instances that match the argument model class | ||
*/ | ||
public getModelInstances<T extends object>(modelClass: Constructable<T>, root?: object): object[] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'd never want to call this without root, and once you have root, you don't need to enumerate keys so you can revert the instancemap back to a weakmap. |
||
return Array.from(this.modelInstanceMap.keys()) | ||
.filter(modelInstance => root === undefined || this.getRoot(modelInstance) === root) | ||
.filter(modelInstance => modelInstance instanceof modelClass); | ||
} | ||
|
||
/** | ||
* Constructs (@see `ModelManager.construct`) then initializes (@see `ModelManager.initialize`) it | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought your use case was to instantiate a model without a renderer? If that's the case, no changes are needed in this repo, just in hyperdash-angular to export certain APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(i,e. model manager's programmatic instantiation APIs)