@@ -79,31 +79,36 @@ async function fetchCode ({ url, fromLine, toLine }: GitHubReference, fetchResul
7979 } )
8080}
8181
82+ function codeReducer ( prevState : any , { type, value } : DispatchMessage ) {
83+ switch ( type ) {
84+ case 'reset' : {
85+ return initialFetchResultState ;
86+ }
87+ case 'loading' : {
88+ return { ...prevState , loading : true } ;
89+ }
90+ case 'loaded' : {
91+ return { ...prevState , code : value , loading : false } ;
92+ }
93+ case 'error' : {
94+ return { ...prevState , error : value , loading : false } ;
95+ }
96+ default :
97+ return prevState ;
98+ }
99+ }
100+
82101function ReferenceCode ( props : ReferenceCodeBlockProps ) {
83102 const [ fetchResultState , fetchResultStateDispatcher ] = useReducer (
84- ( prevState : any , { type, value } : DispatchMessage ) => {
85- switch ( type ) {
86- case 'reset' : {
87- return initialFetchResultState ;
88- }
89- case 'loading' : {
90- return { ...prevState , loading : true } ;
91- }
92- case 'loaded' : {
93- return { ...prevState , code : value , loading : false } ;
94- }
95- case 'error' : {
96- return { ...prevState , error : value , loading : false } ;
97- }
98- default :
99- return prevState ;
100- }
101- } ,
103+ codeReducer ,
102104 initialFetchResultState ,
103105 )
104106
105107 const codeSnippetDetails = parseReference ( props . children )
106- fetchCode ( codeSnippetDetails , fetchResultStateDispatcher )
108+ if ( fetchResultState . loading !== false ) {
109+ fetchCode ( codeSnippetDetails , fetchResultStateDispatcher )
110+ }
111+
107112 const customProps = {
108113 ...props ,
109114 metastring : ` title="${ codeSnippetDetails . title } "` ,
0 commit comments