event_store_client 0.1.15 → 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc9b72a371be013731424338e4faed48bbd625f39a4e18a1b77a8f32fbc0b16d
4
- data.tar.gz: 97c351e9404da7efa5add4fd1375d35cca4d1bbc133cfe80704614f86ec2ec5c
3
+ metadata.gz: b27db234d64f8cbe377e6eb65b5b194c7b42178bf4201323baae5ac6a7e99f57
4
+ data.tar.gz: b4df0eaec15c72dbaeccd1fa64485a23df385f889e5d448e1cd24be96913ed65
5
5
  SHA512:
6
- metadata.gz: cb3e3b0834d95a0a4d3139b7499360f555e53f60256e5b71b3993db2deca3a0904c524f958b91bbec8243b6d680c7705af5e100a714af919db3250ca4873a1d7
7
- data.tar.gz: cb344c5a518110c8d9c22bfb0fba2ea107c20b2dd0e7566f688a9e1ec0bd6a9694e13a5df92eacf70ebb27bce901f7930dc03bb0fc4c7b6d876a4d90df9549e2
6
+ metadata.gz: 5c0f0760c3c0ce01b39dc8c9501b8112c38f911ab4415bd64fda77cda17d0e5684d4f8d362d3f1728939a6ad85b88bba3a71a2161d3399999ea425a781893564
7
+ data.tar.gz: 7a5d1d6983f90d6ad38c4b57323f4290934f38e687b1302e712b9a0723b26d873ce49128d3341a6334ac62abe0c8896608943012b679d4788bac778bcdc35742
@@ -6,11 +6,7 @@ module EventStoreClient
6
6
  subscriptions.each do |subscription|
7
7
  new_events = connection.consume_feed(subscription.stream, subscription.name)
8
8
  next if new_events.none?
9
- new_events.each do |event|
10
- subscription.subscribers.each do |subscriber|
11
- subscriber.call(event)
12
- end
13
- end
9
+ new_events.each { |event| subscription.subscriber.call(event) }
14
10
  end
15
11
  end
16
12
 
@@ -19,6 +19,10 @@ module EventStoreClient
19
19
 
20
20
  def delete_stream(stream); end
21
21
 
22
+ def join_streams(name, streams)
23
+ client.join_streams(name, streams)
24
+ end
25
+
22
26
  def subscribe(stream, name:)
23
27
  client.subscribe_to_stream(stream, name)
24
28
  end
@@ -27,18 +31,12 @@ module EventStoreClient
27
31
  response = client.consume_feed(stream, subscription)
28
32
  return [] unless response.body
29
33
  body = JSON.parse(response.body)
30
- ack_uri =
31
- body['links'].find { |link| link['relation'] == 'ackAll' }.
32
- try(:[], 'uri')
34
+
35
+ ack = body['links'].find { |link| link['relation'] == 'ackAll' }
36
+ return unless ack
37
+ ack_uri = ack['uri']
33
38
  events = body['entries'].map do |entry|
34
- event = EventStoreClient::Event.new(
35
- id: entry['eventId'],
36
- title: entry['title'],
37
- type: entry['eventType'],
38
- data: entry['data'] || '{}',
39
- metadata: entry['isMetaData'] ? entry['metaData'] : '{}'
40
- )
41
- mapper.deserialize(event)
39
+ deserialize_event(entry)
42
40
  end
43
41
  client.ack(ack_uri)
44
42
  events
@@ -112,9 +110,10 @@ module EventStoreClient
112
110
  id: entry['eventId'],
113
111
  title: entry['title'],
114
112
  type: entry['eventType'],
115
- data: entry['data'],
116
- metadata: entry['metaData']
113
+ data: entry['data'] || '{}',
114
+ metadata: entry['isMetaData'] ? entry['metaData'] : '{}'
117
115
  )
116
+
118
117
  mapper.deserialize(event)
119
118
  end
120
119
  end
@@ -38,6 +38,24 @@ module EventStoreClient
38
38
  )
39
39
  end
40
40
 
41
+ def join_streams(name, streams)
42
+ data = <<~STRING
43
+ fromStreams(#{streams})
44
+ .when({
45
+ $any: function(s,e) {
46
+ linkTo("#{name}", e)
47
+ }
48
+ })
49
+ STRING
50
+
51
+ make_request(
52
+ :post,
53
+ "/projections/continuous?name=#{name}&type=js&enabled=true&emit=true%26trackemittedstreams=true", # rubocop:disable Metrics/LineLength
54
+ body: data,
55
+ headers: {}
56
+ )
57
+ end
58
+
41
59
  def subscribe_to_stream(
42
60
  stream_name, subscription_name, stats: true, start_from: 0, retries: 5
43
61
  )
@@ -48,7 +66,7 @@ module EventStoreClient
48
66
  extraStatistics: stats,
49
67
  startFrom: start_from,
50
68
  maxRetryCount: retries,
51
- resolveLinkTos: true
69
+ resolveLinktos: true
52
70
  },
