@streamplace/components 0.6.37 → 1.0.0

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.
Files changed (55) hide show
  1. package/package.json +8 -34
  2. package/LICENSE +0 -18
  3. package/README.md +0 -35
  4. package/dist/index.js +0 -6
  5. package/dist/livestream-provider/index.js +0 -20
  6. package/dist/livestream-provider/websocket.js +0 -41
  7. package/dist/livestream-store/chat.js +0 -162
  8. package/dist/livestream-store/context.js +0 -2
  9. package/dist/livestream-store/index.js +0 -3
  10. package/dist/livestream-store/livestream-state.js +0 -1
  11. package/dist/livestream-store/livestream-store.js +0 -39
  12. package/dist/livestream-store/websocket-consumer.js +0 -55
  13. package/dist/player-store/context.js +0 -2
  14. package/dist/player-store/index.js +0 -6
  15. package/dist/player-store/player-provider.js +0 -53
  16. package/dist/player-store/player-state.js +0 -22
  17. package/dist/player-store/player-store.js +0 -146
  18. package/dist/player-store/single-player-provider.js +0 -109
  19. package/dist/streamplace-provider/context.js +0 -2
  20. package/dist/streamplace-provider/index.js +0 -16
  21. package/dist/streamplace-provider/poller.js +0 -46
  22. package/dist/streamplace-provider/xrpc.js +0 -0
  23. package/dist/streamplace-store/index.js +0 -2
  24. package/dist/streamplace-store/streamplace-store.js +0 -37
  25. package/dist/streamplace-store/user.js +0 -47
  26. package/dist/streamplace-store/xrpc.js +0 -12
  27. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/37be0eec +0 -0
  28. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/56540125 +0 -0
  29. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/67b1eb60 +0 -0
  30. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/7c275f90 +0 -0
  31. package/src/index.tsx +0 -6
  32. package/src/livestream-provider/index.tsx +0 -37
  33. package/src/livestream-provider/websocket.tsx +0 -47
  34. package/src/livestream-store/chat.tsx +0 -224
  35. package/src/livestream-store/context.tsx +0 -10
  36. package/src/livestream-store/index.tsx +0 -3
  37. package/src/livestream-store/livestream-state.tsx +0 -18
  38. package/src/livestream-store/livestream-store.tsx +0 -56
  39. package/src/livestream-store/websocket-consumer.tsx +0 -62
  40. package/src/player-store/context.tsx +0 -11
  41. package/src/player-store/index.tsx +0 -6
  42. package/src/player-store/player-provider.tsx +0 -90
  43. package/src/player-store/player-state.tsx +0 -159
  44. package/src/player-store/player-store.tsx +0 -217
  45. package/src/player-store/single-player-provider.tsx +0 -181
  46. package/src/streamplace-provider/context.tsx +0 -10
  47. package/src/streamplace-provider/index.tsx +0 -32
  48. package/src/streamplace-provider/poller.tsx +0 -55
  49. package/src/streamplace-provider/xrpc.tsx +0 -0
  50. package/src/streamplace-store/index.tsx +0 -2
  51. package/src/streamplace-store/streamplace-store.tsx +0 -89
  52. package/src/streamplace-store/user.tsx +0 -57
  53. package/src/streamplace-store/xrpc.tsx +0 -15
  54. package/tsconfig.json +0 -9
  55. package/tsconfig.tsbuildinfo +0 -1
