-
Notifications
You must be signed in to change notification settings - Fork 273
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
refactor(framework): Deprecate RenderScheduler in favor of Render.js #2728
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b95453e
refactor(framework): Deprecate RenderScheduler in favor of Render.js
vladitasev f273ccf
remove register,deregister
vladitasev ded8226
fix tests
vladitasev ef3eaa5
upgrade chromedriver
vladitasev 110118b
retry build
vladitasev ae398e2
fix early assignment
vladitasev 97f2698
Merge remote-tracking branch 'origin/master' into deprecate-render-sc…
vladitasev 45257bd
rename whenFinished to renderFinished
vladitasev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import EventProvider from "./EventProvider.js"; | ||
import RenderQueue from "./RenderQueue.js"; | ||
import { getAllRegisteredTags } from "./CustomElementsRegistry.js"; | ||
import { isRtlAware } from "./locale/RTLAwareRegistry.js"; | ||
|
||
const registeredElements = new Set(); | ||
const eventProvider = new EventProvider(); | ||
|
||
const invalidatedWebComponents = new RenderQueue(); // Queue for invalidated web components | ||
|
||
let renderTaskPromise, | ||
renderTaskPromiseResolve; | ||
|
||
let mutationObserverTimer; | ||
|
||
let queuePromise; | ||
|
||
|
||
/** | ||
* Schedules a render task (if not already scheduled) to render the component | ||
* | ||
* @param webComponent | ||
* @returns {Promise} | ||
*/ | ||
const renderDeferred = async webComponent => { | ||
// Enqueue the web component | ||
invalidatedWebComponents.add(webComponent); | ||
|
||
// Schedule a rendering task | ||
await scheduleRenderTask(); | ||
}; | ||
|
||
/** | ||
* Renders a component synchronously and adds it to the registry of rendered components | ||
* | ||
* @param webComponent | ||
*/ | ||
const renderImmediately = webComponent => { | ||
eventProvider.fireEvent("beforeComponentRender", webComponent); | ||
registeredElements.add(webComponent); | ||
webComponent._render(); | ||
}; | ||
|
||
/** | ||
* Cancels the rendering of a component, if awaiting to be rendered, and removes it from the registry of rendered components | ||
* | ||
* @param webComponent | ||
*/ | ||
const cancelRender = webComponent => { | ||
invalidatedWebComponents.remove(webComponent); | ||
registeredElements.delete(webComponent); | ||
}; | ||
|
||
/** | ||
* Schedules a rendering task, if not scheduled already | ||
*/ | ||
const scheduleRenderTask = async () => { | ||
if (!queuePromise) { | ||
queuePromise = new Promise(resolve => { | ||
window.requestAnimationFrame(() => { | ||
// Render all components in the queue | ||
|
||
// console.log(`--------------------RENDER TASK START------------------------------`); // eslint-disable-line | ||
invalidatedWebComponents.process(renderImmediately); | ||
// console.log(`--------------------RENDER TASK END------------------------------`); // eslint-disable-line | ||
|
||
// Resolve the promise so that callers of renderDeferred can continue | ||
queuePromise = null; | ||
resolve(); | ||
|
||
// Wait for Mutation observer before the render task is considered finished | ||
if (!mutationObserverTimer) { | ||
mutationObserverTimer = setTimeout(() => { | ||
mutationObserverTimer = undefined; | ||
if (invalidatedWebComponents.isEmpty()) { | ||
_resolveTaskPromise(); | ||
} | ||
}, 200); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
await queuePromise; | ||
}; | ||
|
||
/** | ||
* return a promise that will be resolved once all invalidated web components are rendered | ||
*/ | ||
const whenDOMUpdated = () => { | ||
if (renderTaskPromise) { | ||
return renderTaskPromise; | ||
} | ||
|
||
renderTaskPromise = new Promise(resolve => { | ||
renderTaskPromiseResolve = resolve; | ||
window.requestAnimationFrame(() => { | ||
if (invalidatedWebComponents.isEmpty()) { | ||
renderTaskPromise = undefined; | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
return renderTaskPromise; | ||
}; | ||
|
||
const whenAllCustomElementsAreDefined = () => { | ||
const definedPromises = getAllRegisteredTags().map(tag => customElements.whenDefined(tag)); | ||
return Promise.all(definedPromises); | ||
}; | ||
|
||
const whenFinished = async () => { | ||
await whenAllCustomElementsAreDefined(); | ||
await whenDOMUpdated(); | ||
}; | ||
|
||
const _resolveTaskPromise = () => { | ||
if (!invalidatedWebComponents.isEmpty()) { | ||
// More updates are pending. Resolve will be called again | ||
return; | ||
} | ||
|
||
if (renderTaskPromiseResolve) { | ||
renderTaskPromiseResolve(); | ||
renderTaskPromiseResolve = undefined; | ||
renderTaskPromise = undefined; | ||
} | ||
}; | ||
|
||
/** | ||
* Re-renders all UI5 Elements on the page, with the option to specify filters to rerender only some components. | ||
* | ||
* Usage: | ||
* reRenderAllUI5Elements() -> re-renders all components | ||
* reRenderAllUI5Elements({tag: "ui5-button"}) -> re-renders only instances of ui5-button | ||
* reRenderAllUI5Elements({rtlAware: true}) -> re-renders only rtlAware components | ||
* reRenderAllUI5Elements({languageAware: true}) -> re-renders only languageAware components | ||
* reRenderAllUI5Elements({rtlAware: true, languageAware: true}) -> re-renders components that are rtlAware or languageAware | ||
* etc... | ||
* | ||
* @public | ||
* @param {Object|undefined} filters - Object with keys that can be "rtlAware" or "languageAware" | ||
* @returns {Promise<void>} | ||
*/ | ||
const reRenderAllUI5Elements = async filters => { | ||
registeredElements.forEach(element => { | ||
const tag = element.constructor.getMetadata().getTag(); | ||
const rtlAware = isRtlAware(element.constructor); | ||
const languageAware = element.constructor.getMetadata().isLanguageAware(); | ||
if (!filters || (filters.tag === tag) || (filters.rtlAware && rtlAware) || (filters.languageAware && languageAware)) { | ||
renderDeferred(element); | ||
} | ||
}); | ||
await whenFinished(); | ||
}; | ||
|
||
const attachBeforeComponentRender = listener => { | ||
eventProvider.attachEvent("beforeComponentRender", listener); | ||
}; | ||
|
||
const detachBeforeComponentRender = listener => { | ||
eventProvider.detachEvent("beforeComponentRender", listener); | ||
}; | ||
|
||
export { | ||
renderDeferred, | ||
renderImmediately, | ||
cancelRender, | ||
whenFinished, | ||
reRenderAllUI5Elements, | ||
attachBeforeComponentRender, | ||
detachBeforeComponentRender, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,179 +1,40 @@ | ||
import EventProvider from "./EventProvider.js"; | ||
import RenderQueue from "./RenderQueue.js"; | ||
import { getAllRegisteredTags } from "./CustomElementsRegistry.js"; | ||
import { isRtlAware } from "./locale/RTLAwareRegistry.js"; | ||
|
||
const registeredElements = new Set(); | ||
const eventProvider = new EventProvider(); | ||
|
||
// Queue for invalidated web components | ||
const invalidatedWebComponents = new RenderQueue(); | ||
|
||
let renderTaskPromise, | ||
renderTaskPromiseResolve; | ||
|
||
let mutationObserverTimer; | ||
|
||
let queuePromise; | ||
import { | ||
renderDeferred, | ||
renderImmediately, | ||
whenFinished, | ||
} from "./Render.js"; | ||
|
||
/** | ||
* Class that manages the rendering/re-rendering of web components | ||
* This is always asynchronous | ||
* @deprecated Use the Render.js module instead | ||
*/ | ||
class RenderScheduler { | ||
constructor() { | ||
throw new Error("Static class"); | ||
} | ||
|
||
/** | ||
* Schedules a render task (if not already scheduled) to render the component | ||
* | ||
* @param webComponent | ||
* @returns {Promise} | ||
* @deprecated Use renderDeferred from the Render.js module instead | ||
*/ | ||
static async renderDeferred(webComponent) { | ||
// Enqueue the web component | ||
invalidatedWebComponents.add(webComponent); | ||
|
||
// Schedule a rendering task | ||
await RenderScheduler.scheduleRenderTask(); | ||
await renderDeferred(webComponent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a warning in the console, otherwise it is too easy to miss. |
||
} | ||
|
||
/** | ||
* Renders a component synchronously | ||
* | ||
* @param webComponent | ||
* @deprecated Use renderImmediately from the Render.js module instead | ||
*/ | ||
static renderImmediately(webComponent) { | ||
eventProvider.fireEvent("beforeComponentRender", webComponent); | ||
webComponent._render(); | ||
return renderImmediately(webComponent); | ||
} | ||
|
||
/** | ||
* Cancels the rendering of a component, added to the queue with renderDeferred | ||
* | ||
* @param webComponent | ||
* @deprecated Use whenFinished from the Render.js module instead | ||
* @returns {Promise<void>} | ||
*/ | ||
static cancelRender(webComponent) { | ||
invalidatedWebComponents.remove(webComponent); | ||
} | ||
|
||
/** | ||
* Schedules a rendering task, if not scheduled already | ||
*/ | ||
static async scheduleRenderTask() { | ||
if (!queuePromise) { | ||
queuePromise = new Promise(resolve => { | ||
window.requestAnimationFrame(() => { | ||
// Render all components in the queue | ||
|
||
// console.log(`--------------------RENDER TASK START------------------------------`); // eslint-disable-line | ||
invalidatedWebComponents.process(RenderScheduler.renderImmediately); | ||
// console.log(`--------------------RENDER TASK END------------------------------`); // eslint-disable-line | ||
|
||
// Resolve the promise so that callers of renderDeferred can continue | ||
queuePromise = null; | ||
resolve(); | ||
|
||
// Wait for Mutation observer before the render task is considered finished | ||
if (!mutationObserverTimer) { | ||
mutationObserverTimer = setTimeout(() => { | ||
mutationObserverTimer = undefined; | ||
if (invalidatedWebComponents.isEmpty()) { | ||
RenderScheduler._resolveTaskPromise(); | ||
} | ||
}, 200); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
await queuePromise; | ||
} | ||
|
||
/** | ||
* return a promise that will be resolved once all invalidated web components are rendered | ||
*/ | ||
static whenDOMUpdated() { | ||
if (renderTaskPromise) { | ||
return renderTaskPromise; | ||
} | ||
|
||
renderTaskPromise = new Promise(resolve => { | ||
renderTaskPromiseResolve = resolve; | ||
window.requestAnimationFrame(() => { | ||
if (invalidatedWebComponents.isEmpty()) { | ||
renderTaskPromise = undefined; | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
return renderTaskPromise; | ||
} | ||
|
||
static whenAllCustomElementsAreDefined() { | ||
const definedPromises = getAllRegisteredTags().map(tag => customElements.whenDefined(tag)); | ||
return Promise.all(definedPromises); | ||
} | ||
|
||
static async whenFinished() { | ||
await RenderScheduler.whenAllCustomElementsAreDefined(); | ||
await RenderScheduler.whenDOMUpdated(); | ||
} | ||
|
||
static _resolveTaskPromise() { | ||
if (!invalidatedWebComponents.isEmpty()) { | ||
// More updates are pending. Resolve will be called again | ||
return; | ||
} | ||
|
||
if (renderTaskPromiseResolve) { | ||
renderTaskPromiseResolve.call(this); | ||
renderTaskPromiseResolve = undefined; | ||
renderTaskPromise = undefined; | ||
} | ||
} | ||
|
||
static register(element) { | ||
registeredElements.add(element); | ||
} | ||
|
||
static deregister(element) { | ||
registeredElements.delete(element); | ||
} | ||
|
||
/** | ||
* Re-renders all UI5 Elements on the page, with the option to specify filters to rerender only some components. | ||
* | ||
* Usage: | ||
* reRenderAllUI5Elements() -> rerenders all components | ||
* reRenderAllUI5Elements({tag: "ui5-button"}) -> re-renders only instances of ui5-button | ||
* reRenderAllUI5Elements({rtlAware: true}) -> re-renders only rtlAware components | ||
* reRenderAllUI5Elements({languageAware: true}) -> re-renders only languageAware components | ||
* reRenderAllUI5Elements({rtlAware: true, languageAware: true}) -> re-renders components that are rtlAware or languageAware | ||
* etc... | ||
* | ||
* @public | ||
* @param {Object|undefined} filters - Object with keys that can be "rtlAware" or "languageAware" | ||
*/ | ||
static reRenderAllUI5Elements(filters) { | ||
registeredElements.forEach(element => { | ||
const tag = element.constructor.getMetadata().getTag(); | ||
const rtlAware = isRtlAware(element.constructor); | ||
const languageAware = element.constructor.getMetadata().isLanguageAware(); | ||
if (!filters || (filters.tag === tag) || (filters.rtlAware && rtlAware) || (filters.languageAware && languageAware)) { | ||
RenderScheduler.renderDeferred(element); | ||
} | ||
}); | ||
} | ||
|
||
static attachBeforeComponentRender(listener) { | ||
eventProvider.attachEvent("beforeComponentRender", listener); | ||
} | ||
|
||
static detachBeforeComponentRender(listener) { | ||
eventProvider.detachEvent("beforeComponentRender", listener); | ||
await whenFinished(); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related to this change, but I think we should improve this with a loop, as the DOM Update promise might result in more undefined custom elements.