1- import { computed , reactive } from "domily" ;
1+ import { reactive , type Ref , ref } from "domily" ;
2+
3+ class Req implements Disposable {
4+ url : string ;
5+ loading : Ref < boolean > ;
6+ reqInit ?: RequestInit ;
7+ response ?: Response ;
8+ constructor ( url : string , req ?: RequestInit & { loading ?: Ref < boolean > } ) {
9+ const { loading, ...reqInit } = req || { } ;
10+ this . url = url ;
11+ this . loading = loading || ref ( false ) ;
12+ this . reqInit = reqInit ;
13+ loading . value = true ;
14+ }
15+
16+ async fetch ( ) {
17+ this . response = await fetch ( this . url , this . reqInit ) ;
18+ }
19+
20+ [ Symbol . dispose ] ( ) {
21+ this . loading . value = false ;
22+ }
23+ }
224
325export default function MapList ( ) {
26+ const loading = ref ( false ) ;
427 const state = reactive < {
5- loading : boolean ;
628 data : {
729 map : Map < string , any > ;
830 array : string [ ] ;
931 set : Set < string > ;
1032 } ;
1133 key : number ;
1234 } > ( {
13- loading : false ,
1435 data : {
1536 map : new Map ( ) ,
1637 array : [ ] ,
@@ -31,35 +52,23 @@ export default function MapList() {
3152 } ) ;
3253
3354 const getData = async ( ) => {
34- state . loading = true ;
35- try {
36- const rs = await fetch ( "https://api.pearktrue.cn/api/countdownday/" ) ;
37- const json = ( await rs . json ( ) ) as { data : string [ ] } ;
38- style . color = "red" ;
39- state . data . map = new Map (
40- json . data . map ( ( item , index ) => [ `${ index } ` , item ] )
41- ) ;
42- state . data . set = new Set ( json . data ) ;
43- state . data . array = json . data ;
44- } finally {
45- state . loading = false ;
46- }
55+ await using req = new Req ( "https://api.pearktrue.cn/api/countdownday/" , {
56+ loading,
57+ } ) ;
58+ await req . fetch ( ) ;
59+ const json = ( await req . response . json ( ) ) as { data : string [ ] } ;
60+ style . color = "red" ;
61+ state . data . map = new Map (
62+ json . data . map ( ( item , index ) => [ `${ index } ` , item ] ) ,
63+ ) ;
64+ state . data . set = new Set ( json . data ) ;
65+ state . data . array = json . data ;
4766 } ;
4867
49- const mapText = computed ( ( ) => {
50- if ( state . loading ) {
51- return "Loading..." ;
52- }
53- const data = Array . from ( state . data . map . values ( ) ) ;
54- return JSON . stringify ( data , null , 2 ) ;
55- } ) ;
56-
57- const setText = ( ) => {
58- if ( state . loading ) {
59- return "Loading..." ;
60- }
61- const data = Array . from ( state . data . set . values ( ) ) ;
62- return JSON . stringify ( data , null , 2 ) ;
68+ const Loading = {
69+ tag : "div" ,
70+ domIf : ( ) => loading . value ,
71+ text : "Loading..." ,
6372 } ;
6473
6574 return {
@@ -92,7 +101,7 @@ export default function MapList() {
92101 click : ( ) => {
93102 state . data . array . shift ( ) ;
94103 state . data . map = new Map (
95- state . data . array . map ( ( item , index ) => [ `${ index } ` , item ] )
104+ state . data . array . map ( ( item , index ) => [ `${ index } ` , item ] ) ,
96105 ) ;
97106 state . data . set = new Set ( state . data . array ) ;
98107 } ,
@@ -107,7 +116,7 @@ export default function MapList() {
107116 const value = ( Math . random ( ) * 5000 ) . toFixed ( 0 ) + "-" + Date . now ( ) ;
108117 state . data . array . unshift ( value ) ;
109118 state . data . map = new Map (
110- state . data . array . map ( ( item , index ) => [ `${ index } ` , item ] )
119+ state . data . array . map ( ( item , index ) => [ `${ index } ` , item ] ) ,
111120 ) ;
112121 state . data . set = new Set ( state . data . array ) ;
113122 } ,
@@ -178,11 +187,27 @@ export default function MapList() {
178187 tag : "h3" ,
179188 text : "Map Data" ,
180189 } ,
190+ Loading ,
181191 {
182192 tag : "div" ,
183193 style :
184194 'white-space: pre-wrap; font-family: "Courier New", Courier, monospace;' ,
185- text : mapText ,
195+ children : [
196+ {
197+ tag : "ul" ,
198+ domIf : ( ) => ! loading . value ,
199+ mapList : {
200+ list : ( ) => state . data . map ,
201+ map : ( [ index , item ] ) => {
202+ return {
203+ tag : "li" ,
204+ key : `list-${ index } ` ,
205+ text : item ,
206+ } ;
207+ } ,
208+ } ,
209+ } ,
210+ ] ,
186211 } ,
187212 ] ,
188213 } ,
@@ -195,11 +220,27 @@ export default function MapList() {
195220 tag : "h3" ,
196221 text : "Set Data" ,
197222 } ,
223+ Loading ,
198224 {
199225 tag : "div" ,
200226 style :
201227 'white-space: pre-wrap; font-family: "Courier New", Courier, monospace;' ,
202- text : setText ,
228+ children : [
229+ {
230+ tag : "ul" ,
231+ domIf : ( ) => ! loading . value ,
232+ mapList : {
233+ list : ( ) => state . data . set ,
234+ map : ( item : string , index : number ) => {
235+ return {
236+ tag : "li" ,
237+ key : `list-${ index } ` ,
238+ text : item ,
239+ } ;
240+ } ,
241+ } ,
242+ } ,
243+ ] ,
203244 } ,
204245 ] ,
205246 } ,
@@ -212,14 +253,10 @@ export default function MapList() {
212253 tag : "h3" ,
213254 text : "Array Data" ,
214255 } ,
215- {
216- tag : "div" ,
217- domIf : ( ) => state . loading ,
218- text : "Loading..." ,
219- } ,
256+ Loading ,
220257 {
221258 tag : "ul" ,
222- domIf : ( ) => ! state . loading ,
259+ domIf : ( ) => ! loading . value ,
223260 mapList : {
224261 list : ( ) => state . data . array ,
225262 map : ( item : string , index : number ) => {
0 commit comments