Skip to content

Commit 2029c42

Browse files
authored
fix: deprecate CoreNode.preventCleanup and re-introduce preload option (#486)
The `textureOptions.preload` was removed during the Texture Throttling refactor, reintroduced it even though its loading will be throttled. While looking at the properties, decided to move `preventCleanup` to `textureOptions` instead of as a property on the `CoreNode`. Technically this controls the behaviour of the texture, not the core node. Hence it better belongs on `textureOptions`. Will throw a deprecation warning in dev mode for a couple of releases and then we'll drop it.
2 parents 1906c2a + 01a240b commit 2029c42

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/core/CoreNode.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import {
2121
assertTruthy,
2222
getNewId,
23+
isProductionEnvironment,
2324
mergeColorAlphaPremultiplied,
2425
} from '../utils.js';
2526
import type { TextureOptions } from './CoreTextureManager.js';
@@ -413,7 +414,11 @@ export interface CoreNodeProps {
413414
texture: Texture | null;
414415

415416
/**
416-
* Whether to prevent the node from being cleaned up
417+
* [Deprecated]: Prevents the texture from being cleaned up when the Node is removed
418+
*
419+
* @remarks
420+
* Please use the `preventCleanup` property on {@link TextureOptions} instead.
421+
*
417422
* @default false
418423
*/
419424
preventCleanup: boolean;
@@ -768,6 +773,12 @@ export class CoreNode extends EventEmitter {
768773
UpdateType.RenderState,
769774
);
770775

776+
if (isProductionEnvironment() === false && props.preventCleanup === true) {
777+
console.warn(
778+
'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
779+
);
780+
}
781+
771782
// if the default texture isn't loaded yet, wait for it to load
772783
// this only happens when the node is created before the stage is ready
773784
if (
@@ -790,7 +801,12 @@ export class CoreNode extends EventEmitter {
790801
// We do this in a microtask to allow listeners to be attached in the same
791802
// synchronous task after calling loadTexture()
792803
queueMicrotask(() => {
793-
texture.preventCleanup = this.props.preventCleanup;
804+
if (this.textureOptions.preload === true) {
805+
this.stage.txManager.loadTexture(texture);
806+
}
807+
808+
texture.preventCleanup =
809+
this.props.textureOptions?.preventCleanup ?? false;
794810
texture.on('loaded', this.onTextureLoaded);
795811
texture.on('failed', this.onTextureFailed);
796812
texture.on('freed', this.onTextureFreed);
@@ -2027,11 +2043,17 @@ export class CoreNode extends EventEmitter {
20272043
}
20282044

20292045
get preventCleanup(): boolean {
2030-
return this.props.preventCleanup;
2046+
return this.props.textureOptions.preventCleanup || false;
20312047
}
20322048

20332049
set preventCleanup(value: boolean) {
2034-
this.props.preventCleanup = value;
2050+
if (isProductionEnvironment() === false) {
2051+
console.warn(
2052+
'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
2053+
);
2054+
}
2055+
2056+
this.props.textureOptions.preventCleanup = value;
20352057
}
20362058

20372059
get rtt(): boolean {

src/core/CoreTextureManager.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ export type ResizeModeOptions =
107107
* multiple Nodes each using a different set of options.
108108
*/
109109
export interface TextureOptions {
110+
/**
111+
* Preload the texture immediately even if it's not being rendered to the
112+
* screen.
113+
*
114+
* @remarks
115+
* This allows the texture to be used immediately without any delay when it
116+
* is first needed for rendering. Otherwise the loading process will start
117+
* when the texture is first rendered, which may cause a delay in that texture
118+
* being shown properly.
119+
*
120+
* @defaultValue `false`
121+
*/
122+
preload?: boolean;
123+
124+
/**
125+
* Prevent clean up of the texture when it is no longer being used.
126+
*
127+
* @remarks
128+
* This is useful when you want to keep the texture in memory for later use.
129+
* Regardless of whether the texture is being used or not, it will not be
130+
* cleaned up.
131+
*
132+
* @defaultValue `false`
133+
*/
134+
preventCleanup?: boolean;
135+
110136
/**
111137
* Flip the texture horizontally when rendering
112138
*

0 commit comments

Comments
 (0)