Skip to content

Commit 07634e9

Browse files
feat: add synchronous mode to SSE::Client
1 parent e54bf5f commit 07634e9

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/ld-eventsource/client.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Client
5252
# The default HTTP method for requests.
5353
DEFAULT_HTTP_METHOD = "GET"
5454

55+
THREAD_NAME = 'LD/SSEClient'
56+
5557
#
5658
# Creates a new SSE client.
5759
#
@@ -97,6 +99,9 @@ class Client
9799
# @param http_client_options [Hash] (nil) additional options to pass to
98100
# the HTTP client, such as `socket_factory` or `proxy`. These settings will override
99101
# the socket factory and proxy settings.
102+
# @param async [Boolean] (true) whether to run the stream in a separate thread
103+
# if true, the stream will be run in a separate thread
104+
# if false, the stream will be run in the current thread
100105
# @yieldparam [Client] client the new client instance, before opening the connection
101106
#
102107
def initialize(uri,
@@ -112,7 +117,8 @@ def initialize(uri,
112117
method: DEFAULT_HTTP_METHOD,
113118
payload: nil,
114119
retry_enabled: true,
115-
http_client_options: nil)
120+
http_client_options: nil,
121+
async: true)
116122
@uri = URI(uri)
117123
@stopped = Concurrent::AtomicBoolean.new(false)
118124
@retry_enabled = retry_enabled
@@ -167,7 +173,11 @@ def initialize(uri,
167173

168174
yield self if block_given?
169175

170-
Thread.new { run_stream }.name = 'LD/SSEClient'
176+
if async
177+
Thread.new { run_stream }.name = THREAD_NAME
178+
else
179+
run_stream
180+
end
171181
end
172182

173183
#

0 commit comments

Comments
 (0)