dexiecable 2.0.0 → 2.0.1
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 +12 -12
- data/lib/dexiecable/concern.rb +18 -21
- 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: 05e9525058173a0392999802c6243cac3eb5c0406a28cb405963a9069d20b098
|
|
4
|
+
data.tar.gz: 3394af2d34854137a4032dd672bb0e1ebc6073e907872b168a5e31e9ffea1daf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 936fa88cb0cd6dc55665e9214d54064eb914863552bf8b84e58e1ca4d616751863f9572740ceb00c7b09503ed9996fe9ec52640a65279b2bc5249498d3671684
|
|
7
|
+
data.tar.gz: 1b9a73d2e21d3ac4e5138feeb1c13c958f9b4eccffeb7968c0f7fbc28a45cd24b8dff7f6ab4e2f976730536dcee3fc30758cae5aaa04c4bd8dd3940ec83b5ef6
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
>
|
|
6
6
|
> Full synchronization utilizing event streams will arrive in DexieCable 3.0.
|
|
7
7
|
|
|
8
|
-
DexieCable gives your ActionCable channel a query DSL that mirrors the Dexie.js API, letting you push database mutations from the server to the client in real time. It also gives you a [`syncs_to_dexie`](#syncs_to_dexie
|
|
8
|
+
DexieCable gives your ActionCable channel a query DSL that mirrors the Dexie.js API, letting you push database mutations from the server to the client in real time. It also gives you a [`syncs_to_dexie`](#syncs_to_dexie-automatic-model-streaming) ActiveRecord macro for automatic change syncing.
|
|
9
9
|
|
|
10
10
|
Push Dexie table updates to a client from anywhere on the server:
|
|
11
11
|
|
|
@@ -18,7 +18,7 @@ class NotificationsController < ApplicationController
|
|
|
18
18
|
end
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
Or sync model changes automatically with the `syncs_to_dexie` macro (more info [below](#syncs_to_dexie
|
|
21
|
+
Or sync model changes automatically with the `syncs_to_dexie` macro (more info [below](#syncs_to_dexie-automatic-model-streaming))
|
|
22
22
|
|
|
23
23
|
```ruby
|
|
24
24
|
class Notification < ApplicationRecord
|
|
@@ -114,7 +114,7 @@ const stopStreaming = subscription.addStream(userStream);
|
|
|
114
114
|
stopStreaming(); // equivalent to subscription.removeStream(userStream)
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
-
`addStream`/`removeStream` perform `add_stream`/`remove_stream` on `DexieChannel`, which verifies the token and then `stream_from`/`stop_stream_from` the decoded identifier. `removeAllStreams()` performs `remove_all_streams`, stopping every current stream
|
|
117
|
+
`addStream`/`removeStream` perform `add_stream`/`remove_stream` on `DexieChannel`, which verifies the token and then `stream_from`/`stop_stream_from` the decoded identifier. `removeAllStreams()` performs `remove_all_streams`, stopping every current stream. Handy on logout:
|
|
118
118
|
|
|
119
119
|
```js
|
|
120
120
|
subscription.removeAllStreams();
|
|
@@ -161,11 +161,11 @@ Pass params from the client when adding a stream:
|
|
|
161
161
|
subscription.addStream(userStream, { last_seq_id: 100 });
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
-
`subscribed_to` runs after the stream is opened, and `table(...)` transmits to just this subscriber
|
|
164
|
+
`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 })`.
|
|
165
165
|
|
|
166
166
|
#### Public streams
|
|
167
167
|
|
|
168
|
-
For data that's public (a global feed, announcements, etc.), skip the signature. Use a string target
|
|
168
|
+
For data that's public (a global feed, announcements, etc.), skip the signature. Use a string target. It's namespaced under `public:` automatically:
|
|
169
169
|
|
|
170
170
|
```ruby
|
|
171
171
|
DexieChannel["feed"].table("announcements").add(announcement)
|
|
@@ -176,11 +176,11 @@ class Announcement < ApplicationRecord
|
|
|
176
176
|
end
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
Then subscribe by name
|
|
179
|
+
Then subscribe by name. No token required:
|
|
180
180
|
|
|
181
181
|
```js
|
|
182
|
-
const stopPublicStream = subscription.
|
|
183
|
-
stopPublicStream(); // equivalent to subscription.
|
|
182
|
+
const stopPublicStream = subscription.addStream("feed");
|
|
183
|
+
stopPublicStream(); // equivalent to subscription.removeStream("feed")
|
|
184
184
|
```
|
|
185
185
|
|
|
186
186
|
Public streams are namespaced under `public:`, so this path can never reach a signed (private) stream.
|
|
@@ -222,7 +222,7 @@ DexieChannel[current_user]
|
|
|
222
222
|
|
|
223
223
|
The full query chain is serialized as JSON and sent over ActionCable. The JS client replays every method call against the local Dexie database in order.
|
|
224
224
|
|
|
225
|
-
### `syncs_to_dexie
|
|
225
|
+
### `syncs_to_dexie`: automatic model streaming
|
|
226
226
|
|
|
227
227
|
Add to any ActiveRecord model. Optionally provide the broadcast target.
|
|
228
228
|
|
|
@@ -231,7 +231,7 @@ class Message < ApplicationRecord
|
|
|
231
231
|
# Calls send(:receiver), then broadcasts: DexieChannel.broadcast_to(receiver, ...)
|
|
232
232
|
syncs_to_dexie via: :receiver
|
|
233
233
|
|
|
234
|
-
# String = public stream (subscribe via
|
|
234
|
+
# String = public stream (subscribe via addStream("public"))
|
|
235
235
|
syncs_to_dexie via: "public"
|
|
236
236
|
|
|
237
237
|
# Procs are also supported. If an array is returned, multiple broadcasts are made
|
|
@@ -263,8 +263,8 @@ Internally, `syncs_to_dexie` sets up the following ActiveRecord callbacks:
|
|
|
263
263
|
| `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
|
|
264
264
|
| `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
|
|
265
265
|
| `with:` | `:as_json_for_dexie` | Method name (Symbol) or Proc for serializing records |
|
|
266
|
-
| `if:` | *(none)* | Symbol (method name) or Proc
|
|
267
|
-
| `unless:` | *(none)* | Symbol (method name) or Proc
|
|
266
|
+
| `if:` | *(none)* | Symbol (method name) or Proc. Only sync when it returns truthy |
|
|
267
|
+
| `unless:` | *(none)* | Symbol (method name) or Proc. Skip sync when it returns truthy |
|
|
268
268
|
|
|
269
269
|
You can combine multiple `syncs_to_dexie` declarations, each with different conditions:
|
|
270
270
|
|
data/lib/dexiecable/concern.rb
CHANGED
|
@@ -20,7 +20,7 @@ module DexieCable
|
|
|
20
20
|
#
|
|
21
21
|
# subscribe(db) # subscribes to "DexieChannel"
|
|
22
22
|
# subscription.addStream(DexieChannel.stream_token_for(current_user))
|
|
23
|
-
# subscription.
|
|
23
|
+
# subscription.addStream("feed") # plain strings are public streams
|
|
24
24
|
#
|
|
25
25
|
extend ActiveSupport::Concern
|
|
26
26
|
|
|
@@ -73,38 +73,35 @@ module DexieCable
|
|
|
73
73
|
|
|
74
74
|
def add_stream(data)
|
|
75
75
|
record = resolve_subscribe_target(data["stream"])
|
|
76
|
-
return unless record
|
|
77
|
-
|
|
78
|
-
stream_for record
|
|
79
76
|
params = data.except("action", "stream").with_indifferent_access
|
|
80
|
-
|
|
77
|
+
|
|
78
|
+
if record
|
|
79
|
+
stream_for record
|
|
80
|
+
subscribed_to(record, params)
|
|
81
|
+
else
|
|
82
|
+
name = data["stream"].to_s
|
|
83
|
+
return if name.blank?
|
|
84
|
+
|
|
85
|
+
stream_from public_stream_name(name)
|
|
86
|
+
subscribed_to(name, params)
|
|
87
|
+
end
|
|
81
88
|
end
|
|
82
89
|
|
|
83
90
|
def remove_stream(data)
|
|
84
91
|
record = resolve_subscribe_target(data["stream"])
|
|
85
|
-
return unless record
|
|
86
92
|
|
|
87
|
-
|
|
93
|
+
if record
|
|
94
|
+
stop_stream_from self.class.broadcasting_for(record)
|
|
95
|
+
else
|
|
96
|
+
name = data["stream"].to_s
|
|
97
|
+
stop_stream_from public_stream_name(name) if name.present?
|
|
98
|
+
end
|
|
88
99
|
end
|
|
89
100
|
|
|
90
101
|
def remove_all_streams(_data)
|
|
91
102
|
stop_all_streams
|
|
92
103
|
end
|
|
93
104
|
|
|
94
|
-
def add_public_stream(data)
|
|
95
|
-
name = data["stream"].to_s
|
|
96
|
-
return if name.blank?
|
|
97
|
-
|
|
98
|
-
stream_from public_stream_name(name)
|
|
99
|
-
params = data.except("action", "stream").with_indifferent_access
|
|
100
|
-
subscribed_to(name, params)
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def remove_public_stream(data)
|
|
104
|
-
name = data["stream"].to_s
|
|
105
|
-
stop_stream_from public_stream_name(name) if name.present?
|
|
106
|
-
end
|
|
107
|
-
|
|
108
105
|
# Override to push initial data when a stream is added. +record+ is the
|
|
109
106
|
# resolved target — a record for private streams, or the stream name for
|
|
110
107
|
# public streams — and +params+ are any extra params sent from the client.
|
data/lib/dexiecable/version.rb
CHANGED