Something went wrong. Try again.
Postman-like nvim client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114local M = {}
---@type Request|nillocal current_request = nil
---@type Response|nillocal current_response = nil
---@type booleanlocal is_loading = false
---@type string|nillocal error_message = nil
---@type function[]local listeners = {}
---Register a listener callback for state changes---@param callback function Callback function(state_key, new_value, old_value)local function subscribe(callback) table.insert(listeners, callback)end
---Notify all listeners of a state change---@param key string State key that changed---@param new_value any New value---@param old_value any Previous valuelocal function notify(key, new_value, old_value) for _, callback in ipairs(listeners) do callback(key, new_value, old_value) endend
---Set the current request---@param request Request|nilfunction M.set_request(request) local old = current_request current_request = request notify("request", request, old)end
---Get the current request---@return Request|nilfunction M.get_request() return current_requestend
---Set the current response---@param response Response|nilfunction M.set_response(response) local old = current_response current_response = response notify("response", response, old)end
---Get the current response---@return Response|nilfunction M.get_response() return current_responseend
---Set loading state---@param loading booleanfunction M.set_loading(loading) local old = is_loading is_loading = loading notify("loading", loading, old)end
---Get loading state---@return booleanfunction M.is_loading() return is_loadingend
---Set error message---@param error string|nilfunction M.set_error(error) local old = error_message error_message = error notify("error", error, old)end
---Get error message---@return string|nilfunction M.get_error() return error_messageend
---Get all state---@return tablefunction M.get_all() return { request = current_request, response = current_response, is_loading = is_loading, error = error_message, }end
---Reset all statefunction M.reset() current_request = nil current_response = nil is_loading = false error_message = nil notify("reset", {}, nil)end
---Subscribe to state changesM.subscribe = subscribe
return M