Replace throw with console.error to prevent blocking the main thread (#2077)

* chore: replace throw with console.error to prevent blocking the main thread
This commit is contained in:
Kbscript
2024-05-09 18:10:22 +08:00
committed by GitHub
parent 4b2834500f
commit 3b5d2945e2
3 changed files with 14 additions and 6 deletions

View File

@@ -91,7 +91,8 @@ export class Shader implements IReferable {
const shaderInfo = Shader._shaderLab.parseShader(nameOrShaderSource);
if (shaderMap[shaderInfo.name]) {
throw `Shader named "${shaderInfo.name}" already exists.`;
console.error(`Shader named "${shaderInfo.name}" already exists.`);
return;
}
const subShaderList = shaderInfo.subShaders.map((subShaderInfo) => {
const passList = subShaderInfo.passes.map((passInfo) => {
@@ -136,7 +137,8 @@ export class Shader implements IReferable {
return shader;
} else {
if (shaderMap[nameOrShaderSource]) {
throw `Shader named "${nameOrShaderSource}" already exists.`;
console.error(`Shader named "${nameOrShaderSource}" already exists.`);
return;
}
if (typeof vertexSourceOrShaderPassesOrSubShaders === "string") {
const shaderPass = new ShaderPass(vertexSourceOrShaderPassesOrSubShaders, fragmentSource);

View File

@@ -29,9 +29,10 @@ describe("Shader", () => {
customShader = Shader.create("custom", [new SubShader("Default", [new ShaderPass(customVS, customFS)])]);
// Create same name shader
expect(() => {
Shader.create("custom", [new SubShader("Default", [new ShaderPass(customVS, customFS)])]);
}).throw();
const errorSpy = chai.spy.on(console, "error");
Shader.create("custom", [new SubShader("Default", [new ShaderPass(customVS, customFS)])]);
expect(errorSpy).to.have.been.called.with('Shader named "custom" already exists.');
chai.spy.restore(console, "error");
// Create shader by empty SubShader array
expect(() => {

View File

@@ -228,8 +228,13 @@ describe("ShaderLab", () => {
const shaderInstance = Shader.create(demoShader);
expect(shaderInstance).instanceOf(Shader);
expect(Shader.create.bind(null, demoShader)).to.throw('Shader named "Gem" already exists.');
const errorSpy = chai.spy.on(console, "error");
Shader.create(demoShader);
expect(errorSpy).to.have.been.called.with('Shader named "Gem" already exists.');
shaderInstance.destroy();
chai.spy.restore(console, "error");
const sameNameShader = Shader.create(demoShader);
expect(sameNameShader).instanceOf(Shader);
});