Skip to content

Commit

Permalink
feat(server): ready utility function
Browse files Browse the repository at this point in the history
A promise that resolves when the plugin has been setup.

Useful for other plugins that depend on it!
  • Loading branch information
Pkmmte committed Apr 28, 2024
1 parent 2bf83fe commit 7490206
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-feet-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@robojs/server': patch
---

feat: `ready` utility function
13 changes: 13 additions & 0 deletions packages/plugin-api/src/core/plugin-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { pluginOptions } from '~/events/_start.js'
import type { BaseEngine } from '~/engines/base.js'

export let _readyPromiseResolve: () => void

const _readyPromise = new Promise<void>((resolve) => {
_readyPromiseResolve = resolve
})

export function getServerEngine<T extends BaseEngine = BaseEngine>() {
return pluginOptions.engine as T
}

/**
* Returns a promise that resolves when the plugin is all set up.
*/
export function ready(): Promise<void> {
return _readyPromise
}
2 changes: 2 additions & 0 deletions packages/plugin-api/src/events/_start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '../core/logger.js'
import { hasDependency } from '../core/runtime-utils.js'
import { _readyPromiseResolve } from '~/core/plugin-utils.js'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { portal } from 'robo.js'
Expand Down Expand Up @@ -76,6 +77,7 @@ export default async (_client: Client, options: PluginOptions) => {

logger.debug(`Starting server...`)
await engine.start({ port })
_readyPromiseResolve()
}

async function getDefaultEngine() {
Expand Down

0 comments on commit 7490206

Please sign in to comment.