Skip to content

Commit

Permalink
Created IfSetMessagePort and IfGetMessagePort interfaces (#446)
Browse files Browse the repository at this point in the history
Refactored components to use the new interface modules and reduce code replication.
  • Loading branch information
lvcabral authored Jan 9, 2025
1 parent a465ded commit 263f571
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 440 deletions.
39 changes: 7 additions & 32 deletions src/core/brsTypes/components/RoAppMemoryMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { BrsValue, ValueKind, BrsInvalid, BrsBoolean } from "../BrsType";
import { BrsValue, ValueKind, BrsBoolean } from "../BrsType";
import { BrsComponent } from "./BrsComponent";
import { BrsType, RoMessagePort, toAssociativeArray } from "..";
import { Callable, StdlibArgument } from "../Callable";
import { Interpreter } from "../../interpreter";
import { Int32 } from "../Int32";
import { IfSetMessagePort } from "../interfaces/IfSetMessagePort";
import { IfGetMessagePort } from "../interfaces/IfGetMessagePort";

export class RoAppMemoryMonitor extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
private port?: RoMessagePort;

constructor() {
super("roAppMemoryMonitor");
const setPortIface = new IfSetMessagePort(this);
const getPortIface = new IfGetMessagePort(this);
this.registerMethods({
ifAppMemoryMonitor: [
this.enableMemoryWarningEvent,
this.getMemoryLimitPercent,
this.getChannelAvailableMemory, // Since OS 12.5
this.getChannelMemoryLimit, // Since OS 13.0
],
ifSetMessagePort: [this.setMessagePort],
ifGetMessagePort: [this.getMessagePort],
ifSetMessagePort: [setPortIface.setMessagePort],
ifGetMessagePort: [getPortIface.getMessagePort],
});
}

Expand Down Expand Up @@ -89,33 +93,4 @@ export class RoAppMemoryMonitor extends BrsComponent implements BrsValue {
return toAssociativeArray(memLimit);
},
});

// ifGetMessagePort ----------------------------------------------------------------------------------

/** Returns the message port (if any) currently associated with the object */
private readonly getMessagePort = new Callable("getMessagePort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

// ifSetMessagePort ----------------------------------------------------------------------------------

/** Sets the roMessagePort to be used for all events from the screen */
private readonly setMessagePort = new Callable("setMessagePort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
port.addReference();
this.port?.removeReference();
this.port = port;
return BrsInvalid.Instance;
},
});
}
64 changes: 6 additions & 58 deletions src/core/brsTypes/components/RoAudioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Callable, StdlibArgument } from "../Callable";
import { Interpreter } from "../../interpreter";
import { Int32 } from "../Int32";
import { DataType } from "../../common";
import { IfSetMessagePort } from "../interfaces/IfSetMessagePort";
import { IfGetMessagePort } from "../interfaces/IfGetMessagePort";

export class RoAudioPlayer extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
Expand All @@ -28,6 +30,8 @@ export class RoAudioPlayer extends BrsComponent implements BrsValue {
postMessage(new Array<string>());
postMessage("audio,loop,false");
postMessage("audio,next,-1");
const setPortIface = new IfSetMessagePort(this, this.getNewEvents.bind(this));
const getPortIface = new IfGetMessagePort(this);
this.registerMethods({
ifAudioPlayer: [
this.setContentList,
Expand All @@ -42,8 +46,8 @@ export class RoAudioPlayer extends BrsComponent implements BrsValue {
this.seek,
this.setTimedMetadataForKeys,
],
ifSetMessagePort: [this.setMessagePort, this.setPort],
ifGetMessagePort: [this.getMessagePort, this.getPort],
ifSetMessagePort: [setPortIface.setMessagePort, setPortIface.setPort],
ifGetMessagePort: [getPortIface.getMessagePort, getPortIface.getPort],
});
}

Expand Down Expand Up @@ -232,60 +236,4 @@ export class RoAudioPlayer extends BrsComponent implements BrsValue {
return BrsInvalid.Instance;
},
});

// ifGetMessagePort ----------------------------------------------------------------------------------

/** Returns the message port (if any) currently associated with the object */
private readonly getMessagePort = new Callable("getMessagePort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

/** Returns the message port (if any) currently associated with the object */
private readonly getPort = new Callable("getPort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

// ifSetMessagePort ----------------------------------------------------------------------------------

/** Sets the roMessagePort to be used for all events from the audio player */
private readonly setMessagePort = new Callable("setMessagePort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
const component = this.getComponentName();
this.port?.unregisterCallback(component);
this.port = port;
this.port.registerCallback(component, this.getNewEvents.bind(this));
return BrsInvalid.Instance;
},
});

