Skip to content

Commit b7cf465

Browse files
committed
add silent updates to deep stores
- additionally, update camera-controls
1 parent cba984f commit b7cf465

10 files changed

+101
-35
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"./src",
2828
"package.json",
2929
"README.md"
30-
],
31-
"types": "./types/index.d.ts",
32-
"exports": {
30+
],
31+
"types": "./types/index.d.ts",
32+
"exports": {
3333
"./host/*": "./dist/host/*",
3434
"./ui/*": "./dist/ui/*"
35-
},
35+
},
3636
"repository": {
3737
"type": "git",
3838
"url": "git+https://github.com/alexpineda/titan-reactor.git"
@@ -65,7 +65,7 @@
6565
"bl": "^5.0.0",
6666
"buffer": "^6.0.3",
6767
"bw-chk": "^1.4.0",
68-
"camera-controls": "1.37.2",
68+
"camera-controls": "2.7.3",
6969
"concat-stream": "^2.0.0",
7070
"deep-diff": "^1.0.2",
7171
"deepmerge": "^4.2.2",

src/camera/game-viewport.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ export class GameViewPort extends Sizeable {
128128
this.cameraShake.restore( this.camera );
129129
}
130130

131-
update( targetDamping: number, delta: number ) {
131+
update( targetSmoothTime: number, delta: number ) {
132132

133133
this.viewport.copy(this.getActualSize());
134134
this.#direction32 = this.rotateSprites
135135
? getDirection32( this.projectedView.center, this.camera.getWorldPosition( _target ))
136136
: 0;
137137

138-
this.orbit.dampingFactor = MathUtils.damp(
139-
this.orbit.dampingFactor,
140-
targetDamping,
138+
this.orbit.smoothTime = MathUtils.damp(
139+
this.orbit.smoothTime,
140+
targetSmoothTime,
141141
0.0001,
142142
delta
143143
);

src/common/get-app-settings-leva-config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ const getInputConfig = (
284284
),
285285
},
286286
"input.dampingFactor": {
287-
label: "Camera Movement Damping",
287+
label: "Camera Movement Time",
288288
value: input.dampingFactor,
289-
min: 0.01,
290-
max: 0.1,
291-
step: 0.01,
289+
min: 0.1,
290+
max: 10,
291+
step: 0.1,
292292
},
293293
"input.zoomLevels": {
294294
label: "Camera Zoom Levels",

src/core/titan-reactor.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ logCapabilities();
185185
// }
186186
// metaVerse().setSession(session);
187187

188-
await initCacheDB();
188+
try {
189+
await initCacheDB();
190+
} catch (e) {}
189191

190192
await sceneStore().loadScene(new LoadingScene());
191193

src/core/world/plugin-session-store.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ export const createPluginSessionStore = (
8888

8989
const vars = plugins.reduce( ( acc, plugin ) => {
9090
Object.keys( plugin.rawConfig ?? {} ).forEach( ( key ) => {
91-
if ( key !== "system" ) {
92-
const compKey = [ plugin.name, key ];
93-
lSet( acc, compKey, store.createVariable( compKey ) );
94-
}
91+
const compKey = [ plugin.name, key ];
92+
lSet( acc, compKey, store.createVariable( compKey ) );
9593
} );
9694

9795
return acc;

src/core/world/settings-session-store.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ export const createSettingsSessionStore = ( events: TypeEmitter<WorldEvents> ) =
6262
const store = createOperatableStore(
6363
createDeepStore<PartialSettings>( {
6464
initialState: sourceOfTruth.clone(),
65-
validateMerge: ( newSettings, rhs ) =>
66-
events.emit( "settings-changed", { settings: newSettings, rhs } ) !==
67-
false,
65+
onUpdate: ( newSettings, rhs ) =>
66+
events.emit( "settings-changed", { settings: newSettings, rhs } ),
6867
} ),
6968
sourceOfTruth,
7069
( path, state ) =>

src/image/loader/indexed-db-cache.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,58 @@ export const cacheDBStoreNames = ["general-casc-cache", "image-cache"] as const;
1212

1313
export let appCacheDb: IDBDatabase;
1414

15+
let _cacheWasOpened = false;
16+
1517
export const initCacheDB = () =>
1618
new Promise((resolve, reject) => {
19+
const _timeout = setTimeout(() => {
20+
console.error("IndexedDB timeout");
21+
reject(new Error("IndexedDB timeout"));
22+
}, 1000);
23+
1724
console.log("Opening IndexedDB");
1825
const dbReq = indexedDB.open(DB_NAME, DB_VERSION);
1926

2027
dbReq.onblocked = () => {
28+
clearTimeout(_timeout);
2129
console.error("IndexedDB blocked");
2230
reject(new Error("IndexedDB blocked"));
2331
};
2432

2533
// Create the schema
2634
dbReq.onupgradeneeded = () => {
35+
clearTimeout(_timeout);
2736
console.log("IndexedDB upgrade needed");
2837
appCacheDb = dbReq.result;
2938
for (const storeName of cacheDBStoreNames) {
3039
if (!appCacheDb.objectStoreNames.contains(storeName)) {
3140
appCacheDb.createObjectStore(storeName, { keyPath: "id" });
3241
}
3342
}
43+
_cacheWasOpened = true;
3444
resolve(undefined);
3545
};
3646

3747
// Error handler
3848
dbReq.onerror = (error) => {
49+
clearTimeout(_timeout);
3950
console.error(error);
4051
reject(error);
4152
};
4253

4354
// Success handler
4455
dbReq.onsuccess = () => {
56+
clearTimeout(_timeout);
4557
console.log("IndexedDB opened successfully");
4658
appCacheDb = dbReq.result;
59+
_cacheWasOpened = true;
4760
resolve(undefined);
4861
};
4962
});
5063

5164
export class IndexedDBCache {
5265
#storeName: CacheDBStoreName;
53-
#enabled = true;
66+
#enabled = _cacheWasOpened;
5467

5568
constructor(storeName: CacheDBStoreName) {
5669
this.#storeName = storeName;
@@ -61,6 +74,9 @@ export class IndexedDBCache {
6174
}
6275

6376
set enabled(value: boolean) {
77+
if (_cacheWasOpened === false) {
78+
return;
79+
}
6480
if (value !== this.#enabled && value === false) {
6581
this.clear();
6682
}

src/stores/deep-store.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,17 @@ export function createDeepStore<T extends Record<string, any>>( {
4747

4848
const getValue = ( path: string[] ) => lGet( store, path ) as unknown;
4949

50-
const setValue = ( path: string[], value: any ) => {
51-
merge( lSet<DeepPartial<T>>( {}, path, value ), path, value );
50+
/**
51+
* Merge changes to our deep store
52+
* Typically we want a copy so that it can be diffed against the change, but this incurs quite a cost
53+
* So we provide the option to "silentUpdate" to avoid this cost. Only use if performance critical.
54+
*/
55+
const setValue = ( path: string[], value: any, silentUpdate = false ) => {
56+
if (silentUpdate) {
57+
lSet<DeepPartial<T>>( store, path, value );
58+
} else {
59+
merge( lSet<DeepPartial<T>>( {}, path, value ), path, value );
60+
}
5261
};
5362

5463
return {
@@ -61,7 +70,7 @@ export function createDeepStore<T extends Record<string, any>>( {
6170

6271
export interface DeepStore<T extends object> {
6372
getState: () => T;
64-
setValue: ( path: string[], value: unknown ) => void;
73+
setValue: ( path: string[], value: unknown, silentUpdate?: boolean ) => void;
6574
getValue: ( path: string[] ) => unknown;
6675
merge: ( rhs: DeepPartial<T> ) => void;
6776
}

src/stores/operatable-store.ts

+48-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SourceOfTruth } from "./source-of-truth";
77
export type MutationVariable = ReturnType<ReturnType<typeof createMutationVariable>>;
88

99
export const createMutationVariable =
10-
( operate: ( operation: Operation ) => void, getValue: ( path: string[] ) => unknown ) =>
10+
( operate: OperationRequest, getValue: ( path: string[] ) => unknown ) =>
1111
( path: string[] ) => {
1212
const _data: {
1313
value: unknown;
@@ -19,10 +19,10 @@ export const createMutationVariable =
1919
operator: Operator.Set,
2020
};
2121

22-
const apply = ( operator: Operator, value?: any ) => {
22+
const apply = ( operator: Operator, value?: any, silentUpdate = false ) => {
2323
_data.operator = operator;
2424
_data.value = value;
25-
operate( _data );
25+
operate( _data, x => x, silentUpdate );
2626
};
2727

2828
const fn = function ( value?: unknown ) {
@@ -69,11 +69,50 @@ export const createMutationVariable =
6969
* Reset the value of the property to the default.
7070
*/
7171
toggle: () => apply( Operator.Toggle ),
72+
73+
// silent update variants
74+
75+
$set: ( value: any ) => apply( Operator.Set, value, true ),
76+
77+
/**
78+
* Increase the value of the property.
79+
*/
80+
$inc: () => apply( Operator.Increase, null, true ),
81+
/**
82+
* Increase the value of the property. Loop around if the value is greater than the maximum.
83+
*/
84+
$incCycle: () => apply( Operator.IncreaseCycle, null, true ),
85+
/**
86+
* Decrease the value of the property.
87+
*/
88+
$dec: () => apply( Operator.Decrease, null, true ),
89+
/**
90+
* Decrease the value of the property. Loop around if the value is less than the minimum.
91+
*/
92+
$decCycle: () => apply( Operator.DecreaseCycle, null, true ),
93+
/**
94+
* Set the value of the property to the minimum.
95+
*/
96+
$min: () => apply( Operator.Min, null, true ),
97+
/**
98+
* Set the value of the property to the maximum.
99+
*/
100+
$max: () => apply( Operator.Max, null, true ),
101+
/**
102+
* Reset the value of the property to the default.
103+
*/
104+
$reset: () => apply( Operator.SetToDefault, null, true ),
105+
/**
106+
* Reset the value of the property to the default.
107+
*/
108+
$toggle: () => apply( Operator.Toggle, null, true ),
72109
} );
73110
};
74111

112+
type OperationRequest = ( action: Operation, transformPath?: ( path: string[] ) => string[], silentUpdate?: boolean ) => void;
113+
75114
export type OperatableStore<T extends object> = DeepStore<T> & {
76-
operate: ( action: Operation, transformPath?: ( path: string[] ) => string[] ) => void;
115+
operate: OperationRequest;
77116
createVariable: ( path: string[] ) => MutationVariable;
78117
sourceOfTruth: SourceOfTruth<T>;
79118
};
@@ -98,7 +137,9 @@ export function createOperatableStore<T extends object>(
98137

99138
const operate = (
100139
operation: Operation,
101-
transformPath: ( path: string[] ) => string[] = ( x ) => x
140+
// this should probably belong to the store itself
141+
transformPath: ( path: string[] ) => string[] = ( x ) => x,
142+
silentUpdate = false
102143
) => {
103144
const path = transformPath( operation.path );
104145

@@ -112,7 +153,8 @@ export function createOperatableStore<T extends object>(
112153
field,
113154
operation.value,
114155
sourceOfTruth.getValue( path )
115-
)
156+
),
157+
silentUpdate
116158
);
117159
} else {
118160
log.warn( "Session field is no found." );

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2522,10 +2522,10 @@ camelcase@^6.2.0:
25222522
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a"
25232523
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
25242524

2525-
camera-controls@1.37.2:
2526-
version "1.37.2"
2527-
resolved "https://registry.yarnpkg.com/camera-controls/-/camera-controls-1.37.2.tgz#12d808c98b3fea1fffd72c8dfeb024f2495ab307"
2528-
integrity sha512-b+8DYJc/honm3b/PzjTQbVjsnkdgiDnI44/PRAWKlLhF0lzggGtaU9ZSGRM/lll2HkF0KN+OHx/ZN80wJmEbqA==
2525+
camera-controls@2.7.3:
2526+
version "2.7.3"
2527+
resolved "https://registry.yarnpkg.com/camera-controls/-/camera-controls-2.7.3.tgz#99e0449f68d203bf5f98f6c4ac0021c10b5c13a8"
2528+
integrity sha512-L4mxjBd3u8qiOLozdWrH2P8ZybSsDXBF7iyNyqNEFJhPUkovmuARWR8JTc1B/qlclOIg6FvZZA/0uAZMMim0mw==
25292529

25302530
caniuse-lite@^1.0.30001449:
25312531
version "1.0.30001480"

0 commit comments

Comments
 (0)