@clockworkdog/cogs-client-react 3.0.0-alpha.1 → 3.0.0-alpha.11

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.
@@ -0,0 +1,6 @@
1
+ import { CogsConnection } from '@clockworkdog/cogs-client';
2
+ import React from 'react';
3
+ export interface MediaSurfaceProps {
4
+ cogsConnection: CogsConnection<any, any>;
5
+ }
6
+ export declare function MediaSurface({ cogsConnection }: MediaSurfaceProps): React.JSX.Element;
@@ -0,0 +1,45 @@
1
+ import { SurfaceManager } from '@clockworkdog/cogs-client';
2
+ import React, { useEffect, useRef, useState } from 'react';
3
+ export function MediaSurface({ cogsConnection }) {
4
+ const surfaceManagerRef = useRef(undefined);
5
+ const surfaceStateRef = useRef(undefined);
6
+ const [surfaceElem, setSurfaceElem] = useState(null);
7
+ // Keep updated list of audio outputs
8
+ const audioOutputs = useRef({});
9
+ useEffect(() => {
10
+ async function updateAudioOutputs() {
11
+ const audioOutputs = {};
12
+ const devices = await navigator.mediaDevices.enumerateDevices();
13
+ const outputs = devices.filter((device) => device.kind === 'audiooutput');
14
+ outputs.forEach((output) => {
15
+ audioOutputs[output.label] = output.deviceId;
16
+ });
17
+ }
18
+ updateAudioOutputs();
19
+ navigator.mediaDevices.addEventListener('devicechange', updateAudioOutputs);
20
+ return () => navigator.mediaDevices.removeEventListener('devicechange', updateAudioOutputs);
21
+ }, []);
22
+ // Create and attach new surface manager
23
+ useEffect(() => {
24
+ const sm = new SurfaceManager((url) => cogsConnection.getAssetUrl(url), (outputLabel) => audioOutputs.current[outputLabel]);
25
+ surfaceManagerRef.current = sm;
26
+ surfaceElem?.replaceChildren(sm.element);
27
+ return () => {
28
+ surfaceManagerRef.current?.setState({});
29
+ surfaceManagerRef.current = undefined;
30
+ surfaceElem?.replaceChildren( /* empty */);
31
+ };
32
+ }, [surfaceElem, cogsConnection.getAssetUrl]);
33
+ // Listen to messages
34
+ useEffect(() => {
35
+ function updateSurfaceState({ message }) {
36
+ if (message.type === 'media_state' && message.media_strategy === 'state' && surfaceManagerRef.current) {
37
+ surfaceStateRef.current = message.state;
38
+ surfaceManagerRef.current.setState(message.state);
39
+ }
40
+ }
41
+ cogsConnection.addEventListener('message', updateSurfaceState);
42
+ return () => cogsConnection.removeEventListener('message', updateSurfaceState);
43
+ }, [cogsConnection]);
44
+ return React.createElement("div", { className: "media-surface", ref: setSurfaceElem });
45
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { default as CogsConnectionProvider, useCogsConnection, useAudioPlayer, useVideoPlayer } from './providers/CogsConnectionProvider';
2
+ export { MediaSurface, MediaSurfaceProps } from './components/MediaSurface';
2
3
  export { default as useIsConnected } from './hooks/useIsConnected';
3
4
  export { default as useCogsConfig } from './hooks/useCogsConfig';
4
5
  export { default as useCogsEvent } from './hooks/useCogsEvent';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Utilities
2
2
  export { default as CogsConnectionProvider, useCogsConnection, useAudioPlayer, useVideoPlayer } from './providers/CogsConnectionProvider';
3
+ export { MediaSurface } from './components/MediaSurface';
3
4
  export { default as useIsConnected } from './hooks/useIsConnected';
4
5
  export { default as useCogsConfig } from './hooks/useCogsConfig';
5
6
  export { default as useCogsEvent } from './hooks/useCogsEvent';
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.1",
6
+ "version": "3.0.0-alpha.11",
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.1"
31
+ "@clockworkdog/cogs-client": "^3.0.0-alpha.11"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "^18.0.0 || ^19.0.0",