Skip to content

Commit

Permalink
feat: core session now has access to it's hero instance
Browse files Browse the repository at this point in the history
  • Loading branch information
calebjclark committed Feb 7, 2022
1 parent 505dd15 commit 62e17bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions client/connections/ConnectionToCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import CoreSession from '../lib/CoreSession';
import CoreSessions from '../lib/CoreSessions';
import DisconnectedFromCoreError from './DisconnectedFromCoreError';
import { IHeroCreateOptions } from '../index';
import { scriptInstance } from '../lib/Hero';
import Hero, { scriptInstance } from '../lib/Hero';

const { log } = Log(module);

Expand Down Expand Up @@ -181,8 +181,8 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
/////// SESSION FUNCTIONS //////////////////////////////////////////////////////////////////////////////////////////

public async createSession(
options: Omit<ISessionCreateOptions, 'sessionId'> &
Pick<IHeroCreateOptions, 'sessionId'>,
options: Omit<ISessionCreateOptions, 'sessionId'> & Pick<IHeroCreateOptions, 'sessionId'>,
hero: Hero,
): Promise<CoreSession> {
try {
if (options.sessionId) options.sessionId = await options.sessionId;
Expand All @@ -196,13 +196,14 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
if (restoreMode) {
this.commandQueue.mode = restoreMode;
}
const session = new CoreSession(
const coreSession = new CoreSession(
{ ...sessionMeta, sessionName: options.sessionName },
this,
hero,
options.mode,
);
this.coreSessions.track(session);
return session;
this.coreSessions.track(coreSession);
return coreSession;
} catch (error) {
if (error instanceof DisconnectedFromCoreError && this.isDisconnecting) return null;
throw error;
Expand Down
4 changes: 4 additions & 0 deletions client/lib/CoreSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ICollectedElement from '@ulixee/hero-interfaces/ICollectedElement';
import ICollectedSnippet from '@ulixee/hero-interfaces/ICollectedSnippet';
import ICollectedResource from '@ulixee/hero-interfaces/ICollectedResource';
import { IOutputChangeToRecord } from '../interfaces/ICoreSession';
import Hero from './Hero';

export default class CoreSession implements IJsPathEventTarget {
public tabsById = new Map<number, CoreTab>();
Expand All @@ -31,6 +32,7 @@ export default class CoreSession implements IJsPathEventTarget {
public eventHeap: CoreEventHeap;
public emitter = new EventEmitter();
public readonly mode: ISessionCreateOptions['mode'];
public readonly hero: Hero;

public get lastCommandId(): number {
return this.commandId;
Expand All @@ -56,6 +58,7 @@ export default class CoreSession implements IJsPathEventTarget {
constructor(
sessionMeta: ISessionMeta & { sessionName: string },
connectionToCore: ConnectionToCore,
hero: Hero,
mode: ISessionCreateOptions['mode'],
) {
this.mode = mode;
Expand All @@ -66,6 +69,7 @@ export default class CoreSession implements IJsPathEventTarget {
sessionId,
};
this.connectionToCore = connectionToCore;
this.hero = hero;
loggerSessionIdNames.set(sessionId, sessionName);
this.commandQueue = new CoreCommandQueue(
{ sessionId, sessionName },
Expand Down
2 changes: 1 addition & 1 deletion client/lib/DomExtender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const NodeExtensionFns: INodeExtensionFns = {
},
async $type(...typeInteractions: ITypeInteraction[]): Promise<void> {
const coreFrame = await getCoreFrame(this);
await this.$click();
await this.focus();
await Interactor.run(
coreFrame,
typeInteractions.map(t => ({ type: t })),
Expand Down
2 changes: 1 addition & 1 deletion client/lib/Hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export default class Hero extends AwaitedEventTarget<{
#getCoreSessionOrReject(): Promise<CoreSession> {
if (!this.#coreSessionPromise) {
this.#coreSessionPromise = this.#connectionToCore
.createSession(this.#options)
.createSession(this.#options, this)
.then(session => {
if (session instanceof CoreSession) this.#initializeClientPlugins(this.#clientPlugins);
return session;
Expand Down

0 comments on commit 62e17bb

Please sign in to comment.