Skip to content

Commit

Permalink
chore: init delegates as explicitly undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jul 27, 2020
1 parent ec924d9 commit f6f6988
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/internal/scheduler/dateTimestampProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { TimestampProvider } from "../types";

interface DateTimestampProvider extends TimestampProvider {
delegate?: TimestampProvider;
delegate: TimestampProvider | undefined;
}

export const dateTimestampProvider: DateTimestampProvider = {
now() { return (this.delegate || Date).now(); }
now() {
// Use the variable rather than `this` so that the function can be called
// without being bound to the provider.
return (dateTimestampProvider.delegate || Date).now();
},
delegate: undefined
};
9 changes: 7 additions & 2 deletions src/internal/scheduler/performanceTimestampProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { TimestampProvider } from "../types";

interface PerformanceTimestampProvider extends TimestampProvider {
delegate?: TimestampProvider;
delegate: TimestampProvider | undefined;
}

export const performanceTimestampProvider: PerformanceTimestampProvider = {
now() { return (this.delegate || performance).now(); }
now() {
// Use the variable rather than `this` so that the function can be called
// without being bound to the provider.
return (performanceTimestampProvider.delegate || performance).now();
},
delegate: undefined
};
11 changes: 7 additions & 4 deletions src/internal/scheduler/requestAnimationFrameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { Subscription } from "../Subscription";

type RequestAnimationFrameProvider = {
schedule(callback: FrameRequestCallback): Subscription;
delegate?: {
delegate: {
requestAnimationFrame: typeof requestAnimationFrame;
cancelAnimationFrame: typeof cancelAnimationFrame;
};
} | undefined;
};

export const requestAnimationFrameProvider: RequestAnimationFrameProvider = {
schedule(callback) {
const { delegate } = this;
// Use the variable rather than `this` so that the function can be called
// without being bound to the provider.
const { delegate } = requestAnimationFrameProvider;
const request = delegate?.requestAnimationFrame || requestAnimationFrame;
const cancel = delegate?.cancelAnimationFrame || cancelAnimationFrame;
const handle = request(callback);
return new Subscription(() => cancel(handle));
}
},
delegate: undefined
};

0 comments on commit f6f6988

Please sign in to comment.