forked from mystiq/hydrogen-web
extract dom event => promise code
This commit is contained in:
parent
35e6dffd0b
commit
9bc1d38d73
2 changed files with 39 additions and 29 deletions
|
@ -15,6 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {BlobHandle} from "./BlobHandle.js";
|
import {BlobHandle} from "./BlobHandle.js";
|
||||||
|
import {domEventAsPromise} from "./utils.js";
|
||||||
|
|
||||||
export class ImageHandle {
|
export class ImageHandle {
|
||||||
static async fromBlob(blob) {
|
static async fromBlob(blob) {
|
||||||
|
@ -106,15 +107,7 @@ export function hasReadPixelPermission() {
|
||||||
async function loadImgFromBlob(blob) {
|
async function loadImgFromBlob(blob) {
|
||||||
const img = document.createElement("img");
|
const img = document.createElement("img");
|
||||||
let detach;
|
let detach;
|
||||||
const loadPromise = new Promise((resolve, _reject) => {
|
const loadPromise = domEventAsPromise(img, "load");
|
||||||
const reject = evt => _reject(evt.target.error);
|
|
||||||
detach = () => {
|
|
||||||
img.removeEventListener("load", resolve);
|
|
||||||
img.removeEventListener("error", reject);
|
|
||||||
};
|
|
||||||
img.addEventListener("load", resolve);
|
|
||||||
img.addEventListener("error", reject);
|
|
||||||
});
|
|
||||||
img.src = blob.url;
|
img.src = blob.url;
|
||||||
await loadPromise;
|
await loadPromise;
|
||||||
detach();
|
detach();
|
||||||
|
@ -124,32 +117,14 @@ async function loadImgFromBlob(blob) {
|
||||||
async function loadVideoFromBlob(blob) {
|
async function loadVideoFromBlob(blob) {
|
||||||
const video = document.createElement("video");
|
const video = document.createElement("video");
|
||||||
video.muted = true;
|
video.muted = true;
|
||||||
let detach;
|
const loadPromise = domEventAsPromise(video, "loadedmetadata");
|
||||||
const loadPromise = new Promise((resolve, _reject) => {
|
|
||||||
const reject = evt => _reject(evt.target.error);
|
|
||||||
detach = () => {
|
|
||||||
video.removeEventListener("loadedmetadata", resolve);
|
|
||||||
video.removeEventListener("error", reject);
|
|
||||||
};
|
|
||||||
video.addEventListener("loadedmetadata", resolve);
|
|
||||||
video.addEventListener("error", reject);
|
|
||||||
});
|
|
||||||
video.src = blob.url;
|
video.src = blob.url;
|
||||||
video.load();
|
video.load();
|
||||||
await loadPromise;
|
await loadPromise;
|
||||||
// seek to the first 1/10s to make sure that drawing the video
|
// seek to the first 1/10s to make sure that drawing the video
|
||||||
// on a canvas won't give a blank image
|
// on a canvas won't give a blank image
|
||||||
const seekPromise = new Promise((resolve, _reject) => {
|
const seekPromise = domEventAsPromise(video, "seeked");
|
||||||
const reject = evt => _reject(evt.target.error);
|
|
||||||
detach = () => {
|
|
||||||
video.removeEventListener("seeked", resolve);
|
|
||||||
video.removeEventListener("error", reject);
|
|
||||||
};
|
|
||||||
video.addEventListener("seeked", resolve);
|
|
||||||
video.addEventListener("error", reject);
|
|
||||||
});
|
|
||||||
video.currentTime = 0.1;
|
video.currentTime = 0.1;
|
||||||
await seekPromise;
|
await seekPromise;
|
||||||
detach();
|
|
||||||
return video;
|
return video;
|
||||||
}
|
}
|
||||||
|
|
35
src/platform/web/dom/utils.js
Normal file
35
src/platform/web/dom/utils.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function domEventAsPromise(element, successEvent) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let detach;
|
||||||
|
const handleError = evt => {
|
||||||
|
detach();
|
||||||
|
reject(evt.target.error);
|
||||||
|
};
|
||||||
|
const handleSuccess = () => {
|
||||||
|
detach();
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
detach = () => {
|
||||||
|
element.removeEventListener(successEvent, handleSuccess);
|
||||||
|
element.removeEventListener("error", handleError);
|
||||||
|
};
|
||||||
|
element.addEventListener(successEvent, handleSuccess);
|
||||||
|
element.addEventListener("error", handleError);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue