1- import { injectable } from 'inversify' ;
1+ import { injectable , inject } from 'inversify' ;
22import * as createPaths from './create-paths' ;
3- import { posix , splitSketchPath } from './create-paths' ;
3+ import { posix } from './create-paths' ;
44import { AuthenticationClientService } from '../auth/authentication-client-service' ;
55import { ArduinoPreferences } from '../arduino-preferences' ;
6+ import { SketchCache } from '../widgets/cloud-sketchbook/cloud-sketch-cache' ;
67
78export interface ResponseResultProvider {
89 ( response : Response ) : Promise < any > ;
@@ -15,10 +16,11 @@ export namespace ResponseResultProvider {
1516
1617type ResourceType = 'f' | 'd' ;
1718
18- export let sketchCache : Create . Sketch [ ] = [ ] ;
19-
2019@injectable ( )
2120export class CreateApi {
21+ @inject ( SketchCache )
22+ protected readonly sketchCache : SketchCache ;
23+
2224 protected authenticationService : AuthenticationClientService ;
2325 protected arduinoPreferences : ArduinoPreferences ;
2426
@@ -32,33 +34,19 @@ export class CreateApi {
3234 return this ;
3335 }
3436
35- public sketchCompareByPath = ( param : string ) => {
36- return ( sketch : Create . Sketch ) => {
37- const [ , spath ] = splitSketchPath ( sketch . path ) ;
38- return param === spath ;
39- } ;
40- } ;
41-
42- async findSketchInCache (
43- compareFn : ( sketch : Create . Sketch ) => boolean ,
44- trustCache = true
45- ) : Promise < Create . Sketch | undefined > {
46- const sketch = sketchCache . find ( ( sketch ) => compareFn ( sketch ) ) ;
47- if ( trustCache ) {
48- return Promise . resolve ( sketch ) ;
49- }
50- return await this . sketch ( { id : sketch ?. id } ) ;
37+ public wipeCache ( ) : void {
38+ this . sketchCache . init ( ) ;
5139 }
5240
5341 getSketchSecretStat ( sketch : Create . Sketch ) : Create . Resource {
5442 return {
5543 href : `${ sketch . href } ${ posix . sep } ${ Create . arduino_secrets_file } ` ,
5644 modified_at : sketch . modified_at ,
45+ created_at : sketch . created_at ,
5746 name : `${ Create . arduino_secrets_file } ` ,
5847 path : `${ sketch . path } ${ posix . sep } ${ Create . arduino_secrets_file } ` ,
5948 mimetype : 'text/x-c++src; charset=utf-8' ,
6049 type : 'file' ,
61- sketchId : sketch . id ,
6250 } ;
6351 }
6452
@@ -92,7 +80,7 @@ export class CreateApi {
9280 method : 'GET' ,
9381 headers,
9482 } ) ;
95- sketchCache = result . sketches ;
83+ result . sketches . forEach ( ( sketch ) => this . sketchCache . addSketch ( sketch ) ) ;
9684 return result . sketches ;
9785 }
9886
@@ -118,7 +106,7 @@ export class CreateApi {
118106
119107 async readDirectory (
120108 posixPath : string ,
121- options : { recursive ?: boolean ; match ?: string ; secrets ?: boolean } = { }
109+ options : { recursive ?: boolean ; match ?: string } = { }
122110 ) : Promise < Create . Resource [ ] > {
123111 const url = new URL (
124112 `${ this . domain ( ) } /files/d/$HOME/sketches_v2${ posixPath } `
@@ -131,58 +119,21 @@ export class CreateApi {
131119 }
132120 const headers = await this . headers ( ) ;
133121
134- const sketchProm = options . secrets
135- ? this . sketches ( )
136- : Promise . resolve ( sketchCache ) ;
137-
138- return Promise . all ( [
139- this . run < Create . RawResource [ ] > ( url , {
140- method : 'GET' ,
141- headers,
142- } ) ,
143- sketchProm ,
144- ] )
145- . then ( async ( [ result , sketches ] ) => {
146- if ( options . secrets ) {
147- // for every sketch with secrets, create a fake arduino_secrets.h
148- result . forEach ( async ( res ) => {
149- if ( res . type !== 'sketch' ) {
150- return ;
151- }
152-
153- const [ , spath ] = createPaths . splitSketchPath ( res . path ) ;
154- const sketch = await this . findSketchInCache (
155- this . sketchCompareByPath ( spath )
156- ) ;
157- if ( sketch && sketch . secrets && sketch . secrets . length > 0 ) {
158- result . push ( this . getSketchSecretStat ( sketch ) ) ;
159- }
160- } ) ;
161-
162- if ( posixPath !== posix . sep ) {
163- const sketch = await this . findSketchInCache (
164- this . sketchCompareByPath ( posixPath )
165- ) ;
166- if ( sketch && sketch . secrets && sketch . secrets . length > 0 ) {
167- result . push ( this . getSketchSecretStat ( sketch ) ) ;
168- }
122+ return this . run < Create . RawResource [ ] > ( url , {
123+ method : 'GET' ,
124+ headers,
125+ } )
126+ . then ( async ( result ) => {
127+ // add arduino_secrets.h to the results, when reading a sketch main folder
128+ if ( posixPath . length && posixPath !== posix . sep ) {
129+ const sketch = this . sketchCache . getSketch ( posixPath ) ;
130+
131+ if ( sketch && sketch . secrets && sketch . secrets . length > 0 ) {
132+ result . push ( this . getSketchSecretStat ( sketch ) ) ;
169133 }
170134 }
171- const sketchesMap : Record < string , Create . Sketch > = sketches . reduce (
172- ( prev , curr ) => {
173- return { ...prev , [ curr . path ] : curr } ;
174- } ,
175- { }
176- ) ;
177-
178- // add the sketch id and isPublic to the resource
179- return result . map ( ( resource ) => {
180- return {
181- ...resource ,
182- sketchId : sketchesMap [ resource . path ] ?. id || '' ,
183- isPublic : sketchesMap [ resource . path ] ?. is_public || false ,
184- } ;
185- } ) ;
135+
136+ return result ;
186137 } )
187138 . catch ( ( reason ) => {
188139 if ( reason ?. status === 404 ) return [ ] as Create . Resource [ ] ;
@@ -214,18 +165,16 @@ export class CreateApi {
214165
215166 let resources ;
216167 if ( basename === Create . arduino_secrets_file ) {
217- const sketch = await this . findSketchInCache (
218- this . sketchCompareByPath ( parentPosixPath )
219- ) ;
168+ const sketch = this . sketchCache . getSketch ( parentPosixPath ) ;
220169 resources = sketch ? [ this . getSketchSecretStat ( sketch ) ] : [ ] ;
221170 } else {
222171 resources = await this . readDirectory ( parentPosixPath , {
223172 match : basename ,
224173 } ) ;
225174 }
226-
227- resources . sort ( ( left , right ) => left . path . length - right . path . length ) ;
228- const resource = resources . find ( ( { name } ) => name === basename ) ;
175+ const resource = resources . find (
176+ ( { path } ) => createPaths . splitSketchPath ( path ) [ 1 ] === posixPath
177+ ) ;
229178 if ( ! resource ) {
230179 throw new CreateError ( `Not found: ${ posixPath } .` , 404 ) ;
231180 }
@@ -248,10 +197,7 @@ export class CreateApi {
248197 return data ;
249198 }
250199
251- const sketch = await this . findSketchInCache ( ( sketch ) => {
252- const [ , spath ] = splitSketchPath ( sketch . path ) ;
253- return spath === createPaths . parentPosix ( path ) ;
254- } , true ) ;
200+ const sketch = this . sketchCache . getSketch ( createPaths . parentPosix ( path ) ) ;
255201
256202 if (
257203 sketch &&
@@ -273,14 +219,11 @@ export class CreateApi {
273219
274220 if ( basename === Create . arduino_secrets_file ) {
275221 const parentPosixPath = createPaths . parentPosix ( posixPath ) ;
276- const sketch = await this . findSketchInCache (
277- this . sketchCompareByPath ( parentPosixPath ) ,
278- false
279- ) ;
222+ const sketch = this . sketchCache . getSketch ( parentPosixPath ) ;
280223
281224 let file = '' ;
282225 if ( sketch && sketch . secrets ) {
283- for ( const item of sketch ? .secrets ) {
226+ for ( const item of sketch . secrets ) {
284227 file += `#define ${ item . name } "${ item . value } "\r\n` ;
285228 }
286229 }
@@ -310,9 +253,9 @@ export class CreateApi {
310253
311254 if ( basename === Create . arduino_secrets_file ) {
312255 const parentPosixPath = createPaths . parentPosix ( posixPath ) ;
313- const sketch = await this . findSketchInCache (
314- this . sketchCompareByPath ( parentPosixPath )
315- ) ;
256+
257+ const sketch = this . sketchCache . getSketch ( parentPosixPath ) ;
258+
316259 if ( sketch ) {
317260 const url = new URL ( `${ this . domain ( ) } /sketches/${ sketch . id } ` ) ;
318261 const headers = await this . headers ( ) ;
@@ -357,8 +300,7 @@ export class CreateApi {
357300 } ;
358301
359302 // replace the sketch in the cache, so other calls will not overwrite each other
360- sketchCache = sketchCache . filter ( ( skt ) => skt . id !== sketch . id ) ;
361- sketchCache . push ( { ...sketch , secrets } ) ;
303+ this . sketchCache . addSketch ( sketch ) ;
362304
363305 const init = {
364306 method : 'POST' ,
@@ -543,8 +485,9 @@ export namespace Create {
543485 */
544486 readonly path : string ;
545487 readonly type : ResourceType ;
546- readonly sketchId : string ;
488+ readonly sketchId ? : string ;
547489 readonly modified_at : string ; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
490+ readonly created_at : string ; // As an ISO-8601 formatted string: `YYYY-MM-DDTHH:mm:ss.sssZ`
548491 readonly children ?: number ; // For 'sketch' and 'folder' types.
549492 readonly size ?: number ; // For 'sketch' type only.
550493 readonly isPublic ?: boolean ; // For 'sketch' type only.
0 commit comments