Skip to content

Commit

Permalink
Merge pull request #98 from braintree/DTBTSDK-8333
Browse files Browse the repository at this point in the history
fix: create index.js to remove circular dependency in lib directory
  • Loading branch information
oscarleonnogales committed Jun 21, 2023
2 parents 00533b1 + c5f06ca commit a742162
Show file tree
Hide file tree
Showing 26 changed files with 67 additions and 51 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- Fix circular dependency issue ([Issue #97](https://github.com/braintree/framebus/issues/97))

# 5.2.0

- Add ability to target specific frames with `targetFrames` configuration
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@wdio/sync": "^7.25.2",
"async": "^3.2.4",
"browserify": "^17.0.0",
"chromedriver": "^110.0.0",
"chromedriver": "^114.0.2",
"del": "^6.0.0",
"ejs": "^3.1.8",
"eslint": "^8.35.0",
Expand Down
5 changes: 2 additions & 3 deletions spec/unit/attach.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { attach, detach } from "../../src/lib/attach";
import { onmessage } from "../../src/lib/message";
import { attach, detach, onMessage } from "../../src/lib";

describe("_attach", () => {
let addEventSpy: jest.SpyInstance;
Expand All @@ -12,7 +11,7 @@ describe("_attach", () => {
it("should add listener to scope", () => {
attach();

expect(addEventSpy).toBeCalledWith("message", onmessage, false);
expect(addEventSpy).toBeCalledWith("message", onMessage, false);
});

it("should only add listener to scope once", () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/broadcast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { broadcast } from "../../src/lib/broadcast";
import { broadcast } from "../../src/lib";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
function mkFrame() {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/dispatch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Framebus = require("../../src/");
import { dispatch } from "../../src/lib/dispatch";
import { dispatch } from "../../src/lib";

describe("dispatch", () => {
it("should execute subscribers for the given event and origin", () => {
Expand Down
5 changes: 1 addition & 4 deletions spec/unit/framebus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
jest.mock("../../src/lib/broadcast");
jest.mock("../../src/lib/send-message");

import { attach } from "../../src/lib/attach";
import { broadcast } from "../../src/lib/broadcast";
import { sendMessage } from "../../src/lib/send-message";
import { attach, broadcast, sendMessage, subscribers } from "../../src/lib";
import { Framebus } from "../../src/framebus";
import { subscribers } from "../../src/lib/constants";

describe("Framebus", () => {
let bus: Framebus;
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/off.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { subscribers } from "../../src/lib/constants";
import { subscribers } from "../../src/lib";
import Framebus = require("../../src/");

describe("off", () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/on.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { subscribers } from "../../src/lib/constants";
import { subscribers } from "../../src/lib";
import Framebus = require("../../src/");

describe("on", () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/package-payload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { packagePayload } from "../../src/lib/package-payload";
import { packagePayload } from "../../src/lib";

const messagePrefix = "/*framebus*/";

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/send-message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sendMessage } from "../../src/lib/send-message";
import { sendMessage } from "../../src/lib";

function mkFrame() {
return {
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/subscribe-replier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { subscribers } from "../../src/lib/constants";
import { subscribeReplier } from "../../src/lib/subscribe-replier";
import { subscribers, subscribeReplier } from "../../src/lib";

describe("subscribeReplier", () => {
it("should return UUID of reply event", () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/subscription-args-invalid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { subscriptionArgsInvalid } from "../../src/lib/subscription-args-invalid";
import { subscriptionArgsInvalid } from "../../src/lib";

describe("subscriptionArgsInvalid", () => {
it("should return false for valid types", () => {
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/unpack-payload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { unpackPayload } from "../../src/lib/unpack-payload";
import { unpackPayload } from "../../src/lib";
import type {
FramebusPayload,
FramebusSubscriberArg,
FramebusSubscribeHandler,
} from "../../src/lib/types";
} from "../../src/lib";

const messagePrefix = "/*framebus*/";

Expand Down
21 changes: 13 additions & 8 deletions src/framebus.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { isntString } from "./lib/is-not-string";
import { subscriptionArgsInvalid } from "./lib/subscription-args-invalid";
import { broadcast } from "./lib/broadcast";
import { packagePayload } from "./lib/package-payload";
import { sendMessage } from "./lib/send-message";

import { childWindows, subscribers } from "./lib/constants";
import {
isntString,
subscriptionArgsInvalid,
broadcast,
packagePayload,
sendMessage,
childWindows,
subscribers,
} from "./lib";

import type {
FramebusSubscriberArg,
FramebusSubscribeHandler,
FramebusOnHandler,
FramebusReplyHandler,
} from "./lib/types";
} from "./lib";

type Listener = {
eventName: string;
handler: FramebusOnHandler;
originalHandler: FramebusOnHandler;
};

type VerifyDomainMethod = (domain: string) => boolean;
// this is a mixed type so that users can add iframes to the array
// before they have been added to the DOM (in which case, they
// would not have a contentWindow yet). When accessing these
// windows in Framebus, the targetFramesAsWindows private
// method should be used
type IFrameOrWindowList = Array<HTMLIFrameElement | Window>;

type FramebusOptions = {
channel?: string;
origin?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { attach } from "./lib/attach";
import { attach } from "./lib";
import { Framebus } from "./framebus";

attach();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/attach.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onmessage } from "./message";
import { onMessage } from "./";

let isAttached = false;

Expand All @@ -8,12 +8,12 @@ export function attach(): void {
}

isAttached = true;
window.addEventListener("message", onmessage, false);
window.addEventListener("message", onMessage, false);
}

// removeIf(production)
export function detach(): void {
isAttached = false;
window.removeEventListener("message", onmessage, false);
window.removeEventListener("message", onMessage, false);
}
// endRemoveIf(production)
3 changes: 1 addition & 2 deletions src/lib/broadcast-to-child-windows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { broadcast } from "./broadcast";
import { childWindows } from "./constants";
import { broadcast, childWindows } from "./";

export function broadcastToChildWindows(
payload: string,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasOpener } from "./has-opener";
import { hasOpener } from "./";

type BroadcastOptions = {
origin: string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FramebusSubscriber } from "./types";
import type { FramebusSubscriber } from "./";

export const prefix = "/*framebus*/";
export const childWindows: Window[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { subscribers } from "./constants";
import { subscribers } from "./";

import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./types";
import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./";

export function dispatch(
origin: string,
Expand Down
14 changes: 14 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export * from "./attach";
export * from "./broadcast-to-child-windows";
export * from "./broadcast";
export * from "./constants";
export * from "./dispatch";
export * from "./has-opener";
export * from "./is-not-string";
export * from "./message";
export * from "./package-payload";
export * from "./send-message";
export * from "./subscribe-replier";
export * from "./subscription-args-invalid";
export * from "./types";
export * from "./unpack-payload";
14 changes: 8 additions & 6 deletions src/lib/message.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./types";
import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./";

import { isntString } from "./is-not-string";
import { unpackPayload } from "./unpack-payload";
import { dispatch } from "./dispatch";
import { broadcastToChildWindows } from "./broadcast-to-child-windows";
import {
isntString,
unpackPayload,
dispatch,
broadcastToChildWindows,
} from "./";

export function onmessage(e: MessageEvent): void {
export function onMessage(e: MessageEvent): void {
if (isntString(e.data)) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib/package-payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { subscribeReplier } from "./subscribe-replier";
import { prefix } from "./constants";
import { subscribeReplier, prefix } from "./";

import type {
FramebusPayload,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/subscribe-replier.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Framebus } from "../framebus";
import generateUUID from "@braintree/uuid";

import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./types";
import type { FramebusSubscriberArg, FramebusSubscribeHandler } from "./";

export function subscribeReplier(
fn: FramebusSubscribeHandler,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/subscription-args-invalid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isntString } from "./is-not-string";
import { isntString } from "./";

import type { FramebusOnHandler } from "./types";
import type { FramebusOnHandler } from "./";

export function subscriptionArgsInvalid(
event: string,
Expand Down
5 changes: 2 additions & 3 deletions src/lib/unpack-payload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { prefix } from "./constants";
import { packagePayload } from "./package-payload";
import { prefix, packagePayload } from "./";

import type { FramebusPayload, FramebusSubscriberArg } from "./types";
import type { FramebusPayload, FramebusSubscriberArg } from "./";

export function unpackPayload(e: MessageEvent): FramebusPayload | false {
let payload: FramebusPayload;
Expand Down

0 comments on commit a742162

Please sign in to comment.