@@ -23,6 +23,9 @@ export type OpenProcessingCurationResponse = Array<{
2323 title : string ;
2424 /** Description of sketch */
2525 description : string ;
26+ instructions : string ;
27+ mode : string ;
28+ createdOn : string ;
2629 userID : string ;
2730 submittedOn : string ;
2831 /** Author's name */
@@ -36,16 +39,19 @@ export type OpenProcessingCurationResponse = Array<{
3639 * @param limit max number of sketches to return
3740 * @returns sketches
3841 */
39- export const getCurationSketches = async (
42+ export const getCurationSketches = memoize ( async (
4043 limit ?: number ,
4144) : Promise < OpenProcessingCurationResponse > => {
4245 const limitParam = limit ? `limit=${ limit } ` : "" ;
4346 const response = await fetch (
4447 `${ openProcessingEndpoint } curation/${ curationId } /sketches?${ limitParam } ` ,
4548 ) ;
49+ if ( ! response . ok ) { //log error instead of throwing error to not cache result in memoize
50+ console . error ( 'getCurationSketches' , response . status , response . statusText )
51+ }
4652 const payload = await response . json ( ) ;
4753 return payload as OpenProcessingCurationResponse ;
48- } ;
54+ } ) ;
4955
5056/**
5157 * API Response from a call to the Sketch endpoint
@@ -69,26 +75,50 @@ export type OpenProcessingSketchResponse = {
6975
7076/**
7177 * Get info about a specific sketch from the OpenProcessing API
78+ * First checks if the sketch is in the memoized curated sketches and returns the data if so,
79+ * Otherwise calls OpenProcessing API for this specific sketch
7280 *
7381 * https://documenter.getpostman.com/view/16936458/2s9YC1Xa6X#7cd344f6-6e87-426a-969b-2b4a79701dd1
7482 * @param id
7583 * @returns
7684 */
77- export const getSketch = memoize ( async (
78- id : string ,
79- ) : Promise < OpenProcessingSketchResponse > => {
85+ export const getSketch = memoize (
86+ async ( id : string ) : Promise < OpenProcessingSketchResponse > => {
87+ // check for memoized sketch in curation sketches
88+ const curationSketches = await getCurationSketches ( ) ;
89+ const memoizedSketch = curationSketches . find ( ( el ) => el . visualID === id ) ;
90+ if ( memoizedSketch ) {
91+ return {
92+ ...memoizedSketch ,
93+ license : "" ,
94+ } as OpenProcessingSketchResponse ;
95+ }
96+
97+ // check for sketch data in Open Processing API
8098 const response = await fetch ( `${ openProcessingEndpoint } sketch/${ id } ` ) ;
99+ if ( ! response . ok ) {
100+ //log error instead of throwing error to not cache result in memoize
101+ console . error ( "getSketch" , id , response . status , response . statusText ) ;
102+ }
81103 const payload = await response . json ( ) ;
82104 return payload as OpenProcessingSketchResponse ;
83105} ) ;
84106
107+ /**
108+ * Note: this currently calls `/api/sketch/:id/code`
109+ * But only uses the width and height properties from this call
110+ * Width and height should instead be added to properties for `/api/sketch/:id` or `api/curation/:curationId/sketches` instead
111+ */
85112export const getSketchSize = memoize ( async ( id : string ) => {
86113 const sketch = await getSketch ( id )
87114 if ( sketch . mode !== 'p5js' ) {
88115 return { width : undefined , height : undefined } ;
89116 }
90117
91118 const response = await fetch ( `${ openProcessingEndpoint } sketch/${ id } /code` ) ;
119+ if ( ! response . ok ) { //log error instead of throwing error to not cache result in memoize
120+ console . error ( 'getSketchSize' , id , response . status , response . statusText )
121+ }
92122 const payload = await response . json ( ) ;
93123
94124 for ( const tab of payload ) {
0 commit comments