-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(event): Subscriber ID miss usage (#11047)
PARTIALLY RESOLVES FRMW-2876 **What** Fix wrong usage of the `subscriberId` in the event bus. It happens that the subscriber id coming from the context was not used at all. This issue lead to duplicated event subscriber with the same subscriber id, it also prevent unsubscribing from event since rand id will be assigned. **NOTE** This PR does not handle overide strategy for subscribers with the same id. this still needs to be discussed
- Loading branch information
Showing
4 changed files
with
75 additions
and
31 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
"@medusajs/event-bus-local": patch | ||
"@medusajs/utils": patch | ||
--- | ||
|
||
fix(event): Subscriber ID missusage |
52 changes: 52 additions & 0 deletions
52
packages/core/utils/src/event-bus/__tests__/abstract-event-bus-module.spec.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,52 @@ | ||
import { EventBusTypes } from "@medusajs/types" | ||
import { AbstractEventBusModuleService } from ".." | ||
|
||
class MockEventBusModuleService extends AbstractEventBusModuleService { | ||
constructor() { | ||
super({}, {}, {} as any) | ||
} | ||
|
||
async emit<T>( | ||
data: EventBusTypes.Message<T> | EventBusTypes.Message<T>[], | ||
options: Record<string, unknown> | ||
): Promise<void> { | ||
return Promise.resolve() | ||
} | ||
|
||
async releaseGroupedEvents(eventGroupId: string): Promise<void> { | ||
return Promise.resolve() | ||
} | ||
|
||
async clearGroupedEvents(eventGroupId: string): Promise<void> { | ||
return Promise.resolve() | ||
} | ||
} | ||
|
||
describe("AbstractEventBusModuleService", () => { | ||
it("should be able to subscribe to an event", () => { | ||
const eventBus = new MockEventBusModuleService() | ||
const subscriber = jest.fn() | ||
eventBus.subscribe("test", subscriber) | ||
expect(eventBus.eventToSubscribersMap.get("test")).toEqual([ | ||
{ id: (subscriber as any).subscriberId, subscriber }, | ||
]) | ||
}) | ||
|
||
it("should throw an error if a subscriber with the same id is already subscribed to an event", () => { | ||
const eventBus = new MockEventBusModuleService() | ||
const subscriber = jest.fn() | ||
const subscriberId = "test" | ||
eventBus.subscribe("test", subscriber, { subscriberId }) | ||
expect(() => | ||
eventBus.subscribe("test", subscriber, { subscriberId }) | ||
).toThrow() | ||
}) | ||
|
||
it("should be able to unsubscribe from an event", () => { | ||
const eventBus = new MockEventBusModuleService() | ||
const subscriber = jest.fn() | ||
eventBus.subscribe("test", subscriber) | ||
eventBus.unsubscribe("test", subscriber) | ||
expect(eventBus.eventToSubscribersMap.get("test")).toEqual([]) | ||
}) | ||
}) |
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