From 12c0558e08dc7284e48a8e38a4653f8b47d38bcb Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Fri, 25 May 2018 19:45:17 -0300 Subject: [PATCH] fix(docz-core): data server class --- packages/docz-core/src/DataServer.ts | 58 +++++++++++++++++----------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/packages/docz-core/src/DataServer.ts b/packages/docz-core/src/DataServer.ts index cbe5c329e..0dad341ae 100644 --- a/packages/docz-core/src/DataServer.ts +++ b/packages/docz-core/src/DataServer.ts @@ -19,8 +19,16 @@ export class DataServer { private config: Config constructor({ server, port, host, config }: DataServerOpts) { - this.config = config - this.server = new WS.Server({ server, port, host }) + this.server = new WS.Server({ + server, + port, + host, + }) + + this.config = { + ...config, + websocketPort: port, + } } public async processEntries(): Promise { @@ -31,12 +39,14 @@ export class DataServer { ignored: /(^|[\/\\])\../, }) - const handleConnection = async (socket: WS) => { - watcher.on('change', this.updateEntries) - watcher.on('unlink', this.updateEntries) - watcher.on('raw', (event: string, path: string, details: any) => { + const handleConnection = (socket: WS) => { + const update = this.updateEntries(socket) + + watcher.on('change', async () => update(this.config)) + watcher.on('unlink', async () => update(this.config)) + watcher.on('raw', async (event: string, path: string, details: any) => { if (details.event === 'moved' && details.type === 'directory') { - this.updateEntries(socket) + await update(this.config) } }) @@ -53,11 +63,13 @@ export class DataServer { const watcher = chokidar.watch(finds('docz')) const handleConnection = async (socket: WS) => { - watcher.on('add', this.updateConfig) - watcher.on('change', this.updateConfig) - watcher.on('unlink', this.updateConfig) + const update = this.updateConfig(socket) + + watcher.on('add', update) + watcher.on('change', update) + watcher.on('unlink', update) - this.updateConfig(socket) + update() } this.server.on('connection', handleConnection) @@ -76,23 +88,25 @@ export class DataServer { return this.dataObj('docz.config', config.themeConfig) } - private async updateEntries(socket: WS): Promise { - const config = this.config - - if (isSocketOpened(socket)) { - const newEntries = new Entries(config) - const newMap = await newEntries.getMap() + private updateEntries(socket: WS): (config: Config) => Promise { + return async config => { + if (isSocketOpened(socket)) { + const newEntries = new Entries(config) + const newMap = await newEntries.getMap() - await Entries.rewrite(newMap) - socket.send(this.entriesData(newMap)) + await Entries.rewrite(newMap) + socket.send(this.entriesData(newMap)) + } } } - private updateConfig(socket: WS): void { + private updateConfig(socket: WS): () => void { const config = load('docz', {}, true) - if (isSocketOpened(socket)) { - socket.send(this.configData(config)) + return () => { + if (isSocketOpened(socket)) { + socket.send(this.configData(config)) + } } } }