Skip to content

Commit

Permalink
feat(loader): incremental reload for group plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 19, 2024
1 parent 238a5fd commit c8181db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
11 changes: 2 additions & 9 deletions packages/cordis/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { Entry } from '@cordisjs/loader'
import { Context } from './index.js'
import { group } from '@cordisjs/loader'

export const inject = ['loader']

export function apply(ctx: Context, config: Entry[]) {
for (const entry of config) {
ctx.loader.reload(ctx, entry)
}
}
export default group
30 changes: 26 additions & 4 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, EffectScope, ForkScope } from '@cordisjs/core'
import { Dict, isNullable, valueMap } from 'cosmokit'
import { defineProperty, Dict, isNullable, valueMap } from 'cosmokit'
import { constants, promises as fs } from 'fs'
import { interpolate } from './utils.ts'
import * as yaml from 'js-yaml'
Expand Down Expand Up @@ -250,9 +250,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> {
}

async start() {
for (const entry of this.config) {
this.reload(this.app, entry)
}
this.app.plugin(group, this.config)

this.app.on('dispose', () => {
this.fullReload()
Expand Down Expand Up @@ -284,4 +282,28 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> {
}
}

export function group(ctx: Context, config: Entry[]) {
for (const entry of config) {
ctx.loader.reload(ctx, entry)
}

ctx.accept((neo: Entry[]) => {
// update config reference
const old = ctx.scope.config as Entry[]
const oldMap = Object.fromEntries(old.map(entry => [entry.id, entry]))
const neoMap = Object.fromEntries(neo.map(entry => [entry.id, entry]))

// update inner plugins
for (const id in { ...oldMap, ...neoMap }) {
if (!neoMap[id]) {
ctx.loader.unload(ctx, oldMap[id])
} else {
ctx.loader.reload(ctx, neoMap[id])
}
}
}, { passive: true })
}

defineProperty(group, 'inject', ['loader'])

export default Loader

0 comments on commit c8181db

Please sign in to comment.