Skip to content

Commit

Permalink
Account for change to podium emit(), no longer can be awaited. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jun 20, 2022
1 parent 14cebba commit 12c1453
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
20 changes: 13 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1539,11 +1539,11 @@ async function example() {
const server = Hapi.server({ port: 80 });
server.event('test');
server.events.on('test', (update) => console.log(update));
await server.events.emit('test', 'hello');
await server.events.gauge('test', 'hello');
}
```

### <a name="server.events.emit()" /> `await server.events.emit(criteria, data)`
### <a name="server.events.emit()" /> `server.events.emit(criteria, data)`

Emits a custom application event to all the subscribed listeners where:

Expand All @@ -1569,7 +1569,7 @@ async function example() {
const server = Hapi.server({ port: 80 });
server.event('test');
server.events.on('test', (update) => console.log(update));
await server.events.emit('test', 'hello'); // await is optional
server.events.emit('test', 'hello');
}
```

Expand Down Expand Up @@ -1633,7 +1633,7 @@ async function example() {
const server = Hapi.server({ port: 80 });
server.event('test');
server.events.on('test', (update) => console.log(update));
await server.events.emit('test', 'hello');
server.events.emit('test', 'hello');
}
```

Expand All @@ -1651,8 +1651,8 @@ async function example() {
const server = Hapi.server({ port: 80 });
server.event('test');
server.events.once('test', (update) => console.log(update));
await server.events.emit('test', 'hello');
await server.events.emit('test', 'hello'); // Ignored
server.events.emit('test', 'hello');
server.events.emit('test', 'hello'); // Ignored
}
```

Expand All @@ -1670,11 +1670,17 @@ async function example() {
const server = Hapi.server({ port: 80 });
server.event('test');
const pending = server.events.once('test');
await server.events.emit('test', 'hello');
server.events.emit('test', 'hello');
const update = await pending;
}
```

### <a name="server.events.gauge()" /> `await server.events.gauge(criteria, data)`

Behaves identically to [`server.events.emit()`](#server.events.emit()), but also returns an array of the results of all the event listeners that run. The return value is that of `Promise.allSettled()`, where each item in the resulting array is `{ status: 'fulfilled', value }` in the case of a successful handler, or `{ status: 'rejected', reason }` in the case of a handler that throws.

Please note that system errors such as a `TypeError` are not handled specially, and it's recommended to scrutinize any rejections using something like [bounce](https://hapi.dev/module/bounce/).

### <a name="server.expose()" /> `server.expose(key, value, [options])`

Used within a plugin to expose a property via [`server.plugins[name]`](#server.plugins) where:
Expand Down
4 changes: 2 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ exports = module.exports = internals.Core = class {
}

this.phase = 'started';
await this.events.emit('start');
this.events.emit('start');

try {
if (this.controlled) {
Expand Down Expand Up @@ -415,7 +415,7 @@ exports = module.exports = internals.Core = class {
this.caches.forEach((cache) => caches.push(cache.client.stop()));
await Promise.all(caches);

await this.events.emit('stop');
this.events.emit('stop');
this.heavy.stop();

if (this.controlled) {
Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ describe('Server', () => {

server.events.emit('test', 1);
server.events.emit({ name: 'test', channel: 'x' }, 2);
await plugin.events.emit({ name: 'test', channel: 'y' }, 3);
plugin.events.emit({ name: 'test', channel: 'y' }, 3);

expect(updates).to.equal([
{ id: 'server', update: 1 },
Expand Down

0 comments on commit 12c1453

Please sign in to comment.