Skip to content

Commit

Permalink
fix(docz-core): close dataserver connection on build
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Sep 29, 2018
1 parent 9e29730 commit 0f6bd7b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
18 changes: 16 additions & 2 deletions packages/docz-core/src/DataServer.ts
Expand Up @@ -27,6 +27,7 @@ export interface Params {
export interface State {
init: (params: Params) => Promise<any>
update: (params: Params) => any
close: (params: Params) => any
}

export class DataServer {
Expand All @@ -52,7 +53,7 @@ export class DataServer {
await Promise.all(
Array.from(this.states).map(
async state =>
state.init &&
isFn(state.init) &&
state.init({
state: { ...this.state },
setState: this.setState(),
Expand All @@ -75,10 +76,23 @@ export class DataServer {
}
}

public async close(): Promise<void> {
await Promise.all(
Array.from(this.states).map(
async state =>
isFn(state.close) &&
state.close({
state: { ...this.state },
setState: this.setState(),
})
)
)
}

private handleConnection(socket: WS): () => void {
const states = Array.from(this.states).map(
async state =>
state.update &&
isFn(state.update) &&
state.update({
state: this.state,
setState: this.setState(socket),
Expand Down
1 change: 1 addition & 0 deletions packages/docz-core/src/commands/build.ts
Expand Up @@ -27,6 +27,7 @@ export const build = async (args: Config) => {
await run('onPreBuild', config)
await bundler.build(await bundler.getConfig(env))
await run('onPostBuild', config)
await dataServer.close()
} catch (err) {
logger.fatal(err)
process.exit(1)
Expand Down
7 changes: 5 additions & 2 deletions packages/docz-core/src/states/config.ts
Expand Up @@ -41,12 +41,15 @@ const updateConfig = (config: Config) => async ({ setState }: Params) =>
export const state = (config: Config): State => {
const watcher = chokidar.watch(finds('docz'), {
cwd: paths.root,
ignored: /(^|[\/\\])\../,
ignored: /(((^|[\/\\])\..+)|(node_modules))/,
persistent: true,
})

const handleClose = () => watcher.close()

return {
init: updateConfig(config),
close: handleClose,
update: async params => {
const update = updateConfig(config)
const fn = async () => update(params)
Expand All @@ -55,7 +58,7 @@ export const state = (config: Config): State => {
watcher.on('change', fn)
watcher.on('unlink', fn)

return () => watcher.close()
return handleClose
},
}
}
7 changes: 5 additions & 2 deletions packages/docz-core/src/states/entries.ts
Expand Up @@ -30,12 +30,15 @@ export const state = (entries: Entries, config: Config): State => {
const files = path.join(src, config.files)
const watcher = chokidar.watch(files, {
cwd: paths.root,
ignored: /(^|[\/\\])\../,
ignored: /(((^|[\/\\])\..+)|(node_modules))/,
persistent: true,
})

const handleClose = () => watcher.close()

return {
init: updateEntries(entries),
close: handleClose,
update: async params => {
const update = updateEntries(entries)

Expand All @@ -47,7 +50,7 @@ export const state = (entries: Entries, config: Config): State => {
}
})

return () => watcher.close()
return handleClose
},
}
}

0 comments on commit 0f6bd7b

Please sign in to comment.