turbo_reflex 0.0.29 → 0.0.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,9 @@ import renderer from '../renderer'
7
7
 
8
8
  function aborted (event) {
9
9
  const xhr = event.target
10
- dispatch(lifecycle.events.abort, document, { xhr, ...event.detail })
10
+ dispatch(lifecycle.events.abort, document, {
11
+ detail: { ...event.detail, xhr }
12
+ })
11
13
  }
12
14
 
13
15
  function errored (event) {
@@ -22,7 +24,7 @@ function errored (event) {
22
24
  dispatch(
23
25
  lifecycle.events.clientError,
24
26
  document,
25
- { xhr, ...event.detail, error },
27
+ { detail: { ...event.detail, error, xhr } },
26
28
  true
27
29
  )
28
30
  }
@@ -14,17 +14,11 @@ export const stateEvents = {
14
14
 
15
15
  export const allEvents = { ...lifecycleEvents, ...stateEvents }
16
16
 
17
- export function dispatch (name, target = document, detail = {}, raise = false) {
18
- try {
19
- target = target || document
20
- const event = new CustomEvent(name, {
21
- detail,
22
- cancelable: false,
23
- bubbles: true
24
- })
25
- target.dispatchEvent(event)
26
- } catch (error) {
27
- if (raise) throw error
28
- dispatch(lifecycleEvents.clientError, target, { error, ...detail }, true)
29
- }
17
+ export function dispatch (name, target, options = {}) {
18
+ options = options || {}
19
+ options.detail = options.detail || {}
20
+ target = target || document
21
+ const evt = new CustomEvent(name, { ...options, bubbles: true })
22
+ target.dispatchEvent(evt)
23
+ return evt
30
24
  }
@@ -13,6 +13,16 @@ import state from './state'
13
13
  import urls from './urls'
14
14
  import uuids from './uuids'
15
15
 
16
+ function buildReflexPayload (id, element) {
17
+ return {
18
+ id, // reflex id
19
+ name: element.dataset.turboReflex, // reflex name
20
+ elementId: element.id.length > 0 ? element.id : null,
21
+ elementAttributes: elements.buildAttributePayload(element),
22
+ startedAt: new Date().getTime()
23
+ }
24
+ }
25
+
16
26
  function invokeReflex (event) {
17
27
  let element
18
28
  let payload = {}
@@ -22,22 +32,38 @@ function invokeReflex (event) {
22
32
  if (!element) return
23
33
  if (!delegates.isRegisteredForElement(event.type, element)) return
24
34
 
25
- const driver = drivers.find(element)
35
+ const reflexId = `reflex-${uuids.v4()}`
36
+ let driver = drivers.find(element)
37
+ let payload = {
38
+ ...buildReflexPayload(reflexId, element),
39
+ driver: driver.name,
40
+ frameId: driver.frame ? driver.frame.id : null,
41
+ src: driver.src
42
+ }
43
+
44
+ const startEvent = dispatch(lifecycle.events.start, element, {
45
+ cancelable: true,
46
+ detail: payload
47
+ })
48
+
49
+ if (startEvent.defaultPrevented)
50
+ return dispatch(lifecycle.events.abort, element, {
51
+ detail: {
52
+ message: `An event handler for '${lifecycle.events.start}' prevented default behavior and blocked reflex invocation!`,
53
+ source: startEvent
54
+ }
55
+ })
26
56
 
27
- // payload sent to server (also used for lifecycle event.detail)
57
+ // the element and thus the driver may have changed based on the start event handler(s)
58
+ driver = drivers.find(element)
28
59
  payload = {
29
- id: `reflex-${uuids.v4()}`,
30
- name: element.dataset.turboReflex,
60
+ ...buildReflexPayload(reflexId, element),
31
61
  driver: driver.name,
32
- src: driver.src,
33
62
  frameId: driver.frame ? driver.frame.id : null,
34
- elementId: element.id.length > 0 ? element.id : null,
35
- elementAttributes: elements.buildAttributePayload(element),
36
- startedAt: new Date().getTime()
63
+ src: driver.src
37
64
  }
38
65
 
39
66
  activity.add(payload)
40
- dispatch(lifecycle.events.start, element, payload)
41
67
 
42
68
  if (['frame', 'window'].includes(driver.name)) event.preventDefault()
43
69
 
@@ -56,8 +82,7 @@ function invokeReflex (event) {
56
82
  }
57
83
  } catch (error) {
58
84
  dispatch(lifecycle.events.clientError, element, {
59
- error,
60
- ...payload
85
+ detail: { ...payload, error }
61
86
  })
62
87
  }
63
88
  }
@@ -4,7 +4,10 @@ import { dispatch, lifecycleEvents as events } from './events'
4
4
  function finish (event) {
5
5
  event.detail.endedAt = new Date().getTime()
6
6
  event.detail.milliseconds = event.detail.endedAt - event.detail.startedAt
7
- setTimeout(() => dispatch(events.finish, event.target, event.detail), 20)
7
+ setTimeout(
8
+ () => dispatch(events.finish, event.target, { detail: event.detail }),
9
+ 25
10
+ )
8
11
  }
9
12
 
10
13
  addEventListener(events.serverError, finish)
@@ -13,7 +13,9 @@ function loadState () {
13
13
  loadedState = { ...currentState }
14
14
  delete meta.element.dataset.clientStateChange
15
15
  setTimeout(() =>
16
- dispatch(events.stateLoad, meta.element, { state: currentState })
16
+ dispatch(events.stateLoad, meta.element, {
17
+ detail: { state: currentState }
18
+ })
17
19
  )
18
20
  }
19
21
 
@@ -9,13 +9,13 @@ function observable (object, parent = null) {
9
9
  const proxy = new Proxy(object, {
10
10
  deleteProperty (target, key) {
11
11
  delete target[key]
12
- dispatch(events.stateChange, meta.element, { state: head })
12
+ dispatch(events.stateChange, meta.element, { detail: { state: head } })
13
13
  return true
14
14
  },
15
15
 
16
16
  set (target, key, value, receiver) {
17
17
  target[key] = observable(value, this)
18
- dispatch(events.stateChange, meta.element, { state: head })
18
+ dispatch(events.stateChange, meta.element, { detail: { state: head } })
19
19
  return true
20
20
  }
21
21
  })
@@ -45,7 +45,7 @@ addEventListener('turbo:before-fetch-response', event => {
45
45
  dispatch(
46
46
  lifecycle.events.clientError,
47
47
  document,
48
- { ...event.detail, error },
48
+ { detail: { ...event.detail, error } },
49
49
  true
50
50
  )
51
51
  }
@@ -17,11 +17,14 @@ require_relative "attribute_set"
17
17
  # * render ...................... Renders Rails templates, partials, etc. (doesn't halt controller request handling)
18
18
  # * render_response ............. Renders a full controller response
19
19
  # * renderer .................... An ActionController::Renderer
20
- # * prevent_controller_action ... Prevents the rails controller/action from running (i.e. the reflex handles the response entirely)
21
20
  # * turbo_stream ................ A Turbo Stream TagBuilder
22
21
  # * turbo_streams ............... A list of Turbo Streams to append to the response (also aliased as streams)
23
22
  # * state ....................... An object that stores ephemeral `state`
24
23
  #
24
+ # They also have access to the following class methods:
25
+ #
26
+ # * prevent_controller_action ... Prevents the rails controller/action from running (i.e. the reflex handles the response entirely)
27
+ #
25
28
  class TurboReflex::Base
26
29
  class << self
27
30
  def preventers
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "turbo-rails"
4
+ require "turbo_ready"
4
5
  require_relative "version"
5
6
  require_relative "base"
6
7
  require_relative "controller_pack"
8
+ require_relative "../../app/controllers/concerns/turbo_reflex/controller"
7
9
 
8
10
  module TurboReflex
9
11
  def self.config
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TurboReflex
4
- VERSION = "0.0.29"
4
+ VERSION = "0.0.31"
5
5
  end
data/lib/turbo_reflex.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "turbo_ready"
3
4
  require "turbo_reflex/engine"
data/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "turbo_reflex",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "Reflexes for Turbo Frames that help you build robust reactive applications",
5
5
  "main": "app/javascript/index.js",
6
6
  "repository": "https://github.com/hopsoft/turbo_reflex",
7
7
  "author": "Nate Hopkins (hopsoft) <natehop@gmail.com>",
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
- "turbo_ready": ">= 0.1.3"
10
+ "turbo_ready": ">= 0.1.4"
11
11
  },
12
12
  "peerDependencies": {
13
13
  "@hotwired/turbo-rails": ">= 7.2.0"