Skip to content

Commit dfed549

Browse files
Merge pull request #25 from Exabyte-io/fix/SOF-6197
chore: add functions for creating/extending/patching registryContainer
2 parents cd3f9ba + eb7003e commit dfed549

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/context/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import {
1010
} from "./mixins";
1111
import { JobContextPickKeysForMixin, WorkflowContextPickKeysForMixin } from "./pickers";
1212
import { ContextProvider } from "./provider";
13-
import { ContextProviderRegistryContainer } from "./registry";
13+
import {
14+
ContextProviderRegistryContainer,
15+
createAndPatchRegistry,
16+
extendAndPatchRegistry,
17+
} from "./registry";
1418

1519
export {
1620
ContextProvider,
1721
ContextProviderRegistryContainer,
22+
extendAndPatchRegistry,
23+
createAndPatchRegistry,
1824
JobContextPickKeysForMixin,
1925
JSONSchemaFormDataProvider,
2026
WorkflowContextPickKeysForMixin,

src/context/registry.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,42 @@ export class ContextProviderRegistryContainer {
3131
this.providers = this.providers.filter((p) => p.name === name);
3232
}
3333
}
34+
35+
/** Extends an existing context provider registry container and patches static class variables if applicable.
36+
*
37+
* @param {ContextProviderRegistryContainer} registryContainer
38+
* @param {Object} classConfigMap
39+
* @param {Object} classesToPatch
40+
* @example
41+
* const classConfigMap = {
42+
* PlanewaveCutoffDataManager: {
43+
* providerCls: PlanewaveCutoffsContextProvider,
44+
* config: _makeImportant({ name: "cutoffs", entityName: "subworkflow" })
45+
* },
46+
* };
47+
*
48+
*/
49+
export const extendAndPatchRegistry = (registryContainer, classConfigMap, classesToPatch = {}) => {
50+
Object.entries(classConfigMap).forEach(([name, { providerCls, config }]) => {
51+
Object.entries(classesToPatch).forEach(([clsName, cls]) => {
52+
if (providerCls[clsName]) {
53+
providerCls[clsName] = cls;
54+
}
55+
});
56+
registryContainer.addProvider({
57+
instance: providerCls.getConstructorConfig(config),
58+
name,
59+
});
60+
});
61+
return registryContainer;
62+
};
63+
64+
/** Creates a new context provider registry container and patches static class variables if applicable.
65+
*
66+
* @param {Object} classConfigMap
67+
* @param {{Material: SpecificMockMaterial}} classesToPatch
68+
*/
69+
export const createAndPatchRegistry = (classConfigMap, classesToPatch = {}) => {
70+
const registryContainer = new ContextProviderRegistryContainer();
71+
return extendAndPatchRegistry(registryContainer, classConfigMap, classesToPatch);
72+
};

tests/context.tests.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { expect } from "chai";
22
import { mix } from "mixwith";
33

4-
import { ApplicationContextMixin, MaterialContextMixin } from "../src/context/mixins";
5-
import { InMemoryEntity } from "../src/entity";
4+
import {
5+
ApplicationContextMixin,
6+
ContextProvider,
7+
createAndPatchRegistry,
8+
MaterialContextMixin,
9+
} from "../src/context";
610

711
class MockMaterial {
812
static createDefault() {
@@ -28,7 +32,7 @@ class SpecificMockApplication {
2832
}
2933
}
3034

31-
class ProviderEntity extends mix(InMemoryEntity).with(
35+
class ProviderEntity extends mix(ContextProvider).with(
3236
MaterialContextMixin,
3337
ApplicationContextMixin,
3438
) {
@@ -43,6 +47,10 @@ class DerivedProviderEntity extends ProviderEntity {
4347
static Application = SpecificMockApplication;
4448
}
4549

50+
class ApplicationContextProvider extends mix(ContextProvider).with(ApplicationContextMixin) {
51+
static Application = SpecificMockApplication;
52+
}
53+
4654
describe("Material & Application ContextMixin", () => {
4755
const config = {};
4856

@@ -58,3 +66,26 @@ describe("Material & Application ContextMixin", () => {
5866
expect(provider.application).to.be.equal("defaultSpecificMockApplication");
5967
});
6068
});
69+
70+
describe("ContextProviderRegistryContainer", () => {
71+
const classConfigObj = {
72+
DataManager: {
73+
providerCls: ProviderEntity,
74+
config: { name: "example1", domain: "important" },
75+
},
76+
ApplicationDataManager: {
77+
providerCls: ApplicationContextProvider,
78+
config: { name: "example2", domain: "important" },
79+
},
80+
};
81+
82+
it("can be created and patched", () => {
83+
const registry = createAndPatchRegistry(classConfigObj, { Material: SpecificMockMaterial });
84+
const _dataProvider = registry.findProviderInstanceByName("DataManager");
85+
const dataProvider = new _dataProvider.constructor(_dataProvider.config);
86+
const _appProvider = registry.findProviderInstanceByName("ApplicationDataManager");
87+
const appProvider = new _appProvider.constructor(_appProvider.config);
88+
expect(dataProvider.material).to.be.equal("defaultSpecificMockMaterial");
89+
expect(appProvider.application).to.be.equal("defaultSpecificMockApplication");
90+
});
91+
});

0 commit comments

Comments
 (0)