mirror of
https://github.com/nasa/openmct.git
synced 2024-12-19 05:07:52 +00:00
parent
b637307de6
commit
f3d593bc1e
@ -47,6 +47,7 @@ define([
|
||||
this.providers = {};
|
||||
this.rootRegistry = new RootRegistry();
|
||||
this.rootProvider = new RootObjectProvider.default(this.rootRegistry);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,6 +155,11 @@ define([
|
||||
* has been saved, or be rejected if it cannot be saved
|
||||
*/
|
||||
ObjectAPI.prototype.get = function (identifier) {
|
||||
let keystring = this.makeKeyString(identifier);
|
||||
if (this.cache[keystring] !== undefined) {
|
||||
return this.cache[keystring];
|
||||
}
|
||||
|
||||
identifier = utils.parseKeyString(identifier);
|
||||
const provider = this.getProvider(identifier);
|
||||
|
||||
@ -165,7 +171,15 @@ define([
|
||||
throw new Error('Provider does not support get!');
|
||||
}
|
||||
|
||||
return provider.get(identifier);
|
||||
let objectPromise = provider.get(identifier);
|
||||
|
||||
this.cache[keystring] = objectPromise;
|
||||
|
||||
return objectPromise.then(result => {
|
||||
delete this.cache[keystring];
|
||||
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
ObjectAPI.prototype.delete = function () {
|
||||
|
@ -30,9 +30,11 @@ describe("The Object API", () => {
|
||||
beforeEach(() => {
|
||||
mockProvider = jasmine.createSpyObj("mock provider", [
|
||||
"create",
|
||||
"update"
|
||||
"update",
|
||||
"get"
|
||||
]);
|
||||
mockProvider.create.and.returnValue(Promise.resolve(true));
|
||||
mockProvider.get.and.returnValue(Promise.resolve(mockDomainObject));
|
||||
objectAPI.addProvider(TEST_NAMESPACE, mockProvider);
|
||||
});
|
||||
it("Calls 'create' on provider if object is new", () => {
|
||||
@ -57,6 +59,14 @@ describe("The Object API", () => {
|
||||
expect(mockProvider.create).not.toHaveBeenCalled();
|
||||
expect(mockProvider.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("Caches multiple requests for the same object", () => {
|
||||
expect(mockProvider.get.calls.count()).toBe(0);
|
||||
objectAPI.get(mockDomainObject.identifier);
|
||||
expect(mockProvider.get.calls.count()).toBe(1);
|
||||
objectAPI.get(mockDomainObject.identifier);
|
||||
expect(mockProvider.get.calls.count()).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user