Allow null and undefined values in persistent refs #73

Closed
thepaperpilot wants to merge 1 commit from thepaperpilot:feat/null-persistence into main
4 changed files with 14 additions and 4 deletions

View file

@ -27,6 +27,13 @@ module.exports = {
allowNullableObject: true,
allowNullableBoolean: true
}
],
"eqeqeq": [
"error",
"always",
{
"null": "never"
}
]
},
globals: {

View file

@ -62,6 +62,8 @@ export type State =
| number
| boolean
| DecimalSource
| null
| undefined
| { [key: string]: State }
| { [key: number]: State };
@ -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);
@ -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(

View file

@ -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 {

View file

@ -109,7 +109,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