From 80c1c14e97f8ff00103ced0606cd929823ddb7b9 Mon Sep 17 00:00:00 2001 From: Christopher Dwyer-Perkins Date: Wed, 13 Dec 2023 09:55:19 -0400 Subject: [PATCH 1/2] Fixed an issue with notifications and recursive promises --- src/source/promises.bs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/source/promises.bs b/src/source/promises.bs index b75fb56..deb81c1 100644 --- a/src/source/promises.bs +++ b/src/source/promises.bs @@ -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 @@ -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) + 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__") From 651f4ff2f7210256469bca35bb54d9f354a5b6ff Mon Sep 17 00:00:00 2001 From: Christopher Dwyer-Perkins Date: Wed, 13 Dec 2023 10:01:17 -0400 Subject: [PATCH 2/2] Formatting --- src/source/promises.bs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/source/promises.bs b/src/source/promises.bs index deb81c1..73e513f 100644 --- a/src/source/promises.bs +++ b/src/source/promises.bs @@ -344,8 +344,8 @@ namespace promises.internal ' 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) - end function + return m.doesExist("__promises__" + promise.id) + end function ' We use an internal value to represent unset. Check if the parameter is that value function isSet(value as dynamic) as boolean