Android Question [SOLVED] How to handle multiple possible events using Resumable Sub?

Sandman

Expert
Licensed User
Longtime User
Let's say I have this code:
B4X:
Sub Class_Globals
    Private http_client As OkHttpClient
End Sub

Public Sub Initialize (req As OkHttpRequest, taskId as int)
    http_client.Initialize("http_client")
    http_client.Execute(req, taskId)
End Sub

Sub http_client_ResponseSuccess (Response As OkHttpResponse, ResponseTaskId As Int)
    ToastMessageShow("Success", False)
End Sub

Sub http_client_ResponseError (Response As OkHttpResponse, Reason As String, ResponseStatusCode As Int, ResponseTaskId As Int)
    ToastMessageShow("Error: " & Reason, False)
End Sub

So basically I'm doing something that can result in one of two events in the end, either the ResponseSuccess or the ResponseError.

Considering that we use a single event signature in the Wait For, how can I use a resumable sub in this scenario and handle two possible events?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Thank you @DonManfred , but I'm sure you understood that I actually asked about the concept, not any kind of http clients. But, to clarify, here's an adjusted example:

Let's say I have this code:
B4X:
Sub Class_Globals
    Private someJob As Something
End Sub

Public Sub Initialize (data as String)
    someJob.Initialize("someJob", Me)
    someJob.Process(data)
End Sub

Sub someJob_Success (Result As String)
    ToastMessageShow("Success: " & Result, False)
End Sub

Sub someJob_Error (ErrorMessage as String)
    ToastMessageShow("Error: " & ErrorMessage, False)
End Sub

So basically I'm doing something that can result in one of two events in the end, either the Success or the Error.

Considering that we use a single event signature in the Wait For, how can I use a resumable sub in this scenario and handle two possible events?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Maybe this:
B4X:
Sub Class_Globals
   Private someJob As Something
End Sub

Public Sub Initialize (data As String)
   someJob.Initialize("someJob", Me)
   someJob.Process(data)
   Wait For i_am_done
End Sub

Sub someJob_Success (Result As String)
   ToastMessageShow("Success: " & Result, False)
   CallSubDelayed(Me, "i_am_done")
End Sub

Sub someJob_Error (ErrorMessage As String)
   ToastMessageShow("Error: " & ErrorMessage, False)
   CallSubDelayed(Me, "i_am_done")
End Sub
[code]
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I don't think that's quite it. But in any case that would still leave the event subs.

I know it doesn't work like below, but it would be easier for me to understand if it was something like that. Is there some B4A-way of doing it and getting rid of the event subs? (I understand that I could wrap Something in a layer to achieve sort of this, but I'm talking about what the language itself allows.)

B4X:
Sub Class_Globals
    Private someJob As Something
End Sub

Public Sub Initialize (data as String)

    someJob.Initialize("", Me)

    Wait For (someJob.Process(data)) Complete (Event as String, Data as Map)

    Select Event

        Case "Success"
            ToastMessageShow("Success: " & Data.Get("Result"), False)

        Case "Error"
            ToastMessageShow("Error: " & Data.Get("ErrorMessage"), False)

        Case Else
            ToastMessageShow("Unhandled event: " & Event, False)

    End Select

End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
That's weird (your code sample) because I'm just doing something like that for a project I'm working on (wrapping an event routine). I even thought about a return example like yours, but since only one item can be returned, I thought about wrapping the multiple returns in a list:

B4X:
Sub Class_Globals
   Private someJob As Something
End Sub

Public Sub Initialize (data As String)

   someJob.Initialize("", Me)

   Wait For (someJob.Process(data)) Complete (Returns As List)

   Dim Event As String = Returns.Get(0)
   Dim data As Map = Returns.Get(1)
   
   Select Event

     Case "Success"
       ToastMessageShow("Success: " & data.Get("Result"), False)

     Case "Error"
       ToastMessageShow("Error: " & data.Get("ErrorMessage"), False)

     Case Else
       ToastMessageShow("Unhandled event: " & Event, False)

   End Select

End Sub

It'll be a week before I'm done with my test project (multiple hats) and I may post the results here in the forum.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Considering that we use a single event signature in the Wait For, how can I use a resumable sub in this scenario and handle two possible events?
Wait For waits for a single event. If you need to wait for multiple events then you can delegate the events as Oliver did.

Libraries developers should design the API with this in mind and prefer raising a single event with a parameter over raising different events.
 
Upvote 0
Top