Skip to content

Commit

Permalink
fixing issue with rxjava based promise implementations not handling a…
Browse files Browse the repository at this point in the history
… void/null promise return time
  • Loading branch information
davydotcom committed Jan 15, 2025
1 parent c4c29da commit 6f020ce
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class RxPromise<T> implements Promise<T> {
protected boolean finished = false

RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {

this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
try {
singleSubscriber.onSuccess((T)callable.call())
singleSubscriber.onSuccess((T)runCallable(callable))
} catch (Throwable t) {
singleSubscriber.onError(t)
}
Expand Down Expand Up @@ -158,4 +159,13 @@ class RxPromise<T> implements Promise<T> {
}
}
}

static Object runCallable(Closure callable) {
Object rtn = callable.call()
if(rtn == null) {
return Void
} else {
return rtn
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ class RxPromiseSpec extends Specification {
result == [one: 1, two: 2, four: 4]
}

void 'Test promise null handling'() {

when: 'a promise map is created'
def promise = Promises.createPromise {
sleep 1000
return null
}
def result = promise.get()

then: 'result is void'
result == Void
}

void 'Test promise list handling'() {

when: 'a promise list is created from two promises'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RxPromise<T> implements Promise<T> {
RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {
this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
try {
singleSubscriber.onSuccess((T)callable.call())
singleSubscriber.onSuccess((T)runCallable(callable))
} catch (Throwable t) {
singleSubscriber.onError(t)
}
Expand Down Expand Up @@ -156,4 +156,13 @@ class RxPromise<T> implements Promise<T> {
}
}
}

static Object runCallable(Closure callable) {
Object rtn = callable.call()
if(rtn == null) {
return Void
} else {
return rtn
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ class RxPromiseSpec extends Specification {

}

void 'Test promise null handling'() {

when: 'a promise map is created'
def promise = Promises.createPromise {
sleep 1000
return null
}
def result = promise.get()

then: 'result is void'
result == Void
}

void 'Test promise map handling'() {

when: 'a promise map is created'
Expand Down

0 comments on commit 6f020ce

Please sign in to comment.