Skip to content

Commit

Permalink
fix confirmation error using async await (change target to ES2017)
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Nov 29, 2021
1 parent b77dcbf commit 9caeecf
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 33 deletions.
22 changes: 12 additions & 10 deletions Signum.React.Extensions/Workflow/Case/CaseFrameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ export default class CaseFrameModal extends React.Component<CaseFrameModalProps,
allowExchangeEntity: false,
prefix: this.prefix,
isExecuting: () => this.state.executing == true,
execute: action => {
execute: async action => {
if (this.state.executing)
return;

this.setState({ executing: true });
action()
.finally(() => this.setState({ executing: undefined }))
.done();
try {
await action();
} finally {
this.setState({ executing: undefined });
}
}

};

var activityPack = { entity: pack.activity, canExecute: pack.canExecuteActivity };
Expand Down Expand Up @@ -243,15 +244,16 @@ export default class CaseFrameModal extends React.Component<CaseFrameModalProps,
allowExchangeEntity: false,
prefix: this.prefix,
isExecuting: () => this.state.executing == true,
execute: action => {
execute: async action => {
if (this.state.executing)
return;

this.setState({ executing: true });

action()
.finally(() => this.setState({ executing: undefined }))
.done();
try {
await action();
} finally {
this.setState({ executing: undefined })
}
}
};

Expand Down
20 changes: 12 additions & 8 deletions Signum.React.Extensions/Workflow/Case/CaseFramePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ export default class CaseFramePage extends React.Component<CaseFramePageProps, C
allowExchangeEntity: false,
prefix: "caseFrame",
isExecuting: () => this.state.executing == true,
execute: action => {
execute: async action => {
if (this.state.executing)
return;

this.setState({ executing: true });
action()
.finally(() => { this.setState({ executing: undefined }) })
.done();
try {
await action();
} finally {
this.setState({ executing: undefined });
}
}
};

Expand Down Expand Up @@ -245,14 +247,16 @@ export default class CaseFramePage extends React.Component<CaseFramePageProps, C
allowExchangeEntity: false,
prefix: "caseFrame",
isExecuting: () => this.state.executing == true,
execute: action => {
execute: async action => {
if (this.state.executing)
return;

this.setState({ executing: true });
action()
.finally(() => this.setState({ executing: undefined }))
.done();
try {
await action();
} finally {
this.setState({ executing: undefined });
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion Signum.React.Extensions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2017",
"sourceMap": true,
"module": "esnext",
"moduleResolution": "node",
Expand Down
15 changes: 8 additions & 7 deletions Signum.React/Scripts/Frames/FrameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,19 @@ export const FrameModal = React.forwardRef(function FrameModal(p: FrameModalProp
allowExchangeEntity: p.buttons == "close" && (p.allowExchangeEntity ?? true),
prefix: prefix,
isExecuting: () => pc.executing == true,
execute: action => {
execute: async action => {
if (pc.executing)
return;

pc.executing = true;
forceUpdate();
action()
.finally(() => {
pc.executing = undefined;
forceUpdate();
})
.done();
try {
await action();

} finally {
pc.executing = undefined;
forceUpdate();
}
}
};

Expand Down
10 changes: 7 additions & 3 deletions Signum.React/Scripts/Frames/FramePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,18 @@ export default function FramePage(p: FramePageProps) {
entityComponent: entityComponent.current,
pack: state.pack,
isExecuting: () => state.executing == true,
execute: action => {
execute: async action => {
if (state.executing)
return;

state.executing = true;
forceUpdate();
action()
.finally(() => { state.executing = undefined; forceUpdate(); }).done();
try {
await action();
} finally {
state.executing = undefined;
forceUpdate();
}
},
onReload: (pack, reloadComponent, callback) => {

Expand Down
2 changes: 1 addition & 1 deletion Signum.React/Scripts/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class EntityOperationContext<T extends Entity> {
return this.settings.onClick(this);
else
return defaultOnClick(this);
});
}).done();
}

textOrNiceName() {
Expand Down
2 changes: 1 addition & 1 deletion Signum.React/Scripts/Reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export function createBinding(parentValue: any, lambdaMembers: LambdaMember[]):


const functionRegex = /^function\s*\(\s*([$a-zA-Z_][0-9a-zA-Z_$]*)\s*\)\s*{\s*(\"use strict\"\;)?\s*(var [^;]*;)?\s*return\s*([^;]*)\s*;?\s*}$/;
const lambdaRegex = /^\s*\(?\s*([$a-zA-Z_][0-9a-zA-Z_$]*)\s*\)?\s*=>\s*{?\s*(return\s+)?([^;]*)\s*;?\s*}?$/;
const lambdaRegex = /^\s*\(?\s*([$a-zA-Z_][0-9a-zA-Z_$]*)\s*\)?\s*=>(\s*{?\s*(return\s+)?([^;]*)\s*;?\s*}?)$/;
const memberRegex = /^(.*)\.([$a-zA-Z_][0-9a-zA-Z_$]*)$/;
const memberIndexerRegex = /^(.*)\["([$a-zA-Z_][0-9a-zA-Z_$]*)"\]$/;
const mixinMemberRegex = /^(.*)\.mixins\["([$a-zA-Z_][0-9a-zA-Z_$]*)"\]$/; //Necessary for some crazy minimizers
Expand Down
2 changes: 1 addition & 1 deletion Signum.React/Scripts/TypeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export interface EntityFrame {
allowExchangeEntity: boolean;

isExecuting(): boolean;
execute: (action: () => Promise<void>) => void;
execute: (action: () => Promise<void>) => Promise<void>;

createNew?: (oldPack: EntityPack<ModifiableEntity>) => (Promise<EntityPack<ModifiableEntity> | undefined>) | undefined;
prefix: string;
Expand Down
2 changes: 1 addition & 1 deletion Signum.React/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2017",
"sourceMap": false,
"module": "esnext",
"moduleResolution": "node",
Expand Down

2 comments on commit 9caeecf

@olmobrutall
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Target ES2017 (async await welcomed!)

In order to fix a bug in Operation executing the simplest solution was to use async await.

I've been avoiding using async await for two reasons:

  1. The translation to ES5 (for IE compatibility) was quite horrible to understand and debug.
  2. Writing a try { await x } catch(e) { showErrorModal(e) } everywhere is way too long compared to x.done().

This two reasons are outdated now:

  1. Since the migration to Bootstrap 5 we are not longer compatible with IE, and the support for async await is quite good already.
  2. Browsers now have events to notify of unhanded rejected promises

So for now I've changed the tsconfig.json target to ES2017, so your generated JavaScript will contain arrow functions (lambdas), classes and async await.

l In the near future I will probably remove the done() method and implement ModalError using unhandledrejection.

@MehdyKarimpour
Copy link
Contributor

@MehdyKarimpour MehdyKarimpour commented on 9caeecf Nov 30, 2021 via email

Choose a reason for hiding this comment

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

Please sign in to comment.