/** Sets the roMessagePort to be used for all events from the audio player */
private readonly setPort = new Callable("setPort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
const component = this.getComponentName();
this.port?.unregisterCallback(component);
this.port = port;
this.port.registerCallback(component, this.getNewEvents.bind(this));
return BrsInvalid.Instance;
},
});
}
42 changes: 12 additions & 30 deletions src/core/brsTypes/components/RoCECStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { BrsValue, ValueKind, BrsInvalid, BrsBoolean } from "../BrsType";
import { BrsValue, ValueKind, BrsBoolean } from "../BrsType";
import { BrsComponent } from "./BrsComponent";
import { BrsEvent, BrsType, RoMessagePort } from "..";
import { Callable, StdlibArgument } from "../Callable";
import { Callable } from "../Callable";
import { Interpreter } from "../../interpreter";
import { RoCECStatusEvent } from "./RoCECStatusEvent";
import { DataType } from "../../common";
import { IfSetMessagePort } from "../interfaces/IfSetMessagePort";
import { IfGetMessagePort } from "../interfaces/IfGetMessagePort";

export class RoCECStatus extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
Expand All @@ -16,8 +18,14 @@ export class RoCECStatus extends BrsComponent implements BrsValue {
super("roCECStatus");
this.interpreter = interpreter;
this.active = 1; // Default to active
const setPortIface = new IfSetMessagePort(this, this.getNewEvents.bind(this));
const getPortIface = new IfGetMessagePort(this);
this.registerMethods({
ifCECStatus: [this.isActiveSource, this.getMessagePort, this.setMessagePort],
ifCECStatus: [
this.isActiveSource,
setPortIface.setMessagePort,
getPortIface.getMessagePort,
],
});
}

Expand All @@ -33,7 +41,7 @@ export class RoCECStatus extends BrsComponent implements BrsValue {
this.port?.unregisterCallback(this.getComponentName());
}

// System Log Event -------------------------------------------------------------------------------
// CEC Status Event -------------------------------------------------------------------------------

private getNewEvents() {
const events: BrsEvent[] = [];
Expand All @@ -58,30 +66,4 @@ export class RoCECStatus extends BrsComponent implements BrsValue {
return BrsBoolean.from(cecActive !== 0);
},
});

/** Returns the message port (if any) currently associated with the object */
private readonly getMessagePort = new Callable("getMessagePort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

/** Sets the roMessagePort to be used for all events from the screen */
private readonly setMessagePort = new Callable("setMessagePort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
const component = port.getComponentName();
this.port?.unregisterCallback(component);
this.port = port;
this.port.registerCallback(component, this.getNewEvents.bind(this));
return BrsInvalid.Instance;
},
});
}
33 changes: 6 additions & 27 deletions src/core/brsTypes/components/RoChannelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { RoChannelStoreEvent } from "./RoChannelStoreEvent";
import { RoAssociativeArray } from "./RoAssociativeArray";
import { AppData } from "../../common";
import { parseString, processors } from "xml2js";
import { IfSetMessagePort } from "../interfaces/IfSetMessagePort";
import { IfGetMessagePort } from "../interfaces/IfGetMessagePort";

export class RoChannelStore extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
Expand All @@ -25,6 +27,8 @@ export class RoChannelStore extends BrsComponent implements BrsValue {
this.orderInfo = BrsInvalid.Instance;
this.credData = "";
this.fakeServerEnabled = false;
const setPortIface = new IfSetMessagePort(this);
const getPortIface = new IfGetMessagePort(this);
this.registerMethods({
ifChannelStore: [
this.getIdentity,
Expand All @@ -46,8 +50,8 @@ export class RoChannelStore extends BrsComponent implements BrsValue {
this.getDeviceAttestation,
this.requestPartnerOrder,
this.confirmPartnerOrder,
this.setMessagePort,
this.getMessagePort,
setPortIface.setMessagePort,
getPortIface.getMessagePort,
],
});
}
Expand Down Expand Up @@ -519,29 +523,4 @@ ZZwGPYCKEHMPrIOOXJ-S9ZjArgaEpBUpMXWJibFxnkpVUVzbC22GEaqz_SjOJXFMQU7TaCKkDeCYVKyl
return toAssociativeArray({ errorCode: -1, errorMessage: "", status: "Failure" });
},
});

