diff --git a/examples/hello-world/Gemfile b/examples/hello-world/Gemfile new file mode 100644 index 0000000..27d6a9d --- /dev/null +++ b/examples/hello-world/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'puma' +gem 'rack' +# gem 'datastar' +gem 'datastar', path: '../../../sdk/ruby' diff --git a/examples/hello-world/Gemfile.lock b/examples/hello-world/Gemfile.lock new file mode 100644 index 0000000..cdb417b --- /dev/null +++ b/examples/hello-world/Gemfile.lock @@ -0,0 +1,29 @@ +PATH + remote: ../../../sdk/ruby + specs: + datastar (1.0.0.beta.3) + json + logger + rack (>= 3.1.14) + +GEM + remote: https://rubygems.org/ + specs: + json (2.12.2) + logger (1.7.0) + nio4r (2.7.4) + puma (6.6.0) + nio4r (~> 2.0) + rack (3.1.16) + +PLATFORMS + arm64-darwin-24 + ruby + +DEPENDENCIES + datastar! + puma + rack + +BUNDLED WITH + 2.6.3 diff --git a/examples/hello-world/hello-world.html b/examples/hello-world/hello-world.html new file mode 100644 index 0000000..2fd7e90 --- /dev/null +++ b/examples/hello-world/hello-world.html @@ -0,0 +1,35 @@ + + + + +
+
+ + SSE events will be streamed from the backend to the frontend. +
+Slow thread: waiting
+Fast thread: waiting
+Disconnected...
+ + +HTML + +trap('INT') { exit } + +run do |env| + # Initialize Datastar with callbacks + datastar = Datastar + .from_rack_env(env) + .on_connect do |sse| + sse.patch_elements(%(Connected...
)) + p ['connect', sse] + end.on_server_disconnect do |sse| + sse.patch_elements(%(Done...
)) + p ['server disconnect', sse] + end.on_client_disconnect do |socket| + p ['client disconnect', socket] + end.on_error do |error| + p ['exception', error] + puts error.backtrace.join("\n") + end + + if datastar.sse? + # This will run in its own thread / fiber + datastar.stream do |sse| + 11.times do |i| + sleep 1 + # Raising an error to demonstrate error handling + # raise ArgumentError, 'This is an error' if i > 5 + + sse.patch_elements(%(#{i})) + end + end + + # Another thread / fiber + datastar.stream do |sse| + 1000.times do |i| + sleep 0.01 + sse.patch_elements(%(#{i})) + end + end + else + [200, { 'content-type' => 'text/html' }, [INDEX]] + end +end