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 2 commits
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
15 changes: 15 additions & 0 deletions src/dashboard/dashboard-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,19 @@ describe('Dashboard manager', () => {
expect(mockModelManager.destroy).toHaveBeenCalledWith(mockOriginalDataSource);
expect(mockDataSourceManager.setRootDataSource).toHaveBeenCalledWith(mockNewDataSource, dashboard.root);
});

test('can retrieve model instances', () => {
const dashboard = dashboardManager.create({
type: 'serialized-model'
});

const mockDataSource = class MockDataSource {
public readonly value: number = Math.random();
};

mockModelManager.getModelInstances = jest.fn().mockReturnValue([new mockDataSource()]);

expect(dashboard.getModelInstances(mockDataSource).length).toBe(1);
expect(mockModelManager.getModelInstances).toHaveBeenCalledWith(mockDataSource);
});
});
8 changes: 7 additions & 1 deletion src/dashboard/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Dashboard<TRoot extends object = object> {
setTimeRange(timeRange: TimeRange): this;

/**
* Returns a serialized from of this dashboard. Note this does not
* Returns a serialized form of this dashboard. Note this does not
* affect the dashboard in any way, it must be explicitly destroyed
* if it is no longer in use.
*/
Expand Down Expand Up @@ -56,4 +56,10 @@ export interface Dashboard<TRoot extends object = object> {
*/
// tslint:disable-next-line: no-any
getRootDataSource<T extends DataSource<any>>(): T | undefined;

/**
* Returns a shallow copy array of model instances within a dashboard that match the
* argument model class
*/
getModelInstances<T extends object>(modelClass: Constructable<T>): object[];
Copy link
Contributor

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.

Copy link
Contributor

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)

}
7 changes: 7 additions & 0 deletions src/dashboard/default-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ export class DefaultDashboard<TRoot extends object> implements Dashboard<TRoot>
public getRootDataSource<T extends DataSource<any>>(): T | undefined {
return this.dataSourceManager.getRootDataSource(this.root) as T | undefined;
}

/**
* @inheritdoc
*/
public getModelInstances<T extends object>(modelClass: Constructable<T>): object[] {
return this.modelManager.getModelInstances<T>(modelClass);
}
}
17 changes: 17 additions & 0 deletions src/model/manager/model-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ describe('Model manager', () => {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
const testClass1 = class TestClass1 {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
const testClass2 = class TestClass2 {
// Value used to disambiguate equality between instances
public readonly value: number = Math.random();
};
let manager: ModelManager;
let mockLogger: PartialObjectMock<Logger>;
let mockApiBuilder: PartialObjectMock<ModelApiBuilder<ModelApi>>;
Expand Down Expand Up @@ -59,6 +67,15 @@ describe('Model manager', () => {
manager.registerModelApiBuilder(mockApiBuilder as ModelApiBuilder<ModelApi>);
});

test('returns array of instances matching arg', () => {
manager.construct(testClass);
manager.construct(testClass);
manager.construct(testClass1);
expect(manager.getModelInstances(testClass).length).toBe(2);
expect(manager.getModelInstances(testClass1).length).toBe(1);
expect(manager.getModelInstances(testClass2).length).toBe(0);
});

test('allows constructing new models', () => {
const instance = manager.construct(testClass);
expect(instance.constructor).toBe(testClass);
Expand Down
9 changes: 8 additions & 1 deletion src/model/manager/model-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highlighting this change so others see it in the review.

Copy link
Contributor

Choose a reason for hiding this comment

The 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[] = [];

Expand All @@ -24,6 +24,13 @@ 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>): object[] {
return Array.from(this.modelInstanceMap.keys()).filter(modelInstance => modelInstance instanceof modelClass);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the behavior you're looking for. The model instance map in this singleton is all models, you're using this from the context of one dashboard. Instead, you'd want to pass in the root from that dashboard and then use get children to walk that model's tree.

}

/**
* Constructs (@see `ModelManager.construct`) then initializes (@see `ModelManager.initialize`) it
*
Expand Down