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 +4 -4
- data/README.md +30 -10
- data/lib/dexiecable/dexie_channel.rb +20 -11
- data/lib/dexiecable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2c2de6d52cf60491a659aebc8452a88ca8b8878142d08e842fadc8fb4bb1050b
|
|
4
|
+
data.tar.gz: 6be72c303e815a4809aff6b88a718b3e9ec1df8d5ebd341af03768225ee86bc3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
-
####
|
|
110
|
+
#### Extending DexieChannel
|
|
111
111
|
|
|
112
|
-
To
|
|
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
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
|
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
|
-
#
|
|
17
|
-
#
|
|
16
|
+
# Reopen this channel in your app to push initial data or add custom
|
|
17
|
+
# actions:
|
|
18
18
|
#
|
|
19
|
-
# DexieChannel
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
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
|
-
|
|
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)
|
data/lib/dexiecable/version.rb
CHANGED