Skip to content

Commit

Permalink
fix(transport): add skipBeforeEmit emit options
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Nov 6, 2023
1 parent 2c809e3 commit daa0d2c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions examples/iframe/src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ const init = () => {
});
};

window.addEventListener('beforeunload', function (e) {
const confirmationMessage = 'something';
e.preventDefault(); //
e.returnValue = confirmationMessage; // Gecko or Trident
return confirmationMessage; // WebKit
});

init();
3 changes: 3 additions & 0 deletions examples/iframe/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ module.exports = {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devServer: {
host: '0.0.0.0',
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-transport",
"version": "4.3.1",
"version": "4.3.2",
"description": "A simple and responsible transport",
"main": "lib/index.js",
"unpkg": "dist/index.umd.js",
Expand Down
4 changes: 4 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export interface EmitParameter<T> {
* silent for timeout error, false by default.
*/
silent?: boolean;
/**
* skip before emit, false by default.
*/
skipBeforeEmit?: boolean;
}

export type EmitOptions<T> = T | EmitParameter<T>;
Expand Down
13 changes: 8 additions & 5 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export abstract class Transport<T extends BaseInteraction = any> {
private [logKey]?: (listenOptions: ListenerOptions<any>) => void;
private [verboseKey]: boolean;
protected [beforeEmitKey]?: Promise<void>;
protected [beforeEmitResolveKey]?: () => void;
protected [beforeEmitResolveKey]?: () => void;
/**
* dispose transport
*/
Expand Down Expand Up @@ -278,9 +278,6 @@ export abstract class Transport<T extends BaseInteraction = any> {
[transportKey]: transportId,
requestId: this.id,
};
if (this[beforeEmitKey]) {
await this[beforeEmitKey];
}
if (this[verboseKey]) {
if (typeof this[logKey] === 'function') {
this[logKey]!(rawRequestData);
Expand All @@ -289,12 +286,18 @@ export abstract class Transport<T extends BaseInteraction = any> {
}
}
if (!hasRespond) {
if (this[beforeEmitKey] && !params.skipBeforeEmit) {
await this[beforeEmitKey];
}
this[senderKey](rawRequestData);
return Promise.resolve(undefined as Response<T['emit'][K]>);
}
let timeoutId: NodeJS.Timeout | number;
const promise = Promise.race<any>([
new Promise((resolve) => {
new Promise(async (resolve) => {
if (this[beforeEmitKey] && !params.skipBeforeEmit) {
await this[beforeEmitKey];
}
this[requestsMapKey].set(transportId, resolve);
this[senderKey](rawRequestData);
}),
Expand Down

0 comments on commit daa0d2c

Please sign in to comment.