Skip to content

Commit 80c1c14

Browse files
committed
Fixed an issue with notifications and recursive promises
1 parent 674aa38 commit 80c1c14

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/source/promises.bs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ namespace promises.internal
301301
' unregister any observers once the promise is completed
302302
promises.internal.unobserveFieldScoped(originalPromise, promises.internal.PromiseField.promiseState)
303303
promiseStorage = promises.internal.getPromiseStorage(originalPromise)
304+
' Delete the storage for this promise since we are going to handled all of the current listeners.
305+
' Any new listeners created as a result of the logic in the callbacks will
306+
' register a new instance of the promise storage item. If a new storage item is created
307+
' we will notify the new listeners when we are done with the current ones.
308+
promises.internal.clearPromiseStorage(originalPromise)
304309

305310
promiseState = originalPromise.promiseState
306311
promiseResult = originalPromise.promiseResult
@@ -326,11 +331,22 @@ namespace promises.internal
326331
'TODO giant memory leak. if you see this, delete it immediately!
327332
m.__promises__debug.push(promiseStorage)
328333
#end if
329-
'delete the storage for this promise since we've handled all of the listeners
330-
promises.internal.clearPromiseStorage(originalPromise)
334+
335+
if promises.internal.hasStorage(originalPromise) then
336+
' There were listeners added as a result of some of the callback notifications
337+
' Re-trigger the notification process for the new listeners
338+
promises.internal.delay(sub (event as object)
339+
promises.internal.notifyListeners(event)
340+
end sub, event)
341+
end if
331342
end if
332343
end sub
333344

345+
' Used to check if there is a storage item of listeners for the supplied promise
346+
function hasStorage(promise as dynamic) as boolean
347+
return m.doesExist("__promises__" + promise.id)
348+
end function
349+
334350
' We use an internal value to represent unset. Check if the parameter is that value
335351
function isSet(value as dynamic) as boolean
336352
return not (promises.internal.isNonEmptyString(value) and value = "__INVALID__")

0 commit comments

Comments
 (0)