From b0cbf93b7ef77e42d6bca3c174f3a1cd4660fa6a Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Tue, 5 Jul 2016 01:09:51 +0300 Subject: [PATCH] Use a callback in client:sync() to determine when to stop the loop Instead of adding more and more possible conditions on which the while-loop inside client:sync() could stop, support passing a callback function which is used to determine when to stop the loop. The old "so N iterations behaviour" can be achieved with something like: function iterate_n_times(n) return function () n = n - 1 return n == 0 end end client:sync(iterate_n_times(10)) If no function is passed, the client:sync() function loops forever. --- matrix/client.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/matrix/client.lua b/matrix/client.lua index 30ac5e3..0c8ae0c 100644 --- a/matrix/client.lua +++ b/matrix/client.lua @@ -429,12 +429,16 @@ function Client:_sync(options) end end -function Client:sync(niters) - while niters == nil or niters > 0 do - self:_sync { timeout = 15000 } - if niters then - niters = niters - 1 - end +local function return_false() + return false +end + +function Client:sync(stop, timeout) + if not stop then + stop = return_false + end + while not stop(self) do + self:_sync { timeout = timeout or 15000 } end end