( playerId: string, )
| 143 | }; |
| 144 | |
| 145 | export const loadExcoPlayer = ( |
| 146 | playerId: string, |
| 147 | ): Promise<ExcoPlayerRegistry> => { |
| 148 | const cached = registries.get(playerId); |
| 149 | |
| 150 | if (cached) { |
| 151 | return cached; |
| 152 | } |
| 153 | |
| 154 | const registry = new Promise<void>((resolve, reject) => { |
| 155 | const script = document.createElement('script'); |
| 156 | script.className = 'exco-player'; |
| 157 | script.src = getExcoPlayerScriptSrc(playerId); |
| 158 | script.async = true; |
| 159 | script.setAttribute('programmatic', 'true'); |
| 160 | script.setAttribute('multiple', 'true'); |
| 161 | script.addEventListener('load', () => resolve()); |
| 162 | script.addEventListener('error', () => |
| 163 | reject(new Error('Failed to load the EX.CO player')), |
| 164 | ); |
| 165 | document.head.appendChild(script); |
| 166 | }) |
| 167 | .then(() => { |
| 168 | if (!window.ExCoPlayer) { |
| 169 | throw new Error('The EX.CO player script defined no API'); |
| 170 | } |
| 171 | |
| 172 | return window.ExCoPlayer.connect(playerId); |
| 173 | }) |
| 174 | .catch((error) => { |
| 175 | setExcoPlayerBlocked(playerId); |
| 176 | throw error; |
| 177 | }); |
| 178 | |
| 179 | registries.set(playerId, registry); |
| 180 | |
| 181 | return registry; |
| 182 | }; |
no test coverage detected