Class: Mayu::State::ActionCreator::AsyncActionCreator

Inherits:
Base
  • Object
show all
Defined in:
lib/mayu/state/action_creator.rb

Constant Summary collapse

ProcType =
T.type_alias do
  T.proc.params(arg0: Store, args: T.untyped, kwargs: T.untyped).void
end

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Base

#===

Constructor Details

#initialize(type, &block) ⇒ AsyncActionCreator

Returns a new instance of AsyncActionCreator.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mayu/state/action_creator.rb', line 80

def initialize(type, &block)
  super(type)
  @block = block
  @pending =
    T.let(
      StaticActionCreator.new(:"#{type}/pending"),
      StaticActionCreator
    )
  @fulfilled =
    T.let(
      StaticActionCreator.new(:"#{type}/fulfilled"),
      StaticActionCreator
    )
  @rejected =
    T.let(
      StaticActionCreator.new(:"#{type}/rejected"),
      StaticActionCreator
    )
end

Instance Attribute Details

#fulfilledObject (readonly)

Returns the value of attribute fulfilled.



75
76
77
# File 'lib/mayu/state/action_creator.rb', line 75

def fulfilled
  @fulfilled
end

#pendingObject (readonly)

Returns the value of attribute pending.



73
74
75
# File 'lib/mayu/state/action_creator.rb', line 73

def pending
  @pending
end

#rejectedObject (readonly)

Returns the value of attribute rejected.



77
78
79
# File 'lib/mayu/state/action_creator.rb', line 77

def rejected
  @rejected
end

Instance Method Details

#call(*args, parent: Async::Task.current, **kwargs) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mayu/state/action_creator.rb', line 105

def call(*args, parent: Async::Task.current, **kwargs)
  ->(store) do
    parent.async do |task|
      request_id = Nanoid.generate()
      store.dispatch(pending, *args, **kwargs.merge(request_id:))
      result =
        T.unsafe(@block).call(
          store,
          *args,
          **kwargs.merge(task:, request_id:)
        )
      store.dispatch(fulfilled, result)
    rescue => error
      store.dispatch(rejected, { error:, request_id: })
    end
  end
end