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

[ReactiveElement] Adds scheduleUpdate() #2097

Merged
merged 4 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-dancers-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/reactive-element': patch
---

Adds `scheduleUpdate()` to control update timing. This should be implemented instead of `performUpdate()`; however, existing overrides of `performUpdate()` will continue to work.
36 changes: 27 additions & 9 deletions packages/reactive-element/src/reactive-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ export abstract class ReactiveElement
// `window.onunhandledrejection`.
Promise.reject(e);
}
const result = this.performUpdate();
// If `performUpdate` returns a Promise, we await it. This is done to
const result = this.scheduleUpdate();
// If `scheduleUpdate` returns a Promise, we await it. This is done to
// enable coordinating updates with a scheduler. Note, the result is
// checked to avoid delaying an additional microtask unless we need to.
if (result != null) {
Expand All @@ -1017,22 +1017,40 @@ export abstract class ReactiveElement
}

/**
* Performs an element update. Note, if an exception is thrown during the
* update, `firstUpdated` and `updated` will not be called.
*
* You can override this method to change the timing of updates. If this
* method is overridden, `super.performUpdate()` must be called.
* Schedules an element update. You can override this method to change the
* timing of updates. If this method is overridden, `super.scheduleUpdate()`
* must be called.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add that it should return a promise when overridden.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*
* For instance, to schedule updates to occur just before the next frame:
*
* ```ts
* override protected async performUpdate(): Promise<unknown> {
* override protected async scheduleUpdate(): Promise<unknown> {
* await new Promise((resolve) => requestAnimationFrame(() => resolve()));
* super.performUpdate();
* super.scheduleUpdate();
* }
* ```
* @category updates
*/
protected scheduleUpdate(): void | Promise<unknown> {
return this.performUpdate();
}

/**
* Performs an element update. Note, if an exception is thrown during the
* update, `firstUpdated` and `updated` will not be called.
*
* Call performUpdate() to immediately process a pending update. This should
* generally not be needed, but it can be done in rare cases when you need to
* update synchronously.
*
* Note, to ensure `performUpdate()` synchronously completes a pending update,
* it should not be overridden. Previously `performUpdate()` was also used
* for customizing update timing. Instead, use `scheduleUpdate()`. For
* backwards compatibility, scheduling updates via `performUpdate()`
* continues to work.
sorvell marked this conversation as resolved.
Show resolved Hide resolved
*
* @category updates
*/
protected performUpdate(): void | Promise<unknown> {
// Abort any update if one is not pending when this is called.
// This can happen if `performUpdate` is called early to "flush"
Expand Down
26 changes: 13 additions & 13 deletions packages/reactive-element/src/test/reactive-element_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2331,17 +2331,17 @@ suite('ReactiveElement', () => {
assert.equal((el as any).zug, objectValue);
});

test('can override performUpdate()', async () => {
test('can override scheduleUpdate()', async () => {
let resolve: ((value?: unknown) => void) | undefined;

class A extends ReactiveElement {
performUpdateCalled = false;
scheduleUpdateCalled = false;
updateCalled = false;

override async performUpdate() {
this.performUpdateCalled = true;
override async scheduleUpdate() {
this.scheduleUpdateCalled = true;
await new Promise((r) => (resolve = r));
await super.performUpdate();
await super.scheduleUpdate();
}

override update(changedProperties: Map<PropertyKey, unknown>) {
Expand All @@ -2365,21 +2365,21 @@ suite('ReactiveElement', () => {
await new Promise((r) => setTimeout(r, 10));
assert.isFalse(a.updateCalled);

// update is called after performUpdate allowed to complete
// update is called after scheduleUpdate allowed to complete
resolve!();
await a.updateComplete;
assert.isTrue(a.updateCalled);
});

test('overriding performUpdate() allows nested invalidations', async () => {
test('overriding scheduleUpdate() allows nested invalidations', async () => {
class A extends ReactiveElement {
performUpdateCalledCount = 0;
scheduleUpdateCalledCount = 0;
updatedCalledCount = 0;

override async performUpdate() {
this.performUpdateCalledCount++;
override async scheduleUpdate() {
this.scheduleUpdateCalledCount++;
await new Promise((r) => setTimeout(r));
super.performUpdate();
super.scheduleUpdate();
}

override updated(_changedProperties: Map<PropertyKey, unknown>) {
Expand All @@ -2399,14 +2399,14 @@ suite('ReactiveElement', () => {
const updateComplete1 = a.updateComplete;
await updateComplete1;
assert.equal(a.updatedCalledCount, 1);
assert.equal(a.performUpdateCalledCount, 1);
assert.equal(a.scheduleUpdateCalledCount, 1);

const updateComplete2 = a.updateComplete;
assert.notStrictEqual(updateComplete1, updateComplete2);

await updateComplete2;
assert.equal(a.updatedCalledCount, 2);
assert.equal(a.performUpdateCalledCount, 2);
assert.equal(a.scheduleUpdateCalledCount, 2);
});

test('update does not occur before element is connected', async () => {
Expand Down