Class: Estore::Session

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/estore/session.rb

Overview

The Session class is responsible for maintaining a full-duplex connection between the client and the Event Store server. An Estore session is thread-safe, and it is recommended to only have one instance per application.

All operations are handled fully asynchronously, returning a promise. If you need to execute synchronously, simply call .sync on the returned promise.

To get maximum performance from the connection, it is recommended to use it asynchronously.

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 2113) ⇒ Session

Returns a new instance of Session.



20
21
22
# File 'lib/estore/session.rb', line 20

def initialize(host, port = 2113)
  @connection = Connection.new(host, port)
end

Instance Method Details

#append(stream, events, options = {}) ⇒ Object



51
52
53
# File 'lib/estore/session.rb', line 51

def append(stream, events, options = {})
  command(Commands::Append, stream, events, options).call
end

#closeObject



24
25
26
# File 'lib/estore/session.rb', line 24

def close
  @connection.close
end

#pingObject



28
29
30
# File 'lib/estore/session.rb', line 28

def ping
  command(Commands::Ping)
end

#read(stream, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/estore/session.rb', line 32

def read(stream, options = {})
  from = options[:from] || 0
  limit = options[:limit]

  if limit
    read_batch(stream, from, limit)
  else
    read_forward(stream, from)
  end
end

#read_batch(stream, from, limit) ⇒ Object



43
44
45
# File 'lib/estore/session.rb', line 43

def read_batch(stream, from, limit)
  command(Commands::ReadBatch, stream, from, limit).call
end

#read_forward(stream, from, batch_size = nil, &block) ⇒ Object



47
48
49
# File 'lib/estore/session.rb', line 47

def read_forward(stream, from, batch_size = nil, &block)
  command(Commands::ReadForward, stream, from, batch_size, &block).call
end

#subscription(stream, options = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/estore/session.rb', line 55

def subscription(stream, options = {})
  if options[:from]
    command(Commands::CatchUpSubscription, stream, options[:from], options)
  else
    command(Commands::Subscription, stream, options)
  end
end