Skip to content

Commit

Permalink
feat: Improve event management
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Sep 2, 2022
1 parent 8143685 commit 6d20130
Show file tree
Hide file tree
Showing 67 changed files with 296 additions and 337 deletions.
37 changes: 11 additions & 26 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,6 @@ export default class MyCustomService {
private readonly manager: EntityManager;

constructor(private readonly container: ConstructorParams) {
super();
this.manager = container.manager;
}

Expand Down Expand Up @@ -624,24 +623,6 @@ export default class ProductService extends MedusaProductService {
super(container);
this.manager = container.manager;
}

@OnMedusaEntityEvent.Before.Insert(Product, { async: true })
public async attachStoreToProduct(
params: MedusaEventHandlerParams<Product, 'Insert'>
): Promise<EntityEventType<Product, 'Insert'>> {
const loggedInUser = this.container.loggedInUser;
const { event } = params;
event.entity.store_id = loggedInUser.store_id;
event.entity.handle = loggedInUser.store_id.replace('store_', '') + '-' + event.entity.handle;
return event;
}

public prepareListQuery_(selector: Record<string, any>, config: FindConfig<Product>): object {
if (Object.keys(this.container).includes('loggedInUser')) {
selector['store_id'] = this.container.loggedInUser.store_id;
}
return super.prepareListQuery_(selector, config);
}
}
```

Expand Down Expand Up @@ -677,18 +658,19 @@ import UserService from './user.service';
@Middleware({ requireAuth: true, routes: [{ method: "all", path: '*' }] })
export class LoggedInUserMiddleware implements MedusaMiddleware {
public async consume(req: MedusaAuthenticatedRequest, res: Response, next: NextFunction): Promise<void> {
let loggedInUser = null
if (req.user && req.user.userId) {
const userService = req.scope.resolve('userService') as UserService;
const loggedInUser = await userService.retrieve(req.user.userId, {
select: ['id', 'your_custom_field'],
});

req.scope.register({
loggedInUser: {
resolve: () => loggedInUser,
},
});
}

req.scope.register({
loggedInUser: {
resolve: () => loggedInUser,
},
});
next();
}
}
Expand Down Expand Up @@ -916,7 +898,7 @@ export default class ProductSubscriber implements EntitySubscriberInterface<Prod
}

public async beforeInsert(event: InsertEvent<Product>): Promise<InsertEvent<Product>> {
const eventName = OnMedusaEntityEvent.Before.InsertEvent(User, UserService, 'attachStoreToUser');
const eventName = OnMedusaEntityEvent.Before.InsertEvent(User);
await eventEmitter.emitAsync<InsertEvent<User>>(eventName, {
event,
transactionalEntityManager: event.manager,
Expand Down Expand Up @@ -980,6 +962,9 @@ Here, we are listening to the product creation, and before the entity is
inserted into the database, we are retrieving the `loggedInUser` through the container
and attach the `store_id` to the product entity.

> NOTE: If the number of listener reach the limit, you can control that through `eventEmitter.setMaxListeners()`
> Too learn more about it you can find the doc [here](https://nodejs.org/api/events.html#emittersetmaxlistenersn)

[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/cloudy.png)](#utilities-wrench)

Expand Down
8 changes: 4 additions & 4 deletions docs/classes/Medusa.Medusa-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Load medusa and apply all components

#### Defined in

[src/Medusa.ts:39](https://github.com/adrien2p/medusa-extender/blob/a984300/src/Medusa.ts#L39)
[src/Medusa.ts:39](https://github.com/adrien2p/medusa-extender/blob/8143685/src/Medusa.ts#L39)

## Properties

Expand All @@ -46,7 +46,7 @@ Load medusa and apply all components

#### Defined in

[src/Medusa.ts:32](https://github.com/adrien2p/medusa-extender/blob/a984300/src/Medusa.ts#L32)
[src/Medusa.ts:32](https://github.com/adrien2p/medusa-extender/blob/8143685/src/Medusa.ts#L32)

___

Expand All @@ -56,7 +56,7 @@ ___

#### Defined in

[src/Medusa.ts:33](https://github.com/adrien2p/medusa-extender/blob/a984300/src/Medusa.ts#L33)
[src/Medusa.ts:33](https://github.com/adrien2p/medusa-extender/blob/8143685/src/Medusa.ts#L33)

## Methods

Expand All @@ -76,4 +76,4 @@ ___

#### Defined in

[src/Medusa.ts:47](https://github.com/adrien2p/medusa-extender/blob/a984300/src/Medusa.ts#L47)
[src/Medusa.ts:47](https://github.com/adrien2p/medusa-extender/blob/8143685/src/Medusa.ts#L47)
2 changes: 1 addition & 1 deletion docs/classes/core_componentMap.ComponentMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ Map.get

#### Defined in

[src/core/componentMap.ts:4](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/componentMap.ts#L4)
[src/core/componentMap.ts:4](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/componentMap.ts#L4)
17 changes: 9 additions & 8 deletions docs/classes/core_event_emmiter.Internals.CustomEventEmitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ EventEmitter.constructor

#### Defined in

[src/core/event-emmiter.ts:22](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/event-emmiter.ts#L22)
[src/core/event-emmiter.ts:24](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/event-emmiter.ts#L24)

## Properties

### #listeners

`Private` **#listeners**: `Set`<[`ListenerDescriptor`](../modules/core_event_emmiter.Internals.md#listenerdescriptor)<`unknown`\>\>
`Private` **#listeners**: `Map`<`string` \| `symbol`, [`ListenerDescriptor`](../modules/core_event_emmiter.Internals.md#listenerdescriptor)<`unknown`\>[]\>

#### Defined in

[src/core/event-emmiter.ts:20](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/event-emmiter.ts#L20)
[src/core/event-emmiter.ts:22](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/event-emmiter.ts#L22)

## Methods

Expand Down Expand Up @@ -79,7 +79,7 @@ Emit an asynchrone event entity based and wait for the result.

#### Defined in

[src/core/event-emmiter.ts:74](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/event-emmiter.ts#L74)
[src/core/event-emmiter.ts:94](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/event-emmiter.ts#L94)

___

Expand Down Expand Up @@ -109,16 +109,17 @@ Register a new event handler.

#### Defined in

[src/core/event-emmiter.ts:32](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/event-emmiter.ts#L32)
[src/core/event-emmiter.ts:34](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/event-emmiter.ts#L34)

___

### registerListeners

**registerListeners**(`container`): `void`

Apply all event handlers hold by the `listenerDescriptor`.
Only unregister and register again non singleton based event listeners
Apply all event handlers hold by the `listenerDescriptors`.
Only unregister and register again non singleton based event listeners.
No duplicate listener can exist on one handler.

#### Parameters

Expand All @@ -132,4 +133,4 @@ Only unregister and register again non singleton based event listeners

#### Defined in

[src/core/event-emmiter.ts:45](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/event-emmiter.ts#L45)
[src/core/event-emmiter.ts:47](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/event-emmiter.ts#L47)
26 changes: 13 additions & 13 deletions docs/classes/core_logger.Logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Internal logger to display information about the build.

#### Defined in

[src/core/logger.ts:20](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L20)
[src/core/logger.ts:20](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L20)

## Properties

Expand All @@ -59,7 +59,7 @@ Internal logger to display information about the build.

#### Defined in

[src/core/logger.ts:10](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L10)
[src/core/logger.ts:10](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L10)

___

Expand All @@ -69,7 +69,7 @@ ___

#### Defined in

[src/core/logger.ts:9](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L9)
[src/core/logger.ts:9](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L9)

___

Expand All @@ -79,7 +79,7 @@ ___

#### Defined in

[src/core/logger.ts:8](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L8)
[src/core/logger.ts:8](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L8)

## Accessors

Expand All @@ -93,7 +93,7 @@ ___

#### Defined in

[src/core/logger.ts:16](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L16)
[src/core/logger.ts:16](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L16)

___

Expand All @@ -107,7 +107,7 @@ ___

#### Defined in

[src/core/logger.ts:12](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L12)
[src/core/logger.ts:12](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L12)

## Methods

Expand All @@ -131,7 +131,7 @@ Build logs taking in count the level color

#### Defined in

[src/core/logger.ts:89](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L89)
[src/core/logger.ts:89](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L89)

___

Expand All @@ -154,7 +154,7 @@ Display error logs in red immediately

#### Defined in

[src/core/logger.ts:66](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L66)
[src/core/logger.ts:66](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L66)

___

Expand All @@ -170,7 +170,7 @@ display all queues logs and then reset the queue.

#### Defined in

[src/core/logger.ts:73](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L73)
[src/core/logger.ts:73](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L73)

___

Expand All @@ -193,7 +193,7 @@ Display log immediately.

#### Defined in

[src/core/logger.ts:48](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L48)
[src/core/logger.ts:48](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L48)

___

Expand All @@ -216,7 +216,7 @@ Push new logs to queue and then show them later on.

#### Defined in

[src/core/logger.ts:34](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L34)
[src/core/logger.ts:34](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L34)

___

Expand All @@ -239,7 +239,7 @@ Display warning immediately.

#### Defined in

[src/core/logger.ts:57](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L57)
[src/core/logger.ts:57](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L57)

___

Expand All @@ -260,4 +260,4 @@ ___

#### Defined in

[src/core/logger.ts:25](https://github.com/adrien2p/medusa-extender/blob/a984300/src/core/logger.ts#L25)
[src/core/logger.ts:25](https://github.com/adrien2p/medusa-extender/blob/8143685/src/core/logger.ts#L25)
Loading

0 comments on commit 6d20130

Please sign in to comment.