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: onReady plugin hook #210

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/modern-walls-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kopflos-cms/core": patch
"kopflos": patch
---

New plugin hook: `onReady`
1 change: 1 addition & 0 deletions packages/cli/lib/command/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async function run({

const server = app.listen(port, host, () => {
log.info(`Server running on ${port}. API URL: ${config.baseIri}`)
instance.ready()
})

if (config.watch) {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/lib/Kopflos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface PluginConfig {
export interface KopflosPlugin {
readonly name?: string
onStart?(): Promise<void> | void
onReady?(): Promise<void> | void
onStop?(): Promise<void> | void
apiTriples?(): Promise<DatasetCore | Stream> | DatasetCore | Stream
}
Expand Down Expand Up @@ -121,6 +122,7 @@ export default class Impl implements Kopflos {
readonly env: KopflosEnvironment
readonly plugins: Array<KopflosPlugin>
readonly start: () => Promise<void>
readonly ready: () => Promise<void>

constructor({ variables = {}, ...config }: KopflosConfig, private readonly options: Options = {}) {
this.env = createEnv({ variables, ...config })
Expand Down Expand Up @@ -148,6 +150,10 @@ export default class Impl implements Kopflos {
this.start = onetime(async function (this: Impl) {
await Promise.all(this.plugins.map(plugin => plugin.onStart?.()))
}).bind(this)

this.ready = onetime(async function (this: Impl) {
await Promise.all(this.plugins.map(plugin => plugin.onReady?.()))
giacomociti marked this conversation as resolved.
Show resolved Hide resolved
}).bind(this)
}

get graph() {
Expand Down
40 changes: 40 additions & 0 deletions packages/core/test/lib/Kopflos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,46 @@ describe('lib/Kopflos', () => {
await instance.stop()
})
})

describe('ready', () => {
it('calls onReady on plugins', async function () {
// given
const onReady = sinon.spy()
const plugin = class {
onReady = onReady
}
const instance = new Kopflos({
...config,
sparql: {
default: inMemoryClients(this.rdf),
},
}, {
plugins: [plugin],
})

// when
await instance.ready()

// then
expect(onReady).to.have.been.called
})

it('ignores plugins without onReady', async function () {
// given
const plugin = class {}
const instance = new Kopflos({
...config,
sparql: {
default: inMemoryClients(this.rdf),
},
}, {
plugins: [plugin],
})

// when
await instance.ready()
})
})
})

const testHandler: Handler = ({ subject, property, object }) => ({
Expand Down