-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Split external event processing into separate processors (#86)
* chore: split external event processing into seperate processors * chore: remove circular dependency * chore: add missing file * chore: rename files and folder * chore: refactor switch case * chore: remove export of ExternalEventsProcessor * chore: move map to ExternalEventProcessorRegistry * chore: add missing file * chore: add ExternalEventProcessorRegistry to foder index * chore: unify constructors * chore: move change item seperation * chore: move to getter * chore: remove unused code and rename address to ownAddress * chore: fix generic constructor --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
36daefa
commit dfc630d
Showing
12 changed files
with
172 additions
and
136 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 0 additions & 112 deletions
112
packages/transport/src/modules/sync/ExternalEventsProcessor.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/transport/src/modules/sync/externalEventProcessors/ExternalEventProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { EventBus } from "@js-soft/ts-utils"; | ||
import { AccountController } from "../../accounts/AccountController"; | ||
import { Message } from "../../messages/local/Message"; | ||
import { Relationship } from "../../relationships/local/Relationship"; | ||
import { BackboneExternalEvent } from "../backbone/BackboneExternalEvent"; | ||
|
||
export type ExternalEventProcessorConstructor = new (eventBus: EventBus, accountController: AccountController) => ExternalEventProcessor; | ||
|
||
export abstract class ExternalEventProcessor { | ||
public constructor( | ||
protected readonly eventBus: EventBus, | ||
protected readonly accountController: AccountController | ||
) {} | ||
public abstract execute(externalEvent: BackboneExternalEvent): Promise<Message | Relationship | undefined>; | ||
protected get ownAddress(): string { | ||
return this.accountController.identity.address.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ages/transport/src/modules/sync/externalEventProcessors/ExternalEventProcessorRegistry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { TransportError } from "../../../core"; | ||
import { ExternalEventProcessorConstructor } from "./ExternalEventProcessor"; | ||
import { MessageDeliveredExternalEventProcessor } from "./MessageDeliveredExternalEventProcessor"; | ||
import { MessageReceivedExternalEventProcessor } from "./MessageReceivedExternalEventProcessor"; | ||
import { RelationshipChangeCompletedExternalEventProcessor } from "./RelationshipChangeCompletedExternalEventProcessor"; | ||
import { RelationshipChangeCreatedExternalEventProcessor } from "./RelationshipChangeCreatedExternalEventProcessor"; | ||
|
||
export class ExternalEventProcessorRegistry { | ||
private readonly processors = new Map<string, ExternalEventProcessorConstructor>(); | ||
public constructor() { | ||
this.registerProcessor("MessageReceived", MessageReceivedExternalEventProcessor); | ||
this.registerProcessor("MessageDelivered", MessageDeliveredExternalEventProcessor); | ||
this.registerProcessor("RelationshipChangeCreated", RelationshipChangeCreatedExternalEventProcessor); | ||
this.registerProcessor("RelationshipChangeCompleted", RelationshipChangeCompletedExternalEventProcessor); | ||
} | ||
|
||
public registerProcessor(externalEventName: string, externalEventProcessor: ExternalEventProcessorConstructor): void { | ||
if (this.processors.has(externalEventName)) { | ||
throw new TransportError(`There is already a externalEventProcessor registered for '${externalEventName}'. Use 'replaceProcessorForType' if you want to replace it.`); | ||
} | ||
this.processors.set(externalEventName, externalEventProcessor); | ||
} | ||
|
||
public registerOrReplaceProcessor(externalEventName: string, externalEventProcessor: ExternalEventProcessorConstructor): void { | ||
this.processors.set(externalEventName, externalEventProcessor); | ||
} | ||
|
||
public getProcessorForItem(externalEventName: string): ExternalEventProcessorConstructor { | ||
const externalEventProcessor = this.processors.get(externalEventName); | ||
if (!externalEventProcessor) { | ||
throw new TransportError(`There was no processor registered for '${externalEventName}'.`); | ||
} | ||
return externalEventProcessor; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...nsport/src/modules/sync/externalEventProcessors/MessageDeliveredExternalEventProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MessageDeliveredEvent } from "../../../events"; | ||
import { Message } from "../../messages/local/Message"; | ||
import { BackboneExternalEvent } from "../backbone/BackboneExternalEvent"; | ||
import { ExternalEventProcessor } from "./ExternalEventProcessor"; | ||
|
||
export class MessageDeliveredExternalEventProcessor extends ExternalEventProcessor { | ||
public override async execute(externalEvent: BackboneExternalEvent): Promise<Message> { | ||
const messageReceivedPayload = externalEvent.payload as { id: string }; | ||
const updatedMessages = await this.accountController.messages.updateCache([messageReceivedPayload.id]); | ||
|
||
const deliveredMessage = updatedMessages[0]; | ||
|
||
this.eventBus.publish(new MessageDeliveredEvent(this.ownAddress, deliveredMessage)); | ||
return deliveredMessage; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ansport/src/modules/sync/externalEventProcessors/MessageReceivedExternalEventProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CoreId } from "../../../core"; | ||
import { MessageReceivedEvent } from "../../../events"; | ||
import { Message } from "../../messages/local/Message"; | ||
import { BackboneExternalEvent } from "../backbone/BackboneExternalEvent"; | ||
import { ExternalEventProcessor } from "./ExternalEventProcessor"; | ||
|
||
export class MessageReceivedExternalEventProcessor extends ExternalEventProcessor { | ||
public override async execute(externalEvent: BackboneExternalEvent): Promise<Message> { | ||
const newMessagePayload = externalEvent.payload as { id: string }; | ||
const newMessage = await this.accountController.messages.loadPeerMessage(CoreId.from(newMessagePayload.id)); | ||
|
||
this.eventBus.publish(new MessageReceivedEvent(this.ownAddress, newMessage)); | ||
return newMessage; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...modules/sync/externalEventProcessors/RelationshipChangeCompletedExternalEventProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { RelationshipChangedEvent } from "../../../events"; | ||
import { Relationship } from "../../relationships/local/Relationship"; | ||
import { BackboneExternalEvent } from "../backbone/BackboneExternalEvent"; | ||
import { ExternalEventProcessor } from "./ExternalEventProcessor"; | ||
|
||
export class RelationshipChangeCompletedExternalEventProcessor extends ExternalEventProcessor { | ||
public override async execute(externalEvent: BackboneExternalEvent): Promise<Relationship | undefined> { | ||
const payload = externalEvent.payload as { changeId: string }; | ||
const relationship = await this.accountController.relationships.applyChangeById(payload.changeId); | ||
|
||
if (relationship) { | ||
this.eventBus.publish(new RelationshipChangedEvent(this.ownAddress, relationship)); | ||
return relationship; | ||
} | ||
return; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...c/modules/sync/externalEventProcessors/RelationshipChangeCreatedExternalEventProcessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { RelationshipChangedEvent } from "../../../events"; | ||
import { Relationship } from "../../relationships/local/Relationship"; | ||
import { BackboneExternalEvent } from "../backbone/BackboneExternalEvent"; | ||
import { ExternalEventProcessor } from "./ExternalEventProcessor"; | ||
|
||
export class RelationshipChangeCreatedExternalEventProcessor extends ExternalEventProcessor { | ||
public override async execute(externalEvent: BackboneExternalEvent): Promise<Relationship | undefined> { | ||
const payload = externalEvent.payload as { changeId: string; relationshipId: string }; | ||
const relationship = await this.accountController.relationships.applyChangeById(payload.changeId); | ||
|
||
if (relationship) { | ||
this.eventBus.publish(new RelationshipChangedEvent(this.ownAddress, relationship)); | ||
return relationship; | ||
} | ||
return; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/transport/src/modules/sync/externalEventProcessors/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./ExternalEventProcessorRegistry"; | ||
export * from "./MessageDeliveredExternalEventProcessor"; | ||
export * from "./MessageReceivedExternalEventProcessor"; | ||
export * from "./RelationshipChangeCompletedExternalEventProcessor"; | ||
export * from "./RelationshipChangeCreatedExternalEventProcessor"; |