Skip to content

Commit

Permalink
Merge pull request #316 from raveljs/bugfix/315
Browse files Browse the repository at this point in the history
Missing await on lifecycle-decorated methods
  • Loading branch information
Ghnuberath authored Mar 28, 2021
2 parents 2971972 + cacd1ea commit d7bc61a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion documentation.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Ravel API
version: 1.0.0-rc.13
version: 1.0.0-rc.14
favicon: assets/logo.png
versions:
- 1.0.0-rc.14
- 1.0.0-rc.13
- 1.0.0-rc.12
- 1.0.0-rc.11
Expand Down
51 changes: 43 additions & 8 deletions jest/integration/ravel-lifecycle.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { fail } = require('assert');

let app;
let postinjectHandlerCalled = 0;
let anotherPostinjectHandlerCalled = 0;
Expand Down Expand Up @@ -59,20 +61,49 @@ describe('Ravel lifeycle test', () => {
}

@postinject
doPostInject () {
async doPostInject () {
expect(this.another.test).toBe('hello world');
postinjectHandlerCalled += 1;
try {
await request('localhost:8080').get('').expect(200);
fail('Ravel application should not be listening at this lifecycle stage.');
} catch (err) {}
await new Promise((resolve) => {
process.nextTick(() => {
postinjectHandlerCalled += 1;
expect(postinitHandlerCalled).toBe(0);
expect(anotherPostinitHandlerCalled).toBe(0);
resolve();
});
});
}

@postinject
doAnotherPostInject () {
async doAnotherPostInject () {
expect(this.another.test).toBe('hello world');
anotherPostinjectHandlerCalled += 1;
await new Promise((resolve) => {
process.nextTick(() => {
anotherPostinjectHandlerCalled += 1;
expect(postinitHandlerCalled).toBe(0);
expect(anotherPostinitHandlerCalled).toBe(0);
resolve();
});
});
}

@postinit
doPostInit () {
postinitHandlerCalled += 1;
async doPostInit () {
try {
await request('localhost:8080').get('').expect(200);
fail('Ravel application should not be listening at this lifecycle stage.');
} catch (err) {}
expect(postinjectHandlerCalled).toBe(1);
expect(anotherPostinjectHandlerCalled).toBe(1);
await new Promise((resolve) => {
process.nextTick(() => {
postinitHandlerCalled += 1;
resolve();
});
});
}

@postinit
Expand All @@ -81,12 +112,16 @@ describe('Ravel lifeycle test', () => {
}

@prelisten
doPreListen () {
async doPreListen () {
try {
await request('localhost:8080').get('').expect(200);
fail('Ravel application should not be listening at this lifecycle stage.');
} catch (err) {}
prelistenHandlerCalled += 1;
}

@postlisten
doPostListen () {
async doPostListen () {
postlistenHandlerCalled += 1;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/core/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async function initModule (ravelInstance) {
function connectHandlers (decorator, event) {
const handlers = Metadata.getClassMeta(Object.getPrototypeOf(self), decorator, Object.create(null));
for (const f of Object.keys(handlers)) {
ravelInstance.once(event, (...args) => {
ravelInstance.once(event, async (...args) => {
ravelInstance.$log.trace(`${name}: Invoking ${decorator} ${f}`);
handlers[f].apply(self, args);
await handlers[f].apply(self, args);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ravel",
"version": "1.0.0-rc.13",
"version": "1.0.0-rc.14",
"author": "Sean McIntyre <[email protected]>",
"description": "Ravel Rapid Application Development Framework",
"engines": {
Expand Down

0 comments on commit d7bc61a

Please sign in to comment.