signalize 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f252599b06ea9725abc4dd66d0b23562cd5b245dd970106c441b22a0890fe42
4
- data.tar.gz: 1e46dc796f6bd28fc80292d0ef526ab5691642ec1fb34d213e547a9b9a0b3b22
3
+ metadata.gz: 4e4f4fc25e9d3e2f4e343a22ecd6bf37daa4628da775f9c5d7709177881c927e
4
+ data.tar.gz: e91df598b0d5c11a6f4a803481e3fabb8476e59ac873c6fa8d0e2f57d3851cae
5
5
  SHA512:
6
- metadata.gz: b4eb905112dc61711495cca9cca462f37a1a50751b7d0434fd15b916276af57c73e08ac055d14d0e45534e8d82e8a2ad1576197772ffd544036cccc27d548c72
7
- data.tar.gz: 8e4bf5a24a1ede2912a1f0b9265598d616829d2d9b0ef28eece14dbb5859d1c1fd75c8a19bbe3eb6ee9bb5922b65a5079e7d578cc75788cac4d13f0f18aca247
6
+ metadata.gz: d9bb28f9f5e35093aaf5567df71a9f8c05edd1467c5788f6afd7a4678790a9bd6a8f08c2296641a9bc7bed8c303da594b17ee308f7b57dcb6754d1bc6a9a7451
7
+ data.tar.gz: a85f5867add9ecce1cad3bb7bcc9cd975c69097436ec8d9bfccdc260245aa1c5c93f1031b4aa0337cc144ad5453bc4a67ec71611b8dad850e17481d614b5b5c6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- signalize (1.0.0)
4
+ signalize (1.0.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -44,6 +44,7 @@ GEM
44
44
 
45
45
  PLATFORMS
46
46
  arm64-darwin-21
47
+ x86_64-linux
47
48
 
48
49
  DEPENDENCIES
49
50
  guard
data/README.md CHANGED
@@ -32,6 +32,8 @@ Signalize's public API consists of four methods (you can think of them almost li
32
32
  The first building block is the `Signalize::Signal` class. You can think of this as a reactive value object which wraps an underlying primitive like String, Integer, Array, etc.
33
33
 
34
34
  ```ruby
35
+ require "signalize"
36
+
35
37
  counter = Signalize.signal(0)
36
38
 
37
39
  # Read value from signal, logs: 0
@@ -44,6 +46,7 @@ counter.value = 1
44
46
  You can include the `Signalize::API` mixin to access these methods directly in any context:
45
47
 
46
48
  ```ruby
49
+ require "signalize"
47
50
  include Signalize::API
48
51
 
49
52
  counter = signal(0)
@@ -56,6 +59,7 @@ counter.value += 1
56
59
  You derive computed state by accessing a signal's value within a `computed` block and returning a new value. Every time that signal value is updated, a computed value will likewise be updated. Actually, that's not quite accurate — the computed value only computes when it's read. In this sense, we can call computed values "lazily-evaluated".
57
60
 
58
61
  ```ruby
62
+ require "signalize"
59
63
  include Signalize::API
60
64
 
61
65
  name = signal("Jane")
@@ -82,6 +86,7 @@ puts full_name.value
82
86
  Effects are callbacks which are executed whenever values which the effect has "subscribed" to by referencing them have changed. An effect callback is run immediately when defined, and then again for any future mutations.
83
87
 
84
88
  ```ruby
89
+ require "signalize"
85
90
  include Signalize::API
86
91
 
87
92
  name = signal("Jane")
@@ -99,6 +104,7 @@ name.value = "John"
99
104
  You can dispose of an effect whenever you want, thereby unsubscribing it from signal notifications.
100
105
 
101
106
  ```ruby
107
+ require "signalize"
102
108
  include Signalize::API
103
109
 
104
110
  name = signal("Jane")
@@ -117,11 +123,62 @@ dispose.()
117
123
  surname.value = "Doe 2"
118
124
  ```
119
125
 
126
+ **IMPORTANT:** you cannot use `return` or `break` within an effect block. Doing so will raise an exception (due to it breaking the underlying execution model).
127
+
128
+ ```ruby
129
+ def my_method(signal_obj)
130
+ effect do
131
+ return if signal_obj.value > 5 # DON'T DO THIS!
132
+
133
+ puts signal_obj.value
134
+ end
135
+
136
+ # more code here
137
+ end
138
+ ```
139
+
140
+ Instead, try to resolve it using more explicit logic:
141
+
142
+ ```ruby
143
+ def my_method(signal_obj)
144
+ should_exit = false
145
+
146
+ effect do
147
+ should_exit = true && next if signal_obj.value > 5
148
+
149
+ puts signal_obj.value
150
+ end
151
+
152
+ return if should_exit
153
+
154
+ # more code here
155
+ end
156
+ ```
157
+
158
+ However, there's no issue if you pass in a method proc directly:
159
+
160
+ ```ruby
161
+ def my_method(signal_obj)
162
+ @signal_obj = signal_obj
163
+
164
+ effect &method(:an_effect_method)
165
+
166
+ # more code here
167
+ end
168
+
169
+ def an_effect_method
170
+ return if @signal_obj.value > 5
171
+
172
+ puts @signal_obj.value
173
+ end
174
+ ```
175
+
120
176
  ### `batch { }`
121
177
 
122
- You can write to multiple signals within a batch, and flush the updates at all once (thereby notifying computed refreshes and effects).
178
+ You can write to multiple signals within a batch, and flush the updates at all once (thereby notifying computed refreshes and effects).
123
179
 
124
180
  ```ruby
181
+ require "signalize"
125
182
  include Signalize::API
126
183
 
127
184
  name = signal("Jane")
@@ -132,8 +189,8 @@ full_name = computed { name.value + " " + surname.value }
132
189
  dispose = effect { puts full_name.value }
133
190
 
134
191
  batch do
135
- name.value = "Foo"
136
- surname.value = "Bar"
192
+ name.value = "Foo"
193
+ surname.value = "Bar"
137
194
  end
138
195
  ```
139
196
 
@@ -142,6 +199,7 @@ end
142
199
  You can explicitly subscribe to a signal signal value and be notified on every change. (Essentially the Observable pattern.) In your block, the new signal value will be supplied as an argument.
143
200
 
144
201
  ```ruby
202
+ require "signalize"
145
203
  include Signalize::API
146
204
 
147
205
  counter = signal(0)
@@ -158,17 +216,18 @@ counter.value = 1 # logs the new value
158
216
  If you need to access a signal's value inside an effect without subscribing to that signal's updates, use the `peek` method instead of `value`.
159
217
 
160
218
  ```ruby
219
+ require "signalize"
161
220
  include Signalize::API
162
221
 
163
222
  counter = signal(0)
164
223
  effect_count = signal(0)
165
224
 
166
225
  effect do
167
- puts counter.value
226
+ puts counter.value
168
227
 
169
- # Whenever this effect is triggered, increase `effect_count`.
170
- # But we don't want this signal to react to `effect_count`
171
- effect_count.value = effect_count.peek
228
+ # Whenever this effect is triggered, increase `effect_count`.
229
+ # But we don't want this signal to react to `effect_count`
230
+ effect_count.value = effect_count.peek + 1
172
231
  end
173
232
  ```
174
233
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Signalize
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
data/lib/signalize.rb CHANGED
@@ -17,7 +17,7 @@ module Signalize
17
17
  end
18
18
 
19
19
  def self.cycle_detected
20
- raise "Cycle detected"
20
+ raise Signalize::Error, "Cycle detected"
21
21
  end
22
22
 
23
23
  RUNNING = 1 << 0
@@ -339,7 +339,7 @@ module Signalize
339
339
  end
340
340
 
341
341
  def end_effect(effect, prev_context, *_) # allow additional args for currying
342
- raise "Out-of-order effect" if eval_context != effect
342
+ raise Signalize::Error, "Out-of-order effect" if eval_context != effect
343
343
 
344
344
  cleanup_sources(effect)
345
345
  self.eval_context = prev_context
@@ -586,8 +586,13 @@ module Signalize
586
586
  finis = _start
587
587
 
588
588
  begin
589
+ compute_executed = false
589
590
  @_cleanup = _compute.() if (@_flags & DISPOSED).zero? && @_compute.nil?.!
591
+ compute_executed = true
590
592
  ensure
593
+ unless compute_executed
594
+ raise Signalize::Error, "Early return or break detected in effect block"
595
+ end
591
596
  finis.(nil) # TODO: figure out this weird shit
592
597
  end
593
598
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared White
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-03-08 00:00:00.000000000 Z
12
+ date: 2023-03-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby port of Signals, providing reactive variables, derived computed
15
15
  state, side effect callbacks, and batched updates.