Skip to content

Commit

Permalink
feat: use providedIn: 'root' for global providers
Browse files Browse the repository at this point in the history
  • Loading branch information
avchugaev committed Jan 14, 2021
1 parent 299a94c commit 46587bb
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 73 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"build:prod": "ng build --prod",
"test": "ng test",
"test:coverage": "ng test --coverage",
"test:coverage:watch": "ng test --coverage --watch",
"test:watch": "ng test --watch",
"lint": "ng lint"
},
Expand All @@ -48,6 +49,7 @@
"@angular/core": "~11.0.7",
"@angular/platform-browser": "~11.0.7",
"@angular/platform-browser-dynamic": "~11.0.7",
"@ngry/rx": "^11.4.0",
"@types/jest": "^26.0.20",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
Expand All @@ -65,6 +67,7 @@
"@angular/core": "~11.0.7",
"@angular/platform-browser": "~11.0.7",
"@angular/platform-browser-dynamic": "~11.0.7",
"@ngry/rx": "^11.4.0",
"rxjs": "~6.6.3",
"zone.js": "~0.11.3"
},
Expand Down
3 changes: 0 additions & 3 deletions src/lib/command/command-bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('CommandBus', () => {
it('should execute the command using corresponding command handler', async () => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
CqrsModule.forFeature({
commands: [
ExampleCommandHandler,
Expand Down Expand Up @@ -69,7 +68,6 @@ describe('CommandBus', () => {
it('should throw an error when the command has no handler', async () => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
CqrsModule.forFeature({}),
],
}).compileComponents();
Expand All @@ -82,7 +80,6 @@ describe('CommandBus', () => {
it('should throw an error when the command has multiple handlers', async () => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
CqrsModule.forFeature({
commands: [
ExampleCommandHandler,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/command/command-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
import { ICommand } from './command.interface';
import { CommandHandlerRegistry } from './command-handler-registry';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class CommandBus {

constructor(
Expand Down
4 changes: 3 additions & 1 deletion src/lib/command/command-handler-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
import { CommandHandler } from './command-handler';
import { ICommand } from './command.interface';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class CommandHandlerRegistry {
private readonly handlers = new Set<CommandHandler>();

Expand Down
57 changes: 18 additions & 39 deletions src/lib/cqrs.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Inject, InjectionToken, Injector, ModuleWithProviders, NgModule, Type } from '@angular/core';
import { CommandBus } from './command/command-bus';
import { CommandHandler } from './command/command-handler';
import { CommandHandlerRegistry } from './command/command-handler-registry';
import { EventBus } from './event/event-bus';
import { QueryBus } from './query/query-bus';
import { QueryHandler } from './query/query-handler';
import { QueryHandlerRegistry } from './query/query-handler-registry';
import { Saga } from './saga/saga';
Expand All @@ -18,7 +15,24 @@ export interface CqrsFeatureOptions {
export const CQRS_FEATURE_OPTIONS = new InjectionToken<CqrsFeatureOptions>('CQRS feature');

@NgModule()
export class CqrsFeatureModule {
export class CqrsModule {
static forFeature(options: CqrsFeatureOptions): ModuleWithProviders<CqrsModule> {
const {commands = [], queries = [], sagas = []} = options;

return {
ngModule: CqrsModule,
providers: [
...commands,
...queries,
...sagas,
{
provide: CQRS_FEATURE_OPTIONS,
useValue: options,
},
],
};
}

constructor(
@Inject(CQRS_FEATURE_OPTIONS) options: CqrsFeatureOptions,
injector: Injector,
Expand All @@ -41,38 +55,3 @@ export class CqrsFeatureModule {
}
}
}

@NgModule()
export class CqrsModule {

static forRoot(): ModuleWithProviders<CqrsModule> {
return {
ngModule: CqrsModule,
providers: [
EventBus,
CommandBus,
CommandHandlerRegistry,
QueryBus,
QueryHandlerRegistry,
SagaHandlerRegistry,
],
};
}

static forFeature(options: CqrsFeatureOptions): ModuleWithProviders<CqrsFeatureModule> {
const {commands = [], queries = [], sagas = []} = options;

return {
ngModule: CqrsFeatureModule,
providers: [
...commands,
...queries,
...sagas,
{
provide: CQRS_FEATURE_OPTIONS,
useValue: options,
},
],
};
}
}
7 changes: 1 addition & 6 deletions src/lib/event/event-bus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TestBed } from '@angular/core/testing';
import { CqrsModule } from '../cqrs.module';
import { EventBus } from './event-bus';
import { IEvent } from './event.interface';

Expand All @@ -13,11 +12,7 @@ class ExampleEvent {
describe('EventBus', () => {
describe('publish', () => {
it('should emit the event through the events subject', async () => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
],
}).compileComponents();
await TestBed.configureTestingModule({}).compileComponents();

const eventBus = TestBed.inject(EventBus);
const events: Array<IEvent> = [];
Expand Down
4 changes: 3 additions & 1 deletion src/lib/event/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Observable, Subject } from 'rxjs';
import { Injectable } from '@angular/core';
import { IEvent } from './event.interface';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class EventBus {
private readonly _events = new Subject<IEvent>();

Expand Down
13 changes: 0 additions & 13 deletions src/lib/operator/of-type.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/query/query-bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('QueryBus', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
CqrsModule.forFeature({
queries: [
ExampleQueryHandler,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/query/query-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
import { IQuery } from './query.interface';
import { QueryHandlerRegistry } from './query-handler-registry';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class QueryBus {

constructor(
Expand Down
4 changes: 3 additions & 1 deletion src/lib/query/query-handler-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
import { QueryHandler } from './query-handler';
import { IQuery } from './query.interface';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class QueryHandlerRegistry {
private readonly handlers = new Set<QueryHandler>();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/query/query.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// tslint:disable-next-line:no-empty-interface
export interface IQuery<TResult = any> {
export interface IQuery<TResult = unknown> {
}
4 changes: 3 additions & 1 deletion src/lib/saga/saga-handler-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { CommandBus } from '../command/command-bus';
import { EventBus } from '../event/event-bus';
import { Saga } from './saga';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class SagaHandlerRegistry {
constructor(
private readonly eventBus: EventBus,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/saga/saga.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { merge, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { ofType } from '@ngry/rx';
import { ICommand } from '../command/command.interface';
import { CommandHandler } from '../command/command-handler';
import { IEvent } from '../event/event.interface';
import { EventBus } from '../event/event-bus';
import { ofType } from '../operator/of-type';
import { CqrsModule } from '../cqrs.module';
import { Saga } from './saga';

Expand Down Expand Up @@ -173,7 +173,6 @@ describe('Saga', () => {
it('should execute saga pipeline', async (done) => {
await TestBed.configureTestingModule({
imports: [
CqrsModule.forRoot(),
CqrsModule.forFeature({
commands: [
FirstCommandHandler,
Expand Down
2 changes: 0 additions & 2 deletions src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export * from './lib/query/query.interface';
export * from './lib/query/query-bus';
export * from './lib/query/query-handler';

export * from './lib/operator/of-type';

export * from './lib/saga/saga';

export * from './lib/cqrs.module';

0 comments on commit 46587bb

Please sign in to comment.