Last active
September 13, 2021 14:41
-
-
Save psylone/b800d4126d0f3d62a18562715c397db9 to your computer and use it in GitHub Desktop.
A very simple Elixir's GenServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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