Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
fstasi committed Jul 22, 2021
1 parent cb71ce7 commit 0e5d1ee
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 87 deletions.
86 changes: 6 additions & 80 deletions arduino-ide-extension/src/browser/create/create-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { posix } from './create-paths';
import { AuthenticationClientService } from '../auth/authentication-client-service';
import { ArduinoPreferences } from '../arduino-preferences';
import { SketchCache } from '../widgets/cloud-sketchbook/cloud-sketch-cache';
import { Create, CreateError } from './typings';

export interface ResponseResultProvider {
(response: Response): Promise<any>;
Expand All @@ -19,7 +20,7 @@ type ResourceType = 'f' | 'd';
@injectable()
export class CreateApi {
@inject(SketchCache)
protected readonly sketchCache: SketchCache;
protected sketchCache: SketchCache;

protected authenticationService: AuthenticationClientService;
protected arduinoPreferences: ArduinoPreferences;
Expand All @@ -34,10 +35,6 @@ export class CreateApi {
return this;
}

public wipeCache(): void {
this.sketchCache.init();
}

getSketchSecretStat(sketch: Create.Sketch): Create.Resource {
return {
href: `${sketch.href}${posix.sep}${Create.arduino_secrets_file}`,
Expand All @@ -50,7 +47,7 @@ export class CreateApi {
};
}

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

url.searchParams.set('user_id', 'me');
Expand Down Expand Up @@ -303,7 +300,9 @@ export class CreateApi {
secrets: { data: secrets },
};

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

const init = {
Expand Down Expand Up @@ -458,76 +457,3 @@ void loop() {
`;
}

export namespace Create {
export interface Sketch {
readonly name: string;
readonly path: string;
readonly modified_at: string;
readonly created_at: string;

readonly secrets?: { name: string; value: string }[];

readonly id: string;
readonly is_public: boolean;
// readonly board_fqbn: '',
// readonly board_name: '',
// readonly board_type: 'serial' | 'network' | 'cloud' | '',
readonly href?: string;
readonly libraries: string[];
// readonly tutorials: string[] | null;
// readonly types: string[] | null;
// readonly user_id: string;
}

export type ResourceType = 'sketch' | 'folder' | 'file';
export const arduino_secrets_file = 'arduino_secrets.h';
export interface Resource {
readonly name: string;
/**
* Note: this path is **not** the POSIX path we use. It has the leading segments with the `user_id`.
*/
readonly path: string;
readonly type: ResourceType;
readonly sketchId?: string;
readonly modified_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
readonly created_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
readonly children?: number; // For 'sketch' and 'folder' types.
readonly size?: number; // For 'sketch' type only.
readonly isPublic?: boolean; // For 'sketch' type only.

readonly mimetype?: string; // For 'file' type.
readonly href?: string;
}
export namespace Resource {
export function is(arg: any): arg is Resource {
return (
!!arg &&
'name' in arg &&
typeof arg['name'] === 'string' &&
'path' in arg &&
typeof arg['path'] === 'string' &&
'type' in arg &&
typeof arg['type'] === 'string' &&
'modified_at' in arg &&
typeof arg['modified_at'] === 'string' &&
(arg['type'] === 'sketch' ||
arg['type'] === 'folder' ||
arg['type'] === 'file')
);
}
}

export type RawResource = Omit<Resource, 'sketchId' | 'isPublic'>;
}

export class CreateError extends Error {
constructor(
message: string,
readonly status: number,
readonly details?: string
) {
super(message);
Object.setPrototypeOf(this, CreateError.prototype);
}
}
72 changes: 72 additions & 0 deletions arduino-ide-extension/src/browser/create/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export namespace Create {
export interface Sketch {
readonly name: string;
readonly path: string;
readonly modified_at: string;
readonly created_at: string;

readonly secrets?: { name: string; value: string }[];

readonly id: string;
readonly is_public: boolean;
readonly board_fqbn: '';
readonly board_name: '';
readonly board_type: 'serial' | 'network' | 'cloud' | '';
readonly href?: string;
readonly libraries: string[];
readonly tutorials: string[] | null;
readonly types: string[] | null;
readonly user_id: string;
}

export type ResourceType = 'sketch' | 'folder' | 'file';
export const arduino_secrets_file = 'arduino_secrets.h';
export interface Resource {
readonly name: string;
/**
* Note: this path is **not** the POSIX path we use. It has the leading segments with the `user_id`.
*/
readonly path: string;
readonly type: ResourceType;
readonly sketchId?: string;
readonly modified_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
readonly created_at: string; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
readonly children?: number; // For 'sketch' and 'folder' types.
readonly size?: number; // For 'sketch' type only.
readonly isPublic?: boolean; // For 'sketch' type only.

readonly mimetype?: string; // For 'file' type.
readonly href?: string;
}
export namespace Resource {
export function is(arg: any): arg is Resource {
return (
!!arg &&
'name' in arg &&
typeof arg['name'] === 'string' &&
'path' in arg &&
typeof arg['path'] === 'string' &&
'type' in arg &&
typeof arg['type'] === 'string' &&
'modified_at' in arg &&
typeof arg['modified_at'] === 'string' &&
(arg['type'] === 'sketch' ||
arg['type'] === 'folder' ||
arg['type'] === 'file')
);
}
}

export type RawResource = Omit<Resource, 'sketchId' | 'isPublic'>;
}

export class CreateError extends Error {
constructor(
message: string,
readonly status: number,
readonly details?: string
) {
super(message);
Object.setPrototypeOf(this, CreateError.prototype);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { FileStat } from '@theia/filesystem/lib/common/files';
import { injectable } from 'inversify';
import { Create } from '../../create/create-api';
import { toPosixPath } from '../../create/create-paths';
import { Create } from '../../create/typings';

@injectable()
export class SketchCache {
sketches: Record<string, Create.Sketch> = {};
filestats: Record<string, FileStat> = {};
fileStats: Record<string, FileStat> = {};

init(): void {
// reset the data
this.sketches = {};
this.filestats = {};
this.fileStats = {};
}

addItem(item: FileStat): void {
this.filestats[item.resource.path.toString()] = item;
this.fileStats[item.resource.path.toString()] = item;
}

getItem(path: string): FileStat | null {
return this.filestats[path] || null;
return this.fileStats[path] || null;
}

addSketch(sketch: Create.Sketch): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inject, injectable, postConstruct } from 'inversify';
import { TreeNode } from '@theia/core/lib/browser/tree';
import { posixSegments, splitSketchPath } from '../../create/create-paths';
import { CreateApi, Create } from '../../create/create-api';
import { CreateApi } from '../../create/create-api';
import { CloudSketchbookTree } from './cloud-sketchbook-tree';
import { AuthenticationClientService } from '../../auth/authentication-client-service';
import { SketchbookTreeModel } from '../sketchbook/sketchbook-tree-model';
Expand All @@ -12,6 +12,8 @@ import { FileStat } from '@theia/filesystem/lib/common/files';
import { LocalCacheFsProvider } from '../../local-cache/local-cache-fs-provider';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import URI from '@theia/core/lib/common/uri';
import { SketchCache } from './cloud-sketch-cache';
import { Create } from '../../create/typings';

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

@inject(SketchCache)
protected readonly sketchCache: SketchCache;

@postConstruct()
protected init(): void {
super.init();
Expand All @@ -82,7 +87,7 @@ export class CloudSketchbookTreeModel extends SketchbookTreeModel {
return;
}
this.createApi.init(this.authenticationService, this.arduinoPreferences);
this.createApi.wipeCache();
this.sketchCache.init();
const sketches = await this.createApi.sketches();
const rootFileStats = sketchesToFileStats(sketches);
if (this.workspaceService.opened) {
Expand Down

0 comments on commit 0e5d1ee

Please sign in to comment.