Skip to content

Commit 50c1c7d

Browse files
Akos KittaChristian Weichel
Akos Kitta
authored and
Christian Weichel
committed
Fixed a race condition when requestng core client.
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent e14ac4a commit 50c1c7d

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

arduino-ide-extension/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"@theia/filesystem": "next",
1010
"@theia/languages": "next",
1111
"@theia/monaco": "next",
12-
"@theia/workspace": "next"
12+
"@theia/workspace": "next",
13+
"@types/p-queue": "^3.2.1",
14+
"p-queue": "^5.0.0"
1315
},
1416
"scripts": {
1517
"generate-protoc": "./scripts/generate-protoc.sh",

arduino-ide-extension/src/node/core-client-provider-impl.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import URI from '@theia/core/lib/common/uri';
88
import { CoreClientProvider, Client } from './core-client-provider';
99
import * as fs from 'fs';
1010
import * as path from 'path';
11+
import * as PQueue from 'p-queue';
1112

1213
@injectable()
1314
export class CoreClientProviderImpl implements CoreClientProvider {
1415

16+
protected readonly clientRequestQueue = new PQueue({ autoStart: true, concurrency: 1 });
17+
1518
@inject(FileSystem)
1619
protected readonly fileSystem: FileSystem;
1720

@@ -21,20 +24,24 @@ export class CoreClientProviderImpl implements CoreClientProvider {
2124
protected clients = new Map<string, Client>();
2225

2326
async getClient(workspaceRootOrResourceUri?: string): Promise<Client> {
24-
const roots = await this.workspaceServiceExt.roots();
25-
if (!workspaceRootOrResourceUri) {
26-
return this.getOrCreateClient(roots[0]);
27-
}
28-
const root = roots
29-
.sort((left, right) => right.length - left.length) // Longest "paths" first
30-
.map(uri => new URI(uri))
31-
.find(uri => uri.isEqualOrParent(new URI(workspaceRootOrResourceUri)));
32-
if (!root) {
33-
console.warn(`Could not retrieve the container workspace root for URI: ${workspaceRootOrResourceUri}.`);
34-
console.warn(`Falling back to ${roots[0]}`);
35-
return this.getOrCreateClient(roots[0]);
36-
}
37-
return this.getOrCreateClient(root.toString());
27+
return this.clientRequestQueue.add(() => new Promise<Client>(async resolve => {
28+
const roots = await this.workspaceServiceExt.roots();
29+
if (!workspaceRootOrResourceUri) {
30+
resolve(this.getOrCreateClient(roots[0]));
31+
return;
32+
}
33+
const root = roots
34+
.sort((left, right) => right.length - left.length) // Longest "paths" first
35+
.map(uri => new URI(uri))
36+
.find(uri => uri.isEqualOrParent(new URI(workspaceRootOrResourceUri)));
37+
if (!root) {
38+
console.warn(`Could not retrieve the container workspace root for URI: ${workspaceRootOrResourceUri}.`);
39+
console.warn(`Falling back to ${roots[0]}`);
40+
resolve(this.getOrCreateClient(roots[0]));
41+
return;
42+
}
43+
resolve(this.getOrCreateClient(root.toString()));
44+
}));
3845
}
3946

4047
protected async getOrCreateClient(rootUri: string): Promise<Client> {

arduino-ide-extension/src/node/library-service-impl.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import { injectable } from 'inversify';
1+
import { injectable, inject } from 'inversify';
22
import { Library, LibraryService } from '../common/protocol/library-service';
3+
import { CoreClientProvider } from './core-client-provider';
4+
import { LibrarySearchReq, LibrarySearchResp } from './cli-protocol/lib_pb';
35

46
@injectable()
57
export class LibraryServiceImpl implements LibraryService {
68

9+
@inject(CoreClientProvider)
10+
protected readonly coreClientProvider: CoreClientProvider;
11+
712
async search(options: { query?: string; }): Promise<{ items: Library[] }> {
13+
const { client, instance } = await this.coreClientProvider.getClient();
14+
15+
const req = new LibrarySearchReq();
16+
req.setQuery(options.query || '');
17+
req.setInstance(instance);
18+
const resp = await new Promise<LibrarySearchResp>((resolve, reject) => client.librarySearch(req, (err, resp) => !!err ? reject(err) : resolve(resp)));
19+
console.log(resp.getSearchOutputList());
20+
821
const { query } = options;
922
const allItems: Library[] = [
1023
<Library>{

yarn.lock

+19
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,13 @@
12851285
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.46.tgz#12161db48a775e8c69c1cfff2be545610381056f"
12861286
integrity sha512-PfnRbk836fFs9T9QnZh0G1k9oC6YXCqIK3LX6vU/6oiXtEBSFCiJFj6UnLZtqIIHTsgMn8Dojq3yhmpwY7QWcw==
12871287

1288+
"@types/p-queue@^3.2.1":
1289+
version "3.2.1"
1290+
resolved "https://registry.yarnpkg.com/@types/p-queue/-/p-queue-3.2.1.tgz#614957d9ed05cf6e02d08ed8f09041c4d100ef70"
1291+
integrity sha512-tgAdn5zEs05NuHzOyRM34cMO0rczStphR/kLo/ZJwwwJ5S2+QVxwA6gST3vDHWPB1oDfUuT6wOouhJvJkBCA0w==
1292+
dependencies:
1293+
p-queue "*"
1294+
12881295
"@types/prop-types@*":
12891296
version "15.7.1"
12901297
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.1.tgz#f1a11e7babb0c3cad68100be381d1e064c68f1f6"
@@ -4638,6 +4645,11 @@ etag@~1.8.1:
46384645
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
46394646
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
46404647

4648+
eventemitter3@^3.1.0:
4649+
version "3.1.2"
4650+
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
4651+
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==
4652+
46414653
events@^3.0.0:
46424654
version "3.0.0"
46434655
resolved "https://registry.yarnpkg.com/events/-/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88"
@@ -8184,6 +8196,13 @@ p-pipe@^1.2.0:
81848196
resolved "https://registry.yarnpkg.com/p-pipe/-/p-pipe-1.2.0.tgz#4b1a11399a11520a67790ee5a0c1d5881d6befe9"
81858197
integrity sha1-SxoROZoRUgpneQ7loMHViB1r7+k=
81868198

8199+
p-queue@*, p-queue@^5.0.0:
8200+
version "5.0.0"
8201+
resolved "https://registry.yarnpkg.com/p-queue/-/p-queue-5.0.0.tgz#80f1741d5e78a6fa72fce889406481baa5617a3c"
8202+
integrity sha512-6QfeouDf236N+MAxHch0CVIy8o/KBnmhttKjxZoOkUlzqU+u9rZgEyXH3OdckhTgawbqf5rpzmyR+07+Lv0+zg==
8203+
dependencies:
8204+
eventemitter3 "^3.1.0"
8205+
81878206
p-reduce@^1.0.0:
81888207
version "1.0.0"
81898208
resolved "https://registry.yarnpkg.com/p-reduce/-/p-reduce-1.0.0.tgz#18c2b0dd936a4690a529f8231f58a0fdb6a47dfa"

0 commit comments

Comments
 (0)