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();
}
CodePudding user response:
What you are missing is that the context is passed to the constructor, so it has access to it there. In your getGlobalStateData()
function context
is not known. The reference to context
probably references a different context object.
One possible solution is to keep a reference to context
and use it as member of the class:
public constructor(private 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();
}
public refresh(offset?: number): void {
this.ctmInfrastructureCache = this.context.globalState.get('ctmInfrastructureCache');
this.parseTree();
if (offset) {
this._onDidChangeTreeData.fire(offset);
} else {
this._onDidChangeTreeData.fire(undefined);
}
}
CodePudding user response:
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.