forked from profectus/Profectus
Cleaned up NonPersistent typing
This commit is contained in:
parent
5fa2e521b2
commit
36f064ebd7
2 changed files with 9 additions and 7 deletions
|
@ -2,14 +2,14 @@ import type { OptionsFunc, Replace } from "features/feature";
|
||||||
import { getUniqueID } from "features/feature";
|
import { getUniqueID } from "features/feature";
|
||||||
import { globalBus } from "game/events";
|
import { globalBus } from "game/events";
|
||||||
import type { BaseLayer } from "game/layers";
|
import type { BaseLayer } from "game/layers";
|
||||||
import type { Persistent, State } from "game/persistence";
|
import type { NonPersistent, Persistent } from "game/persistence";
|
||||||
import { DefaultValue, persistent, PersistentState } from "game/persistence";
|
import { DefaultValue, persistent } from "game/persistence";
|
||||||
import type { Unsubscribe } from "nanoevents";
|
import type { Unsubscribe } from "nanoevents";
|
||||||
import Decimal from "util/bignum";
|
import Decimal from "util/bignum";
|
||||||
import type { Computable, GetComputableType } from "util/computed";
|
import type { Computable, GetComputableType } from "util/computed";
|
||||||
import { processComputable } from "util/computed";
|
import { processComputable } from "util/computed";
|
||||||
import { createLazyProxy } from "util/proxies";
|
import { createLazyProxy } from "util/proxies";
|
||||||
import { isRef, unref, WritableComputedRef } from "vue";
|
import { isRef, unref } from "vue";
|
||||||
|
|
||||||
export const ResetType = Symbol("Reset");
|
export const ResetType = Symbol("Reset");
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ export function createReset<T extends ResetOptions>(
|
||||||
const handleObject = (obj: unknown) => {
|
const handleObject = (obj: unknown) => {
|
||||||
if (obj && typeof obj === "object") {
|
if (obj && typeof obj === "object") {
|
||||||
if (DefaultValue in obj) {
|
if (DefaultValue in obj) {
|
||||||
const persistent = obj as WritableComputedRef<T> & { [DefaultValue]: T };
|
const persistent = obj as NonPersistent;
|
||||||
persistent.value = persistent[DefaultValue];
|
persistent.value = persistent[DefaultValue];
|
||||||
} else if (!(obj instanceof Decimal) && !isRef(obj)) {
|
} else if (!(obj instanceof Decimal) && !isRef(obj)) {
|
||||||
Object.values(obj).forEach(obj =>
|
Object.values(obj).forEach(obj =>
|
||||||
|
|
|
@ -71,13 +71,15 @@ export type Persistent<T extends State = State> = Ref<T> & {
|
||||||
/**
|
/**
|
||||||
* 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.
|
* 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<T> & { [DefaultValue]: T };
|
[NonPersistent]: NonPersistent<T>;
|
||||||
/**
|
/**
|
||||||
* The path this persistent appears in within the save data object. Predominantly used to ensure it's only placed in there one time.
|
* 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;
|
[SaveDataPath]: string[] | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type NonPersistent<T extends State = State> = WritableComputedRef<T> & { [DefaultValue]: T };
|
||||||
|
|
||||||
function getStackTrace() {
|
function getStackTrace() {
|
||||||
return (
|
return (
|
||||||
new Error().stack
|
new Error().stack
|
||||||
|
@ -102,7 +104,7 @@ export function persistent<T extends State>(defaultValue: T | Ref<T>): Persisten
|
||||||
persistent[DefaultValue] = isRef(defaultValue) ? defaultValue.value : defaultValue;
|
persistent[DefaultValue] = isRef(defaultValue) ? defaultValue.value : defaultValue;
|
||||||
persistent[StackTrace] = getStackTrace();
|
persistent[StackTrace] = getStackTrace();
|
||||||
persistent[Deleted] = false;
|
persistent[Deleted] = false;
|
||||||
const nonPersistent: WritableComputedRef<T> & Partial<{ [DefaultValue]: T }> = computed({
|
const nonPersistent: Partial<NonPersistent<T>> = computed({
|
||||||
get() {
|
get() {
|
||||||
return persistent.value;
|
return persistent.value;
|
||||||
},
|
},
|
||||||
|
@ -111,7 +113,7 @@ export function persistent<T extends State>(defaultValue: T | Ref<T>): Persisten
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
nonPersistent[DefaultValue] = persistent[DefaultValue];
|
nonPersistent[DefaultValue] = persistent[DefaultValue];
|
||||||
persistent[NonPersistent] = nonPersistent as WritableComputedRef<T> & { [DefaultValue]: T };
|
persistent[NonPersistent] = nonPersistent as NonPersistent<T>;
|
||||||
persistent[SaveDataPath] = undefined;
|
persistent[SaveDataPath] = undefined;
|
||||||
|
|
||||||
if (addingLayers.length === 0) {
|
if (addingLayers.length === 0) {
|
||||||
|
|
Loading…
Reference in a new issue