From 36f064ebd7a77eab96fa0fd04ce7fa3cd0c69723 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Tue, 6 Dec 2022 07:14:42 -0600 Subject: [PATCH] Cleaned up NonPersistent typing --- src/features/reset.ts | 8 ++++---- src/game/persistence.ts | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/features/reset.ts b/src/features/reset.ts index 0b8153a..92cb506 100644 --- a/src/features/reset.ts +++ b/src/features/reset.ts @@ -2,14 +2,14 @@ import type { OptionsFunc, Replace } from "features/feature"; import { getUniqueID } from "features/feature"; import { globalBus } from "game/events"; import type { BaseLayer } from "game/layers"; -import type { Persistent, State } from "game/persistence"; -import { DefaultValue, persistent, PersistentState } from "game/persistence"; +import type { NonPersistent, Persistent } from "game/persistence"; +import { DefaultValue, persistent } from "game/persistence"; import type { Unsubscribe } from "nanoevents"; import Decimal from "util/bignum"; import type { Computable, GetComputableType } from "util/computed"; import { processComputable } from "util/computed"; import { createLazyProxy } from "util/proxies"; -import { isRef, unref, WritableComputedRef } from "vue"; +import { isRef, unref } from "vue"; export const ResetType = Symbol("Reset"); @@ -45,7 +45,7 @@ export function createReset( const handleObject = (obj: unknown) => { if (obj && typeof obj === "object") { if (DefaultValue in obj) { - const persistent = obj as WritableComputedRef & { [DefaultValue]: T }; + const persistent = obj as NonPersistent; persistent.value = persistent[DefaultValue]; } else if (!(obj instanceof Decimal) && !isRef(obj)) { Object.values(obj).forEach(obj => diff --git a/src/game/persistence.ts b/src/game/persistence.ts index 795a130..ace98eb 100644 --- a/src/game/persistence.ts +++ b/src/game/persistence.ts @@ -71,13 +71,15 @@ export type Persistent = Ref & { /** * A non-persistent ref that just reads and writes ot the persistent ref. Used for passing to other features without duplicating the persistent ref in the constructed save data object. */ - [NonPersistent]: WritableComputedRef & { [DefaultValue]: T }; + [NonPersistent]: NonPersistent; /** * The path this persistent appears in within the save data object. Predominantly used to ensure it's only placed in there one time. */ [SaveDataPath]: string[] | undefined; }; +export type NonPersistent = WritableComputedRef & { [DefaultValue]: T }; + function getStackTrace() { return ( new Error().stack @@ -102,7 +104,7 @@ export function persistent(defaultValue: T | Ref): Persisten persistent[DefaultValue] = isRef(defaultValue) ? defaultValue.value : defaultValue; persistent[StackTrace] = getStackTrace(); persistent[Deleted] = false; - const nonPersistent: WritableComputedRef & Partial<{ [DefaultValue]: T }> = computed({ + const nonPersistent: Partial> = computed({ get() { return persistent.value; }, @@ -111,7 +113,7 @@ export function persistent(defaultValue: T | Ref): Persisten } }); nonPersistent[DefaultValue] = persistent[DefaultValue]; - persistent[NonPersistent] = nonPersistent as WritableComputedRef & { [DefaultValue]: T }; + persistent[NonPersistent] = nonPersistent as NonPersistent; persistent[SaveDataPath] = undefined; if (addingLayers.length === 0) {