From 83b175bb5b9fac7afff0cd56afa6b3f438bbb0ae Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 7 Nov 2020 16:32:08 +0000 Subject: [PATCH] Api: Added ability to watch resource file --- .../lib/services/ResourceEditWatcher/index.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/lib/services/ResourceEditWatcher/index.ts b/packages/lib/services/ResourceEditWatcher/index.ts index 2a87e8a0c..f44bdf426 100644 --- a/packages/lib/services/ResourceEditWatcher/index.ts +++ b/packages/lib/services/ResourceEditWatcher/index.ts @@ -78,6 +78,9 @@ export default class ResourceEditWatcher { openAndWatch: async ({ resourceId }:any) => { return this.openAndWatch(resourceId); }, + watch: async ({ resourceId }:any) => { + await this.watch(resourceId); + }, stopWatching: async ({ resourceId }:any) => { return this.stopWatching(resourceId); }, @@ -87,7 +90,7 @@ export default class ResourceEditWatcher { }; } - private watch(fileToWatch:string) { + private watchFile(fileToWatch:string) { if (!this.chokidar_) return; const makeSaveAction = (resourceId:string, path:string) => { @@ -194,7 +197,7 @@ export default class ResourceEditWatcher { return this.watcher_; } - public async openAndWatch(resourceId:string) { + private async watch(resourceId:string):Promise { let watchedItem = this.watchedItemByResourceId(resourceId); if (!watchedItem) { @@ -224,7 +227,7 @@ export default class ResourceEditWatcher { watchedItem.lastResourceUpdatedTime = resource.updated_time; watchedItem.size = stat.size; - this.watch(editFilePath); + this.watchFile(editFilePath); this.dispatch({ type: 'RESOURCE_EDIT_WATCHER_SET', @@ -233,9 +236,14 @@ export default class ResourceEditWatcher { }); } - bridge().openItem(watchedItem.path); - this.logger().info(`ResourceEditWatcher: Started watching ${watchedItem.path}`); + + return watchedItem; + } + + public async openAndWatch(resourceId:string) { + const watchedItem = await this.watch(resourceId); + bridge().openItem(watchedItem.path); } async stopWatching(resourceId:string) {