Skip to content

Commit 939db5c

Browse files
authored
Merge pull request #299 from proto-kit/refactor/remove-module-definition
Refactor/remove module definition
2 parents c6945f1 + fcd045e commit 939db5c

File tree

72 files changed

+874
-1192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+874
-1192
lines changed

packages/api/src/graphql/GraphqlSequencerModule.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Configurable,
77
log,
88
ModuleContainer,
9-
ModulesConfig,
109
ModulesRecord,
1110
TypedClass,
1211
} from "@proto-kit/common";
@@ -22,20 +21,13 @@ export type GraphqlModulesRecord = ModulesRecord<
2221
TypedClass<GraphqlModule<unknown>>
2322
>;
2423

25-
export interface GraphqlModulesDefintion<
26-
GraphQLModules extends GraphqlModulesRecord,
27-
> {
28-
modules: GraphQLModules;
29-
config?: ModulesConfig<GraphQLModules>;
30-
}
31-
3224
@closeable()
3325
export class GraphqlSequencerModule<GraphQLModules extends GraphqlModulesRecord>
3426
extends ModuleContainer<GraphQLModules>
3527
implements Configurable<unknown>, SequencerModule<unknown>, Closeable
3628
{
3729
public static from<GraphQLModules extends GraphqlModulesRecord>(
38-
definition: GraphqlModulesDefintion<GraphQLModules>
30+
definition: GraphQLModules
3931
): TypedClass<GraphqlSequencerModule<GraphQLModules>> {
4032
return class ScopedGraphQlContainer extends GraphqlSequencerModule<GraphQLModules> {
4133
public constructor() {
@@ -58,8 +50,8 @@ export class GraphqlSequencerModule<GraphQLModules extends GraphqlModulesRecord>
5850
this.graphqlServer.setContainer(this.container);
5951

6052
// eslint-disable-next-line guard-for-in
61-
for (const moduleName in this.definition.modules) {
62-
const moduleClass = this.definition.modules[moduleName];
53+
for (const moduleName in this.definition) {
54+
const moduleClass = this.definition[moduleName];
6355

6456
if (
6557
Object.prototype.isPrototypeOf.call(

packages/api/src/graphql/modules/QueryGraphqlModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class QueryGraphqlModule<
316316
): ObjMap<GraphQLFieldConfig<unknown, unknown>> {
317317
const types: ObjMap<GraphQLFieldConfig<unknown, unknown>> = {};
318318

319-
for (const key in container.definition.modules) {
319+
for (const key in container.definition) {
320320
const query = containerQuery[key];
321321

322322
const moduleTypes: ObjMap<GraphQLFieldConfig<unknown, unknown>> = {};

packages/common/src/config/ModuleContainer.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,6 @@ export type RecursivePartial<T> = {
109109
[Key in keyof T]?: Partial<T[Key]>;
110110
};
111111

112-
/**
113-
* Parameters required when creating a module container instance
114-
*/
115-
export interface ModuleContainerDefinition<Modules extends ModulesRecord> {
116-
modules: Modules;
117-
// config is optional, as it may be provided by the parent/wrapper class
118-
/**
119-
* @deprecated
120-
*/
121-
config?: ModulesConfig<Modules>;
122-
}
123-
124112
// Removes all keys with a "never" value from an object
125113
export type FilterNeverValues<Type extends Record<string, unknown>> = {
126114
[Key in keyof Type as Type[Key] extends never ? never : Key]: Type[Key];
@@ -166,15 +154,15 @@ export class ModuleContainer<Modules extends ModulesRecord>
166154

167155
private eventEmitterProxy: EventEmitterProxy<Modules> | undefined = undefined;
168156

169-
public constructor(public definition: ModuleContainerDefinition<Modules>) {
157+
public constructor(public definition: Modules) {
170158
super();
171159
}
172160

173161
/**
174162
* @returns list of module names
175163
*/
176164
public get moduleNames() {
177-
return Object.keys(this.definition.modules);
165+
return Object.keys(this.definition);
178166
}
179167

180168
/**
@@ -219,7 +207,7 @@ export class ModuleContainer<Modules extends ModulesRecord>
219207
public assertIsValidModuleName(
220208
moduleName: string
221209
): asserts moduleName is StringKeyOf<Modules> {
222-
if (!this.isValidModuleName(this.definition.modules, moduleName)) {
210+
if (!this.isValidModuleName(this.definition, moduleName)) {
223211
throw errors.onlyValidModuleNames(moduleName);
224212
}
225213
}
@@ -349,13 +337,20 @@ export class ModuleContainer<Modules extends ModulesRecord>
349337

350338
public resolveOrFail<ModuleType>(
351339
moduleName: string,
352-
moduleType: TypedClass<ModuleType>
340+
moduleType?: TypedClass<ModuleType>
353341
) {
342+
if (!this.container.isRegistered(moduleName)) {
343+
throw new Error(`Dependency with token ${moduleName} not registered`);
344+
}
345+
354346
const instance = this.container.resolve<ModuleType>(moduleName);
355-
const isValidModuleInstance = instance instanceof moduleType;
356347

357-
if (!isValidModuleInstance) {
358-
throw errors.validModuleInstance(moduleName, moduleType.name);
348+
if (moduleType !== undefined) {
349+
const isValidModuleInstance = instance instanceof moduleType;
350+
351+
if (!isValidModuleInstance) {
352+
throw errors.validModuleInstance(moduleName, moduleType.name);
353+
}
359354
}
360355

361356
return instance;
@@ -501,7 +496,7 @@ export class ModuleContainer<Modules extends ModulesRecord>
501496
});
502497

503498
// register all provided modules when the container is created
504-
this.registerModules(this.definition.modules);
499+
this.registerModules(this.definition);
505500
this.container.register("ParentContainer", { useValue: this });
506501
}
507502

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Startable {
2+
start(): Promise<void>;
3+
}

packages/common/src/events/EventEmitterProxy.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ export class EventEmitterProxy<
4545
public constructor(private readonly container: ModuleContainer<Modules>) {
4646
super();
4747
container.moduleNames.forEach((moduleName) => {
48-
if (
49-
container.isValidModuleName(container.definition.modules, moduleName)
50-
) {
48+
if (container.isValidModuleName(container.definition, moduleName)) {
5149
const module = container.resolve(moduleName);
5250
if (this.isEventEmitter(module)) {
5351
module.events.onAll((events: any, args: any[]) => {

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./config/ConfigurableModule";
33
export * from "./config/ChildContainerProvider";
44
export * from "./config/ChildContainerCreatable";
55
export * from "./config/injectAlias";
6+
export * from "./config/Startable";
67
export * from "./types";
78
export * from "./zkProgrammable/ZkProgrammable";
89
export * from "./zkProgrammable/ProvableMethodExecutionContext";

packages/common/test/config/ContainerEvents.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ describe("test event propagation", () => {
4242
expect.assertions(1);
4343

4444
const container = new TestContainer({
45-
modules: {
46-
test: TestModule,
47-
test2: TestModule2,
48-
},
45+
test: TestModule,
46+
test2: TestModule2,
4947
});
5048

5149
container.configure({

packages/common/test/config/ModuleContainer.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ describe("moduleContainer", () => {
8484

8585
beforeEach(() => {
8686
container = new TestModuleContainer({
87-
modules: {
88-
TestModule,
89-
OtherTestModule,
90-
// this module would not be assignable to TestModuleContainer
91-
// WrongTestModule,
92-
},
87+
TestModule,
88+
OtherTestModule,
89+
// this module would not be assignable to TestModuleContainer
90+
// WrongTestModule,
9391
});
9492
});
9593

packages/deployment/src/environment/Environment.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { log, assertValidTextLogLevel } from "@proto-kit/common";
2-
import { AppChain } from "@proto-kit/sdk";
1+
import { log, assertValidTextLogLevel, Startable } from "@proto-kit/common";
2+
import { AppChain } from "@proto-kit/sequencer";
33
import yargs from "yargs";
44
import { hideBin } from "yargs/helpers";
55

6-
export interface Startable {
7-
start(): Promise<void>;
8-
}
9-
106
export type StartableEnvironment<T> = Record<string, T>;
117

128
export class Environment<T extends Startable> {
@@ -75,7 +71,7 @@ export class Environment<T extends Startable> {
7571

7672
// TODO Temporary workaround
7773
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
78-
(appChain as unknown as AppChain<any, any, any, any>).configurePartial({
74+
(appChain as unknown as AppChain<any>).configurePartial({
7975
Sequencer: {
8076
DatabasePruneModule: {
8177
pruneOnStartup: prune,

packages/indexer/src/Indexer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
ChildContainerProvider,
33
ModuleContainer,
4-
ModuleContainerDefinition,
54
ModulesRecord,
65
TypedClass,
76
} from "@proto-kit/common";
@@ -18,7 +17,7 @@ export class Indexer<
1817
Modules extends IndexerModulesRecord,
1918
> extends ModuleContainer<Modules> {
2019
public static from<Modules extends IndexerModulesRecord>(
21-
definition: ModuleContainerDefinition<Modules>
20+
definition: Modules
2221
): Indexer<Modules> {
2322
return new Indexer(definition);
2423
}

0 commit comments

Comments
 (0)