Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dashboard/dashboard-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ describe('Dashboard manager', () => {
mockModelManager.getModelInstances = jest.fn().mockReturnValue([new mockDataSource()]);

expect(dashboard.getModelInstances(mockDataSource).length).toBe(1);
expect(mockModelManager.getModelInstances).toHaveBeenCalledWith(mockDataSource);
expect(mockModelManager.getModelInstances).toHaveBeenCalledWith(mockDataSource, {});
});
});
2 changes: 1 addition & 1 deletion src/dashboard/default-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ export class DefaultDashboard<TRoot extends object> implements Dashboard<TRoot>
* @inheritdoc
*/
public getModelInstances<T extends object>(modelClass: Constructable<T>): object[] {
return this.modelManager.getModelInstances<T>(modelClass);
return this.modelManager.getModelInstances<T>(modelClass, this.root);
}
}
1 change: 1 addition & 0 deletions src/model/manager/model-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Model manager', () => {
expect(manager.getModelInstances(testClass).length).toBe(2);
expect(manager.getModelInstances(testClass1).length).toBe(1);
expect(manager.getModelInstances(testClass2).length).toBe(0);
expect(manager.getModelInstances(testClass, {}).length).toBe(0);
});

test('allows constructing new models', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/model/manager/model-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export class ModelManager {
/**
* Returns a shallow copy array of model instances that match the argument model class
*/
public getModelInstances<T extends object>(modelClass: Constructable<T>): object[] {
return Array.from(this.modelInstanceMap.keys()).filter(modelInstance => modelInstance instanceof modelClass);
public getModelInstances<T extends object>(modelClass: Constructable<T>, root?: object): object[] {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}

/**
Expand Down