'How to access context.globalState inside a refresh() function of a TreeView class?
I'm trying to access context.globalState.get('data'); inside a TreeView refresh() function.
My refresh() looks like this:
refresh(offset?: number): void {
this.ctmInfrastructureCache = getGlobalStateData();
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
The global state data is supposed to be provided by:
export function getGlobalStateData(): string {
const data: string = context.globalState.get('ctmInfrastructureCache');
return data;
}
However, I can't access context.globalState. I'm getting:
Property 'globalState' does not exist on type 'SuiteFunction'.ts(2339)
The new data is collected before refresh is being called and assigned to a globalState context. All I want is to get the data and provide it as input for the new TreeView.
I've tried adding context: vscode.ExtensionContext to the refresh() to no avail.
How do I get access to globalState inside the refresh?
The class constructor() has access to globalState. This is working:
constructor(context: vscode.ExtensionContext) {
let ctmInfrastructureCacheTmp: any = context.globalState.get('ctmInfrastructureCache');
let ctmInfrastructureCacheType: any = typeof ctmInfrastructureCacheTmp;
// check if json needs to be converted
if (ctmInfrastructureCacheType === "string") {
this.ctmInfrastructureCache = ctmInfrastructureCacheTmp;
} else {
this.ctmInfrastructureCache = JSON.stringify(ctmInfrastructureCacheTmp);
}
this.parseTree();
}
Solution 1:[1]
based on Mike's suggestions I've updated the code and it works now. First I've added the 'context' to the registerCommand() section of my refresh().
ctmInfrastructureProvider.refresh(undefined, context);
Then I've updated the refresh function like this, with 'context?: vscode.ExtensionContext' as an optional input.
public refresh(offset?: number, context?: vscode.ExtensionContext): void {
// get globalState data
let ctmInfrastructureCacheTmp: any = context.globalState.get('ctmInfrastructureCache');
let ctmInfrastructureCacheType: any = typeof ctmInfrastructureCacheTmp;
// check if json needs to be converted
if (ctmInfrastructureCacheType === "string") {
this.ctmInfrastructureCache = ctmInfrastructureCacheTmp;
} else {
this.ctmInfrastructureCache = JSON.stringify(ctmInfrastructureCacheTmp);
}
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
Regards, O.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Orchestrator |