@@ -1,146 +0,0 @@
1
- import { useContext } from "react";
2
- import { createStore, useStore } from "zustand";
3
- import { PlayerContext } from "./context";
4
- import { PlayerProtocol, PlayerStatus, } from "./player-state";
5
- export const makePlayerStore = (id) => {
6
- return createStore()((set) => ({
7
- id: id || Math.random().toString(36).slice(8),
8
- selectedRendition: "source",
9
- setSelectedRendition: (rendition) => set((state) => ({ ...state, selectedRendition: rendition })),
10
- protocol: PlayerProtocol.WEBRTC,
11
- setProtocol: (protocol) => set((state) => ({ ...state, protocol: protocol })),
12
- src: "",
13
- setSrc: (src) => set(() => ({ src })),
14
- ingestStarting: false,
15
- setIngestStarting: (ingestStarting) => set(() => ({ ingestStarting })),
16
- ingestMediaSource: undefined,
17
- setIngestMediaSource: (ingestMediaSource) => set(() => ({ ingestMediaSource })),
18
- ingestConnectionState: null,
19
- setIngestConnectionState: (ingestConnectionState) => set(() => ({ ingestConnectionState })),
20
- ingestAutoStart: false,
21
- setIngestAutoStart: (ingestAutoStart) => set(() => ({ ingestAutoStart })),
22
- ingestStarted: null,
23
- setIngestStarted: (timestamp) => set(() => ({ ingestStarted: timestamp })),
24
- muted: false,
25
- setMuted: (isMuted) => set(() => ({ muted: isMuted, muteWasForced: false })),
26
- volume: 1.0,
27
- setVolume: (volume) => set(() => ({ volume, muteWasForced: false })),
28
- fullscreen: false,
29
- setFullscreen: (isFullscreen) => set(() => ({ fullscreen: isFullscreen })),
30
- status: PlayerStatus.START,
31
- setStatus: (status) => set(() => ({ status })),
32
- playTime: 0,
33
- setPlayTime: (playTime) => set(() => ({ playTime })),
34
- offline: false,
35
- setOffline: (offline) => set(() => ({ offline })),
36
- videoRef: undefined,
37
- setVideoRef: (videoRef) => set(() => ({ videoRef })),
38
- pipMode: false,
39
- setPipMode: (pipMode) => set(() => ({ pipMode })),
40
- // * Whether mute was forced by the browser or not for autoplay
41
- // * Will get set to 'false' if the user has interacted with the volume
42
- muteWasForced: false,
43
- setMuteWasForced: (muteWasForced) => set(() => ({ muteWasForced })),
44
- embedded: false,
45
- setEmbedded: (embedded) => set(() => ({ embedded })),
46
- showControls: true,
47
- controlsTimeout: undefined,
48
- setShowControls: (showControls) => set({ showControls, controlsTimeout: undefined }),
49
- telemetry: true,
50
- setTelemetry: (telemetry) => set(() => ({ telemetry })),
51
- playerEvent: async (url, time, eventType, meta) => set((x) => {
52
- const data = {
53
- time: time,
54
- playerId: x.id,
55
- eventType: eventType,
56
- meta: {
57
- ...meta,
58
- },
59
- };
60
- try {
61
- // fetch url from sp provider
62
- fetch(`${url}/api/player-event`, {
63
- method: "POST",
64
- body: JSON.stringify(data),
65
- });
66
- }
67
- catch (e) {
68
- console.error("error sending player telemetry", e);
69
- }
70
- return {};
71
- }),
72
- // Clear the controls timeout, if it exists.
73
- // Should be called on player unmount.
74
- clearControlsTimeout: () => set((state) => {
75
- if (state.controlsTimeout) {
76
- clearTimeout(state.controlsTimeout);
77
- }
78
- return { controlsTimeout: undefined };
79
- }),
80
- setUserInteraction: () => set((p) => {
81
- // controls timeout
82
- if (p.controlsTimeout) {
83
- clearTimeout(p.controlsTimeout);
84
- }
85
- let controlsTimeout = setTimeout(() => p.setShowControls(false), 1000);
86
- return { showControls: true, controlsTimeout };
87
- }),
88
- }));
89
- };
90
- export function usePlayerContext() {
91
- const context = useContext(PlayerContext);
92
- if (!context) {
93
- throw new Error("usePlayerContext must be used within a PlayerProvider");
94
- }
95
- return context;
96
- }
97
- // Get a specific player store by ID
98
- export function getPlayerStoreById(id) {
99
- const { players } = usePlayerContext();
100
- const playerStore = players[id];
101
- if (!playerStore) {
102
- throw new Error(`No player found with ID: ${id}`);
103
- }
104
- return playerStore;
105
- }
106
- // Will get the first player ID in the context
107
- export function getFirstPlayerID() {
108
- const { players } = usePlayerContext();
109
- const playerIds = Object.keys(players);
110
- if (playerIds.length === 0) {
111
- throw new Error("No players found in context");
112
- }
113
- return playerIds[0];
114
- }
115
- export function getPlayerStoreFromContext() {
116
- console.warn("getPlayerStoreFromContext is deprecated. Use getPlayerStoreById instead.");
117
- const { players } = usePlayerContext();
118
- const playerIds = Object.keys(players);
119
- if (playerIds.length === 0) {
120
- throw new Error("No players found in context");
121
- }
122
- return players[playerIds[0]];
123
- }
124
- // Use a specific player store by ID
125
- // If no ID is provided, it will use the first player in the context
126
- export function usePlayerStore(selector, playerId) {
127
- if (!playerId) {
128
- playerId = Object.keys(usePlayerContext().players)[0];
129
- }
130
- const store = getPlayerStoreById(playerId);
131
- return useStore(store, selector);
132
- }
133
- /* Convenience selectors/hooks */
134
- export const usePlayerProtocol = (playerId) => usePlayerStore((x) => [x.protocol, x.setProtocol], playerId);
135
- export const intoPlayerProtocol = (protocol) => {
136
- switch (protocol) {
137
- case "hls":
138
- return PlayerProtocol.HLS;
139
- case "progressive-mp4":
140
- return PlayerProtocol.PROGRESSIVE_MP4;
141
- case "progressive-webm":
142
- return PlayerProtocol.PROGRESSIVE_WEBM;
143
- default:
144
- return PlayerProtocol.WEBRTC;
145
- }
146
- };
@@ -1,109 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import React, { createContext, useContext, useMemo } from "react";
3
- import { useStore } from "zustand";
4
- import { usePlayerContext } from "../player-store";
5
- import { PlayerProtocol } from "./player-state";
6
- const SinglePlayerContext = createContext(null);
7
- /**
8
- * Provider component for a single player that creates a scoped context
9
- * This allows components to access a specific player's state without passing IDs around
10
- */
11
- export const SinglePlayerProvider = ({ children, playerId: providedPlayerId, protocol = PlayerProtocol.WEBRTC, rendition = "auto", }) => {
12
- const { players, createPlayer } = usePlayerContext();
13
- // Create or get a player ID
14
- const playerId = useMemo(() => {
15
- // If a player ID is provided and exists, use it
16
- if (providedPlayerId && players[providedPlayerId]) {
17
- return providedPlayerId;
18
- }
19
- // If a player ID is provided but doesn't exist, create it
20
- if (providedPlayerId) {
21
- return createPlayer(providedPlayerId);
22
- }
23
- // Otherwise create a new player
24
- return createPlayer();
25
- }, [providedPlayerId, players, createPlayer]);
26
- // Get the player store
27
- const playerStore = useMemo(() => {
28
- return players[playerId];
29
- }, [players, playerId]);
30
- // Set initial protocol and rendition if provided
31
- React.useEffect(() => {
32
- if (protocol) {
33
- playerStore.setState((state) => ({
34
- ...state,
35
- protocol,
36
- }));
37
- }
38
- if (rendition) {
39
- playerStore.setState((state) => ({
40
- ...state,
41
- selectedRendition: rendition,
42
- }));
43
- }
44
- }, [playerStore, protocol, rendition]);
45
- // Create context value
46
- const contextValue = useMemo(() => ({
47
- playerId,
48
- playerStore,
49
- }), [playerId, playerStore]);
50
- return (_jsx(SinglePlayerContext.Provider, { value: contextValue, children: children }));
51
- };
52
- /**
53
- * Hook to access the current single player context
54
- */
55
- export function useSinglePlayerContext() {
56
- const context = useContext(SinglePlayerContext);
57
- if (!context) {
58
- throw new Error("useSinglePlayerContext must be used within a SinglePlayerProvider");
59
- }
60
- return context;
61
- }
62
- /**
63
- * Hook to access the current player ID from the single player context
64
- */
65
- export function useCurrentPlayerId() {
66
- const { playerId } = useSinglePlayerContext();
67
- return playerId;
68
- }
69
- /**
70
- * Hook to access state from the current player without needing to specify the ID
71
- */
72
- export function useCurrentPlayerStore(selector) {
73
- const { playerStore } = useSinglePlayerContext();
74
- return useStore(playerStore, selector);
75
- }
76
- /**
77
- * Hook to get the protocol of the current player
78
- */
79
- export function useCurrentPlayerProtocol() {
80
- return useCurrentPlayerStore((state) => [state.protocol, state.setProtocol]);
81
- }
82
- /**
83
- * Hook to get the selected rendition of the current player
84
- */
85
- export function useCurrentPlayerRendition() {
86
- return useCurrentPlayerStore((state) => [state.selectedRendition, state.setSelectedRendition]);
87
- }
88
- /**
89
- * Hook to get the ingest state of the current player
90
- */
91
- export function useCurrentPlayerIngest() {
92
- return useCurrentPlayerStore((state) => ({
93
- starting: state.ingestStarting,
94
- setStarting: state.setIngestStarting,
95
- connectionState: state.ingestConnectionState,
96
- setConnectionState: state.setIngestConnectionState,
97
- startedTimestamp: state.ingestStarted,
98
- setStartedTimestamp: state.setIngestStarted,
99
- }));
100
- }
101
- /**
102
- * HOC to wrap components with a SinglePlayerProvider
103
- */
104
- export function withSinglePlayer(Component) {
105
- return function WithSinglePlayer(props) {
106
- const { playerId, protocol, rendition, ...componentProps } = props;
107
- return (_jsx(SinglePlayerProvider, { playerId: playerId, protocol: protocol, rendition: rendition, children: _jsx(Component, { ...componentProps }) }));
108
- };
109
- }
@@ -1,2 +0,0 @@
1
- import { createContext } from "react";
2
- export const StreamplaceContext = createContext(null);
@@ -1,16 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useEffect, useRef } from "react";
3
- import { makeStreamplaceStore } from "../streamplace-store/streamplace-store";
4
- import { StreamplaceContext } from "./context";
5
- import Poller from "./poller";
6
- export function StreamplaceProvider({ children, url, oauthSession, }) {
7
- // todo: handle url changes?
8
- const store = useRef(makeStreamplaceStore({ url })).current;
9
- useEffect(() => {
10
- store.setState({ url });
11
- }, [url]);
12
- useEffect(() => {
13
- store.setState({ oauthSession });
14
- }, [oauthSession]);
15
- return (_jsx(StreamplaceContext.Provider, { value: { store: store }, children: _jsx(Poller, { children: children }) }));
16
- }
@@ -1,46 +0,0 @@
1
- import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
- import { useEffect } from "react";
3
- import { StreamplaceAgent } from "streamplace";
4
- import { useDID, useGetBskyProfile, useGetChatProfile, useStreamplaceStore, } from "../streamplace-store";
5
- import { usePDSAgent } from "../streamplace-store/xrpc";
6
- export default function Poller({ children }) {
7
- const url = useStreamplaceStore((state) => state.url);
8
- const setLiveUsers = useStreamplaceStore((state) => state.setLiveUsers);
9
- const did = useDID();
10
- const pdsAgent = usePDSAgent();
11
- const getChatProfile = useGetChatProfile();
12
- const getBskyProfile = useGetBskyProfile();
13
- const liveUserRefresh = useStreamplaceStore((state) => state.liveUsersRefresh);
14
- useEffect(() => {
15
- if (pdsAgent && did) {
16
- getChatProfile();
17
- getBskyProfile();
18
- }
19
- }, [pdsAgent, did]);
20
- useEffect(() => {
21
- const agent = new StreamplaceAgent(url);
22
- const go = async () => {
23
- setLiveUsers({
24
- liveUsersLoading: true,
25
- });
26
- try {
27
- const res = await agent.place.stream.live.getLiveUsers();
28
- setLiveUsers({
29
- liveUsers: res.data.streams || [],
30
- liveUsersLoading: false,
31
- liveUsersError: null,
32
- });
33
- }
34
- catch (e) {
35
- setLiveUsers({
36
- liveUsersLoading: false,
37
- liveUsersError: e.message,
38
- });
39
- }
40
- };
41
- go();
42
- const handle = setInterval(go, 3000);
43
- return () => clearInterval(handle);
44
- }, [url, liveUserRefresh]);
45
- return _jsx(_Fragment, { children: children });
46
- }
File without changes
@@ -1,2 +0,0 @@
1
- export * from "./streamplace-store";
2
- export * from "./user";
@@ -1,37 +0,0 @@
1
- import { useContext } from "react";
2
- import { createStore, useStore } from "zustand";
3
- import { StreamplaceContext } from "../streamplace-provider/context";
4
- export const makeStreamplaceStore = ({ url, }) => {
5
- return createStore()((set) => ({
6
- url,
7
- liveUsers: null,
8
- setLiveUsers: (opts) => {
9
- set({
10
- ...opts,
11
- });
12
- },
13
- liveUsersRefresh: 0,
14
- liveUsersLoading: true,
15
- liveUsersError: null,
16
- oauthSession: null,
17
- handle: null,
18
- chatProfile: null,
19
- }));
20
- };
21
- export function getStreamplaceStoreFromContext() {
22
- const context = useContext(StreamplaceContext);
23
- if (!context) {
24
- throw new Error("useStreamplaceStore must be used within a StreamplaceProvider");
25
- }
26
- return context.store;
27
- }
28
- export function useStreamplaceStore(selector) {
29
- return useStore(getStreamplaceStoreFromContext(), selector);
30
- }
31
- export const useUrl = () => useStreamplaceStore((x) => x.url);
32
- export const useDID = () => useStreamplaceStore((x) => x.oauthSession?.did);
33
- export const useHandle = () => useStreamplaceStore((x) => x.handle);
34
- export const useSetHandle = () => {
35
- const store = getStreamplaceStoreFromContext();
36
- return (handle) => store.setState({ handle });
37
- };
@@ -1,47 +0,0 @@
1
- import { PlaceStreamChatProfile } from "streamplace";
2
- import { getStreamplaceStoreFromContext, useDID, useStreamplaceStore, } from "./streamplace-store";
3
- import { usePDSAgent } from "./xrpc";
4
- export function useGetChatProfile() {
5
- const did = useDID();
6
- const pdsAgent = usePDSAgent();
7
- const store = getStreamplaceStoreFromContext();
8
- return async () => {
9
- if (!did || !pdsAgent) {
10
- throw new Error("No DID or PDS agent");
11
- }
12
- const res = await pdsAgent.com.atproto.repo.getRecord({
13
- repo: did,
14
- collection: "place.stream.chat.profile",
15
- rkey: "self",
16
- });
17
- if (!res.success) {
18
- throw new Error("Failed to get chat profile record");
19
- }
20
- if (PlaceStreamChatProfile.isRecord(res.data.value)) {
21
- store.setState({ chatProfile: res.data.value });
22
- }
23
- else {
24
- console.log("not a record", res.data.value);
25
- }
26
- };
27
- }
28
- export function useGetBskyProfile() {
29
- const did = useDID();
30
- const pdsAgent = usePDSAgent();
31
- const store = getStreamplaceStoreFromContext();
32
- return async () => {
33
- if (!did || !pdsAgent) {
34
- throw new Error("No DID or PDS agent");
35
- }
36
- const res = await pdsAgent.app.bsky.actor.getProfile({
37
- actor: did,
38
- });
39
- if (!res.success) {
40
- throw new Error("Failed to get chat profile record");
41
- }
42
- store.setState({ handle: res.data.handle });
43
- };
44
- }
45
- export function useChatProfile() {
46
- return useStreamplaceStore((x) => x.chatProfile);
47
- }
@@ -1,12 +0,0 @@
1
- import { useMemo } from "react";
2
- import { StreamplaceAgent } from "streamplace";
3
- import { useStreamplaceStore } from ".";
4
- export function usePDSAgent() {
5
- const oauthSession = useStreamplaceStore((state) => state.oauthSession);
6
- return useMemo(() => {
7
- if (!oauthSession) {
8
- return null;
9
- }
10
- return new StreamplaceAgent(oauthSession);
11
- }, [oauthSession]);
12
- }
package/src/index.tsx DELETED
@@ -1,6 +0,0 @@
1
- // barrel file :)
2
- export * from "./livestream-provider";
3
- export * from "./livestream-store";
4
- export * from "./player-store";
5
- export * from "./streamplace-provider";
6
- export * from "./streamplace-store";
@@ -1,37 +0,0 @@
1
- import React, { useContext, useRef } from "react";
2
- import { LivestreamContext, makeLivestreamStore } from "../livestream-store";
3
- import { useLivestreamWebsocket } from "./websocket";
4
-
5
- export function LivestreamProvider({
6
- children,
7
- src,
8
- }: {
9
- children: React.ReactNode;
10
- src: string;
11
- }) {
12
- const context = useContext(LivestreamContext);
13
- const store = useRef(makeLivestreamStore()).current;
14
- if (context) {
15
- // this is ok, there's use cases for having one in another
16
- // like having a player component that's independently usable
17
- // but can also be embedded within an entire livestream page
18
- return <>{children}</>;
19
- }
20
- (window as any).livestreamStore = store;
21
- return (
22
- <LivestreamContext.Provider value={{ store: store }}>
23
- <LivestreamPoller src={src}>{children}</LivestreamPoller>
24
- </LivestreamContext.Provider>
25
- );
26
- }
27
-
28
- export function LivestreamPoller({
29
- children,
30
- src,
31
- }: {
32
- children: React.ReactNode;
33
- src: string;
34
- }) {
35
- useLivestreamWebsocket(src);
36
- return <>{children}</>;
37
- }
@@ -1,47 +0,0 @@
1
- import { useRef } from "react";
2
- import useWebSocket from "react-use-websocket";
3
- import { useHandleWebsocketMessages } from "../livestream-store";
4
- import { useUrl } from "../streamplace-store";
5
-
6
- export function useLivestreamWebsocket(src: string) {
7
- const url = useUrl();
8
- const handleWebSocketMessages = useHandleWebsocketMessages();
9
-
10
- let wsUrl = url.replace(/^http\:/, "ws:");
11
- wsUrl = wsUrl.replace(/^https\:/, "wss:");
12
-
13
- const ref = useRef<any[]>([]);
14
- const handle = useRef<NodeJS.Timeout | null>(null);
15
-
16
- const { readyState } = useWebSocket(`${wsUrl}/api/websocket/${src}`, {
17
- reconnectInterval: 1000,
18
- shouldReconnect: () => true,
19
-
20
- onOpen: () => {
21
- ref.current = [];
22
- },
23
-
24
- onError: (e) => {
25
- console.log("onError", e);
26
- },
27
-
28
- // spamming the redux store with messages causes a zillion re-renders,
29
- // so we batch them up a bit
30
- onMessage: (msg) => {
31
- try {
32
- const data = JSON.parse(msg.data);
33
- ref.current.push(data);
34
- if (handle.current) {
35
- return;
36
- }
37
- handle.current = setTimeout(() => {
38
- handleWebSocketMessages(ref.current);
39
- ref.current = [];
40
- handle.current = null;
41
- }, 250);
42
- } catch (e) {
43
- console.log("onMessage parse error", e);
44
- }
45
- },
46
- });
47
- }