@@ -19,6 +19,7 @@ import (
1919 "context"
2020 "errors"
2121 "fmt"
22+ "iter"
2223 "log/slog"
2324 "sync"
2425
@@ -125,40 +126,42 @@ func (a *ArduinoPlatformUpdater) ListUpgradablePackages(ctx context.Context, _ f
125126}
126127
127128// UpgradePackages implements ServiceUpdater.
128- func (a * ArduinoPlatformUpdater ) UpgradePackages (ctx context.Context , names []string ) (<- chan update.Event , error ) {
129+ func (a * ArduinoPlatformUpdater ) UpgradePackages (ctx context.Context , names []string ) (iter. Seq [ update.Event ] , error ) {
129130 if ! a .lock .TryLock () {
130131 return nil , update .ErrOperationAlreadyInProgress
131132 }
132- eventsCh := make (chan update.Event , 100 )
133133
134- downloadProgressCB := func (curr * rpc.DownloadProgress ) {
135- data := helpers .ArduinoCLIDownloadProgressToString (curr )
136- slog .Debug ("Download progress" , slog .String ("download_progress" , data ))
137- eventsCh <- update .NewDataEvent (update .UpgradeLineEvent , data )
138- }
139- taskProgressCB := func (msg * rpc.TaskProgress ) {
140- data := helpers .ArduinoCLITaskProgressToString (msg )
141- slog .Debug ("Task progress" , slog .String ("task_progress" , data ))
142- eventsCh <- update .NewDataEvent (update .UpgradeLineEvent , data )
143- }
144-
145- go func () {
134+ return func (yield func (update.Event ) bool ) {
146135 defer a .lock .Unlock ()
147- defer close (eventsCh )
148136
149- eventsCh <- update .NewDataEvent (update .StartEvent , "Upgrade is starting" )
137+ downloadProgressCB := func (curr * rpc.DownloadProgress ) {
138+ data := helpers .ArduinoCLIDownloadProgressToString (curr )
139+ slog .Debug ("Download progress" , slog .String ("download_progress" , data ))
140+ // TODO: add termination
141+ _ = yield (update .NewDataEvent (update .UpgradeLineEvent , data ))
142+ }
143+ taskProgressCB := func (msg * rpc.TaskProgress ) {
144+ data := helpers .ArduinoCLITaskProgressToString (msg )
145+ slog .Debug ("Task progress" , slog .String ("task_progress" , data ))
146+ // TODO: add termination
147+ _ = yield (update .NewDataEvent (update .UpgradeLineEvent , data ))
148+ }
149+
150+ if ! yield (update .NewDataEvent (update .StartEvent , "Upgrade is starting" )) {
151+ return
152+ }
150153
151154 logrus .SetLevel (logrus .ErrorLevel ) // Reduce the log level of arduino-cli
152155 srv := commands .NewArduinoCoreServer ()
153156
154157 if err := setConfig (ctx , srv ); err != nil {
155- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error setting config: %w" , err ))
158+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error setting config: %w" , err ) ))
156159 return
157160 }
158161
159162 var inst * rpc.Instance
160163 if resp , err := srv .Create (ctx , & rpc.CreateRequest {}); err != nil {
161- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error creating arduino-cli instance: %w" , err ))
164+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error creating arduino-cli instance: %w" , err ) ))
162165 return
163166 } else {
164167 inst = resp .GetInstance ()
@@ -174,11 +177,11 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
174177 {
175178 stream , _ := commands .UpdateIndexStreamResponseToCallbackFunction (ctx , downloadProgressCB )
176179 if err := srv .UpdateIndex (& rpc.UpdateIndexRequest {Instance : inst }, stream ); err != nil {
177- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error updating index: %w" , err ))
180+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error updating index: %w" , err ) ))
178181 return
179182 }
180183 if err := srv .Init (& rpc.InitRequest {Instance : inst }, commands .InitStreamResponseToCallbackFunction (ctx , nil )); err != nil {
181- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error initializing instance: %w" , err ))
184+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error initializing instance: %w" , err ) ))
182185 return
183186 }
184187 }
@@ -200,13 +203,13 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
200203 ); err != nil {
201204 var alreadyPresent * cmderrors.PlatformAlreadyAtTheLatestVersionError
202205 if errors .As (err , & alreadyPresent ) {
203- eventsCh <- update .NewDataEvent (update .UpgradeLineEvent , alreadyPresent .Error ())
206+ _ = yield ( update .NewDataEvent (update .UpgradeLineEvent , alreadyPresent .Error () ))
204207 return
205208 }
206209
207210 var notFound * cmderrors.PlatformNotFoundError
208211 if ! errors .As (err , & notFound ) {
209- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error upgrading platform: %w" , err ))
212+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error upgrading platform: %w" , err ) ))
210213 return
211214 }
212215 // If the platform is not found, we will try to install it
@@ -223,16 +226,19 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
223226 ),
224227 )
225228 if err != nil {
226- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error installing platform: %w" , err ))
229+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error installing platform: %w" , err ) ))
227230 return
228231 }
229232 } else if respCB ().GetPlatform () == nil {
230- eventsCh <- update .NewErrorEvent (fmt .Errorf ("platform upgrade failed" ))
233+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("platform upgrade failed" ) ))
231234 return
232235 }
233236
234237 cbw := orchestrator .NewCallbackWriter (func (line string ) {
235- eventsCh <- update .NewDataEvent (update .UpgradeLineEvent , line )
238+ // TODO: add termination
239+ if ! yield (update .NewDataEvent (update .UpgradeLineEvent , line )) {
240+ return
241+ }
236242 })
237243
238244 err := srv .BurnBootloader (
@@ -244,10 +250,8 @@ func (a *ArduinoPlatformUpdater) UpgradePackages(ctx context.Context, names []st
244250 commands .BurnBootloaderToServerStreams (ctx , cbw , cbw ),
245251 )
246252 if err != nil {
247- eventsCh <- update .NewErrorEvent (fmt .Errorf ("error burning bootloader: %w" , err ))
253+ _ = yield ( update .NewErrorEvent (fmt .Errorf ("error burning bootloader: %w" , err ) ))
248254 return
249255 }
250- }()
251-
252- return eventsCh , nil
256+ }, nil
253257}
0 commit comments