Skip to content

Commit

Permalink
feat(loader): handle plugin update
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 21, 2024
1 parent b3bf879 commit 7617fd4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
3 changes: 1 addition & 2 deletions packages/cordis/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ export interface Options extends Loader.Options {
export async function start(options: Options) {
const ctx = new Context()
ctx.plugin(Loader, options)
await ctx.loader.init(process.env.CORDIS_LOADER_ENTRY)
if (process.execArgv.includes('--expose-internals')) {
const { internal } = await import('./internal.js')
ctx.loader.internal = internal
}
await ctx.loader.init(process.env.CORDIS_LOADER_ENTRY)
if (options.logger) ctx.plugin(logger, options.logger)
if (options.daemon) ctx.plugin(daemon, options.daemon)
await ctx.loader.readConfig()
await ctx.start()
}
2 changes: 1 addition & 1 deletion packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Watcher extends Service {
this.ctx.loader.exit()
} else {
const config = await loader.readConfig()
this.ctx.root.state.update(config)
loader.entry.update(config)
this.ctx.emit('config')
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
env: process.env,
}

public entry!: ForkScope<Context>
public suspend = false
public writable = false
public mimeType!: string
Expand Down Expand Up @@ -247,7 +248,8 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
}

async start() {
this.app.plugin(group, this.config)
await this.readConfig()
this.entry = this.app.plugin(group, this.config)

this.app.on('dispose', () => {
this.exit()
Expand All @@ -263,7 +265,9 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
if (fork[kUpdate]) return delete fork[kUpdate]
if (!fork.id) return
const { schema } = fork.runtime
fork.parent.scope.config = schema ? schema.simplify(config) : config
const entry = fork.parent.scope.config?.find((entry: Entry) => entry.id === fork.id)
if (!entry) return
entry.config = schema ? schema.simplify(config) : config
this.writeConfig()
})

Expand Down
44 changes: 37 additions & 7 deletions packages/loader/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ describe('@cordisjs/loader', () => {
root.plugin(MockLoader)
root.loader.writable = true

test('loader.createApp()', async () => {
const foo = mock.fn((ctx: Context) => {
ctx.accept()
})
const bar = mock.fn((ctx: Context) => {
ctx.accept()
})
root.loader.register('foo', foo)
root.loader.register('bar', bar)

test('basic support', async () => {
root.loader.config = [{
id: '1',
name: 'foo',
Expand All @@ -24,16 +33,37 @@ describe('@cordisjs/loader', () => {
}],
}]

const foo = mock.fn()
const bar = mock.fn()
root.loader.register('foo', foo)
root.loader.register('bar', bar)

await root.start()

expect(root.registry.get(foo)).to.be.ok
expect(root.registry.get(foo)?.config).to.deep.equal({})
expect(foo.mock.calls).to.have.length(1)
expect(root.registry.get(bar)).to.be.ok
expect(root.registry.get(bar)?.config).to.deep.equal({ a: 1 })
expect(bar.mock.calls).to.have.length(1)
})

test('entry update', async () => {
root.loader.config = [{
id: '1',
name: 'foo',
}]

root.loader.entry.update(root.loader.config)
await new Promise((resolve) => setTimeout(resolve, 0))
expect(root.registry.get(foo)).to.be.ok
expect(root.registry.get(bar)).to.be.not.ok
expect(foo.mock.calls).to.have.length(1)
expect(bar.mock.calls).to.have.length(1)
})

test('plugin update', async () => {
const runtime = root.registry.get(foo)
runtime?.update({ a: 3 })
await new Promise((resolve) => setTimeout(resolve, 0))
expect(root.loader.config).to.deep.equal([{
id: '1',
name: 'foo',
config: { a: 3 },
}])
})
})
6 changes: 5 additions & 1 deletion packages/loader/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dict } from 'cosmokit'
import { Context, Plugin } from '@cordisjs/core'
import { group, Loader } from '../src/shared'
import { Entry, group, Loader } from '../src/shared'

declare module '../src/shared' {
interface Loader {
Expand All @@ -23,4 +23,8 @@ export default class MockLoader extends Loader {
async import(name: string) {
return this.data[name]
}

async readConfig() {
return this.config
}
}

0 comments on commit 7617fd4

Please sign in to comment.