Skip to content

Commit 07510fc

Browse files
committed
add general constructor
1 parent 248762b commit 07510fc

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lib/concurrent/edge/promises.rb

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,46 @@ def resolved_event(default_executor = :io)
127127
ImmediateEventPromise.new(default_executor).event
128128
end
129129

130+
# General constructor. Behaves differently based on the argument's type. It's provided for convenience
131+
# but it's better to be explicit.
132+
#
133+
# @see rejected_future, resolved_event, fulfilled_future
134+
# @!macro promises.param.default_executor
135+
# @return [Event, Future]
136+
#
137+
# @overload create(nil, default_executor = :io)
138+
# @param [nil] nil
139+
# @return [Event] resolved event.
140+
#
141+
# @overload create(a_future = nil, default_executor = :io)
142+
# @param [Future] a_future
143+
# @return [Future] a future which will be resolved when a_future is.
144+
#
145+
# @overload create(an_event = nil, default_executor = :io)
146+
# @param [Event] an_event
147+
# @return [Event] an event which will be resolved when an_event is.
148+
#
149+
# @overload create(exception = nil, default_executor = :io)
150+
# @param [Exception] exception
151+
# @return [Future] a rejected future with the exception as its reason.
152+
#
153+
# @overload create(value = nil, default_executor = :io)
154+
# @param [Object] value when none of the above overloads fits
155+
# @return [Future] a fulfilled future with the value.
156+
def create(argument = nil, default_executor = :io)
157+
case argument
158+
when AbstractEventFuture
159+
# returning wrapper would change nothing
160+
argument
161+
when Exception
162+
rejected_future argument, default_executor
163+
when nil
164+
resolved_event default_executor
165+
else
166+
fulfilled_future argument, default_executor
167+
end
168+
end
169+
130170
# @!macro promises.shortcut.on
131171
# @return [Future]
132172
def delay(*args, &task)
@@ -253,8 +293,6 @@ def any_event_on(default_executor, *futures_and_or_events)
253293
AnyResolvedEventPromise.new(futures_and_or_events, default_executor).event
254294
end
255295

256-
# TODO (pitr-ch 30-Jul-2016): add general constructor behaving based on argument type
257-
258296
# TODO consider adding first(count, *futures)
259297
# TODO consider adding zip_by(slice, *futures) processing futures in slices
260298
end

0 commit comments

Comments
 (0)