Skip to content

Commit

Permalink
feat: support hook afterOrmSyncFinished
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016 committed May 1, 2023
1 parent 7420cea commit ff8fe54
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 35 deletions.
136 changes: 101 additions & 35 deletions demo/test/integration/hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const test = require('test');
test.setup();

describe('Hooks', () => {
const coroutine = require('coroutine');

describe.only('Hooks', () => {
var tappInfo
function setup(opts) {
tappInfo = require('../support/spec_helper').getRandomSqliteBasedApp({
Expand All @@ -10,12 +12,13 @@ describe('Hooks', () => {
}

var triggered = {
beforeSetupRoute: false
beforeSetupRoute: false,
afterOrmSyncFinished: false
};

afterEach(() => {
const resetTriggered = () => {
Object.keys(triggered).forEach(k => triggered[k] = false)
})
}

const apis = [
'get',
Expand All @@ -42,49 +45,112 @@ describe('Hooks', () => {

assert.isFunction(app.filterRequest)
}

it('[beforeSetupRoute] triggered after app instanced', () => {
setup({
hooks: {
beforeSetupRoute () {
assert.isFalse(triggered.beforeSetupRoute)
triggered.beforeSetupRoute = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)
}
}
describe('beforeSetupRoute', () => {
afterEach(() => {
resetTriggered();
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
});

it('[beforeSetupRoute] triggered after app instanced - next', () => {
setup({
hooks: {
beforeSetupRoute (next) {
setTimeout(() => {
it('triggered after app instanced', () => {
setup({
hooks: {
beforeSetupRoute () {
assert.isFalse(triggered.beforeSetupRoute)
triggered.beforeSetupRoute = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)

next()
}, 100)
}
}
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
});

it('triggered after app instanced - next', () => {
setup({
hooks: {
beforeSetupRoute (next) {
setTimeout(() => {
assert.isFalse(triggered.beforeSetupRoute)
triggered.beforeSetupRoute = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)

next()
}, 100)
}
}
}
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
});
});

describe('afterOrmSyncFinished', () => {
afterEach(() => {
resetTriggered();
})

assert.isTrue(triggered.beforeSetupRoute);
assert_apis_exist(tappInfo.app);
it('triggered after app orm instanced', () => {
setup({
hooks: {
afterOrmSyncFinished () {
assert.isFalse(triggered.afterOrmSyncFinished)
triggered.afterOrmSyncFinished = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)
}
}
})

assert.isFalse(triggered.afterOrmSyncFinished);
tappInfo.app.ormPool(orm => {
})
assert.isTrue(triggered.afterOrmSyncFinished);
assert_apis_exist(tappInfo.app);
});

it('triggered after app orm instanced - next', () => {
setup({
hooks: {
afterOrmSyncFinished (next) {
setTimeout(() => {
assert.isFalse(triggered.afterOrmSyncFinished)
triggered.afterOrmSyncFinished = true;

assert.exist(this)
assert.isObject(this)

assert.isObject(this.api)
assert_apis_exist(this)

// assert.isUndefined(next)
next()
}, 100)
}
}
})

assert.isFalse(triggered.afterOrmSyncFinished);
tappInfo.app.ormPool(orm => {})
coroutine.sleep(100);
assert.isTrue(triggered.afterOrmSyncFinished);
assert_apis_exist(tappInfo.app);
});
});
})

Expand Down
2 changes: 2 additions & 0 deletions src/Typo/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ export namespace FibApp {
}
export interface Hooks {
beforeSetupRoute?: FxOrmHook.HookActionCallback
afterOrmSyncFinished?: FxOrmHook.HookActionCallback
// afterOrmSyncFinished?: FxOrmHook.HookActionWithCtxCallback<any, { app: FibApp.FibAppClass, orm: FibApp.FibAppOrmInstance }>
}

export interface GetTestRoutingOptions {
Expand Down
10 changes: 10 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import coroutine = require('coroutine');
import Pool = require('fib-pool');

import graphql = require('./http/graphql');
import Hook = require('./http/hook');

import orm_utils = require('./utils/orm')
import orm_plugins = require('./orm_plugins')
Expand Down Expand Up @@ -50,6 +51,15 @@ export = (app: FibApp.FibAppClass, connStr: string, opts: FibApp.FibAppDbSetupOp
sync_info.lock.release();
}

Hook.wait(
app as any,
app.__opts.hooks.afterOrmSyncFinished as any,
function (err: orm.FxOrmError.ExtendedError) {
if (err) throw err;
},
// { app, orm: ormInstance },
);

// only init graphql when models is not empty
if (ormInstance.models && Object.keys(ormInstance.models).length)
graphql(app, ormInstance);
Expand Down
1 change: 1 addition & 0 deletions typings/Typo/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export declare namespace FibApp {
}
export interface Hooks {
beforeSetupRoute?: FxOrmHook.HookActionCallback;
afterOrmSyncFinished?: FxOrmHook.HookActionCallback;
}
export interface GetTestRoutingOptions {
initRouting?: {
Expand Down

0 comments on commit ff8fe54

Please sign in to comment.