Skip to content

Commit

Permalink
feat(client): children frames property
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Dec 10, 2021
1 parent 6078b95 commit 28709e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 7 additions & 1 deletion client/lib/CoreFrameEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ export default class CoreFrameEnvironment {
public frameId: number;
public sessionId: string;
public commandQueue: CoreCommandQueue;
public parentFrameId: number;

constructor(meta: ISessionMeta & { sessionName: string }, commandQueue: CoreCommandQueue) {
constructor(
meta: ISessionMeta & { sessionName: string },
parentFrameId: number,
commandQueue: CoreCommandQueue,
) {
const { tabId, sessionId, frameId, sessionName } = meta;
this.tabId = tabId;
this.sessionId = sessionId;
this.frameId = frameId;
this.parentFrameId = parentFrameId;
const queueMeta = {
sessionId,
tabId,
Expand Down
14 changes: 11 additions & 3 deletions client/lib/CoreTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ export default class CoreTab implements IJsPathEventTarget {
sessionName,
};
this.connection = connection;
this.commandQueue = new CoreCommandQueue(this.meta, coreSession.mode, connection, coreSession as ICommandCounter);
this.commandQueue = new CoreCommandQueue(
this.meta,
coreSession.mode,
connection,
coreSession as ICommandCounter,
);
this.coreSession = coreSession;
this.eventHeap = new CoreEventHeap(this.meta, connection, coreSession as ICommandCounter);
this.frameEnvironmentsById.set(frameId, new CoreFrameEnvironment(meta, this.commandQueue));
this.frameEnvironmentsById.set(
frameId,
new CoreFrameEnvironment(meta, null, this.commandQueue),
);

const resolvedThis = Promise.resolve(this);
this.eventHeap.registerEventInterceptors({
Expand All @@ -75,7 +83,7 @@ export default class CoreTab implements IJsPathEventTarget {
meta.frameId = frameMeta.id;
this.frameEnvironmentsById.set(
frameMeta.id,
new CoreFrameEnvironment(meta, this.commandQueue),
new CoreFrameEnvironment(meta, frameMeta.parentFrameId, this.commandQueue),
);
}
return this.frameEnvironmentsById.get(frameMeta.id);
Expand Down
19 changes: 16 additions & 3 deletions client/lib/FrameEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ export default class FrameEnvironment {
return getCoreFrameEnvironment(this).then(x => x.frameId);
}

public get children(): Promise<FrameEnvironment[]> {
return getState(this).tab.frameEnvironments.then(async frames => {
const frameId = await this.frameId;

const childFrames: FrameEnvironment[] = [];
for (const frame of frames) {
const parentFrameId = await frame.parentFrameId;
if (parentFrameId === frameId) {
childFrames.push(frame);
}
}
return childFrames;
});
}

public get url(): Promise<string> {
return getCoreFrameEnvironment(this).then(x => x.getUrl());
}
Expand All @@ -122,9 +137,7 @@ export default class FrameEnvironment {
}

public get parentFrameId(): Promise<number | null> {
return getCoreFrameEnvironment(this)
.then(x => x.getFrameMeta())
.then(x => x.parentFrameId);
return getCoreFrameEnvironment(this).then(x => x.parentFrameId);
}

public get cookieStorage(): CookieStorage {
Expand Down
6 changes: 6 additions & 0 deletions docs/main/BasicInterfaces/FrameEnvironment.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Frames cannot be constructed in Hero. They're made available through the [tab.fr

## Properties

### frameEnvironment.children {#child-frames}

Returns child FrameEnvironments for this frame.

#### **Type**: Promise<[`FrameEnvironment`](/docs/basic-interfaces/tab#frame-environments)[]>

### frameEnvironment.cookieStorage {#cookie-storage}

Returns a [CookieStorage](/docs/advanced/cookie-storage) instance to get/set/delete cookies.
Expand Down

0 comments on commit 28709e5

Please sign in to comment.