motion 0.2.2 → 0.3.0

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: c1b1922ab9d0ef3ead51f333bf255e48f1f470a475ccc2463726e7d805d26344
4
- data.tar.gz: 57204e95779dfffb7413bb5080da1fb97f8d0bd519a6ae5f07cce80e4bad58f7
3
+ metadata.gz: 7645381b6b2749a4a9ba90a15c93916636e1b9edfd7b3a6a1bf7d47429fedb3f
4
+ data.tar.gz: 46c929b8aa2d1de8c714eb8b8db1db8524aec892576a13ad1582b7fa858565f7
5
5
  SHA512:
6
- metadata.gz: 3240417ce4c5928aa52c17fb1eb89aa641983d1c119d0ad38b7ec05e60c29172ac95fac9b1ab5cba6ef35ea94b7df819b2b917213cfb193dbb749484b485a33a
7
- data.tar.gz: b7c973c8a93f0c9fac94fffa21a6b597635e064c4363b55d6718704bfdc5936125956e9116ebafb2936d5c555c95c34dff7e474a873605d49b36b7e0bebefeac
6
+ metadata.gz: b8c99a480f51e9d818f5d22de3efbf8923155943d1815188e19332c54b8f24b909bc371abc7282d3a9caac754cf1b0aee4bb3193dad75b7f1d86a9112ed13983
7
+ data.tar.gz: a12e9213ba0ae38cbc5d839ae1408a5aab064be055896300f19a2296ebcd74b49922b96636bc52bd7b7aef62be9868ea47437544938f20facaad45534744bc4e
@@ -17,7 +17,15 @@ export default createClient({
17
17
  // made available at `Motion::Event#extra_data`:
18
18
  //
19
19
  // getExtraDataForEvent(event) {},
20
+
21
+ // By default, the Motion client automatically disconnects all components when
22
+ // it detects the browser navigating away to a new page. This is done to
23
+ // prevent flashes of new content in components with broadcasts because of
24
+ // some action being taken by the controller that the user is navigating to
25
+ // (like submitting a form). If you do not want or need this functionally, you
26
+ // can turn it off:
20
27
  //
28
+ // shutdownBeforeUnload: false,
21
29
 
22
30
  // The data attributes used by Motion can be customized, but these values must
23
31
  // also be updated in the Ruby initializer:
@@ -25,6 +33,5 @@ export default createClient({
25
33
  // keyAttribute: "data-motion-key",
26
34
  // stateAttribute: "data-motion-state",
27
35
  // motionAttribute: "data-motion",
28
- //
29
36
 
30
37
  });
@@ -30,14 +30,29 @@ Motion.configure do |config|
30
30
  # config.renderer_for_connection_proc = ->(websocket_connection) do
31
31
  # ApplicationController.renderer.new(
32
32
  # websocket_connection.env.slice(
33
- # Rack::HTTP_COOKIE,
34
- # Rack::RACK_SESSION,
33
+ # Rack::HTTP_COOKIE, # Cookies
34
+ # Rack::RACK_SESSION, # Session
35
+ # 'warden' # Warden (needed for `current_user` in Devise)
35
36
  # )
36
37
  # )
37
38
  # end
38
39
 
40
+ # This proc will be invoked by Motion when an unhandled error occurs. By
41
+ # default, an error is logged to the application's default logger but no
42
+ # additional action is taken. If you are using an error tracking tool like
43
+ # Bugsnag, Sentry, Honeybadger, or Rollbar, you can provide a proc which
44
+ # notifies that as well:
45
+ #
46
+ # config.error_notification_proc = ->(error, message) do
47
+ # Bugsnag.notify(error) do |report|
48
+ # report.add_tab(:motion, {
49
+ # message: message
50
+ # })
51
+ # end
52
+ # end
53
+
39
54
  # The data attributes used by Motion can be customized, but these values must
40
- # also be updated in the Ruby initializer:
55
+ # also be updated in the JavaScript client configuration:
41
56
  #
42
57
  # config.key_attribute = "data-motion-key"
43
58
  # config.state_attribute = "data-motion-state"
@@ -42,6 +42,10 @@ module Motion
42
42
  config.renderer_for_connection_proc.call(websocket_connection)
43
43
  end
44
44
 
45
+ def self.notify_error(error, message)
46
+ config.error_notification_proc&.call(error, message)
47
+ end
48
+
45
49
  # This method only exists for testing. Changing configuration while Motion is
46
50
  # in use is not supported. It is only safe to call this method when no
47
51
  # components are currently mounted.
@@ -75,10 +75,12 @@ module Motion
75
75
  def process_broadcast(broadcast, message)
76
76
  return unless (handler = _broadcast_handlers[broadcast])
77
77
 
78
- if method(handler).arity.zero?
79
- send(handler)
80
- else
81
- send(handler, message)
78
+ _run_action_callbacks(context: handler) do
79
+ if method(handler).arity.zero?
80
+ send(handler)
81
+ else
82
+ send(handler, message)
83
+ end
82
84
  end
83
85
  end
84
86
 
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/callbacks"
3
4
  require "active_support/concern"
5
+ require "active_support/deprecation"
4
6
 
5
7
  require "motion"
6
8
 
@@ -9,22 +11,102 @@ module Motion
9
11
  module Lifecycle
10
12
  extend ActiveSupport::Concern
11
13
 
14
+ include ActiveSupport::Callbacks
15
+
16
+ included do
17
+ define_callbacks :action, :connect, :disconnect
18
+
19
+ # The built-in triggers defined on the target class will override ours.
20
+ remove_method(:_run_action_callbacks)
21
+ end
22
+
12
23
  class_methods do
13
- # TODO: "IncorrectRevisionError" doesn't make sense for this anymore.
14
- # It should probably be something like "CannotUpgrade" and the error
15
- # message should focus on how to handle deployments gracefully.
16
- def upgrade_from(previous_revision, _instance)
17
- raise IncorrectRevisionError.new(
18
- Motion.config.revision,
19
- previous_revision
24
+ def upgrade_from(previous_revision, instance)
25
+ raise UpgradeNotImplementedError.new(
26
+ instance,
27
+ previous_revision,
28
+ Motion.config.revision
20
29
  )
21
30
  end
31
+
32
+ def before_action(*methods, **options, &block)
33
+ set_action_callback(:before, *methods, **options, &block)
34
+ end
35
+
36
+ def around_action(*methods, **options, &block)
37
+ set_action_callback(:around, *methods, **options, &block)
38
+ end
39
+
40
+ def after_action(*methods, **options, &block)
41
+ set_action_callback(:after, *methods, **options, &block)
42
+ end
43
+
44
+ def after_connect(*methods, **options, &block)
45
+ set_callback(:connect, :after, *methods, **options, &block)
46
+ end
47
+
48
+ def after_disconnect(*methods, **options, &block)
49
+ set_callback(:disconnect, :after, *methods, **options, &block)
50
+ end
51
+
52
+ private
53
+
54
+ def set_action_callback(kind, *methods, **options, &block)
55
+ filters = Array(options.delete(:if))
56
+
57
+ if (only = Array(options.delete(:only))).any?
58
+ filters << action_callback_context_filter(only)
59
+ end
60
+
61
+ if (except = Array(options.delete(:except))).any?
62
+ filters << action_callback_context_filter(except, invert: true)
63
+ end
64
+
65
+ set_callback(:action, kind, *methods, if: filters, **options, &block)
66
+ end
67
+
68
+ def action_callback_context_filter(contexts, invert: false)
69
+ proc { contexts.include?(@_action_callback_context) ^ invert }
70
+ end
71
+ end
72
+
73
+ def process_connect
74
+ _run_connect_callbacks
75
+
76
+ # TODO: Remove at next minor release
77
+ if respond_to?(:connected)
78
+ ActiveSupport::Deprecation.warn(
79
+ "The `connected` lifecycle method is being replaced by the " \
80
+ "`after_connect` callback and will no longer be automatically " \
81
+ "invoked in the next **minor release** of Motion."
82
+ )
83
+
84
+ send(:connected)
85
+ end
22
86
  end
23
87
 
24
- def connected
88
+ def process_disconnect
89
+ _run_disconnect_callbacks
90
+
91
+ # TODO: Remove at next minor release
92
+ if respond_to?(:disconnected)
93
+ ActiveSupport::Deprecation.warn(
94
+ "The `disconnected` lifecycle method is being replaced by the " \
95
+ "`after_disconnect` callback and will no longer be automatically " \
96
+ "invoked in the next **minor release** of Motion."
97
+ )
98
+
99
+ send(:disconnected)
100
+ end
25
101
  end
26
102
 
27
- def disconnected
103
+ def _run_action_callbacks(context:, &block)
104
+ @_action_callback_context = context
105
+
106
+ run_callbacks(:action, &block)
107
+ ensure
108
+ # `@_action_callback_context = nil` would still appear in the state
109
+ remove_instance_variable(:@_action_callback_context)
28
110
  end
29
111
  end
30
112
  end
@@ -47,10 +47,12 @@ module Motion
47
47
  raise MotionNotMapped.new(self, motion)
48
48
  end
49
49
 
50
- if method(handler).arity.zero?
51
- send(handler)
52
- else
53
- send(handler, event)
50
+ _run_action_callbacks(context: handler) do
51
+ if method(handler).arity.zero?
52
+ send(handler)
53
+ else
54
+ send(handler, event)
55
+ end
54
56
  end
55
57
  end
56
58
 
@@ -49,7 +49,9 @@ module Motion
49
49
  def process_periodic_timer(name)
50
50
  return unless (handler, _interval = _periodic_timers[name])
51
51
 
52
- send(handler)
52
+ _run_action_callbacks(context: handler) do
53
+ send(handler)
54
+ end
53
55
  end
54
56
 
55
57
  private
@@ -29,22 +29,28 @@ module Motion
29
29
 
30
30
  def render_in(view_context)
31
31
  raise BlockNotAllowedError, self if block_given?
32
- clear_awaiting_forced_rerender!
33
32
 
34
- html = view_context.capture { without_new_instance_variables { super } }
33
+ html =
34
+ _run_action_callbacks(context: :render) {
35
+ _clear_awaiting_forced_rerender!
36
+
37
+ view_context.capture { _without_new_instance_variables { super } }
38
+ }
39
+
40
+ raise RenderAborted, self if html == false
35
41
 
36
42
  Motion.markup_transformer.add_state_to_html(self, html)
37
43
  end
38
44
 
39
45
  private
40
46
 
41
- def clear_awaiting_forced_rerender!
47
+ def _clear_awaiting_forced_rerender!
42
48
  return unless awaiting_forced_rerender?
43
49
 
44
50
  remove_instance_variable(RERENDER_MARKER_IVAR)
45
51
  end
46
52
 
47
- def without_new_instance_variables
53
+ def _without_new_instance_variables
48
54
  existing_instance_variables = instance_variables
49
55
 
50
56
  yield
@@ -24,13 +24,13 @@ module Motion
24
24
  timing("Connected") do
25
25
  @render_hash = component.render_hash
26
26
 
27
- component.connected
27
+ component.process_connect
28
28
  end
29
29
  end
30
30
 
31
31
  def close
32
32
  timing("Disconnected") do
33
- component.disconnected
33
+ component.process_disconnect
34
34
  end
35
35
 
36
36
  true
@@ -72,6 +72,10 @@ module Motion
72
72
  RevisionCalculator.new(revision_paths: revision_paths).perform
73
73
  end
74
74
 
75
+ # TODO: Is this always the correct key?
76
+ WARDEN_ENV = "warden"
77
+ private_constant :WARDEN_ENV
78
+
75
79
  option :renderer_for_connection_proc do
76
80
  ->(websocket_connection) do
77
81
  require "rack"
@@ -90,12 +94,15 @@ module Motion
90
94
  controller.renderer.new(
91
95
  websocket_connection.env.slice(
92
96
  Rack::HTTP_COOKIE,
93
- Rack::RACK_SESSION
97
+ Rack::RACK_SESSION,
98
+ WARDEN_ENV
94
99
  )
95
100
  )
96
101
  end
97
102
  end
98
103
 
104
+ option(:error_notification_proc) { nil }
105
+
99
106
  option(:key_attribute) { "data-motion-key" }
100
107
  option(:state_attribute) { "data-motion-state" }
101
108
 
@@ -10,6 +10,7 @@ module Motion
10
10
 
11
11
  def initialize(component, message = nil)
12
12
  super(message)
13
+
13
14
  @component = component
14
15
  end
15
16
  end
@@ -20,13 +21,13 @@ module Motion
20
21
  attr_reader :motion
21
22
 
22
23
  def initialize(component, motion)
23
- super(component, <<~MSG)
24
- No component motion handler mapped for motion '#{motion}' in component #{component.class}.
25
-
26
- Fix: Add the following to #{component.class}:
27
-
28
- map_motion :#{motion}
29
- MSG
24
+ super(
25
+ component,
26
+ "No component motion handler mapped for motion `#{motion}` in " \
27
+ "component `#{component.class}`.\n" \
28
+ "\n" \
29
+ "Hint: Consider adding `map_motion :#{motion}` to `#{component.class}`."
30
+ )
30
31
 
31
32
  @motion = motion
32
33
  end
@@ -34,20 +35,32 @@ module Motion
34
35
 
35
36
  class BlockNotAllowedError < ComponentRenderingError
36
37
  def initialize(component)
37
- super(component, <<~MSG)
38
- Motion does not support rendering with a block.
39
-
40
- Fix: Use a plain component and wrap with a motion component.
41
- MSG
38
+ super(
39
+ component,
40
+ "Motion does not support rendering with a block.\n" \
41
+ "\n" \
42
+ "Hint: Try wrapping a plain component with a motion component."
43
+ )
42
44
  end
43
45
  end
44
46
 
45
47
  class MultipleRootsError < ComponentRenderingError
46
48
  def initialize(component)
47
- super(component, <<~MSG)
48
- The template for #{component.class} can only have one root element.
49
+ super(
50
+ component,
51
+ "The template for #{component.class} can only have one root " \
52
+ "element.\n" \
53
+ "\n" \
54
+ "Hint: Wrap all elements in a single element, such as `<div>` or " \
55
+ "`<section>`."
56
+ )
57
+ end
58
+ end
49
59
 
50
- Fix: Wrap all elements in a single element, such as <div> or <section>.
60
+ class RenderAborted < ComponentRenderingError
61
+ def initialize(component)
62
+ super(component, <<~MSG)
63
+ Rendering #{component.class} was aborted by a callback.
51
64
  MSG
52
65
  end
53
66
  end
@@ -56,18 +69,19 @@ module Motion
56
69
 
57
70
  class UnrepresentableStateError < InvalidComponentStateError
58
71
  def initialize(component, cause)
59
- super(component, <<~MSG)
60
- Some state prevented #{component.class} from being serialized into a
61
- string. Motion components must be serializable using Marshal.dump. Many
62
- types of objects are not serializable including procs, references to
63
- anonymous classes, and more. See the documentation for Marshal.dump for
64
- more information.
65
-
66
- Fix: Ensure that any exotic state variables in #{component.class} are
67
- removed or replaced.
68
-
69
- The specific (but probably useless) error from Marshal was: #{cause}
70
- MSG
72
+ super(
73
+ component,
74
+ "Some state prevented `#{component.class}` from being serialized " \
75
+ "into a string. Motion components must be serializable using " \
76
+ "`Marshal.dump`. Many types of objects are not serializable " \
77
+ "including procs, references to anonymous classes, and more. See the " \
78
+ "documentation for `Marshal.dump` for more information.\n" \
79
+ "\n" \
80
+ "The specific error from `Marshal.dump` was: #{cause}\n" \
81
+ "\n" \
82
+ "Hint: Ensure that any exotic state variables in " \
83
+ "`#{component.class}` are removed or replaced."
84
+ )
71
85
  end
72
86
  end
73
87
 
@@ -75,44 +89,48 @@ module Motion
75
89
 
76
90
  class InvalidSerializedStateError < SerializedComponentError
77
91
  def initialize
78
- super(<<~MSG)
79
- The serialized state of your component is not valid.
80
-
81
- Fix: Ensure that you have not tampered with the DOM.
82
- MSG
92
+ super(
93
+ "The serialized state of your component is not valid.\n" \
94
+ "\n" \
95
+ "Hint: Ensure that you have not tampered with the contents of data " \
96
+ "attributes added by Motion in the DOM or changed the value of " \
97
+ "`Motion.config.secret`."
98
+ )
83
99
  end
84
100
  end
85
101
 
86
- class IncorrectRevisionError < SerializedComponentError
87
- attr_reader :expected_revision,
88
- :actual_revision
89
-
90
- def initialize(expected_revision, actual_revision)
91
- super(<<~MSG)
92
- Cannot mount a component from another version of the application.
93
-
94
- Expected revision `#{expected_revision}`;
95
- Got `#{actual_revision}`
96
-
97
- Read more: https://github.com/unabridged/motion/wiki/IncorrectRevisionError
98
-
99
- Fix:
100
- * Avoid tampering with Motion DOM elements and data attributes (e.g. data-motion-state).
101
- * In production, enforce a page refresh for pages with Motion components on deploy.
102
- MSG
103
-
104
- @expected_revision = expected_revision
105
- @actual_revision = actual_revision
102
+ class UpgradeNotImplementedError < ComponentError
103
+ attr_reader :previous_revision,
104
+ :current_revision
105
+
106
+ def initialize(component, previous_revision, current_revision)
107
+ super(
108
+ component,
109
+ "Cannot upgrade `#{component.class}` from a previous revision of the " \
110
+ "application (#{previous_revision}) to the current revision of the " \
111
+ "application (#{current_revision})\n" \
112
+ "\n" \
113
+ "By default, Motion does not allow components from other revisions " \
114
+ "of the application to be mounted because new code with old state " \
115
+ "can lead to unpredictable and unsafe behavior.\n" \
116
+ "\n" \
117
+ "Hint: If you would like to allow this component to surive " \
118
+ "deployments, consider providing an alternative implimentation for " \
119
+ "`#{component.class}.upgrade_from`."
120
+ )
121
+
122
+ @previous_revision = previous_revision
123
+ @current_revision = current_revision
106
124
  end
107
125
  end
108
126
 
109
127
  class AlreadyConfiguredError < Error
110
128
  def initialize
111
- super(<<~MSG)
112
- Motion is already configured.
113
-
114
- Fix: Move all Motion config to config/initializers/motion.rb.
115
- MSG
129
+ super(
130
+ "Motion is already configured.\n" \
131
+ "\n" \
132
+ "Hint: Move all Motion config to `config/initializers/motion.rb`."
133
+ )
116
134
  end
117
135
  end
118
136
 
@@ -120,12 +138,12 @@ module Motion
120
138
  attr_reader :server_version, :client_version
121
139
 
122
140
  def initialize(server_version, client_version)
123
- super(<<~MSG)
124
- The client version (#{client_version}) is newer than the server version
125
- (#{server_version}). Please upgrade the Motion gem.
126
-
127
- Fix: Run `bundle update motion`
128
- MSG
141
+ super(
142
+ "The client version (#{client_version}) is newer than the server " \
143
+ "version (#{server_version}). Please upgrade the Motion gem.\n" \
144
+ "\n" \
145
+ "Hint: Run `bundle add motion --version \">= #{client_version}\"`."
146
+ )
129
147
 
130
148
  @server_version = server_version
131
149
  @client_version = client_version
@@ -136,26 +154,26 @@ module Motion
136
154
  attr_reader :minimum_bytes
137
155
 
138
156
  def initialize(minimum_bytes)
139
- super(<<~MSG)
140
- The secret that you provided is not long enough. It must have at least
141
- #{minimum_bytes} bytes.
142
- MSG
157
+ super(
158
+ "The secret that you provided is not long enough. It must be at " \
159
+ "least #{minimum_bytes} bytes long."
160
+ )
143
161
  end
144
162
  end
145
163
 
146
164
  class BadRevisionError < Error
147
165
  def initialize
148
- super("The revision cannot contain a NULL byte")
166
+ super("The revision cannot contain a NULL byte.")
149
167
  end
150
168
  end
151
169
 
152
170
  class BadRevisionPathsError < Error
153
171
  def initialize
154
- super(<<~MSG)
155
- Revision paths must be a Rails::Paths::Root object or an object
156
- that responds to `all_paths.flat_map(&:existent)` and returns an
157
- Array of strings representing full paths.
158
- MSG
172
+ super(
173
+ "Revision paths must be a `Rails::Paths::Root` object or an object " \
174
+ "that responds to `all_paths.flat_map(&:existent)` and returns an " \
175
+ "Array of strings representing full paths."
176
+ )
159
177
  end
160
178
  end
161
179
  end
@@ -29,6 +29,8 @@ module Motion
29
29
  error_info = error ? ":\n#{indent(format_exception(error))}" : ""
30
30
 
31
31
  logger.error("[#{tag}] #{message}#{error_info}")
32
+
33
+ Motion.notify_error(error, message)
32
34
  end
33
35
 
34
36
  def info(message)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "nokogiri"
4
+ require "active_support/core_ext/object/blank"
4
5
 
5
6
  require "motion"
6
7
 
@@ -21,7 +22,7 @@ module Motion
21
22
  end
22
23
 
23
24
  def add_state_to_html(component, html)
24
- return html if html.blank?
25
+ return if html.blank?
25
26
 
26
27
  key, state = serializer.serialize(component)
27
28
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Motion
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alec Larsen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-07-08 00:00:00.000000000 Z
12
+ date: 2020-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -86,7 +86,7 @@ metadata:
86
86
  source_code_uri: https://github.com/unabridged/motion
87
87
  post_install_message: |
88
88
  Friendly reminder: When updating the motion gem, don't forget to update the
89
- NPM package as well (`bin/yarn add '@unabridged/motion@0.2.2'`).
89
+ NPM package as well (`bin/yarn add '@unabridged/motion@0.3.0'`).
90
90
  rdoc_options: []
91
91
  require_paths:
92
92
  - lib