actionstore 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +184 -0
- data/lib/actionstore/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c94128c58646259de5ca05a0ddfa34011643c0443015ce23459ec4b17ec5a94
|
4
|
+
data.tar.gz: '0971d6f3ac13a40ac0ae568897df4ec40cd4730bfc58bc5778091ea2cddc24b9'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31b3eedd570fd0b54362f560f594843e198a444c9d915da7c610dead9c1dd8bd0c324edd1dba328b5c74cf5b6ed8914c00e13a949352e9abdd386bd706529b55
|
7
|
+
data.tar.gz: a5298c7bc3e1519f64543cc0bce8a25d647b73476db78d866eb5252abb36add17d50038b0266ceb2b4e07431809c5e38b3fd99bd76050bcab4d7456126e451d1
|
data/README.md
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
# ActionStore
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/buhrmi/actionstore.svg?style=shield)](https://circleci.com/gh/buhrmi/actionstore)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/actionstore.svg)](https://rubygems.org/gems/actionstore)
|
5
|
+
[![npm version](https://badge.fury.io/js/@buhrmi%2Factionstore.svg)](https://www.npmjs.com/package/@buhrmi/actionstore)
|
6
|
+
|
7
|
+
ActionStore allows you to push data directly into Svelte stores via ActionCable. Here's an [introductory blog post](https://dev.to/buhrmi/actionstore-real-time-svelte-stores-for-rails-4jhg).
|
8
|
+
|
9
|
+
> **Note**
|
10
|
+
> There's currently no way to configure the Websocket URL. At the moment it only works in the browser, with the default Rails configuration, on the same origin.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
### Ruby gem
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'actionstore'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle install
|
25
|
+
|
26
|
+
### Npm package
|
27
|
+
|
28
|
+
Install the package:
|
29
|
+
|
30
|
+
$ yarn add @buhrmi/actionstore
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Below are some usage examples. For more condensed details, see the [API section](#API).
|
35
|
+
|
36
|
+
### Subscribe to a database record
|
37
|
+
|
38
|
+
In a Svelte component:
|
39
|
+
|
40
|
+
```html
|
41
|
+
<script>
|
42
|
+
import {subscribe} from '@buhrmi/actionstore'
|
43
|
+
|
44
|
+
// you need the signed global id of the record you want to subscribe to
|
45
|
+
export let user_sgid
|
46
|
+
|
47
|
+
// Calling `subscribe` will set up an ActionCable subscription and return a
|
48
|
+
// Svelte store which you can push to
|
49
|
+
const user = subscribe(user_sgid)
|
50
|
+
</script>
|
51
|
+
|
52
|
+
{#if $user}
|
53
|
+
Hello {$user.name}
|
54
|
+
{/if}
|
55
|
+
```
|
56
|
+
|
57
|
+
Now you can populate the store from the backend:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class User < ApplicationRecord
|
61
|
+
has_actionstore
|
62
|
+
|
63
|
+
def subscribed channel
|
64
|
+
push_update name: 'Rich Harris'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### Multiple stores
|
70
|
+
|
71
|
+
You can also push data into stores specified by an id
|
72
|
+
|
73
|
+
```js
|
74
|
+
import {subscribe,store} from '@buhrmi/actionstore'
|
75
|
+
export let user_sgid
|
76
|
+
const user = subscribe(user_sgid)
|
77
|
+
|
78
|
+
// Use the store() method to get an ActionStore by id
|
79
|
+
const messages = store('messages')
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
Now you can push into the "messages" store from the backend
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
user.push_append_into 'messages', text: 'hello'
|
87
|
+
```
|
88
|
+
|
89
|
+
### Autopush changes
|
90
|
+
|
91
|
+
You can automatically sync record changes to the store.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class User < ApplicationRecord
|
95
|
+
has_actionstore
|
96
|
+
|
97
|
+
# this will only push changed attributes
|
98
|
+
after_update_commit -> { push_update saved_changes.transform_values(&:last) }
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
### Perform actions
|
103
|
+
|
104
|
+
With ActionStore you can define actions directly on the model and call them from the frontend.
|
105
|
+
|
106
|
+
```js
|
107
|
+
const user = subscribe(user_sgid)
|
108
|
+
user.perform 'say_hello', 'thomas'
|
109
|
+
```
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class User
|
113
|
+
def perform_say_hello channel, name
|
114
|
+
puts "Hello #{name}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
### Trigger events
|
120
|
+
|
121
|
+
```js
|
122
|
+
const user = subscribe(user_sgid)
|
123
|
+
user.on('show_alert', function(data) {
|
124
|
+
window.alert(data)
|
125
|
+
})
|
126
|
+
```
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
user.push_event 'show_alert', 'Boo!'
|
130
|
+
```
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
## API
|
135
|
+
|
136
|
+
ActionStore consists of two parts, the frontend (Javascript) part and the backend (Ruby) part. Find details below.
|
137
|
+
|
138
|
+
### Javascript
|
139
|
+
|
140
|
+
The `@buhrmi/actionstore` package exports the following functions:
|
141
|
+
|
142
|
+
`const someStore = subscribe(sgid, initial=null, storeId=sgid)` - Sets up a new subscription to the record with the specified global id. You can optionally pass a storeId. If a new subscription with the same storeId is created, the old subscription will be cancelled and removed.
|
143
|
+
|
144
|
+
`const someStore = store(storeId, initial=null)` - Gets the store with the specified id.
|
145
|
+
|
146
|
+
#### Stores
|
147
|
+
|
148
|
+
ActionStores are Svelte stores augmented with the following functions:
|
149
|
+
|
150
|
+
`someStore.on(event, handler)` - Sets up an event handler for server-sent events
|
151
|
+
|
152
|
+
`someStore.perform(action, ...arguments)` - Will call the equivalent `perform_[action]` method on the subscribed model.
|
153
|
+
|
154
|
+
### Ruby
|
155
|
+
|
156
|
+
Adding `has_actionstore` to your ActiveRecord model (or anything else that can be located via [Global ID](https://github.com/rails/globalid)) will create the following instance methods and behavior
|
157
|
+
|
158
|
+
#### Pushing Data
|
159
|
+
|
160
|
+
`record.push_append(data)` - Append data to an array in the default store
|
161
|
+
|
162
|
+
`record.push_update(changes)` - Updats fields of an object in the default store
|
163
|
+
|
164
|
+
`record.push_update_by(key, value, changes)` - Updates fields of an object in the default store, identified by the specified key and value
|
165
|
+
|
166
|
+
`record.push_append_into(store_name, data)` - Append data to an array in a specified store
|
167
|
+
|
168
|
+
`record.push_update_into(store_name, changes)` - Update fields of an object in a specified store
|
169
|
+
|
170
|
+
`record.push_update_by_into(store_name, key, value, changes)` - Update fields of an object in a specified store, identified by the specified key and value
|
171
|
+
|
172
|
+
#### Triggering events
|
173
|
+
|
174
|
+
`record.push_event(event_name, data)` - Trigger an event on the default store
|
175
|
+
|
176
|
+
`record.push_event_into(store_name, event_name, data)` - Trigger an event on a specified store
|
177
|
+
|
178
|
+
#### Actions
|
179
|
+
|
180
|
+
Any method on the object that starts with `perform_` can potentially be called from the frontend. The first argument is always the channel that received the action (useful for authentication), followed by the arguments passed from the frontend.
|
181
|
+
|
182
|
+
#### Callbacks
|
183
|
+
|
184
|
+
Whenever a subscription is created or destroyed, the `subscribed(channel)` or `unsubscribed(channel)` method is called on the subject (if defined).
|
data/lib/actionstore/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Buhrmester
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -31,6 +31,7 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- README.md
|
34
35
|
- Rakefile
|
35
36
|
- lib/actionstore.rb
|
36
37
|
- lib/actionstore/actionstore.rb
|
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
62
|
- !ruby/object:Gem::Version
|
62
63
|
version: '0'
|
63
64
|
requirements: []
|
64
|
-
rubygems_version: 3.
|
65
|
+
rubygems_version: 3.3.7
|
65
66
|
signing_key:
|
66
67
|
specification_version: 4
|
67
68
|
summary: Push data into Svelte stores from Rails
|