Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue with notifications and recursive promises #13

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/source/promises.bs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ namespace promises.internal
' unregister any observers once the promise is completed
promises.internal.unobserveFieldScoped(originalPromise, promises.internal.PromiseField.promiseState)
promiseStorage = promises.internal.getPromiseStorage(originalPromise)
' Delete the storage for this promise since we are going to handled all of the current listeners.
' Any new listeners created as a result of the logic in the callbacks will
' register a new instance of the promise storage item. If a new storage item is created
' we will notify the new listeners when we are done with the current ones.
promises.internal.clearPromiseStorage(originalPromise)

promiseState = originalPromise.promiseState
promiseResult = originalPromise.promiseResult
Expand All @@ -326,11 +331,22 @@ namespace promises.internal
'TODO giant memory leak. if you see this, delete it immediately!
m.__promises__debug.push(promiseStorage)
#end if
'delete the storage for this promise since we've handled all of the listeners
promises.internal.clearPromiseStorage(originalPromise)

if promises.internal.hasStorage(originalPromise) then
' There were listeners added as a result of some of the callback notifications
' Re-trigger the notification process for the new listeners
promises.internal.delay(sub (event as object)
promises.internal.notifyListeners(event)
end sub, event)
end if
end if
end sub

' Used to check if there is a storage item of listeners for the supplied promise
function hasStorage(promise as dynamic) as boolean
return m.doesExist("__promises__" + promise.id)
chrisdp marked this conversation as resolved.
Show resolved Hide resolved
end function

' We use an internal value to represent unset. Check if the parameter is that value
function isSet(value as dynamic) as boolean
return not (promises.internal.isNonEmptyString(value) and value = "__INVALID__")
Expand Down