Skip to content

Commit

Permalink
fix(collect): invalidate cache in all workers
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 17, 2023
1 parent 14da639 commit 0cbf351
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/histoire/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test": "peeky run"
},
"dependencies": {
"@akryum/tinypool": "^0.3.1",
"@histoire/app": "^0.15.4",
"@histoire/controls": "^0.15.4",
"@histoire/shared": "^0.15.4",
Expand Down Expand Up @@ -72,7 +73,6 @@
"sade": "^1.8.1",
"shiki-es": "^0.2.0",
"sirv": "^2.0.2",
"tinypool": "^0.1.2",
"vite-node": "0.28.4"
},
"peerDependencies": {
Expand Down
16 changes: 5 additions & 11 deletions packages/histoire/src/node/collect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ViteNodeServer } from 'vite-node/server'
import type { FetchFunction, ResolveIdFunction } from 'vite-node'
import { relative } from 'pathe'
import pc from 'picocolors'
import Tinypool from 'tinypool'
import Tinypool from '@akryum/tinypool'
import { createBirpc } from 'birpc'
import { cpus } from 'node:os'
import type { ServerStoryFile } from '@histoire/shared'
Expand Down Expand Up @@ -82,20 +82,16 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
}
}

const invalidates = new Set<string>()

if (mainServer) {
mainServer.watcher.on('change', (file) => {
file = slash(file)
if (invalidates.has(file)) return
invalidates.add(file)
threadPool.broadcastMessage({
kind: 'hst:invalidate',
file,
})
})
}

function clearInvalidates () {
invalidates.clear()
}

async function executeStoryFile (storyFile: ServerStoryFile) {
try {
const { workerPort } = createChannel()
Expand All @@ -104,7 +100,6 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
base: server.config.base,
storyFile,
port: workerPort,
invalidates: Array.from(invalidates),
}
const { storyData } = await threadPool.run(payload, {
transferList: [
Expand Down Expand Up @@ -157,6 +152,5 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
clearCache,
executeStoryFile,
destroy,
clearInvalidates,
}
}
14 changes: 8 additions & 6 deletions packages/histoire/src/node/collect/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url'
import type { MessagePort } from 'node:worker_threads'
import { parentPort } from 'node:worker_threads'
import { performance } from 'node:perf_hooks'
import { ModuleCacheMap, ViteNodeRunner } from 'vite-node/client'
import { createBirpc } from 'birpc'
Expand All @@ -16,7 +17,6 @@ export interface Payload {
base: string
port: MessagePort
storyFile: ServerStoryFile
invalidates: string[]
}

export interface ReturnData {
Expand All @@ -30,6 +30,13 @@ let _rpc: ReturnType<typeof createBirpc<{
resolveId: ResolveIdFunction
}>>

// Cleanup module cache
parentPort.on('message', message => {
if (message?.kind === 'hst:invalidate') {
_moduleCache.delete(message.file)
}
})

export default async (payload: Payload): Promise<ReturnData> => {
const startTime = performance.now()
process.env.HST_COLLECT = 'true'
Expand All @@ -54,11 +61,6 @@ export default async (payload: Payload): Promise<ReturnData> => {
},
}))

// Cleanup cache
for (const file of payload.invalidates) {
_moduleCache.delete(file)
}

const { destroy: destroyDomEnv } = createDomEnv()

const el = window.document.createElement('div')
Expand Down
3 changes: 0 additions & 3 deletions packages/histoire/src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
clearCache,
executeStoryFile,
destroy: destroyCollectStories,
clearInvalidates,
} = useCollectStories({
server: nodeServer,
mainServer: server,
Expand Down Expand Up @@ -189,8 +188,6 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
}
console.log(`Collect stories end ${pc.bold(pc.blue(Math.round(performance.now() - time)))}ms`)

clearInvalidates()

invalidateModule(VirtualFiles.RESOLVED_STORIES_ID)
invalidateModule(VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID)

Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0cbf351

Please sign in to comment.