Skip to content

Commit

Permalink
Made onStart synchronous
Browse files Browse the repository at this point in the history
Summary: This makes it easier to aggregate the spring's state with other entities.  Otherwise, the 1 frame delay can make the system report that it's at rest, even when start() was called on the same frame that another object came to rest.

Reviewers: skevy

Reviewed By: skevy

Differential Revision: http://codereview.cc/D3325
  • Loading branch information
appsforartists committed Sep 12, 2017
1 parent 9abf13f commit aaf21a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/__tests__/Springs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,26 @@ describe("Spring", () => {
expect(spring._listeners).toEqual([]);
});

it("notifies listeners that the spring has stopped when .stop() is called mid-simulation", () => {
it("synchronously notifies listeners that the spring has started when .start() is called", () => {
const onStartCallback = jest.fn();

const spring = new Spring({
toValue: 1
});

spring.onStart(onStartCallback);

spring.start();
expect(onStartCallback).toHaveBeenCalledWith(spring);
});

it("synchronously notifies listeners that the spring has stopped when .stop() is called mid-simulation", () => {
const onStopCallback = jest.fn();

const spring = new Spring({
toValue: 1
});

spring
.onStop(s => {
onStopCallback(s);
Expand All @@ -213,8 +227,8 @@ describe("Spring", () => {
jest.runTimersToTime(1000 / 60 * 10);

spring.stop();
expect(spring.isAtRest).toBeFalsy();
expect(onStopCallback).toHaveBeenCalledWith(spring);
expect(spring.isAtRest).toBeFalsy();

jest.runTimersToTime(1000 / 60 * 1);

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class Spring {
this._isAnimating = true;

if (!this._currentAnimationStep) {
this._notifyListeners("onStart");
this._currentAnimationStep = requestAnimationFrame((t: number) => {
this._notifyListeners("onStart");
this._step(t);
});
}
Expand Down

0 comments on commit aaf21a9

Please sign in to comment.