nuxt-media-library 0.0.62 → 0.0.63
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/README.md
CHANGED
|
@@ -217,7 +217,7 @@ The module auto-imports the helpers that read it (a structural type,
|
|
|
217
217
|
| `mlMediaSrcset(media)` | `srcset` from `media.sources`, or `undefined` |
|
|
218
218
|
| `mlPlayableSource(media, { preferFile? })` | `{ src, type: 'hls' \| 'progressive' }`: the HLS ladder, else the file; `null` when locked or not ready |
|
|
219
219
|
| `mlIsPlayableVideo(media)` | a video the viewer can play now |
|
|
220
|
-
| `useMLMediaRenewal(media, renew, { marginSeconds? })` | calls `renew` 5 minutes (default) before tokened links expire; browser only |
|
|
220
|
+
| `useMLMediaRenewal(media, renew, { marginSeconds?, retrySeconds? })` | calls `renew` 5 minutes (default) before tokened links expire, and again every 30 s (default) while it fails; browser only |
|
|
221
221
|
|
|
222
222
|
```vue
|
|
223
223
|
<script setup lang="ts">
|
package/dist/module.json
CHANGED
|
@@ -3,13 +3,18 @@ import type { MLEmbeddedMedia } from '../types/embeddedMedia.js';
|
|
|
3
3
|
export interface UseMLMediaRenewalOptions {
|
|
4
4
|
/** Seconds before expiry to renew. Default 300. */
|
|
5
5
|
marginSeconds?: number;
|
|
6
|
+
/**
|
|
7
|
+
* Seconds to wait before trying again when `renew` rejects (a transient
|
|
8
|
+
* network error must not let the links expire mid-playback). Default 30.
|
|
9
|
+
*/
|
|
10
|
+
retrySeconds?: number;
|
|
6
11
|
}
|
|
7
12
|
/**
|
|
8
13
|
* The renewal loop without the client guard: watches `media` and calls
|
|
9
14
|
* `renew` shortly before its links expire. Returns a stop function. Prefer
|
|
10
15
|
* `useMLMediaRenewal`, which runs this in the browser only.
|
|
11
16
|
*/
|
|
12
|
-
export declare function watchMLMediaRenewal(media: MaybeRefOrGetter<MLEmbeddedMedia | null | undefined>, renew: () => Promise<unknown>, { marginSeconds }?: UseMLMediaRenewalOptions): () => void;
|
|
17
|
+
export declare function watchMLMediaRenewal(media: MaybeRefOrGetter<MLEmbeddedMedia | null | undefined>, renew: () => Promise<unknown>, { marginSeconds, retrySeconds }?: UseMLMediaRenewalOptions): () => void;
|
|
13
18
|
/**
|
|
14
19
|
* Keeps embedded media links alive: tokened links expire (`expiresIn`), so
|
|
15
20
|
* shortly before they do this calls `renew`, which re-fetches the record and
|
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import { onScopeDispose, toValue, watch } from "vue";
|
|
2
2
|
import { refreshDelay } from "../utils/streamUrls.js";
|
|
3
3
|
const DEFAULT_MARGIN_SECONDS = 300;
|
|
4
|
-
|
|
4
|
+
const DEFAULT_RETRY_SECONDS = 30;
|
|
5
|
+
export function watchMLMediaRenewal(media, renew, { marginSeconds = DEFAULT_MARGIN_SECONDS, retrySeconds = DEFAULT_RETRY_SECONDS } = {}) {
|
|
5
6
|
let timer = null;
|
|
7
|
+
let generation = 0;
|
|
6
8
|
const clear = () => {
|
|
9
|
+
generation++;
|
|
7
10
|
if (timer) clearTimeout(timer);
|
|
8
11
|
timer = null;
|
|
9
12
|
};
|
|
13
|
+
const arm = (delay) => {
|
|
14
|
+
const current = generation;
|
|
15
|
+
timer = setTimeout(() => {
|
|
16
|
+
timer = null;
|
|
17
|
+
renew().catch((err) => {
|
|
18
|
+
console.warn("[nuxt-media-library] media renewal failed, retrying:", err);
|
|
19
|
+
if (current === generation) arm(retrySeconds * 1e3);
|
|
20
|
+
});
|
|
21
|
+
}, delay);
|
|
22
|
+
};
|
|
10
23
|
const stopWatch = watch(() => toValue(media), (current) => {
|
|
11
24
|
clear();
|
|
12
25
|
const now = Date.now();
|
|
13
26
|
const delay = refreshDelay(current?.expiresIn, now, now, marginSeconds);
|
|
14
27
|
if (delay == null) return;
|
|
15
|
-
|
|
16
|
-
renew().catch((err) => console.warn("[nuxt-media-library] media renewal failed:", err));
|
|
17
|
-
}, delay);
|
|
28
|
+
arm(delay);
|
|
18
29
|
}, { immediate: true });
|
|
19
30
|
return () => {
|
|
20
31
|
stopWatch();
|