1- local utils = require " .utils.utils"
2-
31local mod = {}
42
53-- Add or remove a user from the queue in the controller
@@ -46,20 +44,33 @@ function mod.setQueued(address, queued)
4644 }
4745end
4846
49- -- Make a handle function use the global queue of the controller
50- --- @param handle HandlerFunction Handle function to wrap
51- --- @param errorHandler fun ( msg : Message , env : Message , err : unknown )? Optional error handler
52- --- @return HandlerFunction
53- function mod .useQueue (handle , errorHandler )
54- return function (msg , env )
55- -- default sender of the interaction is the message sender
56- local sender = msg .From
57- local isCreditNotice = msg .Tags .Action == " Credit-Notice"
58-
59- -- if the message is a credit notice, update the sender
60- if isCreditNotice then
61- sender = msg .Tags .Sender
62- end
47+ -- Get the user to be queued, depending on the type of the message
48+ --- @param msg Message Message to process
49+ --- @return string
50+ function mod .getUserToQueue (msg )
51+ -- return sender if it is a credit notice
52+ if msg .Tags .Action == " Credit-Notice" then
53+ return msg .Tags .Sender
54+ end
55+
56+ -- the msg sender should be queued if it is not a credit-notice
57+ return msg .From
58+ end
59+
60+ -- Make a handler use the global queue of the controller. This is
61+ -- usually needed for handlers that impelement complex behavior by
62+ -- waiting for several message responses before completion to prevent
63+ -- double spending
64+ --- @param config table Configuration for the handler
65+ --- @return table
66+ function mod .useQueue (config )
67+ -- original handle function and error handler
68+ local handle = config .handle
69+ local errorHandler = config .errorHandler
70+
71+ -- override handle function to queue and unqueue
72+ config .handle = function (msg , env )
73+ local sender = mod .getUserToQueue (msg )
6374
6475 -- update and set queue
6576 local res = mod .setQueued (sender , true ).receive ()
@@ -76,7 +87,10 @@ function mod.useQueue(handle, errorHandler)
7687 errorHandler (msg , env , err )
7788 else
7889 -- no error handler, throw the error
79- error (err )
90+ -- do not error() here - that would trigger
91+ -- the handler's error handler, which would
92+ -- try to unqueue the user
93+ Handlers .defaultErrorHandler (msg , env , err )
8094 end
8195
8296 return
@@ -99,6 +113,26 @@ function mod.useQueue(handle, errorHandler)
99113 end
100114 end
101115 end
116+
117+ -- override error handler to unqueue the user
118+ config .errorHandler = function (msg , env , err )
119+ -- call wrapped error handler if provided
120+ if errorHandler ~= nil then
121+ errorHandler (msg , env , err or " Unknown error" )
122+ else
123+ Handlers .defaultErrorHandler (msg , env , err )
124+ end
125+
126+ -- get user to unqueue
127+ local sender = mod .getUserToQueue (msg )
128+
129+ -- unqueue and notify if it failed
130+ mod
131+ .setQueued (sender , false )
132+ .notifyOnFailedQueue ()
133+ end
134+
135+ return config
102136end
103137
104138return mod
0 commit comments