main
js 25 lines 1022 Bytes
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 export function setSrcObject(domElement: Element, tag: string, value: any) {
11 // We optimistically create the URL regardless of object type. This lets us
12 // support cross-realms and any type that the browser supports like new types.
13 const url = URL.createObjectURL(value as any);
14 const loadEvent = tag === 'img' ? 'load' : 'loadstart';
15 const cleanUp = () => {
16 // Once the object has started loading, then it's already collected by the
17 // browser and it won't refer to it by the URL anymore so we can now revoke it.
18 URL.revokeObjectURL(url);
19 domElement.removeEventListener(loadEvent, cleanUp);
20 domElement.removeEventListener('error', cleanUp);
21 };
22 domElement.addEventListener(loadEvent, cleanUp);
23 domElement.addEventListener('error', cleanUp);
24 domElement.setAttribute('src', url);
25 }