-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Доработка вотчера в лаунчсервере (closes #71)
- Loading branch information
Showing
4 changed files
with
51 additions
and
33 deletions.
There are no files selected for viewing
Submodule launcher
updated
58 files
8 changes: 4 additions & 4 deletions
8
packages/server/src/components/commands/commands/updates/DownloadClientCommand.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { LogHelper } from "@root/utils"; | ||
import { FSWatcher, watch } from "chokidar"; | ||
|
||
export class WatchService { | ||
#watcherInstance: FSWatcher; | ||
#timer: NodeJS.Timeout; | ||
|
||
subscribe(dir: string, cwd: string, reload: (path: string) => void) { | ||
this.#watcherInstance = watch(dir, { ignoreInitial: true, cwd }).on( | ||
"all", | ||
(event, path) => { | ||
if (this.#timer) clearTimeout(this.#timer); | ||
this.#timer = setTimeout(() => reload(path), 3000); | ||
LogHelper.debug(event, path); | ||
}, | ||
); | ||
} | ||
|
||
async unsubscribe() { | ||
await this.#watcherInstance.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,40 @@ | ||
import { sep } from "path"; | ||
|
||
import { StorageHelper } from "@root/utils"; | ||
import { Service } from "typedi"; | ||
import { FSWatcher, watch } from 'chokidar'; | ||
import { LogHelper, StorageHelper } from "@root/utils"; | ||
import { ProfilesManager } from "../profiles/ProfilesManager"; | ||
|
||
import { ClientsManager } from "../clients/ClientsManager"; | ||
import { ProfilesManager } from "../profiles/ProfilesManager"; | ||
import { WatchService } from "./WatchService"; | ||
|
||
@Service() | ||
export class Watcher { | ||
#prcess1:FSWatcher | ||
#prcess2:FSWatcher | ||
#clientsWatcher = new WatchService(); | ||
#profilesWatcher = new WatchService(); | ||
|
||
constructor( | ||
private readonly profilesManager:ProfilesManager, | ||
private readonly clientsManager:ClientsManager | ||
private readonly profilesManager: ProfilesManager, | ||
private readonly clientsManager: ClientsManager, | ||
) { | ||
this.subscription(); | ||
} | ||
|
||
async subscription() { | ||
|
||
this.#prcess1 = watch(StorageHelper.clientsDir, {ignoreInitial: true, cwd: StorageHelper.clientsDir}) | ||
.on('all', (event, path) => this.reloadClient(event, path)); | ||
|
||
this.#prcess2 = watch(StorageHelper.profilesDir, {ignoreInitial: true, cwd: '.'}) | ||
.on('all', (event, path) => this.reloadProfile(event, path)); | ||
subscription() { | ||
this.#clientsWatcher.subscribe( | ||
StorageHelper.clientsDir, | ||
StorageHelper.clientsDir, | ||
(path) => { | ||
const dir = path.split(sep); | ||
this.clientsManager.hashClients(dir[0]); | ||
}, | ||
); | ||
this.#profilesWatcher.subscribe(StorageHelper.profilesDir, ".", () => | ||
this.profilesManager.reloadProfiles(), | ||
); | ||
} | ||
|
||
async closeWatcher() { | ||
await this.#prcess1.close() | ||
await this.#prcess2.close() | ||
} | ||
|
||
private reloadClient(event:string, path:string) { | ||
LogHelper.debug(event, path); | ||
const dir = path.split(sep); | ||
this.clientsManager.hashClients(dir[0]); | ||
} | ||
|
||
private reloadProfile(event:string, path:string) { | ||
LogHelper.debug(event, path); | ||
this.profilesManager.reloadProfiles(); | ||
await this.#clientsWatcher.unsubscribe(); | ||
await this.#profilesWatcher.unsubscribe(); | ||
} | ||
} | ||
} |