@@ -120,6 +120,7 @@ import type {
120120 DownloadProgress ,
121121} from " ~/types/docker" ;
122122
123+ // #region State
123124const imageName = ref (" " );
124125const tag = ref (" " );
125126const loading = ref (false );
@@ -137,7 +138,9 @@ const downloadSummary = ref<{
137138 downloaded: number ;
138139} | null >(null );
139140const activeEventSource = ref <EventSource | null >(null );
141+ // #endregion
140142
143+ // #region Computed
141144const canPull = computed (
142145 () =>
143146 imageName .value &&
@@ -149,29 +152,46 @@ const canPull = computed(
149152const canFetchPlatforms = computed (
150153 () => imageName .value && tag .value && ! loading .value
151154);
155+ // #endregion
152156
153- const resetState = () => {
157+ // #region Reset Helpers
158+ const clearActiveEventSource = () => {
154159 if (activeEventSource .value ) {
155160 activeEventSource .value .close ();
156161 activeEventSource .value = null ;
157162 }
163+ };
164+
165+ const resetDownloadState = () => {
158166 activeDownloads .value .clear ();
159- platforms .value = [];
160- selectedPlatform .value = null ;
161- error .value = " " ;
162167 downloadProgress .value = {};
163168 downloadComplete .value = false ;
164169 downloadSummary .value = null ;
170+ currentManifest .value = null ;
171+ };
172+
173+ const resetSelectionState = () => {
174+ platforms .value = [];
175+ selectedPlatform .value = null ;
176+ };
177+
178+ const resetAllState = () => {
179+ clearActiveEventSource ();
180+ resetDownloadState ();
181+ resetSelectionState ();
182+ error .value = " " ;
165183};
184+ // #endregion
166185
167186watch ([imageName , tag ], () => {
168- resetState ();
187+ resetAllState ();
169188});
170189
171190const handlePlatformSelect = (platform : DockerPlatform ) => {
172191 selectedPlatform .value = platform ;
173192};
174193
194+ // #region API
175195const fetchToken = async () => {
176196 const response = await $fetch <TokenResponse >(" /api/docker/token" , {
177197 params: {
@@ -204,7 +224,9 @@ const fetchManifestDetail = async (targetManifest: any) => {
204224 },
205225 });
206226};
227+ // #endregion
207228
229+ // #region Download
208230const handleDownloadProgress = async (eventSource : EventSource , layers : any []) => {
209231 return new Promise ((resolve , reject ) => {
210232 eventSource .onmessage = async (event ) => {
@@ -275,7 +297,9 @@ const downloadAllLayers = async (layers: any[]) => {
275297
276298 await handleDownloadProgress (eventSource , layers );
277299};
300+ // #endregion
278301
302+ // #region Assemble
279303const assembleImage = async () => {
280304 try {
281305 const response = await $fetch (" /api/docker/assemble-image" , {
@@ -309,16 +333,15 @@ const assembleImage = async () => {
309333 error .value = " 组装镜像失败" ;
310334 }
311335};
336+ // #endregion
312337
338+ // #region Actions
313339const handlePull = async () => {
314340 if (! canPull .value ) return ;
315341
316342 loading .value = true ;
317343 error .value = " " ;
318- downloadProgress .value = {};
319- currentManifest .value = null ;
320- downloadComplete .value = false ;
321- downloadSummary .value = null ;
344+ resetDownloadState ();
322345
323346 try {
324347 await fetchToken ();
@@ -354,9 +377,9 @@ const initPlatforms = async () => {
354377
355378 loading .value = true ;
356379 error .value = " " ;
357- platforms . value = [] ;
358- selectedPlatform . value = null ;
359-
380+ resetSelectionState () ;
381+ resetDownloadState () ;
382+
360383 try {
361384 await fetchToken ();
362385 const manifests = await fetchManifests ();
@@ -368,11 +391,11 @@ const initPlatforms = async () => {
368391 loading .value = false ;
369392 }
370393};
394+ // #endregion
371395
396+ // #region Lifecycle
372397onBeforeUnmount (() => {
373- if (activeEventSource .value ) {
374- activeEventSource .value .close ();
375- activeEventSource .value = null ;
376- }
398+ clearActiveEventSource ();
377399});
400+ // #endregion
378401 </script >
0 commit comments