Skip to content

Commit

Permalink
Allow null and undefined values in persistent refs
Browse files Browse the repository at this point in the history
  • Loading branch information
thepaperpilot authored and thepaperpilot committed Mar 29, 2024
1 parent c30724d commit dfb14ac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ module.exports = {
allowNullableObject: true,
allowNullableBoolean: true
}
],
"eqeqeq": [
"error",
"always",
{
"null": "never"
}
]
},
globals: {
Expand Down
6 changes: 4 additions & 2 deletions src/game/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export type State =
| number
| boolean
| DecimalSource
| null
| undefined
| { [key: string]: State }
| { [key: number]: State };

Expand Down Expand Up @@ -227,7 +229,7 @@ export function noPersist<T extends Persistent<S>, S extends State>(persistent:
if (key === PersistentState) {
return false;
}
if (key == SkipPersistence) {
if (key === SkipPersistence) {
return true;
}
return Reflect.has(target, key);
Expand Down Expand Up @@ -279,7 +281,7 @@ globalBus.on("addLayer", (layer: GenericLayer, saveData: Record<string, unknown>
// Handle SaveDataPath
const newPath = [layer.id, ...path, key];
if (
value[SaveDataPath] != undefined &&
value[SaveDataPath] != null &&
JSON.stringify(newPath) !== JSON.stringify(value[SaveDataPath])
) {
console.error(
Expand Down
3 changes: 2 additions & 1 deletion src/game/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export default window.player = player;

/** Convert a player save data object into a JSON string. Unwraps refs. */
export function stringifySave(player: Player): string {
return JSON.stringify(player, (key, value) => unref(value));
// Convert undefineds into nulls for proper parsing
return JSON.stringify(player, (key, value) => unref(value) ?? null);
}

declare global {
Expand Down
2 changes: 1 addition & 1 deletion src/util/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function loadSave(playerObj: Partial<Player>): Promise<void> {
playerObj.time &&
playerObj.devSpeed !== 0
) {
if (playerObj.offlineTime == undefined) playerObj.offlineTime = 0;
if (playerObj.offlineTime == null) playerObj.offlineTime = 0;
playerObj.offlineTime += Math.min(
playerObj.offlineTime + (Date.now() - playerObj.time) / 1000,
projInfo.offlineLimit * 3600
Expand Down

0 comments on commit dfb14ac

Please sign in to comment.