Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: model onReady method fires at wrong moment #622

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@

export const otherProps = ["onReady", "onError", "url"] as const;

let readyEventListener: ((model: CesiumModel) => void) | null = null

Check warning on line 84 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Insert `;`
bigkrp marked this conversation as resolved.
Show resolved Hide resolved

const Model = createCesiumComponent<CesiumModel, ModelProps>({
name: "Model",
async create(context, { scene, url, colorBlendMode, ...props }) {
Expand All @@ -98,15 +100,20 @@
resultURL = maybePromiseURL as typeof resultURL;
}


Check warning on line 103 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Delete `⏎`
let element;

readyEventListener = (model: CesiumModel) => {
props.onReady?.(model)

Check warning on line 107 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Insert `;`
}

Check warning on line 108 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Insert `;`
try {
element = await CesiumModel.fromGltfAsync({
...props,
url: resultURL,
colorBlendMode: colorBlendMode as ColorBlendMode,
scene: scene || context.scene,
});
props.onReady?.(element);
element.readyEvent.addEventListener(readyEventListener)

Check warning on line 116 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Insert `;`
} catch (e) {
props.onError?.(e);
return;
Expand All @@ -116,6 +123,9 @@
return element;
},
destroy(element, context) {
if (readyEventListener) {
element.readyEvent.removeEventListener(readyEventListener)

Check warning on line 127 in src/Model/Model.ts

View workflow job for this annotation

GitHub Actions / CI

Insert `;`
}
if (context.primitiveCollection && !context.primitiveCollection.isDestroyed()) {
context.primitiveCollection.remove(element);
}
Expand Down
Loading