fixed some minor bugs
This commit is contained in:
parent
3e42c7027f
commit
82cbfd9e45
1 changed files with 7 additions and 17 deletions
|
@ -1,5 +1,7 @@
|
||||||
import {useEffect, useState} from "react";
|
import {useEffect, useState} from "react";
|
||||||
import * as _ from "lodash";
|
import * as _ from "lodash";
|
||||||
|
import {ReadonlyKeys, WritableKeys} from "tc-shared/proto";
|
||||||
|
import {useDependentState} from "tc-shared/ui/react-elements/Helper";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* To deliver optimized performance, we only promisify the values we need.
|
* To deliver optimized performance, we only promisify the values we need.
|
||||||
|
@ -10,18 +12,6 @@ import * as _ from "lodash";
|
||||||
export type UiVariable = Transferable | undefined | null | number | string | object;
|
export type UiVariable = Transferable | undefined | null | number | string | object;
|
||||||
export type UiVariableMap = { [key: string]: any }; //UiVariable | Readonly<UiVariable>
|
export type UiVariableMap = { [key: string]: any }; //UiVariable | Readonly<UiVariable>
|
||||||
|
|
||||||
type IfEquals<X, Y, A=X, B=never> =
|
|
||||||
(<T>() => T extends X ? 1 : 2) extends
|
|
||||||
(<T>() => T extends Y ? 1 : 2) ? A : B;
|
|
||||||
|
|
||||||
type WritableKeys<T> = {
|
|
||||||
[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P, never>
|
|
||||||
}[keyof T];
|
|
||||||
|
|
||||||
type ReadonlyKeys<T> = {
|
|
||||||
[P in keyof T]: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>
|
|
||||||
}[keyof T];
|
|
||||||
|
|
||||||
export type ReadonlyVariables<Variables extends UiVariableMap> = Pick<Variables, ReadonlyKeys<Variables>>
|
export type ReadonlyVariables<Variables extends UiVariableMap> = Pick<Variables, ReadonlyKeys<Variables>>
|
||||||
export type WriteableVariables<Variables extends UiVariableMap> = Pick<Variables, WritableKeys<Variables>>
|
export type WriteableVariables<Variables extends UiVariableMap> = Pick<Variables, WritableKeys<Variables>>
|
||||||
|
|
||||||
|
@ -247,7 +237,7 @@ export abstract class UiVariableConsumer<Variables extends UiVariableMap> {
|
||||||
const haveDefaultValue = arguments.length >= 3;
|
const haveDefaultValue = arguments.length >= 3;
|
||||||
const cacheEntry = this.getOrCreateVariable(variable as string, customData);
|
const cacheEntry = this.getOrCreateVariable(variable as string, customData);
|
||||||
|
|
||||||
const [ localValue, setLocalValue ] = useState<LocalVariableValue>(() => {
|
const [ localValue, setLocalValue ] = useDependentState<LocalVariableValue>(() => {
|
||||||
/* Variable constructor */
|
/* Variable constructor */
|
||||||
cacheEntry.useCount++;
|
cacheEntry.useCount++;
|
||||||
|
|
||||||
|
@ -266,7 +256,7 @@ export abstract class UiVariableConsumer<Variables extends UiVariableMap> {
|
||||||
status: "unset"
|
status: "unset"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
}, [ variable, customData ]);
|
||||||
|
|
||||||
const [, setRemoteVersion ] = useState(0);
|
const [, setRemoteVersion ] = useState(0);
|
||||||
|
|
||||||
|
@ -274,7 +264,7 @@ export abstract class UiVariableConsumer<Variables extends UiVariableMap> {
|
||||||
/* Initial rendered */
|
/* Initial rendered */
|
||||||
if(cacheEntry.status === "loaded" && localValue.status !== "set") {
|
if(cacheEntry.status === "loaded" && localValue.status !== "set") {
|
||||||
/* Update the local value to the current state */
|
/* Update the local value to the current state */
|
||||||
setLocalValue(cacheEntry.currentValue);
|
setLocalValue({ status: "set", value: cacheEntry.currentValue });
|
||||||
}
|
}
|
||||||
|
|
||||||
let listener;
|
let listener;
|
||||||
|
@ -291,7 +281,7 @@ export abstract class UiVariableConsumer<Variables extends UiVariableMap> {
|
||||||
cacheEntry.updateListener.remove(listener);
|
cacheEntry.updateListener.remove(listener);
|
||||||
this.derefVariable(cacheEntry);
|
this.derefVariable(cacheEntry);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [ variable, customData ]);
|
||||||
|
|
||||||
if(cacheEntry.status === "loading") {
|
if(cacheEntry.status === "loading") {
|
||||||
return {
|
return {
|
||||||
|
@ -379,7 +369,7 @@ export abstract class UiVariableConsumer<Variables extends UiVariableMap> {
|
||||||
cacheEntry.updateListener.remove(listener);
|
cacheEntry.updateListener.remove(listener);
|
||||||
this.derefVariable(cacheEntry);
|
this.derefVariable(cacheEntry);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [ variable, customData ]);
|
||||||
|
|
||||||
if(arguments.length >= 3) {
|
if(arguments.length >= 3) {
|
||||||
return cacheEntry.status === "loaded" ? cacheEntry.currentValue : defaultValue;
|
return cacheEntry.status === "loaded" ? cacheEntry.currentValue : defaultValue;
|
||||||
|
|
Loading…
Add table
Reference in a new issue