dexiecable 3.0.1 → 4.0.0

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: bd35d8a96ed1aa2ae0df024ec4d6bd8293b48213fa3b3b0107d805d0031d2b5a
4
- data.tar.gz: 07c321a93ff08c1a57954715f5818d265e2c33076aff5f0f75baea4628ecef34
3
+ metadata.gz: dc8d12fc20298e1698ef329c2003cfab1a5e6508577f9542dd03a24e8d7d88ec
4
+ data.tar.gz: 36c9682301446b416bc5528431e934c93c1902adac0a64f9f3dce6d2f87d4328
5
5
  SHA512:
6
- metadata.gz: 7b0303b8b5094a7a6a9adacfa76bae28cb1efa9d0f9842af8cb91c4808ed147013e6db9743081f2b8f8734a0548480754b9c71b0309af80851c83098381f55d9
7
- data.tar.gz: 9ed45ea925d28bb1395a305c97492fc70e36eed26936c96b8d78a0fc347a5a960cfef403595dd43365e031bff4d29dd7ab19083c156a9453731a084838c52b41
6
+ metadata.gz: 41476f7e6f642abceefc394cf9686572c5dea7e35db568b43d7fbc76b44a5e076783c091f9caff74839bcc66c6db1521f4f467b2847662e1a95e5675496b49f5
7
+ data.tar.gz: a10831e0fe6271f3630520b595d5f4554fbf80fcfaccdd1766b832d8425eaaf5bc71610e5785d2be7fe5e6c0450b605bee9ce75e6360aff59553c81668f6a090
data/README.md CHANGED
@@ -106,7 +106,7 @@ import { subscribe } from "dexiecable";
106
106
  import { db } from "./db";
107
107
 
108
108
  const subscription = subscribe(db);
109
- // Stream tokens come from the server: DexieChannel.stream_token_for(target)
109
+ // Stream tokens come from the server: target.to_stream_token
110
110
  subscription.addStream(streamToken);
111
111
  ```
112
112
 
@@ -135,23 +135,17 @@ class DexieChannel < ApplicationCable::Channel
135
135
  end
136
136
  ```
137
137
 
138
- Stream tokens are signed with the application secret, so a client can only subscribe to streams the server has issued for it:
138
+ Stream tokens are signed with the application secret, so a client can only subscribe to streams the server has issued for it. Generate one on the record itself:
139
139
 
140
140
  ```ruby
141
- DexieChannel.stream_token_for(target)
142
- ```
143
-
144
- For an ActiveRecord model it returns a signed GlobalID (Rails' `signed_id`):
145
-
146
- ```ruby
147
- DexieChannel.stream_token_for(current_user)
141
+ current_user.to_stream_token
148
142
  # => "signed global id"
149
143
  ```
150
144
 
151
145
  Tokens never expire by default. Pass `expires_in:` or `expires_at:` to limit a token's lifetime:
152
146
 
153
147
  ```ruby
154
- DexieChannel.stream_token_for(current_user, expires_in: 1.day)
148
+ current_user.to_stream_token(expires_in: 1.day)
155
149
  ```
156
150
 
157
151
  An expired token, or one whose record no longer exists, is rejected. Only plain strings are treated as public stream names.
@@ -322,7 +316,7 @@ class Message < ApplicationRecord
322
316
  end
323
317
  ```
324
318
 
325
- Broadcasts go out over `DexieChannel`, the channel DexieCable provides. On the client, subscribe to it and add the stream token returned by `DexieChannel.stream_token_for(target)`:
319
+ Broadcasts go out over `DexieChannel`, the channel DexieCable provides. On the client, subscribe to it and add the stream token returned by `record.to_stream_token`:
326
320
 
327
321
  ```js
328
322
  const subscription = subscribe(db);
@@ -117,6 +117,24 @@ module DexieCable
117
117
  end
118
118
  end
119
119
 
120
+ # Returns a signed token the client passes to +subscription.addStream+ to
121
+ # receive this record's stream. Tokens never expire by default; pass
122
+ # +expires_in:+ (a duration) or +expires_at:+ (a time) to limit their
123
+ # lifetime.
124
+ def to_stream_token(expires_at: nil, expires_in: nil)
125
+ options = { for: STREAM_TOKEN_PURPOSE }
126
+
127
+ if expires_at
128
+ options[:expires_at] = expires_at
129
+ elsif expires_in
130
+ options[:expires_in] = expires_in
131
+ else
132
+ options[:expires_at] = nil # never expire
133
+ end
134
+
135
+ to_sgid_param(**options)
136
+ end
137
+
120
138
  private
121
139
 
122
140
  def resolve_channels(via = nil)
@@ -25,7 +25,7 @@ module DexieCable
25
25
  # The client subscribes to that channel and adds streams dynamically:
26
26
  #
27
27
  # subscribe(db) # subscribes to "DexieChannel"
28
- # subscription.addStream(DexieChannel.stream_token_for(current_user))
28
+ # subscription.addStream(current_user.to_stream_token)
29
29
  # subscription.addStream("feed") # plain strings are public streams
30
30
  #
31
31
  extend ActiveSupport::Concern
@@ -68,25 +68,6 @@ module DexieCable
68
68
  {}
69
69
  end
70
70
  end
71
-
72
- # Returns a signed token for +target+ (a record). Send it to the client,
73
- # which passes it to +subscription.addStream+.
74
- #
75
- # Tokens never expire by default. Pass +expires_in:+ (a duration) or
76
- # +expires_at:+ (a time) to limit a token's lifetime.
77
- def stream_token_for(target, expires_at: nil, expires_in: nil)
78
- options = { for: STREAM_TOKEN_PURPOSE }
79
-
80
- if expires_at
81
- options[:expires_at] = expires_at
82
- elsif expires_in
83
- options[:expires_in] = expires_in
84
- else
85
- options[:expires_at] = nil # never expire
86
- end
87
-
88
- target.to_sgid_param(**options)
89
- end
90
71
  end
91
72
 
92
73
  # Build a query against a Dexie table, transmitted to all subscribers
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "3.0.1"
4
+ VERSION = "4.0.0"
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: 3.0.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester