Class: Mayu::Component::Wrapper
- Inherits:
-
Object
- Object
- Mayu::Component::Wrapper
- Extended by:
- T::Sig
- Defined in:
- lib/mayu/component/wrapper.rb
Constant Summary collapse
- UpdateProc =
T.type_alias do T.any( T.proc.params(arg0: State).returns(State), T.proc.params(kwargs: T.untyped).returns(State) ) end
Instance Attribute Summary collapse
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#next_state ⇒ Object
readonly
Returns the value of attribute next_state.
-
#props ⇒ Object
Returns the value of attribute props.
-
#state ⇒ Object
Returns the value of attribute state.
-
#vnode ⇒ Object
readonly
Returns the value of attribute vnode.
Instance Method Summary collapse
- #assets ⇒ Object
- #async(&blk) ⇒ Object
- #did_update(prev_props, prev_state) ⇒ Object
- #dirty! ⇒ Object
- #dirty? ⇒ Boolean
-
#initialize(vnode, klass, props = {}) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #marshal_dump ⇒ Object
- #marshal_load(a) ⇒ Object
- #mount ⇒ Object
- #render ⇒ Object
- #resource ⇒ Object
- #should_update?(next_props, next_state) ⇒ Boolean
- #unmount ⇒ Object
- #update(new_state = nil, &block) ⇒ Object
- #vnode_id ⇒ Object
Constructor Details
#initialize(vnode, klass, props = {}) ⇒ Wrapper
Returns a new instance of Wrapper.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mayu/component/wrapper.rb', line 45 def initialize(vnode, klass, props = {}) @vnode = vnode @props = T.let(props, Props) @state = T.let(klass.get_initial_state(**props), State) @next_state = T.let(@state.dup, State) @dirty = T.let(true, T::Boolean) @instance = T.let(klass.new(self), Base) = T.let(Async::Barrier.new, Async::Barrier) @helpers = T.let(Helpers.new(self), Helpers) end |
Instance Attribute Details
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
30 31 32 |
# File 'lib/mayu/component/wrapper.rb', line 30 def helpers @helpers end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
32 33 34 |
# File 'lib/mayu/component/wrapper.rb', line 32 def instance @instance end |
#next_state ⇒ Object (readonly)
Returns the value of attribute next_state.
27 28 29 |
# File 'lib/mayu/component/wrapper.rb', line 27 def next_state @next_state end |
#props ⇒ Object
Returns the value of attribute props.
23 24 25 |
# File 'lib/mayu/component/wrapper.rb', line 23 def props @props end |
#state ⇒ Object
Returns the value of attribute state.
25 26 27 |
# File 'lib/mayu/component/wrapper.rb', line 25 def state @state end |
#vnode ⇒ Object (readonly)
Returns the value of attribute vnode.
40 41 42 |
# File 'lib/mayu/component/wrapper.rb', line 40 def vnode @vnode end |
Instance Method Details
#assets ⇒ Object
57 58 59 |
# File 'lib/mayu/component/wrapper.rb', line 57 def assets @instance.class.assets end |
#async(&blk) ⇒ Object
105 106 107 |
# File 'lib/mayu/component/wrapper.rb', line 105 def async(&blk) .async(&blk) end |
#did_update(prev_props, prev_state) ⇒ Object
74 75 76 |
# File 'lib/mayu/component/wrapper.rb', line 74 def did_update(prev_props, prev_state) async { @instance.did_update(prev_props, prev_state) } end |
#dirty! ⇒ Object
37 |
# File 'lib/mayu/component/wrapper.rb', line 37 def dirty! = @dirty = true |
#dirty? ⇒ Boolean
35 |
# File 'lib/mayu/component/wrapper.rb', line 35 def dirty? = @dirty |
#marshal_dump ⇒ Object
141 142 143 144 145 146 |
# File 'lib/mayu/component/wrapper.rb', line 141 def marshal_dump [ VDOM::Marshalling.dump_props(@props), VDOM::Marshalling.dump_state(@state) ] end |
#marshal_load(a) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/mayu/component/wrapper.rb', line 149 def marshal_load(a) @props, @state = a @next_state = @state.clone @dirty = true = Async::Barrier.new end |
#mount ⇒ Object
69 70 71 |
# File 'lib/mayu/component/wrapper.rb', line 69 def mount async { @instance.mount } end |
#render ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mayu/component/wrapper.rb', line 86 def render if derived_state = @instance.class.get_derived_state_from_props(props, state) @state = @state.merge(derived_state) end @instance.render rescue NotImplementedError => e raise NotImplementedError, "#{@instance} should implement #render" ensure @dirty = false end |
#resource ⇒ Object
62 63 64 65 66 |
# File 'lib/mayu/component/wrapper.rb', line 62 def resource if @instance.class.respond_to?(:__resource) @instance.class.send(:__resource) end end |
#should_update?(next_props, next_state) ⇒ Boolean
100 101 102 |
# File 'lib/mayu/component/wrapper.rb', line 100 def should_update?(next_props, next_state) @dirty || @instance.should_update?(next_props, next_state) end |
#unmount ⇒ Object
79 80 81 82 83 |
# File 'lib/mayu/component/wrapper.rb', line 79 def unmount @instance.unmount ensure .stop end |
#update(new_state = nil, &block) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mayu/component/wrapper.rb', line 112 def update(new_state = nil, &block) if new_state @next_state = @next_state.merge(new_state) enqueue_update! end return unless block if block.parameters in [[:opt, var]] Console.logger.warn(self, " update do |\#{var}|\n # Are you sure you didn't misspell `\#{var}`?\n # Usually it should be called `state`.\n end\n EOF\n\n update(block.call(@next_state))\n else\n if block.parameters.all? { _1 in [:key | :keyreq, key] }\n keys = block.parameters.map(&:last)\n sliced_state = T.unsafe(@next_state).slice(*keys)\n update(block.call(**sliced_state))\n else\n raise ArgumentError, \"All arguments to #update are not keys.\"\n end\n end\nend\n") unless var == :state |
#vnode_id ⇒ Object
20 |
# File 'lib/mayu/component/wrapper.rb', line 20 def vnode_id = @vnode.id |