Skip to content

Commit

Permalink
feat(docs): update docs for context event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jan 20, 2020
1 parent 4e60847 commit e3de3bf
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions docs/site/Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ the form of metadata) to your intent.

## Context events

The `Context` emits the following events:
An instance of `Context` can emit the following events:

- `bind`: Emitted when a new binding is added to the context.
- binding: the newly added binding object
Expand All @@ -252,12 +252,51 @@ The `Context` emits the following events:
process
- err: the error object thrown

The bind/unbind events are represented as the following type:

```ts
/**
* Events emitted by a context
*/
export type ContextEvent = {
/**
* Source context that emits the event
*/
context: Context;
/**
* Binding that is being added/removed/updated
*/
binding: Readonly<Binding<unknown>>;
/**
* Event type
*/
type: string; // 'bind' or 'unbind'
};
```

When an existing binding key is replaced with a new one, an `unbind` event is
emitted for the existing binding followed by a `bind` event for the new binding.

If a context has a parent, binding events from the parent are re-emitted on the
context when the binding key does not exist within the current context.

A context event listener should conform to the following signature:

```ts
/**
* Synchronous event listener for the `Context` as en event emitter
*/
export type ContextEventListener = (event: ContextEvent) => void;
```

By default, `maxListeners` is set to `Infinity` for context objects to avoid
[memory leak warnings](https://github.com/strongloop/loopback-next/issues/4363).
The value can be reset as follows:

```ts
ctx.setMaxListeners(128);
```

## Context observers

Bindings can be added or removed to a context object. With emitted context
Expand All @@ -275,7 +314,7 @@ come and go. There are a few caveats associated with that:
.bind('foo')
.to('foo-value')
.tag('foo-tag');
ctx.on('bind', binding => {
ctx.on('bind', {binding} => {
console.log(binding.tagNames); // returns an empty array `[]`
});
```
Expand All @@ -293,7 +332,7 @@ come and go. There are a few caveats associated with that:
.to('foo-value')
.tag('foo-tag');
ctx.add(binding);
ctx.on('bind', binding => {
ctx.on('bind', {binding} => {
console.log(binding.tagMap); // returns `['foo-tag']`
});
```
Expand Down Expand Up @@ -344,6 +383,15 @@ export type ContextEventObserver = ContextObserver | ContextObserverFn;

If `filter` is not required, we can simply use `ContextObserverFn`.

Please note that `ContextEventObserver` is different from
`ContextEventListener`:

- A `ContextEventListener` is synchronous and it's invoked when the event is
emitted (before `emit` returns).

- A `ContextEventObserver` is asynchronous and it's invoked by the notification
queue after the event is emitted (after `emit` returns).

2. Context APIs

- `subscribe(observer: ContextEventObserver)`
Expand Down

0 comments on commit e3de3bf

Please sign in to comment.