Currently, I am developing the client-side of a web application using TypeScript. Within my own makeshift "framework," I have crafted a class called ViewBinder which employs an asynchronous "show" method to fetch data from the server, extract HTML content, and inject it into the document within a designated "placeholder" element. Subsequent classes derived from this one can then link data from a client state object to the loaded HTML.
Below is a snippet of the code from this class for reference purposes (please note that it is not executable):
export class ViewBinder extends PropertyChangedNotifier {
// Omitted a lot of member details for brevity...
public async show(callbackAfterShow?: (wasSuccessful: boolean) => void): Promise<HtmlLoadViewResult> {
// More code here...
}
public clear(): void {
// Further code...
}
}
In instances where the client state object undergoes modifications while a ViewBinder is in the midst of executing the 'show' command (during either 'loadView' or 'setupAfterShowing'), resulting in invoking the 'clear' method, the issue arises. This inadvertently eliminates the parent element's content where the HTML was planned to be inserted and data bound.
If the ViewBinder encounters difficulty locating a parent element or finding a suitable location within that element to exhibit data, I view it as a flaw and throw an error. Nonetheless, in certain scenarios, the HTML will be legitimately removed while the asynchronous code awaits outcomes.
I have experimented with utilizing 'showWasCanceled' to avoid potential problems, but there are numerous complexities within derived 'setupAfterShowing' methods which could lead to inconsistencies in abandoning operations if 'showWasCanceled' is true.
Therefore, my question poses:
Is there a way for the 'clear' function to ascertain whether the 'show' function is currently being awaited and halt execution until 'show' has completed?