/** Returns the message port (if any) currently associated with the object */
private readonly getMessagePort = new Callable("getMessagePort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

/** Sets the roMessagePort to be used for all events from the screen */
private readonly setMessagePort = new Callable("setMessagePort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
port.addReference();
this.port?.removeReference();
this.port = port;
return BrsInvalid.Instance;
},
});
}
50 changes: 20 additions & 30 deletions src/core/brsTypes/components/RoDeviceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { BrsValue, ValueKind, BrsString, BrsBoolean, BrsInvalid } from "../BrsType";
import { BrsComponent } from "./BrsComponent";
import { BrsType, RoMessagePort, Int32, FlexObject, toAssociativeArray } from "..";
import { BrsType, RoMessagePort, Int32, FlexObject, toAssociativeArray, BrsEvent } from "..";
import { Callable, StdlibArgument } from "../Callable";
import { Interpreter } from "../../interpreter";
import { RoDeviceInfoEvent } from "./RoDeviceInfoEvent";
import { RoAssociativeArray } from "./RoAssociativeArray";
import { RoArray } from "./RoArray";
import { ConnectionInfo, getRokuOSVersion, isPlatform } from "../../common";
import { IfSetMessagePort } from "../interfaces/IfSetMessagePort";
import { IfGetMessagePort } from "../interfaces/IfGetMessagePort";
import { v4 as uuidv4 } from "uuid";
import * as crypto from "crypto";
/// #if !BROWSER
Expand Down Expand Up @@ -39,6 +41,7 @@ export class RoDeviceInfo extends BrsComponent implements BrsValue {
private readonly displayModeName: string;
private readonly displayResolution: { h: number; w: number };
private readonly displayAspectRatio: string;
private captionsMessageSent: boolean = false;
private captionsMode: string;
private port?: RoMessagePort;

Expand All @@ -63,6 +66,8 @@ export class RoDeviceInfo extends BrsComponent implements BrsValue {
this.displayModeName = "FHD";
}
this.captionsMode = interpreter.deviceInfo.get("captionsMode") ?? "Off";
const setPortIface = new IfSetMessagePort(this, this.getNewEvents.bind(this));
const getPortIface = new IfGetMessagePort(this);
this.registerMethods({
ifDeviceInfo: [
this.getModel,
Expand Down Expand Up @@ -124,8 +129,8 @@ export class RoDeviceInfo extends BrsComponent implements BrsValue {
this.enableScreensaverExitedEvent,
this.enableValidClockEvent,
this.getCreationTime, // undocumented
this.getMessagePort,
this.setMessagePort,
setPortIface.setMessagePort,
getPortIface.getMessagePort,
],
});
}
Expand All @@ -139,7 +144,18 @@ export class RoDeviceInfo extends BrsComponent implements BrsValue {
}

dispose() {
this.port?.removeReference();
this.port?.unregisterCallback(this.getComponentName());
}

// DeviceInfo Event -------------------------------------------------------------------------------

private getNewEvents() {
const events: BrsEvent[] = [];
if (!this.captionsMessageSent) {
events.push(new RoDeviceInfoEvent({ Mode: this.captionsMode, Mute: false }));
this.captionsMessageSent = true;
}
return events;
}

/** Returns the model name for the Roku Streaming Player device running the script. */
Expand Down Expand Up @@ -1029,32 +1045,6 @@ export class RoDeviceInfo extends BrsComponent implements BrsValue {
return new BrsString(interpreter.creationTime ?? "");
},
});

/** Returns the message port (if any) currently associated with the object */
private readonly getMessagePort = new Callable("getMessagePort", {
signature: {
args: [],
returns: ValueKind.Object,
},
impl: (_: Interpreter) => {
return this.port ?? BrsInvalid.Instance;
},
});

/** Sets the roMessagePort to be used for all events from the screen */
private readonly setMessagePort = new Callable("setMessagePort", {
signature: {
args: [new StdlibArgument("port", ValueKind.Dynamic)],
returns: ValueKind.Void,
},
impl: (_: Interpreter, port: RoMessagePort) => {
port.addReference();
this.port?.removeReference();
this.port = port;
this.port.pushMessage(new RoDeviceInfoEvent({ Mode: this.captionsMode, Mute: false }));
return BrsInvalid.Instance;
},
});
}
// Helper Functions
function generateUUID(): string {
Expand Down
Loading

0 comments on commit 263f571

Please sign in to comment.