fix(elements): avoid exception when window is undefined (#23324)

Detect server environment by checking `typeof window` and schedule render immediately instead of waiting for RAF.

This does not make Angular Elements work on platform-server. This is just the first step.

PR Close #23324
This commit is contained in:
Vikram Subramanian 2018-04-11 12:25:03 -07:00 committed by Igor Minar
parent 7e8cee6b61
commit af46d097ff
1 changed files with 6 additions and 1 deletions

View File

@ -21,7 +21,7 @@ export const scheduler = {
* Returns a function that when executed will cancel the scheduled function. * Returns a function that when executed will cancel the scheduled function.
*/ */
schedule(taskFn: () => void, delay: number): () => schedule(taskFn: () => void, delay: number): () =>
void{const id = window.setTimeout(taskFn, delay); return () => window.clearTimeout(id);}, void{const id = setTimeout(taskFn, delay); return () => clearTimeout(id);},
/** /**
* Schedule a callback to be called before the next render. * Schedule a callback to be called before the next render.
@ -32,6 +32,11 @@ export const scheduler = {
scheduleBeforeRender(taskFn: () => void): () => void{ scheduleBeforeRender(taskFn: () => void): () => void{
// TODO(gkalpak): Implement a better way of accessing `requestAnimationFrame()` // TODO(gkalpak): Implement a better way of accessing `requestAnimationFrame()`
// (e.g. accounting for vendor prefix, SSR-compatibility, etc). // (e.g. accounting for vendor prefix, SSR-compatibility, etc).
if (typeof window === 'undefined') {
// For SSR just schedule immediately.
return scheduler.schedule(taskFn, 0);
}
if (typeof window.requestAnimationFrame === 'undefined') { if (typeof window.requestAnimationFrame === 'undefined') {
const frameMs = 16; const frameMs = 16;
return scheduler.schedule(taskFn, frameMs); return scheduler.schedule(taskFn, frameMs);