53
71
  headers: {
54
72
  'Content-Type' => 'application/json'
@@ -60,11 +78,14 @@ module EventStoreClient
60
78
  stream_name,
61
79
  subscription_name,
62
80
  count: 1,
63
- long_poll: 0
81
+ long_poll: 0,
82
+ resolve_links: true
64
83
  )
65
84
  headers = long_poll.positive? ? { 'ES-LongPoll' => long_poll.to_s } : {}
66
85
  headers['Content-Type'] = 'application/vnd.eventstore.competingatom+json'
67
86
  headers['Accept'] = 'application/vnd.eventstore.competingatom+json'
87
+ headers['ES-ResolveLinkTos'] = resolve_links.to_s
88
+
68
89
  make_request(
69
90
  :get,
70
91
  "/subscriptions/#{stream_name}/#{subscription_name}/#{count}",
@@ -126,7 +147,7 @@ module EventStoreClient
126
147
  method = RequestMethod.new(method_name)
127
148
  connection.send(method.to_s, path) do |req|
128
149
  req.headers = req.headers.merge(headers)
129
- req.body = body.to_json
150
+ req.body = body.is_a?(String) ? body : body.to_json
130
151
  req.params['embed'] = 'body' if method == :get
131
152
  end
132
153
  end
@@ -2,15 +2,21 @@
2
2
 
3
3
  module EventStoreClient
4
4
  class Subscription
5
- attr_accessor :subscribers
6
- attr_reader :stream, :name
5
+ attr_reader :stream, :subscriber, :name, :observed_streams
7
6
 
8
7
  private
9
8
 
10
- def initialize(type:, name:)
11
- @name = name
12
- @subscribers = []
13
- @stream = "$et-#{type}"
9
+ def initialize(subscriber, service:, event_types:)
10
+ subscriber_class =
11
+ if subscriber.class.name == 'Class'
12
+ subscriber.name
13
+ else
14
+ subscriber.class.name
15
+ end
16
+ @name = "#{service}-#{subscriber_class}"
17
+ @subscriber = subscriber
18
+ @stream = name
19
+ @observed_streams = event_types.reduce([]) { |r, type| r << "$et-#{type}" }
14
20
  end
15
21
  end
16
22
  end
@@ -3,16 +3,17 @@
3
3
  module EventStoreClient
4
4
  class Subscriptions
5
5
  def create(subscriber, event_types)
6
- event_types.each do |type|
7
- subscription = subscriptions[type.to_s] || Subscription.new(type: type, name: service)
8
- subscription.subscribers |= [subscriber]
9
- create_subscription(subscription) unless @subscriptions.key?(type.to_s)
10
- @subscriptions[type.to_s] = subscription
6
+ subscription = Subscription.new(subscriber, event_types: event_types, service: service)
7
+ connection.join_streams(subscriber.class.name, subscription.observed_streams)
8
+ unless @subscriptions.detect { |sub| sub.name == subscription.name }
9
+ create_subscription(subscription)
11
10
  end
11
+
12
+ subscriptions << subscription
12
13
  end
13
14
 
14
15
  def each
15
- subscriptions.values.each do |subscription|
16
+ subscriptions.each do |subscription|
16
17
  yield(subscription)
17
18
  end
18
19
  end
@@ -24,6 +25,7 @@ module EventStoreClient
24
25
  private
25
26
 
26
27
  def create_subscription(subscription)
28
+ connection.join_streams(subscription.name, subscription.observed_streams)
27
29
  connection.subscribe(subscription.stream, name: subscription.name)
28
30
  end
29
31
 
@@ -32,7 +34,7 @@ module EventStoreClient
32
34
  def initialize(connection:, service: 'default')
33
35
  @connection = connection
34
36
  @service = service
35
- @subscriptions = {}
37
+ @subscriptions = []
36
38
  end
37
39
  end
38
40
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventStoreClient
4
- VERSION = '0.1.15'
4
+ VERSION = '0.1.16'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_store_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Wilgosz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-31 00:00:00.000000000 Z
11
+ date: 2020-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema