dexiecable 2.0.0.alpha2 → 2.0.0.alpha3

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: 714562cd7c933eb48e81e9159acd326d77b6133a807cdd541000f350281c1c94
4
- data.tar.gz: d3bcacdc18d5f9c7a8be970f9ede859be19e282e8ca35aa4d27c81e581310eb3
3
+ metadata.gz: 2c2de6d52cf60491a659aebc8452a88ca8b8878142d08e842fadc8fb4bb1050b
4
+ data.tar.gz: 6be72c303e815a4809aff6b88a718b3e9ec1df8d5ebd341af03768225ee86bc3
5
5
  SHA512:
6
- metadata.gz: ed0dbe4fdfade70f3300d9c2a61882f6a00320c384fe74ad76b6ba11ef0733a26b86f6949606117b4847efdd5221709bf1a091088a510a0155d6ff9f48a87774
7
- data.tar.gz: d5cef46c7417c0e34ff4e08c08e0df846ff653f19ae0eeacbffe9de433a362c9ae9af60b32f669a6daa301b64a05d3844f227587cf20af4da23ecf149b39cfb9
6
+ metadata.gz: ac607ef7627a7a0562459042bcc8b23244362553624cb2f7543a7997430842c5185d5c889b8557c3ad28f10265d286f7a557a1ce76c397473adc97ead82b13d5
7
+ data.tar.gz: 1386543cc03df9409d17f184a8e1b4b80e85f993663009a32324da9b8e79561000c907c42e25c84c683bef4d4f11e525a5c1cd6dda58a0817595d53417880786
data/README.md CHANGED
@@ -73,7 +73,7 @@ setConsumer(createConsumer("wss://example.com/cable"));
73
73
 
74
74
  ### DexieChannel
75
75
 
76
- DexieCable ships one channel — `DexieChannel` — so you never define your own channels or include anything. Every Dexie broadcast goes through it.
76
+ DexieCable ships one channel — `DexieChannel` — so you don't need to define channels yourself. Every Dexie broadcast goes through it, and you can reopen it to add custom actions or push initial data.
77
77
 
78
78
  Stream tokens are signed with the application secret, so a client can only subscribe to streams the server has issued for it:
79
79
 
@@ -107,23 +107,43 @@ subscription.removeStream(userStream);
107
107
  subscription.removeAllStreams();
108
108
  ```
109
109
 
110
- #### Initial data on subscribe
110
+ #### Extending DexieChannel
111
111
 
112
- To push a snapshot when a stream is added, set `DexieChannel.on_subscribe`. It receives the channel and the subscribed record (resolved from the signed GlobalID), so you can dispatch on the record type:
112
+ To add custom actions or push initial data, reopen `DexieChannel` in your app:
113
113
 
114
114
  ```ruby
115
115
  # config/initializers/dexiecable.rb
116
- DexieChannel.on_subscribe = ->(channel, record) do
117
- case record
118
- when User
119
- channel.table("notifications").bulkAdd(record.notifications.map(&:as_json_for_dexie))
120
- when Conversation
121
- channel.table("messages").bulkAdd(record.messages.map(&:as_json_for_dexie))
116
+ class DexieCable::DexieChannel
117
+ # Push a snapshot when a private stream is added.
118
+ def subscribed_to(record, params)
119
+ case record
120
+ when User
121
+ table("notifications").bulkAdd(record.notifications.map(&:as_json_for_dexie))
122
+ when Conversation
123
+ table("messages").bulkAdd(record.messages.where("seq_id > ?", params[:last_seq_id]).map(&:as_json_for_dexie))
124
+ end
125
+ end
126
+
127
+ # Any public method is a custom action the client can perform.
128
+ def mark_as_read(data)
129
+ Message.find(data["id"]).update!(read: true)
122
130
  end
123
131
  end
124
132
  ```
125
133
 
126
- The callback runs after the stream is opened, and `channel.table(...)` transmits to just this subscriber — so the snapshot arrives before any live mutation.
134
+ The client still subscribes to `DexieCable::DexieChannel`, so no extra client config is needed:
135
+
136
+ ```js
137
+ const subscription = subscribe(db);
138
+ ```
139
+
140
+ Pass params from the client when adding a stream:
141
+
142
+ ```js
143
+ subscription.addStream(userStream, { last_seq_id: 100 });
144
+ ```
145
+
146
+ `subscribed_to` runs after the stream is opened, and `table(...)` transmits to just this subscriber — so the snapshot arrives before any live mutation. Custom actions are triggered like any ActionCable action: `subscription.perform("mark_as_read", { id: 42 })`.
127
147
 
128
148
  #### Public streams
129
149
 
@@ -13,23 +13,25 @@ module DexieCable
13
13
  # # public (no signature, namespaced under "public:")
14
14
  # subscription.addPublicStream("feed")
15
15
  #
16
- # When a private stream is added, +on_subscribe+ is invoked with the
17
- # channel and the subscribed record, so initial data can be pushed:
16
+ # Reopen this channel in your app to push initial data or add custom
17
+ # actions:
18
18
  #
19
- # DexieChannel.on_subscribe = ->(channel, record) do
20
- # case record
21
- # when User
22
- # channel.table("notifications").bulkAdd(record.notifications.map(&:as_json_for_dexie))
23
- # when Conversation
24
- # channel.table("messages").bulkAdd(record.messages.map(&:as_json_for_dexie))
19
+ # class DexieCable::DexieChannel
20
+ # def subscribed_to(record, params)
21
+ # case record
22
+ # when User
23
+ # table("notifications").bulkAdd(record.notifications.map(&:as_json_for_dexie))
24
+ # end
25
+ # end
26
+ #
27
+ # def my_custom_action(data)
28
+ # # ...
25
29
  # end
26
30
  # end
27
31
  #
28
32
  class DexieChannel < ActionCable::Channel::Base
29
33
  PUBLIC_STREAM_PREFIX = "public:"
30
34
 
31
- class_attribute :on_subscribe, instance_accessor: false, default: nil
32
-
33
35
  public :transmit
34
36
 
35
37
  # Open a scoped channel for broadcasting to a specific recipient.
@@ -74,7 +76,8 @@ module DexieCable
74
76
  return unless target
75
77
 
76
78
  stream_for target
77
- self.class.on_subscribe&.call(self, resolve_subscribe_target(target))
79
+ params = data.except("action", "stream").with_indifferent_access
80
+ subscribed_to(resolve_subscribe_target(target), params)
78
81
  end
79
82
 
80
83
  def remove_stream(data)
@@ -98,6 +101,12 @@ module DexieCable
98
101
  stop_stream_from public_stream_name(name) if name.present?
99
102
  end
100
103
 
104
+ # Override by reopening the channel to push initial data when a private
105
+ # stream is added. +record+ is the resolved target and +params+ are any
106
+ # extra params sent from the client.
107
+ def subscribed_to(_record, _params)
108
+ end
109
+
101
110
  private
102
111
 
103
112
  def public_stream_name(name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "2.0.0.alpha2"
4
+ VERSION = "2.0.0.alpha3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dexiecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha2
4
+ version: 2.0.0.alpha3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester