-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): add webcomponentsReady function to help bootstrap apps
- Loading branch information
1 parent
0396221
commit aa093a1
Showing
11 changed files
with
108 additions
and
63 deletions.
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
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
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
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,20 @@ | ||
import {} from 'jasmine'; | ||
|
||
import { webcomponentsReady, webcomponentsSupported } from './webcomponents'; | ||
|
||
describe('webcomponentsReady', () => { | ||
it('should return a Promise', () => { | ||
expect(webcomponentsReady()).toEqual(jasmine.any(Promise)); | ||
}); | ||
|
||
it('should not create multiple Promises', () => { | ||
const result = webcomponentsReady(); | ||
expect(webcomponentsReady()).toBe(result); | ||
}); | ||
}); | ||
|
||
describe('webcomponentsSupported', () => { | ||
it('should return true in test environment', () => { | ||
expect(webcomponentsSupported()).toBe(true); | ||
}); | ||
}); |
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,70 @@ | ||
declare global { | ||
interface Window { | ||
Promise: PromiseConstructor; | ||
WebComponents: { | ||
ready: boolean; | ||
}; | ||
} | ||
} | ||
|
||
export function webcomponentsSupported(): boolean { | ||
// HTML imports | ||
/* istanbul ignore if */ | ||
if (!('import' in document.createElement('link'))) { | ||
return false; | ||
} | ||
|
||
// Shadow DOM | ||
/* istanbul ignore if */ | ||
if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype)) { | ||
return false; | ||
} | ||
|
||
// Custom elements | ||
/* istanbul ignore if */ | ||
if (!window.customElements) { | ||
return false; | ||
} | ||
|
||
// Templates | ||
/* istanbul ignore if */ | ||
if (!('content' in document.createElement('template')) || | ||
// Edge has broken fragment cloning which means you cannot clone template.content | ||
!(document.createDocumentFragment().cloneNode() instanceof DocumentFragment)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// webcomponents-lite.js will override Promise. Angular provides its own Promise polyfill, so we | ||
// want to make sure we keep that. | ||
const OriginalPromise = window.Promise; // tslint:disable-line:variable-name | ||
let readyPromise: Promise<void>; | ||
|
||
export function webcomponentsReady(): Promise<void> { | ||
if (!readyPromise) { | ||
/* istanbul ignore next */ | ||
readyPromise = new Promise<void>((resolve, reject) => { | ||
if (window.WebComponents) { | ||
if (window.WebComponents.ready) { | ||
window.Promise = OriginalPromise; | ||
resolve(); | ||
} else { | ||
// tslint:disable-next-line:only-arrow-functions | ||
document.addEventListener('WebComponentsReady', function onready() { | ||
window.Promise = OriginalPromise; | ||
resolve(); | ||
document.removeEventListener('WebComponentsReady', onready); | ||
}); | ||
} | ||
} else if (webcomponentsSupported()) { | ||
resolve(); | ||
} else { | ||
reject(new Error('WebComponent support or polyfills are not present')); | ||
} | ||
}); | ||
} | ||
|
||
return readyPromise; | ||
} |