e621stream 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc6c807b32f91dc3e755c6f97ca580b91cae5c6f2e3c18ee1db04b0b2e5420c9
4
- data.tar.gz: 633f8cb92b1c9e65f87bb0d03011a7680ecca1fbcfc15c64cc60445a2430dc00
3
+ metadata.gz: '058c7ea74d261c3f1012be05f6b7d200551d54bfd197a48a8222b235848866e1'
4
+ data.tar.gz: f6b4f2e3065eb0b58a09e5df28f561e77e0b9ed49d202506a8684e5f73bb24d0
5
5
  SHA512:
6
- metadata.gz: 01727d098cd998cdf67b6c6d79dac43d1caa156cef0f398fff2dfd1094ce3e5540842727cc188bda0cddd8e88d9c1394aa70af28d456daf2a044899280faaefc
7
- data.tar.gz: 8cb060095b29f36cff0c5242896bf8e9310febc92854db10fe5a8cb365b1df95032f8ce5b3105c22cbcfe6f1c1056ed1b155a1f26ce46583ea3157c6f71b5d92
6
+ metadata.gz: 64620623caeb76829554793a7a7096fd2963702cb47de459634932d71c8d7c0735972c9111b8ab7baccded276978b7bb391c492a0097bf83f75cc24d61d2b726
7
+ data.tar.gz: 5a9a4c6718aa17e1cb918e995758ff68794ad824b2e65c72e727f42add2a1e39ab1555ad3dc5efcbad4449d65593fe8e9d75abdc250ec750e5c2cd5b7a08757e
data/README.md CHANGED
@@ -30,6 +30,14 @@ Instead of `username:` & `api_key:`, you can pass `token:` with a token from [/a
30
30
 
31
31
  It reconnects when the connection drops (`reconnect: false` turns that off), and picks up from `client.cursor`. Pass `cursor:` to replay from an event id or ms timestamp. `url:` is the site (`https://e621.stream`), the websocket url comes from its `/info`, or pass a `ws://`/`wss://` url to connect to it directly. `client.subscribe([...])` changes subscriptions. Private events like `dmail.create` have to be subscribed to by name.
32
32
 
33
+ `client.snapshots(resource, ids) { |snapshots| ... }` gets what was last seen of up to 100 records (see [Snapshots](https://e621.stream/snapshots/)), by id, nil for ones that aren't known. Only after `:ready`, and the block is dropped if the connection closes first.
34
+
35
+ ```ruby
36
+ client.on(:ready) do
37
+ client.snapshots("post", [6843484, 6843485]) { |posts| p(posts[6843484]&.dig("tags", "general")) }
38
+ end
39
+ ```
40
+
33
41
  ```sh
34
42
  bundle exec rake # rubocop, zeitwerk, tests & sorbet
35
43
  ```
@@ -16,6 +16,8 @@ module E621Stream
16
16
  extend(T::Sig)
17
17
 
18
18
  Listener = T.type_alias { T.proc.params(arg0: T.untyped).void }
19
+ Snapshots = T.type_alias { T::Hash[Integer, T.nilable(T::Hash[String, T.untyped])] }
20
+ SnapshotCallback = T.type_alias { T.proc.params(snapshots: Snapshots).void }
19
21
  # An event type, `<resource>.*`, `*`, or `{ "type" => ..., "filter" => { ... } }`.
20
22
  Subscription = T.type_alias { T.any(String, T::Hash[T.any(String, Symbol), T.untyped]) }
21
23
 
@@ -83,6 +85,9 @@ module E621Stream
83
85
  @attempt = T.let(0, Integer)
84
86
  @last_message = T.let(0.0, Float)
85
87
  @last_error = T.let(nil, T.nilable(StreamError))
88
+ @ready = T.let(false, T::Boolean)
89
+ @nonce = T.let(0, Integer)
90
+ @pending_snapshots = T.let({}, T::Hash[String, SnapshotCallback])
86
91
  end
87
92
 
88
93
  # Listens for an event type (like "post.create"), or one of :dispatch (every event), :ready, :replay_complete,
@@ -124,6 +129,19 @@ module E621Stream
124
129
  send_message({ op: "subscribe", d: { subscriptions: subscriptions } })
125
130
  end
126
131
 
132
+ # Looks up what was last seen of up to 100 records, see https://e621.stream/snapshots/. The block gets them by id,
133
+ # nil for ones that aren't known. It isn't called if the connection closes first.
134
+ #
135
+ # client.on(:ready) { client.snapshots("post", [1, 2]) { |posts| p(posts[1]&.dig("tags", "general")) } }
136
+ sig { params(resource: String, ids: T::Array[Integer], block: SnapshotCallback).void }
137
+ def snapshots(resource, ids, &block)
138
+ raise("not ready") unless @ready
139
+
140
+ nonce = (@nonce += 1).to_s
141
+ @pending_snapshots[nonce] = block
142
+ send_message({ op: "snapshot", d: { resource: resource, ids: ids, nonce: nonce } })
143
+ end
144
+
127
145
  # Closes the connection for good.
128
146
  sig { void }
129
147
  def close
@@ -148,6 +166,8 @@ module E621Stream
148
166
  ensure
149
167
  heartbeat.stop
150
168
  @connection = nil
169
+ @ready = false
170
+ @pending_snapshots.clear
151
171
  end
152
172
  end
153
173
  # anything but a normal close raises out of read
@@ -189,6 +209,7 @@ module E621Stream
189
209
  send_message({ op: "identify", d: identify })
190
210
  when "ready"
191
211
  @attempt = 0
212
+ @ready = true
192
213
  emit("ready", data)
193
214
  when "dispatch"
194
215
  event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"], private: message["private"] == true)
@@ -199,6 +220,8 @@ module E621Stream
199
220
  emit("replay_complete", data)
200
221
  when "subscribed"
201
222
  emit("subscribed", data["subscriptions"])
223
+ when "snapshot"
224
+ @pending_snapshots.delete(data["nonce"])&.call(data["snapshots"].to_h { |id, snapshot| [id.to_i, snapshot] })
202
225
  when "error"
203
226
  @last_error = StreamError.new(data["code"], data["message"])
204
227
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module E621Stream
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e621stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donovan_DMC