Skip to content

Commit

Permalink
[twisty] Remove warning for setting TwistyPlayer alg using string.
Browse files Browse the repository at this point in the history
This will depend on microsoft/TypeScript#42425 landing in stable TypeScript so we can update the types to reflect it.
  • Loading branch information
lgarron committed Apr 3, 2021
1 parent 185a941 commit 72249a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/cubing/twisty/dom/TwistyPlayer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Alg } from "../../alg";
import { TwistyPlayer } from "./TwistyPlayer";
import "../../alg/test/alg-comparison";

describe("TwistyPlayer", () => {
it("can be constructed", () => {
Expand All @@ -8,4 +9,14 @@ describe("TwistyPlayer", () => {
});
expect(twistyPlayer.alg.experimentalNumUnits()).toEqual(3);
});

it("can set alg using string", () => {
const twistyPlayer = new TwistyPlayer({
alg: new Alg("R U R'"),
});
// TODO(https://github.com/microsoft/TypeScript/pull/42425): remove `@ts-ignore`.
// @ts-ignore
twistyPlayer.alg = "F2";
expect(twistyPlayer.alg).toBeIdentical(new Alg("F2"));
});
});
8 changes: 3 additions & 5 deletions src/cubing/twisty/dom/TwistyPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ export class TwistyPlayer extends ManagedCustomElement {
this.#legacyExperimentalPG3DViewConfig = legacyExperimentalPG3DViewConfig;
}

set alg(newAlg: Alg) {
// TODO(https://github.com/microsoft/TypeScript/pull/42425): Allow setting string in the type decl.
set alg(newAlg: Alg /* | string*/) {
// TODO: do validation for other algs as well.
if (typeof newAlg === "string") {
console.warn(
"`alg` for a `TwistyPlayer` was set using a string. It should be set using a `Sequence`!",
);
newAlg = new Alg((newAlg as unknown) as string);
newAlg = Alg.fromString(newAlg);
}
this.#config.attributes["alg"].setValue(newAlg);
this.cursor?.setAlg(newAlg, this.indexerConstructor()); // TODO: can we ensure the cursor already exists?
Expand Down

0 comments on commit 72249a6

Please sign in to comment.