Skip to content

Commit 03f3045

Browse files
committed
Prevent refresh loop on services example loading + cleanup
Signed-off-by: Seb Julliand <[email protected]>
1 parent e518652 commit 03f3045

File tree

3 files changed

+36
-63
lines changed

3 files changed

+36
-63
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/views/examples/exampleBrowser.ts

Lines changed: 31 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { EventEmitter, MarkdownString, workspace } from "vscode";
2-
import { window } from "vscode";
3-
import { CancellationToken, Event, ExtensionContext, ProviderResult, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, commands } from "vscode";
4-
import { SQLExample, Examples, ServiceInfoLabel } from ".";
5-
import { OSData, fetchSystemInfo } from "../../config";
1+
import { Event, EventEmitter, ExtensionContext, MarkdownString, ThemeIcon, TreeDataProvider, TreeItem, TreeItemCollapsibleState, commands, window, workspace } from "vscode";
2+
import { Examples, SQLExample, ServiceInfoLabel } from ".";
63
import { getInstance } from "../../base";
4+
import { OSData, fetchSystemInfo } from "../../config";
75
import { getServiceInfo } from "../../database/serviceInfo";
86

97
const openExampleCommand = `vscode-db2i.examples.open`;
108

119
export class ExampleBrowser implements TreeDataProvider<any> {
1210
private _onDidChangeTreeData: EventEmitter<TreeItem | undefined | null | void> = new EventEmitter<TreeItem | undefined | null | void>();
1311
readonly onDidChangeTreeData: Event<TreeItem | undefined | null | void> = this._onDidChangeTreeData.event;
14-
15-
private currentFilter: string|undefined;
12+
13+
private currentFilter: string | undefined;
1614

1715
constructor(context: ExtensionContext) {
1816
context.subscriptions.push(
@@ -49,9 +47,9 @@ export class ExampleBrowser implements TreeDataProvider<any> {
4947
// Refresh the examples when we have it, so we only display certain examples
5048
this.refresh();
5149
})
52-
})
50+
})
5351
}
54-
52+
5553
refresh() {
5654
this._onDidChangeTreeData.fire();
5755
}
@@ -60,59 +58,35 @@ export class ExampleBrowser implements TreeDataProvider<any> {
6058
return element;
6159
}
6260

63-
async getChildren(element?: ExampleGroupItem): Promise<any[]> {
64-
// Unlike the bulk of the examples which are defined in views/examples/index.ts, the services examples are retrieved dynamically
65-
if (!Examples[ServiceInfoLabel]) {
66-
getServiceInfo().then(serviceExamples => {
67-
Examples[ServiceInfoLabel] = serviceExamples;
68-
this.refresh();
69-
})
61+
async getChildren(element?: ExampleGroupItem): Promise<SQLExampleItem[]> {
62+
if (element) {
63+
return element.getChildren();
7064
}
71-
72-
if (this.currentFilter) {
73-
// If there is a filter, then show all examples that include this criteria
74-
let items: SQLExampleItem[] = [];
75-
76-
const upperFilter = this.currentFilter.toUpperCase();
77-
78-
for (const exampleName in Examples) {
79-
items.push(
80-
...Examples[exampleName]
81-
.filter(example => exampleWorksForOnOS(example))
82-
.filter(example => example.name.toUpperCase().includes(upperFilter) || example.content.some(line => line.toUpperCase().includes(upperFilter)))
83-
.map(example => new SQLExampleItem(example))
84-
)
65+
else {
66+
// Unlike the bulk of the examples which are defined in views/examples/index.ts, the services examples are retrieved dynamically
67+
if (!Examples[ServiceInfoLabel]) {
68+
Examples[ServiceInfoLabel] = await getServiceInfo();
8569
}
8670

87-
return items;
88-
89-
} else {
90-
if (element) {
91-
return element.getChildren();
92-
} else {
93-
let items: ExampleGroupItem[] = [];
94-
95-
for (const exampleName in Examples) {
96-
items.push(
97-
new ExampleGroupItem(exampleName, Examples[exampleName])
98-
)
99-
}
100-
101-
return items;
71+
if (this.currentFilter) {
72+
// If there is a filter, then show all examples that include this criteria
73+
const upperFilter = this.currentFilter.toUpperCase();
74+
return Object.values(Examples)
75+
.flatMap(examples => examples.filter(exampleWorksForOnOS))
76+
.filter(example => example.name.toUpperCase().includes(upperFilter) || example.content.some(line => line.toUpperCase().includes(upperFilter)))
77+
.map(example => new SQLExampleItem(example));
78+
}
79+
else {
80+
return Object.entries(Examples).map(([name, examples]) => new ExampleGroupItem(name, examples));
10281
}
10382
}
10483
}
105-
106-
getParent?(element: any) {
107-
throw new Error("Method not implemented.");
108-
}
10984
}
11085

11186
class ExampleGroupItem extends TreeItem {
11287
constructor(name: string, private group: SQLExample[]) {
11388
super(name, TreeItemCollapsibleState.Collapsed);
114-
115-
this.iconPath = new ThemeIcon(`folder`);
89+
this.iconPath = ThemeIcon.Folder;
11690
}
11791

11892
getChildren(): SQLExampleItem[] {
@@ -125,8 +99,7 @@ class ExampleGroupItem extends TreeItem {
12599
class SQLExampleItem extends TreeItem {
126100
constructor(example: SQLExample) {
127101
super(example.name, TreeItemCollapsibleState.None);
128-
129-
this.iconPath = new ThemeIcon(`file`);
102+
this.iconPath = ThemeIcon.File;
130103

131104
this.tooltip = new MarkdownString(['```sql', example.content.join(`\n`), '```'].join(`\n`));
132105

@@ -142,11 +115,11 @@ function exampleWorksForOnOS(example: SQLExample): boolean {
142115
if (OSData) {
143116
const myOsVersion = OSData.version;
144117

145-
// If this example has specific system requirements defined..
146-
if (example.requirements && example.requirements[myOsVersion]) {
147-
if (OSData.db2Level < example.requirements[myOsVersion]) {
148-
return false;
149-
}
118+
// If this example has specific system requirements defined
119+
if (example.requirements &&
120+
example.requirements[myOsVersion] &&
121+
OSData.db2Level < example.requirements[myOsVersion]) {
122+
return false;
150123
}
151124
}
152125

src/views/examples/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export interface SQLExample {
1313
requirements?: ExampleSystemRequirements;
1414
};
1515

16-
// Unlike the bulk of the examples defined below, the services examples are retrieved dynamically
17-
export const ServiceInfoLabel = `IBM i (SQL) Services`;
16+
// Unlike the bulk of the examples defined below, the services examples are retrieved dynamically
17+
export const ServiceInfoLabel = `IBM i (SQL) Services`;
1818

19-
export let Examples: SQLExamplesList = {
19+
export const Examples: SQLExamplesList = {
2020
"Data Definition Language (DDL)": [
2121
{
2222
"name": "Create Schema",

0 commit comments

Comments
 (0)