Skip to content

Commit 0e5d1ee

Browse files
committed
code review
1 parent cb71ce7 commit 0e5d1ee

File tree

4 files changed

+90
-87
lines changed

4 files changed

+90
-87
lines changed

arduino-ide-extension/src/browser/create/create-api.ts

+6-80
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { posix } from './create-paths';
44
import { AuthenticationClientService } from '../auth/authentication-client-service';
55
import { ArduinoPreferences } from '../arduino-preferences';
66
import { SketchCache } from '../widgets/cloud-sketchbook/cloud-sketch-cache';
7+
import { Create, CreateError } from './typings';
78

89
export interface ResponseResultProvider {
910
(response: Response): Promise<any>;
@@ -19,7 +20,7 @@ type ResourceType = 'f' | 'd';
1920
@injectable()
2021
export class CreateApi {
2122
@inject(SketchCache)
22-
protected readonly sketchCache: SketchCache;
23+
protected sketchCache: SketchCache;
2324

2425
protected authenticationService: AuthenticationClientService;
2526
protected arduinoPreferences: ArduinoPreferences;
@@ -34,10 +35,6 @@ export class CreateApi {
3435
return this;
3536
}
3637

37-
public wipeCache(): void {
38-
this.sketchCache.init();
39-
}
40-
4138
getSketchSecretStat(sketch: Create.Sketch): Create.Resource {
4239
return {
4340
href: `${sketch.href}${posix.sep}${Create.arduino_secrets_file}`,
@@ -50,7 +47,7 @@ export class CreateApi {
5047
};
5148
}
5249

53-
async sketch(id: string): Promise<Create.Sketch | undefined> {
50+
async sketch(id: string): Promise<Create.Sketch> {
5451
const url = new URL(`${this.domain()}/sketches/byID/${id}`);
5552

5653
url.searchParams.set('user_id', 'me');
@@ -303,7 +300,9 @@ export class CreateApi {
303300
secrets: { data: secrets },
304301
};
305302

306-
// replace the sketch in the cache, so other calls will not overwrite each other
303+
// replace the sketch in the cache with the one we are pushing
304+
// TODO: we should do a get after the POST, in order to be sure the cache
305+
// is updated the most recent metadata
307306
this.sketchCache.addSketch(sketch);
308307

309308
const init = {
@@ -458,76 +457,3 @@ void loop() {
458457
459458
`;
460459
}
461-
462-
export namespace Create {
463-
export interface Sketch {
464-
readonly name: string;
465-
readonly path: string;
466-
readonly modified_at: string;
467-
readonly created_at: string;
468-
469-
readonly secrets?: { name: string; value: string }[];
470-
471-
readonly id: string;
472-
readonly is_public: boolean;
473-
// readonly board_fqbn: '',
474-
// readonly board_name: '',
475-
// readonly board_type: 'serial' | 'network' | 'cloud' | '',
476-
readonly href?: string;
477-
readonly libraries: string[];
478-
// readonly tutorials: string[] | null;
479-
// readonly types: string[] | null;
480-
// readonly user_id: string;
481-
}
482-
483-
export type ResourceType = 'sketch' | 'folder' | 'file';
484-
export const arduino_secrets_file = 'arduino_secrets.h';
485-
export interface Resource {
486-
readonly name: string;
487-
/**
488-
* Note: this path is **not** the POSIX path we use. It has the leading segments with the `user_id`.
489-
*/
490-
readonly path: string;
491-
readonly type: ResourceType;
492-
readonly sketchId?: string;
493-
readonly modified_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
494-
readonly created_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
495-
readonly children?: number; // For 'sketch' and 'folder' types.
496-
readonly size?: number; // For 'sketch' type only.
497-
readonly isPublic?: boolean; // For 'sketch' type only.
498-
499-
readonly mimetype?: string; // For 'file' type.
500-
readonly href?: string;
501-
}
502-
export namespace Resource {
503-
export function is(arg: any): arg is Resource {
504-
return (
505-
!!arg &&
506-
'name' in arg &&
507-
typeof arg['name'] === 'string' &&
508-
'path' in arg &&
509-
typeof arg['path'] === 'string' &&
510-
'type' in arg &&
511-
typeof arg['type'] === 'string' &&
512-
'modified_at' in arg &&
513-
typeof arg['modified_at'] === 'string' &&
514-
(arg['type'] === 'sketch' ||
515-
arg['type'] === 'folder' ||
516-
arg['type'] === 'file')
517-
);
518-
}
519-
}
520-
521-
export type RawResource = Omit<Resource, 'sketchId' | 'isPublic'>;
522-
}
523-
524-
export class CreateError extends Error {
525-
constructor(
526-
message: string,
527-
readonly status: number,
528-
readonly details?: string
529-
) {
530-
super(message);
531-
Object.setPrototypeOf(this, CreateError.prototype);
532-
}
533-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export namespace Create {
2+
export interface Sketch {
3+
readonly name: string;
4+
readonly path: string;
5+
readonly modified_at: string;
6+
readonly created_at: string;
7+
8+
readonly secrets?: { name: string; value: string }[];
9+
10+
readonly id: string;
11+
readonly is_public: boolean;
12+
readonly board_fqbn: '';
13+
readonly board_name: '';
14+
readonly board_type: 'serial' | 'network' | 'cloud' | '';
15+
readonly href?: string;
16+
readonly libraries: string[];
17+
readonly tutorials: string[] | null;
18+
readonly types: string[] | null;
19+
readonly user_id: string;
20+
}
21+
22+
export type ResourceType = 'sketch' | 'folder' | 'file';
23+
export const arduino_secrets_file = 'arduino_secrets.h';
24+
export interface Resource {
25+
readonly name: string;
26+
/**
27+
* Note: this path is **not** the POSIX path we use. It has the leading segments with the `user_id`.
28+
*/
29+
readonly path: string;
30+
readonly type: ResourceType;
31+
readonly sketchId?: string;
32+
readonly modified_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
33+
readonly created_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
34+
readonly children?: number; // For 'sketch' and 'folder' types.
35+
readonly size?: number; // For 'sketch' type only.
36+
readonly isPublic?: boolean; // For 'sketch' type only.
37+
38+
readonly mimetype?: string; // For 'file' type.
39+
readonly href?: string;
40+
}
41+
export namespace Resource {
42+
export function is(arg: any): arg is Resource {
43+
return (
44+
!!arg &&
45+
'name' in arg &&
46+
typeof arg['name'] === 'string' &&
47+
'path' in arg &&
48+
typeof arg['path'] === 'string' &&
49+
'type' in arg &&
50+
typeof arg['type'] === 'string' &&
51+
'modified_at' in arg &&
52+
typeof arg['modified_at'] === 'string' &&
53+
(arg['type'] === 'sketch' ||
54+
arg['type'] === 'folder' ||
55+
arg['type'] === 'file')
56+
);
57+
}
58+
}
59+
60+
export type RawResource = Omit<Resource, 'sketchId' | 'isPublic'>;
61+
}
62+
63+
export class CreateError extends Error {
64+
constructor(
65+
message: string,
66+
readonly status: number,
67+
readonly details?: string
68+
) {
69+
super(message);
70+
Object.setPrototypeOf(this, CreateError.prototype);
71+
}
72+
}

arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketch-cache.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { FileStat } from '@theia/filesystem/lib/common/files';
22
import { injectable } from 'inversify';
3-
import { Create } from '../../create/create-api';
43
import { toPosixPath } from '../../create/create-paths';
4+
import { Create } from '../../create/typings';
55

66
@injectable()
77
export class SketchCache {
88
sketches: Record<string, Create.Sketch> = {};
9-
filestats: Record<string, FileStat> = {};
9+
fileStats: Record<string, FileStat> = {};
1010

1111
init(): void {
1212
// reset the data
1313
this.sketches = {};
14-
this.filestats = {};
14+
this.fileStats = {};
1515
}
1616

1717
addItem(item: FileStat): void {
18-
this.filestats[item.resource.path.toString()] = item;
18+
this.fileStats[item.resource.path.toString()] = item;
1919
}
2020

2121
getItem(path: string): FileStat | null {
22-
return this.filestats[path] || null;
22+
return this.fileStats[path] || null;
2323
}
2424

2525
addSketch(sketch: Create.Sketch): void {

arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-tree-model.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inject, injectable, postConstruct } from 'inversify';
22
import { TreeNode } from '@theia/core/lib/browser/tree';
33
import { posixSegments, splitSketchPath } from '../../create/create-paths';
4-
import { CreateApi, Create } from '../../create/create-api';
4+
import { CreateApi } from '../../create/create-api';
55
import { CloudSketchbookTree } from './cloud-sketchbook-tree';
66
import { AuthenticationClientService } from '../../auth/authentication-client-service';
77
import { SketchbookTreeModel } from '../sketchbook/sketchbook-tree-model';
@@ -12,6 +12,8 @@ import { FileStat } from '@theia/filesystem/lib/common/files';
1212
import { LocalCacheFsProvider } from '../../local-cache/local-cache-fs-provider';
1313
import { FileService } from '@theia/filesystem/lib/browser/file-service';
1414
import URI from '@theia/core/lib/common/uri';
15+
import { SketchCache } from './cloud-sketch-cache';
16+
import { Create } from '../../create/typings';
1517

1618
export function sketchBaseDir(sketch: Create.Sketch): FileStat {
1719
// extract the sketch path
@@ -67,6 +69,9 @@ export class CloudSketchbookTreeModel extends SketchbookTreeModel {
6769
@inject(LocalCacheFsProvider)
6870
protected readonly localCacheFsProvider: LocalCacheFsProvider;
6971

72+
@inject(SketchCache)
73+
protected readonly sketchCache: SketchCache;
74+
7075
@postConstruct()
7176
protected init(): void {
7277
super.init();
@@ -82,7 +87,7 @@ export class CloudSketchbookTreeModel extends SketchbookTreeModel {
8287
return;
8388
}
8489
this.createApi.init(this.authenticationService, this.arduinoPreferences);
85-
this.createApi.wipeCache();
90+
this.sketchCache.init();
8691
const sketches = await this.createApi.sketches();
8792
const rootFileStats = sketchesToFileStats(sketches);
8893
if (this.workspaceService.opened) {

0 commit comments

Comments
 (0)