Skip to content

Commit

Permalink
Merge pull request #145 from tonybaloney/fix_getwebview_inactive_expl…
Browse files Browse the repository at this point in the history
…orer

Raise a more meaningful error when the explorer window is closed and …
  • Loading branch information
tonybaloney authored Sep 30, 2022
2 parents 434458a + 2fd4cfb commit aa7bd93
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"semi": "off",
"no-unused-vars": "error",
"unused-imports/no-unused-imports": "error",
"prefer-const": "error"
"prefer-const": "error",
"@typescript-eslint/no-non-null-assertion": "warn"
}
}
32 changes: 18 additions & 14 deletions media/main-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,12 @@ function saveState() {
var state = new states_1.PetPanelState();
state.petStates = new Array();
exports.allPets.pets().forEach((petItem) => {
state.petStates.push({
state.petStates?.push({
petName: petItem.pet.name(),
petColor: petItem.color,
petType: petItem.type,
petState: petItem.pet.getState(),
petFriend: petItem.pet.friend()
? petItem.pet.friend().name()
: undefined,
petFriend: petItem.pet.friend()?.name() ?? undefined,
elLeft: petItem.el.style.left,
elBottom: petItem.el.style.bottom,
});
Expand All @@ -338,16 +336,16 @@ function recoverState(basePetUri, petSize, floor) {
petCounter = 1;
}
else {
petCounter = state.petCounter;
petCounter = state.petCounter ?? 1;
}
var recoveryMap = new Map();
state.petStates.forEach((p) => {
state.petStates?.forEach((p) => {
// Fixes a bug related to duck animations
if (p.petType === 'rubber duck') {
p.petType = 'rubber-duck';
}
try {
var newPet = addPetToPanel(p.petType, basePetUri, p.petColor, petSize, parseInt(p.elLeft), parseInt(p.elBottom), floor, p.petName);
var newPet = addPetToPanel(p.petType ?? "cat" /* PetType.cat */, basePetUri, p.petColor ?? "brown" /* PetColor.brown */, petSize, parseInt(p.elLeft ?? '0'), parseInt(p.elBottom ?? '0'), floor, p.petName);
exports.allPets.push(newPet);
recoveryMap.set(newPet.pet, p);
}
Expand All @@ -357,7 +355,9 @@ function recoverState(basePetUri, petSize, floor) {
});
recoveryMap.forEach((state, pet) => {
// Recover previous state.
pet.recoverState(state.petState);
if (state.petState !== undefined) {
pet.recoverState(state.petState);
}
// Resolve friend relationships
var friend = undefined;
if (state.petFriend) {
Expand All @@ -383,6 +383,7 @@ function petPanelApp(basePetUri, theme, themeKind, petColor, petSize, petType) {
const ballRadius = calculateBallRadius(petSize);
var floor = 0;
// Apply Theme backgrounds
const foregroundEl = document.getElementById('foreground');
if (theme !== "none" /* Theme.none */) {
var _themeKind = '';
switch (themeKind) {
Expand All @@ -398,12 +399,14 @@ function petPanelApp(basePetUri, theme, themeKind, petColor, petSize, petType) {
break;
}
document.body.style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/background-${_themeKind}-${petSize}.png')`;
document.getElementById('foreground').style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/foreground-${_themeKind}-${petSize}.png')`;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
foregroundEl.style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/foreground-${_themeKind}-${petSize}.png')`;
floor = calculateFloor(petSize, theme); // Themes have pets at a specified height from the ground
}
else {
document.body.style.backgroundImage = '';
document.getElementById('foreground').style.backgroundImage = '';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
foregroundEl.style.backgroundImage = '';
}
/// Bouncing ball components, credit https://stackoverflow.com/a/29982343
const gravity = 0.2, damping = 0.9, traction = 0.8;
Expand Down Expand Up @@ -721,7 +724,7 @@ class BasePetType {
recoverState(state) {
// TODO : Resolve a bug where if it was swiping before, it would fail
// because holdState is no longer valid.
this.currentStateEnum = state.currentStateEnum;
this.currentStateEnum = state.currentStateEnum ?? "sit-idle" /* States.sitIdle */;
this.currentState = (0, states_1.resolveState)(this.currentStateEnum, this);
if (!(0, states_1.isStateAboveGround)(this.currentStateEnum)) {
// Reset the bottom of the sprite to the floor as the theme
Expand Down Expand Up @@ -790,7 +793,7 @@ class BasePetType {
if (this.hasFriend() &&
this.currentStateEnum !== "chase-friend" /* States.chaseFriend */ &&
this.isMoving()) {
if (this.friend().isPlaying() &&
if (this.friend()?.isPlaying() &&
!(0, states_1.isStateAboveGround)(this.currentStateEnum)) {
this.currentState = (0, states_1.resolveState)("chase-friend" /* States.chaseFriend */, this);
this.currentStateEnum = "chase-friend" /* States.chaseFriend */;
Expand Down Expand Up @@ -1355,7 +1358,7 @@ class InvalidPetException {
exports.InvalidPetException = InvalidPetException;
function getPetName(collection, label, count) {
if (collection.has(count)) {
return collection.get(count);
return collection.get(count) ?? (label + count);
}
else {
return label + count;
Expand Down Expand Up @@ -1697,9 +1700,10 @@ class ChaseFriendState {
this.pet = pet;
}
nextFrame() {
if (!this.pet.friend().isPlaying()) {
if (!this.pet.hasFriend() || !this.pet.friend()?.isPlaying()) {
return FrameResult.stateCancel; // Friend is no longer playing.
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (this.pet.left() > this.pet.friend().left()) {
this.horizontalDirection = HorizontalDirection.left;
this.pet.positionLeft(this.pet.left() - this.pet.speed());
Expand Down
24 changes: 13 additions & 11 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class PetSpecification {
for (let index = 0; index < contextTypes.length; index++) {
result.push(
new PetSpecification(
contextColors![index],
contextColors?.[index] ?? DEFAULT_COLOR,
contextTypes[index],
size,
),
Expand Down Expand Up @@ -287,7 +287,7 @@ export function activate(context: vscode.ExtensionContext) {
getConfiguredSize(),
);
collection.forEach((item) => {
PetPanel.currentPanel!.spawnPet(item);
PetPanel.currentPanel?.spawnPet(item);
});
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ export function activate(context: vscode.ExtensionContext) {
const panel = getPetPanel();
if (panel !== undefined) {
panel.listPets();
getWebview()!.onDidReceiveMessage(
getWebview()?.onDidReceiveMessage(
handleRemovePetMessage,
context,
);
Expand All @@ -376,7 +376,7 @@ export function activate(context: vscode.ExtensionContext) {
getConfiguredSize(),
);
collection.forEach((item) => {
PetPanel.currentPanel!.spawnPet(item);
PetPanel.currentPanel?.spawnPet(item);
});
storeCollectionAsMemento(context, collection);
} else {
Expand Down Expand Up @@ -487,7 +487,7 @@ export function activate(context: vscode.ExtensionContext) {
getConfiguredSize(),
);
collection.forEach((item) => {
PetPanel.currentPanel!.spawnPet(item);
PetPanel.currentPanel?.spawnPet(item);
});
storeCollectionAsMemento(context, collection);
} else {
Expand Down Expand Up @@ -528,7 +528,7 @@ export function activate(context: vscode.ExtensionContext) {
getConfiguredSize(),
);
collection.forEach((item) => {
PetPanel.currentPanel!.spawnPet(item);
PetPanel.currentPanel?.spawnPet(item);
});
storeCollectionAsMemento(context, collection);
} else {
Expand Down Expand Up @@ -1042,12 +1042,14 @@ class PetWebviewViewProvider extends PetWebviewContainer {
this._update();
}

public resetPets() {
this.getWebview().postMessage({ command: 'reset-pet' });
}

getWebview(): vscode.Webview {
return this._webviewView!.webview;
if (this._webviewView === undefined) {
throw new Error(
'Panel not active, make sure the pets view is visible before running this command.',
);
} else {
return this._webviewView.webview;
}
}
}

Expand Down
31 changes: 16 additions & 15 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,12 @@ export function saveState() {
state.petStates = new Array();

allPets.pets().forEach((petItem) => {
state.petStates!.push({
state.petStates?.push({
petName: petItem.pet.name(),
petColor: petItem.color,
petType: petItem.type,
petState: petItem.pet.getState(),
petFriend: petItem.pet.friend()
? petItem.pet.friend().name()
: undefined,
petFriend: petItem.pet.friend()?.name() ?? undefined,
elLeft: petItem.el.style.left,
elBottom: petItem.el.style.bottom,
});
Expand All @@ -171,24 +169,24 @@ function recoverState(basePetUri: string, petSize: PetSize, floor: number) {
if (state.petCounter === undefined || isNaN(state.petCounter)) {
petCounter = 1;
} else {
petCounter = state.petCounter!;
petCounter = state.petCounter ?? 1;
}

var recoveryMap: Map<IPetType, PetElementState> = new Map();
state.petStates!.forEach((p) => {
state.petStates?.forEach((p) => {
// Fixes a bug related to duck animations
if ((p.petType as string) === 'rubber duck') {
(p.petType as string) = 'rubber-duck';
}

try {
var newPet = addPetToPanel(
p.petType!,
p.petType ?? PetType.cat,
basePetUri,
p.petColor!,
p.petColor ?? PetColor.brown,
petSize,
parseInt(p.elLeft!),
parseInt(p.elBottom!),
parseInt(p.elLeft ?? '0'),
parseInt(p.elBottom ?? '0'),
floor,
p.petName,
);
Expand All @@ -202,7 +200,9 @@ function recoverState(basePetUri: string, petSize: PetSize, floor: number) {
});
recoveryMap.forEach((state, pet) => {
// Recover previous state.
pet.recoverState(state.petState!);
if (state.petState !== undefined) {
pet.recoverState(state.petState);
}

// Resolve friend relationships
var friend = undefined;
Expand Down Expand Up @@ -240,6 +240,7 @@ export function petPanelApp(
const ballRadius: number = calculateBallRadius(petSize);
var floor = 0;
// Apply Theme backgrounds
const foregroundEl = document.getElementById('foreground');
if (theme !== Theme.none) {
var _themeKind = '';
switch (themeKind) {
Expand All @@ -256,14 +257,14 @@ export function petPanelApp(
}

document.body.style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/background-${_themeKind}-${petSize}.png')`;
document.getElementById(
'foreground',
)!.style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/foreground-${_themeKind}-${petSize}.png')`;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
foregroundEl!.style.backgroundImage = `url('${basePetUri}/backgrounds/${theme}/foreground-${_themeKind}-${petSize}.png')`;

floor = calculateFloor(petSize, theme); // Themes have pets at a specified height from the ground
} else {
document.body.style.backgroundImage = '';
document.getElementById('foreground')!.style.backgroundImage = '';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
foregroundEl!.style.backgroundImage = '';
}

/// Bouncing ball components, credit https://stackoverflow.com/a/29982343
Expand Down
12 changes: 6 additions & 6 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface IPetType {
name(): string;
emoji(): string;
hasFriend(): boolean;
friend(): IPetType;
friend(): IPetType | undefined;
makeFriendsWith(friend: IPetType): boolean;
isPlaying(): boolean;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ abstract class BasePetType implements IPetType {
recoverState(state: PetInstanceState) {
// TODO : Resolve a bug where if it was swiping before, it would fail
// because holdState is no longer valid.
this.currentStateEnum = state.currentStateEnum!;
this.currentStateEnum = state.currentStateEnum ?? States.sitIdle;
this.currentState = resolveState(this.currentStateEnum, this);

if (!isStateAboveGround(this.currentStateEnum)) {
Expand Down Expand Up @@ -391,7 +391,7 @@ abstract class BasePetType implements IPetType {
this.isMoving()
) {
if (
this.friend().isPlaying() &&
this.friend()?.isPlaying() &&
!isStateAboveGround(this.currentStateEnum)
) {
this.currentState = resolveState(States.chaseFriend, this);
Expand Down Expand Up @@ -431,8 +431,8 @@ abstract class BasePetType implements IPetType {
return this._friend !== undefined;
}

friend(): IPetType {
return this._friend!;
friend(): IPetType | undefined {
return this._friend;
}

name(): string {
Expand Down Expand Up @@ -968,7 +968,7 @@ function getPetName(
count: number,
): string {
if (collection.has(count)) {
return collection.get(count)!;
return collection.get(count) ?? label + count;
} else {
return label + count;
}
Expand Down
5 changes: 3 additions & 2 deletions src/panel/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,11 @@ export class ChaseFriendState implements IState {
}

nextFrame(): FrameResult {
if (!this.pet.friend().isPlaying()) {
if (!this.pet.hasFriend() || !this.pet.friend()?.isPlaying()) {
return FrameResult.stateCancel; // Friend is no longer playing.
}
if (this.pet.left() > this.pet.friend().left()) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (this.pet.left() > this.pet.friend()!.left()) {
this.horizontalDirection = HorizontalDirection.left;
this.pet.positionLeft(this.pet.left() - this.pet.speed());
} else {
Expand Down

0 comments on commit aa7bd93

Please sign in to comment.