Skip to content

Commit

Permalink
fix: destroy after resize (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush authored May 31, 2024
1 parent e364e23 commit 6eda252
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Flicking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,9 @@ class Flicking extends Component<FlickingEvents> {

viewport.resize();
await renderer.forceRenderAllPanels(); // Render all panel elements, to update sizes
if (!this._initialized) {
return;
}
renderer.updatePanelSize();
camera.updateAlignPos();
camera.updateRange();
Expand All @@ -1511,6 +1514,9 @@ class Flicking extends Component<FlickingEvents> {
camera.updatePanelOrder();
camera.updateOffset();
await renderer.render();
if (!this._initialized) {
return;
}

if (control.animating) {
// TODO:
Expand Down
17 changes: 16 additions & 1 deletion test/unit/Flicking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Plugin } from "~/type/external";
import { AfterResizeEvent, BeforeResizeEvent } from "~/type/event";

import El from "./helper/El";
import { cleanup, createFlicking, range, simulate, tick, waitEvent } from "./helper/test-util";
import { cleanup, createFlicking, range, simulate, tick, waitEvent, waitTime } from "./helper/test-util";

describe("Flicking", () => {
afterEach(() => {
Expand Down Expand Up @@ -1832,6 +1832,21 @@ describe("Flicking", () => {
expect(cameraSpy.calledOnce).to.be.true;
expect(rendererSpy.calledOnce).to.be.true;
});
it("should call destroy after resize", async () => {
const flicking = await createFlicking(El.DEFAULT_HORIZONTAL);

let hasError = false;
flicking.resize().catch(() => {
// Any remaining resize operations that occur after destroy should not operate.
hasError = true;
});
flicking.destroy();

await waitTime(1000);

// Then
expect(hasError).to.be.equal(false);
});
});

describe("getElement()", () => {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/helper/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ export const waitEvent = (emitter: any, eventName: string) => {
}
};


export const waitTime = (time: number) => {
return new Promise(res => {
(window as any)._real.setTimeout(res, time);
});
}

export class NullClass {}
4 changes: 4 additions & 0 deletions test/unit/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(window as any)._real = {
setTimeout: (window as any).setTimeout.bind((window as any)),
};

(window as any).timer = sinon.useFakeTimers();
(window as any).flickings = [];

Expand Down

0 comments on commit 6eda252

Please sign in to comment.