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, allowNullableObject: true,
allowNullableBoolean: true allowNullableBoolean: true
} }
],
"eqeqeq": [
"error",
"always",
{
"null": "never"
}
] ]
}, },
globals: { globals: {

View file

@ -62,6 +62,8 @@ export type State =
| number | number
| boolean | boolean
| DecimalSource | DecimalSource
| null
| undefined
| { [key: string]: State } | { [key: string]: State }
| { [key: number]: State }; | { [key: number]: State };
@ -227,7 +229,7 @@ export function noPersist<T extends Persistent<S>, S extends State>(persistent:
if (key === PersistentState) { if (key === PersistentState) {
return false; return false;
} }
if (key == SkipPersistence) { if (key === SkipPersistence) {
return true; return true;
} }
return Reflect.has(target, key); return Reflect.has(target, key);
@ -279,7 +281,7 @@ globalBus.on("addLayer", (layer: GenericLayer, saveData: Record<string, unknown>
// Handle SaveDataPath // Handle SaveDataPath
const newPath = [layer.id, ...path, key]; const newPath = [layer.id, ...path, key];
if ( if (
value[SaveDataPath] != undefined && value[SaveDataPath] != null &&
JSON.stringify(newPath) !== JSON.stringify(value[SaveDataPath]) JSON.stringify(newPath) !== JSON.stringify(value[SaveDataPath])
) { ) {
console.error( 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. */ /** Convert a player save data object into a JSON string. Unwraps refs. */
export function stringifySave(player: Player): string { 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 { declare global {

View file

@ -109,7 +109,7 @@ export async function loadSave(playerObj: Partial<Player>): Promise<void> {
playerObj.time && playerObj.time &&
playerObj.devSpeed !== 0 playerObj.devSpeed !== 0
) { ) {
if (playerObj.offlineTime == undefined) playerObj.offlineTime = 0; if (playerObj.offlineTime == null) playerObj.offlineTime = 0;
playerObj.offlineTime += Math.min( playerObj.offlineTime += Math.min(
playerObj.offlineTime + (Date.now() - playerObj.time) / 1000, playerObj.offlineTime + (Date.now() - playerObj.time) / 1000,
projInfo.offlineLimit * 3600 projInfo.offlineLimit * 3600