Skip to content

Commit

Permalink
Merge branch 'main' of github.com:perawallet/onramp
Browse files Browse the repository at this point in the history
  • Loading branch information
mucahit committed Feb 27, 2023
2 parents 0cfc7d4 + e1d86b9 commit e602d1f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
14 changes: 0 additions & 14 deletions src/PeraOnramp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ class PeraOnramp {
if (message.type === "CONTINUE_CLICKED_AFTER_ADD_FUNDS_COMPLETED") {
this.messageResolve("ADDING_FUNDS_COMPLETED");
}

appTellerManager.sendMessage({
message: {
type: "MESSAGE_RECEIVED"
},
targetWindow: messageEvent!.source!
});
}
});
}
Expand Down Expand Up @@ -82,13 +75,6 @@ class PeraOnramp {
onReceiveMessage: (messageEvent) => {
const {message} = messageEvent.data;

appTellerManager.sendMessage({
message: {
type: "MESSAGE_RECEIVED"
},
targetWindow: messageEvent!.source!
});

if (message.type === "OPT_IN_REQUEST" && callbacks.OPT_IN_REQUEST) {
// eslint-disable-next-line new-cap
callbacks.OPT_IN_REQUEST({
Expand Down
26 changes: 6 additions & 20 deletions src/core/util/network/teller/Teller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import appTellerManager, {PeraOnrampTeller} from "./appTellerManager";

const TELLER_MESSAGE_TIMEOUT = 300;
const SEND_MESSAGE_TRY_MAX_COUNT = 50;
const TELLER_MESSAGE_TIMEOUT = 1000;

class Teller<Message> {
private channel: string;
Expand All @@ -16,6 +13,7 @@ class Teller<Message> {
- Call tellerManager.sendMessage({message, targetWindow, origin}) to send a message to the other tab/window/iframe.
- Call tellerManager.setupListener({onReceiveMessage}) to listen for messages from the other tab/window/iframe.
- Call tellerManager.close(); to close the active listener.
Details on: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
*/
constructor(options: TellerOptions) {
Expand All @@ -28,6 +26,9 @@ class Teller<Message> {
}: {
onReceiveMessage: (event: MessageEvent<TellerMessage<Message>>) => void;
}) {
// Close the existing listener if it exists
this.close();

this.listener = (event: MessageEvent<TellerMessage<Message>>) => {
if (typeof event.data === "object") {
try {
Expand All @@ -50,14 +51,7 @@ class Teller<Message> {
origin,
timeout = TELLER_MESSAGE_TIMEOUT
}: TellerMessageOptions<Message>) {
let count = 0;

const sendMessageInterval = setInterval(() => {
count += 1;

if (count >= SEND_MESSAGE_TRY_MAX_COUNT) {
clearInterval(sendMessageInterval);
}
setTimeout(() => {
const tellerMessage = {
channel: this.channel,
message
Expand All @@ -68,14 +62,6 @@ class Teller<Message> {
targetOrigin: origin || "*"
});
}, timeout);

appTellerManager.setupListener({
onReceiveMessage: (messageEvent: MessageEvent<TellerMessage<PeraOnrampTeller>>) => {
if (messageEvent.data.message.type === "MESSAGE_RECEIVED") {
clearInterval(sendMessageInterval);
}
}
});
}

public close() {
Expand Down
3 changes: 1 addition & 2 deletions src/core/util/network/teller/appTellerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export type PeraOnrampTeller =
| {type: "ADD_FUNDS_REQUEST"}
| {type: "ADD_FUNDS_COMPLETED"}
| {type: "CONTINUE_CLICKED_AFTER_ADD_FUNDS_COMPLETED"}
| {type: "ADD_FUNDS_FAILED"}
| {type: "MESSAGE_RECEIVED"};
| {type: "ADD_FUNDS_FAILED"};

const appTellerManager = new Teller<PeraOnrampTeller>({
channel: "pera-onramp"
Expand Down

0 comments on commit e602d1f

Please sign in to comment.