Skip to content

Commit 57588fc

Browse files
committed
code clean up
1 parent 10067dd commit 57588fc

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/storage.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export class Storage {
4444
this._onDidEnabledChanged = new vs.EventEmitter<boolean>();
4545

4646
this._enabled = false;
47-
this.enabled = true;
4847
this.folders = JSON.parse(this.ctx.globalState.get("roots")!);
4948
const host = vs.workspace.getConfiguration().get("redisHost") as string;
5049
const port = parseInt(vs.workspace.getConfiguration().get("redisPort")!);
@@ -58,20 +57,23 @@ export class Storage {
5857

5958
console.log("Config",connectOpts,this.folders);
6059

61-
6260
this.tag = this.getTag();
6361
this.sub = new Redis(connectOpts);
6462
this.pub = new Redis(connectOpts);
6563
this.sub.on("connect",()=>{
66-
console.log("*** CONNECTED ****");
67-
logger.info(`[redis]: Connection made to ${host}:${port} with auth ${username}/${password}`);
64+
let txt = `[redis]: Connection made to ${host}:${port} with auth ${username}/${password}`;
65+
console.log(txt);
66+
logger.info(txt);
6867
this.sub.subscribe(channelID,(err,count) => {
6968
if ( err ){
70-
logger.error(`[redis] Failed to subscribe to channed ${channelID}`,err.message);
71-
console.error("Cannot subscribe !",err);
69+
txt = `[redis] Failed to subscribe to channed ${channelID}: ${err.message}`;
70+
logger.error(txt);
71+
console.error(txt,err);
7272
} else {
73-
logger.info(`[redis] Message bus started on ${channelID}`);
74-
console.log("Subscribed",count);
73+
txt = `[redis] Message bus started on ${channelID} ( count ${count})`;
74+
logger.info(txt);
75+
console.log(txt);
76+
this.startup();
7577
}
7678
});
7779
this.sub.on("message",(channel,msg)=>{
@@ -80,7 +82,12 @@ export class Storage {
8082
});
8183
this.sub.on("error",(e)=>{
8284
logger.error(`[redis]: ${e.name}:${e.message}`);
85+
this.enabled = false;
8386
});
87+
}
88+
89+
startup() {
90+
this.enabled = true;
8491
vs.window.onDidChangeActiveTextEditor((e)=>{
8592
if ( !e?.document ) {
8693
return;
@@ -106,11 +113,10 @@ export class Storage {
106113
return this._enabled;
107114
}
108115

109-
async setContext(file?:string) {
116+
async setContext(file?:string,aCheck?:Tag | null) {
110117
const key = file || (await this.getFileTag());
111118
if ( key ) {
112-
const check = await this.get(key);
113-
console.log(`Check ${key}`,check);
119+
const check = aCheck || (await this.get(key));
114120
const locked = check;
115121
const isOwner = locked && this.isMe(check);
116122
vs.commands.executeCommand('setContext','sharedlock.state',locked ? 'locked':'unlocked');
@@ -137,7 +143,7 @@ export class Storage {
137143
commands.executeCommand('workbench.action.files.setActiveEditorWriteableInSession');
138144
commands.executeCommand(LockCommands.updateLock,LockState.Unlocked);
139145
}
140-
this.setContext(key);
146+
this.setContext(key,check);
141147
}
142148

143149
async onMessage(msg:LockMessage) {

0 commit comments

Comments
 (0)