@vnejs/plugins.core.media 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -2
- package/modules/load.js +23 -31
- package/modules/media.js +1 -2
- package/modules/memory.js +2 -4
- package/package.json +1 -6
- package/modules/gif.js +0 -30
package/index.js
CHANGED
|
@@ -3,9 +3,8 @@ import { regPlugin } from "@vnejs/shared";
|
|
|
3
3
|
import * as constants from "./const/const";
|
|
4
4
|
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
5
5
|
|
|
6
|
-
import { MediaGif } from "./modules/gif";
|
|
7
6
|
import { MediaLoad } from "./modules/load";
|
|
8
7
|
import { Media } from "./modules/media";
|
|
9
8
|
import { MediaMemory } from "./modules/memory";
|
|
10
9
|
|
|
11
|
-
regPlugin("MEDIA", { constants, events: SUBSCRIBE_EVENTS }, [
|
|
10
|
+
regPlugin("MEDIA", { constants, events: SUBSCRIBE_EVENTS }, [MediaLoad, Media, MediaMemory]);
|
package/modules/load.js
CHANGED
|
@@ -18,7 +18,7 @@ export class MediaLoad extends Module {
|
|
|
18
18
|
this.shared.mediaNoTimeoutSources = platform === this.CONST.PLATFORMS.WEB ? ["web"] : [];
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
onMediaLoad = async ({ type, name,
|
|
21
|
+
onMediaLoad = async ({ type, name, isTmp = false, ...otherArgs } = {}) => {
|
|
22
22
|
if (!this.isReady) await this.waitIsReady();
|
|
23
23
|
|
|
24
24
|
const { mediaType, postfix, quality = "original", priority = this.CONST.MEDIA.PRIORITIES.BACKGROUND } = otherArgs;
|
|
@@ -35,12 +35,23 @@ export class MediaLoad extends Module {
|
|
|
35
35
|
const oldMedia = await this.emitOne(this.EVENTS.MEDIA.LOAD_CHECK, { mediaId });
|
|
36
36
|
if (oldMedia) return oldMedia;
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
const promiseFunc = async () => {
|
|
39
|
+
const oldMedia = await this.emitOne(this.EVENTS.MEDIA.LOAD_CHECK, { mediaId });
|
|
40
|
+
if (oldMedia) return oldMedia;
|
|
41
|
+
|
|
42
|
+
this.inProcess[mediaId] = this.loadMedia(mediaId, type, src, priority, isTmp);
|
|
43
|
+
const media = await this.inProcess[mediaId];
|
|
44
|
+
delete this.inProcess[mediaId];
|
|
45
|
+
|
|
46
|
+
return media;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
await this.semaphore.push(mediaId, priority, promiseFunc);
|
|
39
50
|
|
|
40
51
|
return this.emitOne(this.EVENTS.MEDIA.MEMORY_GET, { mediaId });
|
|
41
52
|
};
|
|
42
53
|
|
|
43
|
-
onMediaLoadCheck = async (mediaId) => {
|
|
54
|
+
onMediaLoadCheck = async ({ mediaId } = {}) => {
|
|
44
55
|
if (this.inProcess[mediaId]) return this.inProcess[mediaId];
|
|
45
56
|
|
|
46
57
|
const oldMedia = await this.emitOne(this.EVENTS.MEDIA.MEMORY_GET, { mediaId });
|
|
@@ -49,43 +60,24 @@ export class MediaLoad extends Module {
|
|
|
49
60
|
if (this.inProcess[mediaId]) return this.inProcess[mediaId];
|
|
50
61
|
};
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
(
|
|
54
|
-
async () => {
|
|
55
|
-
const oldMedia = await this.emitOne(this.EVENTS.MEDIA.LOAD_CHECK, { mediaId });
|
|
56
|
-
if (oldMedia) return oldMedia;
|
|
57
|
-
|
|
58
|
-
this.inProcess[mediaId] = this.loadMedia(mediaId, type, src, priority, isConstant, isTmp);
|
|
59
|
-
const media = await this.inProcess[mediaId];
|
|
60
|
-
delete this.inProcess[mediaId];
|
|
61
|
-
|
|
62
|
-
return media;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
loadMedia = async (mediaId, type, src, priority, isConstant = false, isTmp = false) => {
|
|
66
|
-
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "load start", mediaId, type, priority, isConstant, isTmp } });
|
|
63
|
+
loadMedia = async (mediaId, type, src, priority, isTmp = false) => {
|
|
64
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "load start", mediaId, type, priority, isTmp } });
|
|
67
65
|
|
|
68
66
|
const ts = Math.round(new Date().getTime() / 1000 / 60 / 60);
|
|
69
|
-
const
|
|
70
|
-
const splitSrc = src.split(".");
|
|
71
|
-
const ext = splitSrc[splitSrc.length - 1];
|
|
72
|
-
const mediaRequest = await fetch(mediaSrc);
|
|
67
|
+
const mediaRequest = await fetch(`${src}?ts=${ts}&version=${this.options?.global?.version || 0}`);
|
|
73
68
|
const media = type === this.CONST.MEDIA.TYPES.VIDEO ? document.createElement("video") : new this.CONST.MEDIA.CONSTRUCTORS[type]();
|
|
74
|
-
|
|
69
|
+
|
|
75
70
|
media.setAttribute("type", type);
|
|
76
|
-
// IOS - mediaSrc
|
|
77
|
-
media.src = URL.createObjectURL(await mediaRequest.clone().blob());
|
|
78
71
|
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
72
|
+
if (type === this.CONST.MEDIA.TYPES.VIDEO) media.crossOrigin = "anonymous";
|
|
73
|
+
|
|
74
|
+
media.src = URL.createObjectURL(await mediaRequest.clone().blob());
|
|
83
75
|
|
|
84
|
-
await this.emit(this.EVENTS.MEDIA.SET, { media, mediaId,
|
|
76
|
+
await this.emit(this.EVENTS.MEDIA.SET, { media, mediaId, isTmp });
|
|
85
77
|
|
|
86
78
|
if (priority && !this.shared.mediaNoTimeoutSources.length) await this.waitTimeout(Math.round(25 * priority * (Math.random() + 0.618)));
|
|
87
79
|
|
|
88
|
-
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "load end", mediaId, type, priority,
|
|
80
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "load end", mediaId, type, priority, isTmp } });
|
|
89
81
|
|
|
90
82
|
return media;
|
|
91
83
|
};
|
package/modules/media.js
CHANGED
|
@@ -23,8 +23,7 @@ export class Media extends Module {
|
|
|
23
23
|
|
|
24
24
|
return this.prepareMedia(media);
|
|
25
25
|
};
|
|
26
|
-
|
|
27
|
-
onMediaSet = async ({ mediaId, media, isConstant, isTmp } = {}) => this.emit(this.EVENTS.MEDIA.MEMORY_SET, { mediaId, media, isConstant, isTmp });
|
|
26
|
+
onMediaSet = async ({ mediaId, media, isTmp } = {}) => this.emit(this.EVENTS.MEDIA.MEMORY_SET, { mediaId, media, isTmp });
|
|
28
27
|
|
|
29
28
|
loadMedia = (type, name, mediaType, quality, postfix) => {
|
|
30
29
|
const priority = this.CONST.MEDIA.PRIORITIES.HIGH;
|
package/modules/memory.js
CHANGED
|
@@ -37,10 +37,8 @@ export class MediaMemory extends Module {
|
|
|
37
37
|
};
|
|
38
38
|
onRestore = (key, memory, func) => Promise.all(this.getPromises(key, memory, func));
|
|
39
39
|
|
|
40
|
-
onMemorySet = async ({ media, mediaId,
|
|
41
|
-
this.emit(this.EVENTS.MEMORY.SET, { module: this.name, key: mediaId, value: media, isConstant, isTmp });
|
|
40
|
+
onMemorySet = async ({ media, mediaId, isTmp } = {}) => this.emit(this.EVENTS.MEMORY.SET, { module: this.name, key: mediaId, value: media, isTmp });
|
|
42
41
|
onMemoryGet = ({ mediaId }) => this.emitOne(this.EVENTS.MEMORY.GET, { module: this.name, key: mediaId });
|
|
43
42
|
|
|
44
|
-
getPromises = (key, memory, func) =>
|
|
45
|
-
[...document.querySelectorAll(`[parentId="${key}"]`)].map((children) => func(memory, children.id));
|
|
43
|
+
getPromises = (key, memory, func) => [...document.querySelectorAll(`[parentId="${key}"]`)].map((children) => func(memory, children.id));
|
|
46
44
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.core.media",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -16,10 +16,5 @@
|
|
|
16
16
|
"description": "",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"decode-gif": "1.0.1"
|
|
19
|
-
},
|
|
20
|
-
"peerDependencies": {
|
|
21
|
-
"@vnejs/shared": "*",
|
|
22
|
-
"@vnejs/module": "*",
|
|
23
|
-
"@vnejs/semaphore": "*"
|
|
24
19
|
}
|
|
25
20
|
}
|
package/modules/gif.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import decodeGif from "decode-gif";
|
|
2
|
-
|
|
3
|
-
import { Module } from "@vnejs/module";
|
|
4
|
-
|
|
5
|
-
export class MediaGif extends Module {
|
|
6
|
-
name = "media.gif";
|
|
7
|
-
|
|
8
|
-
subscribe = () => {
|
|
9
|
-
this.on(this.EVENTS.MEDIA.GIF, this.onMediaGif);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
onMediaGif = ({ media, mediaId, ab, isConstant, isTmp } = {}) => {
|
|
13
|
-
const { width, height, frames } = decodeGif(new Uint8Array(ab));
|
|
14
|
-
|
|
15
|
-
media.setAttribute("frames", frames.length);
|
|
16
|
-
media.setAttribute("delay", frames[1].timeCode);
|
|
17
|
-
|
|
18
|
-
return Promise.all(this.createFrameFunc(mediaId, width, height, isConstant, isTmp));
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
createFrameFunc = (mediaId, width, height, isConstant, isTmp) => (frame, i) => {
|
|
22
|
-
const media = document.createElement("canvas");
|
|
23
|
-
media.width = width;
|
|
24
|
-
media.height = height;
|
|
25
|
-
media.getContext("2d").putImageData(new ImageData(frame.data, width, height), 0, 0);
|
|
26
|
-
media.setAttribute("parentId", mediaId);
|
|
27
|
-
|
|
28
|
-
return this.emit(this.EVENTS.MEDIA.SET, { media, mediaId: `${mediaId}.frame.${i}`, isConstant, isTmp });
|
|
29
|
-
};
|
|
30
|
-
}
|