Skip to content

Instantly share code, notes, and snippets.

@psylone
Last active September 13, 2021 14:41
Show Gist options
  • Save psylone/b800d4126d0f3d62a18562715c397db9 to your computer and use it in GitHub Desktop.
Save psylone/b800d4126d0f3d62a18562715c397db9 to your computer and use it in GitHub Desktop.
A very simple Elixir's GenServer
class Server
def initialize
@queue = Queue.new
end
def self.start
Server.new.tap(&:start)
end
def start
Thread.new do
loop do
process(@queue.pop)
end
end
end
def push(value)
@queue.push(value)
value
end
private
def process(value)
puts "received: #{value}"
end
end
# s = Server.start
# => #<Server:0x00007fc71183c478 @queue=#<Thread::Queue:0x00007fc71183c3d8>>
#
# s.push(green: 1, blue: 2, white: 3)
# => {:green=>1, :blue=>2, :white=>3}
#
# received: {:green=>1, :blue=>2, :white=>3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment