Skip to content

Commit

Permalink
remove Promise.done() since onunhandledrejection does it authomatically
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarMirzaie committed Aug 5, 2022
1 parent a359d8c commit 390b34f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
8 changes: 0 additions & 8 deletions Signum.React/Scripts/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ declare global {
escape(s: string): string;
}

interface Promise<T> {
done(this: Promise<T>): void;
}

interface Window {
__allowNavigatorWithoutUser?: boolean;
__baseUrl: string;
Expand Down Expand Up @@ -906,10 +902,6 @@ String.prototype.repeat = function (this: string, n: number) {
return result;
};

Promise.prototype.done = function () {
this.catch(error => setTimeout(() => { throw error; }, 0));
};

export module Dic {

var simplesTypes = ["number", "boolean", "string"];
Expand Down
8 changes: 8 additions & 0 deletions Signum.React/Scripts/Modals/ErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ export default function ErrorModal(p: ErrorModalProps) {

ErrorModal.register = () => {

window.onunhandledrejection = p => {
var error = p.reason;
if (Modals.isStarted())
ErrorModal.showErrorModal(error).done();
else
console.error("Unhandled promise rejection:", error);
};

var oldOnError = window.onerror;
window.onerror = (message: Event | string, filename?: string, lineno?: number, colno?: number, error?: Error) => {

Expand Down
4 changes: 2 additions & 2 deletions Signum.React/Scripts/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export function wrapRequest(options: AjaxOptions, makeCall: () => Promise<Respon

const promise = makeCall();

if (!(promise as any).__proto__.done)
(promise as any).__proto__.done = Promise.prototype.done;
if (!(promise as any).__proto__)
(promise as any).__proto__ = Promise.prototype;

return promise;

Expand Down
2 changes: 1 addition & 1 deletion Signum.Upgrade/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void Main(string[] args)
Console.Write(" ApplicationName = "); SafeConsole.WriteLineColor(ConsoleColor.DarkGray, uctx.ApplicationName);


//UpgradeContext.DefaultIgnoreDirectories = UpgradeContext.DefaultIgnoreDirectories.Where(a => a != "Framework").ToArray();
UpgradeContext.DefaultIgnoreDirectories = UpgradeContext.DefaultIgnoreDirectories.Where(a => a != "Framework").ToArray();

new CodeUpgradeRunner(autoDiscover: true).Run(uctx);
}
Expand Down
15 changes: 15 additions & 0 deletions Signum.Upgrade/Upgrades/Upgrade_20220805_DoneIsDone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Signum.Upgrade.Upgrades;

class Upgrade_20220805_DoneIsDone : CodeUpgradeBase
{
public override string Description => "Remove .done() (rejectionhandled does it)";

public static Regex DoneRegex = new Regex(@"\s*\.done\(\)");
public override void Execute(UpgradeContext uctx)
{
uctx.ForeachCodeFile(@"*.tsx, *.ts", file =>
{
file.Replace(DoneRegex, "");
});
}
}

1 comment on commit 390b34f

@olmobrutall
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Promise.done()

This commit removes the method done previously injected into the Promise.prototype that was responsible for throwing the rejection error into the event loop to, ultimately, show an error message to the user.

This functionality can now be automatically archived using the unhandledrejection event that is available in every browser but IE

This also means that async/await can not be used more aggresively. For example a void returning method like this:

function handleOnClick(e : React.MouseEvent){
    Operations.API.executeClick(ctx.value, MyEntityOperation.Save)
    .then(e => reload())
    .done();
}

Was not worth to write it with async/await because of the done method and the radical transpilation required for IE.

Now all this is gone so feel free to write this instead:

async function handleOnClick(e : React.MouseEvent){
  await Operations.API.executeClick(ctx.value, MyEntityOperation.Save);
  reload();
}

How to Upgrade

The Signum.Upgrade Upgrade_20220805_DoneIsDone should remove al your done() calls, like this: e727d00

If you want to replace .then() for async / await you will need to do it manually in a case-by-case basis.

Emjoy

Please sign in to comment.