Skip to content

Commit

Permalink
web: Add callExternalInterface to PlayerV1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinnerbone committed Nov 16, 2024
1 parent 3f12eb2 commit c9d32bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions web/packages/core/src/internal/player/impl_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,8 @@ export class PlayerV1Impl implements PlayerV1 {
set config(value: URLLoadOptions | DataLoadOptions | object) {
this.#inner.config = value;
}

callExternalInterface(name: string, ...args: unknown[]): unknown {
return this.#inner.callExternalInterface(name, args);
}
}
12 changes: 12 additions & 0 deletions web/packages/core/src/public/player/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,16 @@ export interface PlayerV1 {
* @param message The message shown to the user.
*/
displayMessage(message: string): void;

/**
* Calls an External Interface callback with the given name and arguments.
*
* This will call any ActionScript code assigned to the given name.
* If no such External Interface callback exists with the given name, this method silently fails and returns `undefined`.
*
* @param name Name of the callback to call.
* @param args Any arguments to pass to the callback.
* @returns Any value returned by the callback.
*/
callExternalInterface(name: string, ...args: unknown[]): unknown;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../utils.js";
import { expect, use } from "chai";
import chaiHtml from "chai-html";
import { Player } from "ruffle-core";

use(chaiHtml);

Expand Down Expand Up @@ -50,6 +51,10 @@ declare global {

// Going to be redefined as part of a test
redefinedMethod: () => string;

ruffle<V extends keyof Player.APIVersions = 1>(
version?: V,
): Player.APIVersions[V];
}
}

Expand Down Expand Up @@ -135,7 +140,7 @@ ExternalInterface.objectID: "flash_name"
);
});

it("returns a value", async () => {
it("returns a value with legacy API", async () => {
const player = await browser.$("<ruffle-object>");
const returned = await browser.execute(
(player) => player.returnAValue(123.4),
Expand All @@ -154,6 +159,26 @@ ExternalInterface.objectID: "flash_name"
);
});

it("returns a value with V1 API", async () => {
const player = await browser.$("<ruffle-object>");
const returned = await browser.execute(
(player) =>
player.ruffle().callExternalInterface("returnAValue", 123.4),
player,
);

expect(returned).to.eql(123.4);

const actualOutput = await getTraceOutput(browser, player);
expect(actualOutput).to.eql(
`returnAValue called with 123.4
[
123.4
]
`,
);
});

it("calls a method with delay", async () => {
const player = await browser.$("<ruffle-object>");
await browser.execute(
Expand Down

0 comments on commit c9d32bc

Please sign in to comment.