Skip to content

Commit

Permalink
feat: onReady plugin hook (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode authored Feb 12, 2025
1 parent f4b2c64 commit 6b3f86a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
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?.()))
}).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

0 comments on commit 6b3f86a

Please sign in to comment.