@clockworkdog/cogs-client-react 3.0.0-alpha.13 → 3.0.0-alpha.15

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.
@@ -1,14 +1,20 @@
1
- import { SurfaceManager } from '@clockworkdog/cogs-client';
1
+ import { MediaPreloader, SurfaceManager } from '@clockworkdog/cogs-client';
2
2
  import React, { useEffect, useRef, useState } from 'react';
3
3
  export function MediaSurface({ cogsConnection }) {
4
4
  const surfaceManagerRef = useRef(undefined);
5
5
  const surfaceStateRef = useRef(undefined);
6
+ const volumeRef = useRef(cogsConnection.mediaConfig?.globalVolume);
7
+ const mediaPreloaderRef = useRef(null);
6
8
  const [surfaceElem, setSurfaceElem] = useState(null);
7
9
  // Keep updated list of audio outputs
8
10
  const audioOutputs = useRef({});
9
11
  useEffect(() => {
10
12
  async function updateAudioOutputs() {
11
13
  const audioOutputs = {};
14
+ if (!navigator?.mediaDevices) {
15
+ // `navigator.mediaDevices` is undefined on COGS AV <= 4.5 because of secure origin permissions
16
+ return;
17
+ }
12
18
  const devices = await navigator.mediaDevices.enumerateDevices();
13
19
  const outputs = devices.filter((device) => device.kind === 'audiooutput');
14
20
  outputs.forEach((output) => {
@@ -16,12 +22,22 @@ export function MediaSurface({ cogsConnection }) {
16
22
  });
17
23
  }
18
24
  updateAudioOutputs();
19
- navigator.mediaDevices.addEventListener('devicechange', updateAudioOutputs);
20
- return () => navigator.mediaDevices.removeEventListener('devicechange', updateAudioOutputs);
25
+ navigator?.mediaDevices?.addEventListener('devicechange', updateAudioOutputs);
26
+ return () => navigator?.mediaDevices?.removeEventListener('devicechange', updateAudioOutputs);
21
27
  }, []);
22
28
  // Create and attach new surface manager
23
29
  useEffect(() => {
24
- const sm = new SurfaceManager((url) => cogsConnection.getAssetUrl(url), (outputLabel) => audioOutputs.current[outputLabel] ?? '');
30
+ const constructURL = (url) => cogsConnection.getAssetUrl(url);
31
+ const preloader = new MediaPreloader(constructURL);
32
+ mediaPreloaderRef.current = preloader;
33
+ const files = cogsConnection.mediaConfig?.files;
34
+ if (files) {
35
+ preloader.setState(files);
36
+ }
37
+ const sm = new SurfaceManager(constructURL, (outputLabel) => audioOutputs.current[outputLabel] ?? '', {}, preloader);
38
+ if (volumeRef.current !== undefined) {
39
+ sm.volume = volumeRef.current;
40
+ }
25
41
  surfaceManagerRef.current = sm;
26
42
  surfaceElem?.replaceChildren(sm.element);
27
43
  return () => {
@@ -32,14 +48,25 @@ export function MediaSurface({ cogsConnection }) {
32
48
  }, [surfaceElem, cogsConnection.getAssetUrl]);
33
49
  // Listen to messages
34
50
  useEffect(() => {
35
- function updateSurfaceState({ message }) {
36
- if (message.type === 'media_state' && message.media_strategy === 'state' && surfaceManagerRef.current) {
51
+ function handleMessages({ message }) {
52
+ const preloader = mediaPreloaderRef.current;
53
+ const manager = surfaceManagerRef.current;
54
+ if (message.type === 'media_state' && message.media_strategy === 'state' && manager) {
37
55
  surfaceStateRef.current = message.state;
38
- surfaceManagerRef.current.setState(message.state);
56
+ manager.setState(message.state);
57
+ }
58
+ if (message.type === 'media_config_update') {
59
+ volumeRef.current = message.globalVolume;
60
+ if (preloader) {
61
+ preloader.setState(message.files);
62
+ }
63
+ if (manager) {
64
+ manager.volume = message.globalVolume;
65
+ }
39
66
  }
40
67
  }
41
- cogsConnection.addEventListener('message', updateSurfaceState);
42
- return () => cogsConnection.removeEventListener('message', updateSurfaceState);
68
+ cogsConnection.addEventListener('message', handleMessages);
69
+ return () => cogsConnection.removeEventListener('message', handleMessages);
43
70
  }, [cogsConnection]);
44
71
  return React.createElement("div", { className: "media-surface", ref: setSurfaceElem });
45
72
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "React components and hooks to connect to COGS to build a custom Media Master",
4
4
  "author": "Clockwork Dog <info@clockwork.dog>",
5
5
  "homepage": "https://github.com/clockwork-dog/cogs-sdk/tree/main/packages/react",
6
- "version": "3.0.0-alpha.13",
6
+ "version": "3.0.0-alpha.15",
7
7
  "keywords": [],
8
8
  "license": "MIT",
9
9
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "prerelease": "yarn npm publish --access public --tag=next"
29
29
  },
30
30
  "dependencies": {
31
- "@clockworkdog/cogs-client": "^3.0.0-alpha.13"
31
+ "@clockworkdog/cogs-client": "^3.0.0-alpha.15"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "^18.0.0 || ^19.0.0",