Skip to content

Commit

Permalink
refa: migrate core plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 23, 2024
1 parent 41b8b22 commit 0674b9f
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export namespace Plugin {
inject: Dict<Inject.Meta>
isReactive?: boolean
scopes: EffectScope<C>[]
plugin: Plugin<C>
plugin: Plugin
}

export function resolve<C extends Context = Context>(plugin: Plugin<C>): Meta<C> {
Expand Down
39 changes: 9 additions & 30 deletions packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, ForkScope, MainScope, Plugin, Schema, Service } from 'cordis'
import { Context, EffectScope, Plugin, Schema, Service } from 'cordis'
import { Dict, makeArray } from 'cosmokit'
import { ModuleJob, ModuleLoader } from 'cordis/loader'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
Expand Down Expand Up @@ -33,7 +33,7 @@ async function loadDependencies(job: ModuleJob, ignored = new Set<string>()) {

interface Reload {
filename: string
children: ForkScope[]
children: EffectScope[]
}

class Watcher extends Service {
Expand Down Expand Up @@ -195,7 +195,7 @@ class Watcher extends Service {
await this.analyzeChanges()

/** plugins pending classification */
const pending = new Map<ModuleJob, [Plugin, MainScope | undefined]>()
const pending = new Map<ModuleJob, [Plugin, Plugin.Meta | undefined]>()

/** plugins that should be reloaded */
const reloads = new Map<Plugin, Reload>()
Expand Down Expand Up @@ -236,28 +236,7 @@ class Watcher extends Service {

// prepare for reload
if (runtime) {
let isMarked = false
const visited = new Set<MainScope>()
const queued = [runtime]
while (queued.length) {
const runtime = queued.shift()!
if (visited.has(runtime)) continue
visited.add(runtime)
if (reloads.has(plugin)) {
isMarked = true
break
}
for (const fork of runtime.children) {
queued.push(fork.runtime)
}
}
if (!isMarked) {
const children: ForkScope[] = []
reloads.set(plugin, { filename: job.url, children })
for (const fork of runtime.children) {
children.push(fork)
}
}
reloads.set(plugin, { filename: job.url, children: runtime.scopes })
} else {
reloads.set(plugin, { filename: job.url, children: [] })
}
Expand Down Expand Up @@ -290,11 +269,11 @@ class Watcher extends Service {
return rollback()
}

const reload = (plugin: any, children: ForkScope[]) => {
for (const oldFork of children) {
const fork = oldFork.parent.plugin(plugin, oldFork.config)
fork.entry = oldFork.entry
if (fork.entry) fork.entry.fork = fork
const reload = (plugin: any, children: EffectScope[]) => {
for (const oldFiber of children) {
const scope = oldFiber.parent.plugin(plugin, oldFiber.config)
scope.entry = oldFiber.entry
if (scope.entry) scope.entry.fork = scope
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/loader/src/config/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export function apply(ctx: Context) {
}

const checkInject = (scope: EffectScope, name: string) => {
if (!scope.runtime.plugin) return false
if (scope.runtime === scope) {
return scope.runtime.children.every(fork => checkInject(fork, name))
}
if (!scope.meta) return false
if (name in Inject.resolve(scope.entry?.options.inject)) return true
return checkInject(scope.parent.scope, name)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.cjs",
"import": "./lib/index.mjs",
"types": "./lib/index.d.ts"
"import": "./lib/index.mjs"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface LoggerService extends Pick<Logger, Logger.Type | 'extend'> {

export class LoggerService extends Service {
constructor(ctx: Context) {
super(ctx, 'logger', true)
super(ctx, 'logger')

ctx.on('internal/info', function (format, ...args) {
this.logger('app').info(format, ...args)
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.cjs",
"import": "./lib/index.mjs",
"types": "./lib/index.d.ts"
"import": "./lib/index.mjs"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
Expand Down
4 changes: 2 additions & 2 deletions packages/timer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"types": "lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.cjs",
"import": "./lib/index.mjs",
"types": "./lib/index.d.ts"
"import": "./lib/index.mjs"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/timer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type WithDispose<T> = T & { dispose: () => void }

export class TimerService extends Service {
constructor(ctx: Context) {
super(ctx, 'timer', true)
super(ctx, 'timer')
ctx.mixin('timer', ['setTimeout', 'setInterval', 'sleep', 'throttle', 'debounce'])
}

Expand Down

0 comments on commit 0674b9f

Please sign in to comment.