github.com/0chain/gosdk@v1.17.11/wasmsdk/statusbar.go (about) 1 //go:build js && wasm 2 // +build js,wasm 3 4 package main 5 6 import ( 7 "path" 8 "sync" 9 10 "github.com/0chain/gosdk/core/sys" 11 "github.com/0chain/gosdk/zboxcore/sdk" 12 "gopkg.in/cheggaaa/pb.v1" 13 ) 14 15 // StatusBar is to check status of any operation 16 type StatusBar struct { 17 b *pb.ProgressBar 18 wg *sync.WaitGroup 19 success bool 20 err error 21 22 totalBytes int 23 completedBytes int 24 objURL string 25 localPath string 26 callback func(totalBytes int, completedBytes int, fileName, objURL, err string) 27 isRepair bool 28 totalBytesMap map[string]int 29 } 30 31 var jsCallbackMutex sync.Mutex 32 33 // Started for statusBar 34 func (s *StatusBar) Started(allocationID, filePath string, op int, totalBytes int) { 35 if logEnabled { 36 s.b = pb.StartNew(totalBytes) 37 s.b.Set(0) 38 } 39 fileName := path.Base(filePath) 40 s.totalBytes = totalBytes 41 if s.callback != nil { 42 if !s.isRepair || op == sdk.OpUpload || op == sdk.OpUpdate { 43 if s.isRepair { 44 fileName = filePath 45 } 46 jsCallbackMutex.Lock() 47 defer jsCallbackMutex.Unlock() 48 s.totalBytesMap[filePath] = totalBytes 49 s.callback(totalBytes, s.completedBytes, fileName, "", "") 50 } 51 } 52 } 53 54 // InProgress for statusBar 55 func (s *StatusBar) InProgress(allocationID, filePath string, op int, completedBytes int, todo_name_var []byte) { 56 if logEnabled && s.b != nil { 57 s.b.Set(completedBytes) 58 } 59 fileName := path.Base(filePath) 60 if s.callback != nil { 61 if !s.isRepair || op == sdk.OpUpload || op == sdk.OpUpdate { 62 if s.isRepair { 63 fileName = filePath 64 } 65 jsCallbackMutex.Lock() 66 defer jsCallbackMutex.Unlock() 67 s.callback(s.totalBytesMap[filePath], completedBytes, fileName, "", "") 68 } 69 } 70 } 71 72 // Completed for statusBar 73 func (s *StatusBar) Completed(allocationID, filePath string, filename string, mimetype string, size int, op int) { 74 if logEnabled && s.b != nil { 75 s.b.Finish() 76 } 77 s.success = true 78 79 if s.localPath != "" { 80 fs, _ := sys.Files.Open(s.localPath) 81 mf, _ := fs.(*sys.MemFile) 82 s.objURL = CreateObjectURL(mf.Buffer, mimetype) 83 } 84 if s.callback != nil { 85 if !s.isRepair || op == sdk.OpUpload || op == sdk.OpUpdate { 86 if s.isRepair { 87 filename = filePath 88 } 89 jsCallbackMutex.Lock() 90 defer jsCallbackMutex.Unlock() 91 totalBytes := s.totalBytesMap[filePath] 92 delete(s.totalBytesMap, filePath) 93 s.callback(totalBytes, totalBytes, filename, s.objURL, "") 94 } 95 } 96 if !s.isRepair { 97 defer s.wg.Done() 98 } 99 sdkLogger.Info("Status completed callback. Type = " + mimetype + ". Name = " + filename + ". URL = " + s.objURL) 100 } 101 102 // Error for statusBar 103 func (s *StatusBar) Error(allocationID string, filePath string, op int, err error) { 104 if s.b != nil { 105 s.b.Finish() 106 } 107 s.success = false 108 s.err = err 109 defer func() { 110 if r := recover(); r != nil { 111 PrintError("Recovered in statusBar Error", r) 112 } 113 }() 114 fileName := path.Base(filePath) 115 PrintError("Error in file operation." + err.Error()) 116 if s.callback != nil { 117 if !s.isRepair || op == sdk.OpUpload || op == sdk.OpUpdate { 118 if s.isRepair { 119 fileName = filePath 120 } 121 jsCallbackMutex.Lock() 122 defer jsCallbackMutex.Unlock() 123 s.callback(s.totalBytesMap[filePath], s.completedBytes, fileName, "", err.Error()) 124 } 125 } 126 if !s.isRepair { 127 s.wg.Done() 128 } 129 } 130 131 // RepairCompleted when repair is completed 132 func (s *StatusBar) RepairCompleted(filesRepaired int) { 133 s.wg.Done() 134 }