Skip to content

Commit

Permalink
feat: send invalidated message
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 2, 2024
1 parent 2bc5804 commit 421cb73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/hot_hook/src/hot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class Hot {
if (this.#hasOneDeclinedPath(message.paths)) {
process.send?.({ type: 'hot-hook:full-reload', paths: message.paths })
this.#options.onFullReloadAsked?.()
} else {
process.send?.({ type: 'hot-hook:invalidated', paths: message.paths })
}

for (const url of message.paths) {
const callback = this.#disposeCallbacks.get(url)
callback?.()
}

Check failure on line 34 in packages/hot_hook/src/hot.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
}
}

Expand Down
48 changes: 48 additions & 0 deletions packages/hot_hook/tests/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,52 @@ test.group('Loader', () => {
const result = await pEvent(server.child, 'message', (message: any) => message?.type === 'ok')
assert.isDefined(result)
})

test('send invalidated message when file is invalidated', async ({ fs, assert }) => {
await fakeInstall(fs.basePath)

await fs.createJson('package.json', { type: 'module' })
await createHandlerFile({ path: 'config/test.js', response: 'Hello' })
await fs.create(
'server.js',
`import * as http from 'http'
import { hot } from 'hot-hook'
import { join } from 'node:path'
await hot.init({
projectRoot: join(import.meta.dirname, '.'),
})
const server = http.createServer(async (request, response) => {
const app = await import('./config/test.js')
await app.default(request, response)
})
server.listen(3333, () => {
console.log('Server is running')
})`
)

const server = runProcess('server.js', {
cwd: fs.basePath,
env: { NODE_DEBUG: 'hot-hook' },
})

await server.waitForOutput('Server is running')
await setTimeout(100)

await supertest('http://localhost:3333').get('/').expect(200).expect('Hello')

createHandlerFile({ path: 'config/test.js', response: 'Hello Updated' })

const result = await pEvent(
server.child,
'message',
(message: any) =>
message?.type === 'hot-hook:invalidated' &&
message.paths.includes(join(fs.basePath, 'config/test.js'))
)

assert.isDefined(result)
})
})

0 comments on commit 421cb73

Please sign in to comment.