Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context): improve context listener and events #4451

Merged
merged 4 commits into from
Jan 21, 2020

Conversation

raymondfeng
Copy link
Contributor

@raymondfeng raymondfeng commented Jan 17, 2020

Spin-off from #4377

  1. Add ContextEventListener interface
  2. Tidy up parent context event handling
  3. Set maxListeners to Infinity by default

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

}
this._parentEventListeners = undefined;
if (this._parent && this.parentEventListener) {
this._parent.removeListener('bind', this.parentEventListener);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code removes all the listeners, any reason why only moves the bind and unbind now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code uses a map for registered listeners. With this PR, only one function is used on bind/unbind events.

@achrinza achrinza added feature IoC/Context @loopback/context: Dependency Injection, Inversion of Control labels Jan 19, 2020
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for creating this smaller pull request, it makes it much easier to review the changes and iterate on the best design & implementation 🙇

packages/context/src/__tests__/unit/context.unit.ts Outdated Show resolved Hide resolved
packages/context/src/context.ts Show resolved Hide resolved
docs/site/Context.md Show resolved Hide resolved
docs/site/Context.md Show resolved Hide resolved
packages/context/src/context.ts Outdated Show resolved Hide resolved
packages/context/src/context.ts Outdated Show resolved Hide resolved
@raymondfeng
Copy link
Contributor Author

@bajtos Your comments have been addressed. PTAL.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Please get at least one more person to review & approve this new version.

}
this.emit('bind', binding, this);
this.emitEvent('bind', {binding, context: this, type: 'bind'});
Copy link
Member

@bajtos bajtos Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can emitEvent read the event name from event.type?

Suggested change
this.emitEvent('bind', {binding, context: this, type: 'bind'});
this.emitEvent({binding, context: this, type: 'bind'});

docs/site/Context.md Show resolved Hide resolved
…ling

BREAKING CHANGE: Context events are now emitted as `ContextEvent` objects
instead of positional arguments. Context listener functions must switch from
the old style to new style as follows:

1. Old style

```ts
ctx.on('bind', (binding, context) => {
// ...
});
```

2. New style

```ts
ctx.on('bind', (event: ContextEvent) => {
// ...
});
```

Or:

```ts
ctx.on('bind', ({binding, context, type}) => {
// ...
});
```
@raymondfeng raymondfeng merged commit ae5febc into master Jan 21, 2020
@raymondfeng raymondfeng deleted the context-listener branch January 21, 2020 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature IoC/Context @loopback/context: Dependency Injection, Inversion of Control
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants