Refactor IdeContextStore (#8278)

This commit is contained in:
Tommaso Sciortino
2025-09-11 11:22:20 -07:00
committed by GitHub
parent 538e6cd19a
commit d892cde0b0
10 changed files with 92 additions and 114 deletions
+21 -35
View File
@@ -69,25 +69,18 @@ export type DiffUpdateResult =
content: undefined;
};
type IdeContextSubscriber = (ideContext: IdeContext | undefined) => void;
type IdeContextSubscriber = (ideContext?: IdeContext) => void;
/**
* Creates a new store for managing the IDE's context.
* This factory function encapsulates the state and logic, allowing for the creation
* of isolated instances, which is particularly useful for testing.
*
* @returns An object with methods to interact with the IDE context.
*/
export function createIdeContextStore() {
let ideContextState: IdeContext | undefined = undefined;
const subscribers = new Set<IdeContextSubscriber>();
export class IdeContextStore {
private ideContextState?: IdeContext;
private readonly subscribers = new Set<IdeContextSubscriber>();
/**
* Notifies all registered subscribers about the current IDE context.
*/
function notifySubscribers(): void {
for (const subscriber of subscribers) {
subscriber(ideContextState);
private notifySubscribers(): void {
for (const subscriber of this.subscribers) {
subscriber(this.ideContextState);
}
}
@@ -95,11 +88,11 @@ export function createIdeContextStore() {
* Sets the IDE context and notifies all registered subscribers of the change.
* @param newIdeContext The new IDE context from the IDE.
*/
function setIdeContext(newIdeContext: IdeContext): void {
set(newIdeContext: IdeContext): void {
const { workspaceState } = newIdeContext;
if (!workspaceState) {
ideContextState = newIdeContext;
notifySubscribers();
this.ideContextState = newIdeContext;
this.notifySubscribers();
return;
}
@@ -147,24 +140,24 @@ export function createIdeContextStore() {
workspaceState.openFiles = openFiles.slice(0, IDE_MAX_OPEN_FILES);
}
}
ideContextState = newIdeContext;
notifySubscribers();
this.ideContextState = newIdeContext;
this.notifySubscribers();
}
/**
* Clears the IDE context and notifies all registered subscribers of the change.
*/
function clearIdeContext(): void {
ideContextState = undefined;
notifySubscribers();
clear(): void {
this.ideContextState = undefined;
this.notifySubscribers();
}
/**
* Retrieves the current IDE context.
* @returns The `IdeContext` object if a file is active; otherwise, `undefined`.
*/
function getIdeContext(): IdeContext | undefined {
return ideContextState;
get(): IdeContext | undefined {
return this.ideContextState;
}
/**
@@ -176,22 +169,15 @@ export function createIdeContextStore() {
* @param subscriber The function to be called when the IDE context changes.
* @returns A function that, when called, will unsubscribe the provided subscriber.
*/
function subscribeToIdeContext(subscriber: IdeContextSubscriber): () => void {
subscribers.add(subscriber);
subscribe(subscriber: IdeContextSubscriber): () => void {
this.subscribers.add(subscriber);
return () => {
subscribers.delete(subscriber);
this.subscribers.delete(subscriber);
};
}
return {
setIdeContext,
getIdeContext,
subscribeToIdeContext,
clearIdeContext,
};
}
/**
* The default, shared instance of the IDE context store for the application.
*/
export const ideContext = createIdeContextStore();
export const ideContextStore = new IdeContextStore();