-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change chat to use state to show views
- Loading branch information
Showing
9 changed files
with
163 additions
and
45 deletions.
There are no files selected for viewing
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
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,50 @@ | ||
import { Disposable } from "vscode"; | ||
import { Mutex } from "await-semaphore"; | ||
import EventEmitterBasedState from "../utils/EventEmitterBasedState"; | ||
import { State } from "./state"; | ||
import { getState, tabNineProcess } from "./requests/requests"; | ||
import { Logger } from "../utils/logger"; | ||
|
||
const SESSION_POLL_INTERVAL = 10_000; | ||
|
||
export class BinaryState extends EventEmitterBasedState<State> { | ||
private updateStateLock = new Mutex(); | ||
|
||
private intervalDisposabled: Disposable | null = null; | ||
|
||
start(): Disposable { | ||
if (!this.intervalDisposabled) { | ||
let interval: NodeJS.Timeout | undefined; | ||
void tabNineProcess.onReady.then(() => { | ||
interval = setInterval(() => { | ||
void this.checkForUpdates(); | ||
}, SESSION_POLL_INTERVAL); | ||
}); | ||
|
||
this.intervalDisposabled = new Disposable(() => { | ||
if (interval) { | ||
clearInterval(interval); | ||
} | ||
}); | ||
} | ||
|
||
return this.intervalDisposabled; | ||
} | ||
|
||
private async checkForUpdates() { | ||
try { | ||
await this.updateStateLock.use(async () => { | ||
const state = await getState(); | ||
|
||
if (state) { | ||
this.set(state); | ||
} | ||
}); | ||
} catch (error) { | ||
Logger.warn("Failed to refetch state", error); | ||
} | ||
} | ||
} | ||
|
||
const BINARY_STATE = new BinaryState(); | ||
export default BINARY_STATE; |
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
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
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,45 @@ | ||
import { Disposable } from "vscode"; | ||
import EventEmitterBasedState from "./EventEmitterBasedState"; | ||
|
||
export type DerivedState<T> = Disposable & EventEmitterBasedState<T>; | ||
|
||
export default function deriveState<I, O, S extends EventEmitterBasedState<I>>( | ||
state: S, | ||
mapping: (value: I) => O | ||
): DerivedState<O> { | ||
class TempDerivedState | ||
extends EventEmitterBasedState<O> | ||
implements Disposable { | ||
useStateDisposabled!: Disposable; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.useStateDisposabled = state.useState((inputState) => { | ||
this.set(mapping(inputState)); | ||
}); | ||
} | ||
|
||
dispose() { | ||
this.useStateDisposabled.dispose(); | ||
} | ||
} | ||
|
||
return new TempDerivedState(); | ||
} | ||
|
||
export function useDerviedState<I, O, S extends EventEmitterBasedState<I>>( | ||
state: S, | ||
mapping: (value: I) => O, | ||
onChange: (newValue: O) => void | ||
): Disposable { | ||
const derviedState = deriveState(state, mapping); | ||
const disposable = derviedState.useState(onChange); | ||
|
||
return { | ||
dispose() { | ||
derviedState.dispose(); | ||
disposable.dispose(); | ||
}, | ||
}; | ||
} |