MCPcopy Create free account
hub / github.com/Streamerbot/client / useStreamerbot

Function useStreamerbot

packages/vue/src/composables/useStreamerbot.ts:21–141  ·  view source on GitHub ↗
(options: UseStreamerbotOptions)

Source from the content-addressed store, hash-verified

19
20export type UseStreamerbotOptions = MaybeRefOrGetter<Partial<StreamerbotClientOptions>>;
21
22export type UseStreamerbotReturn = {
23 data: Ref<any>;
24 status: Ref<WebSocketStatus>;
25 error: Ref<string | undefined>;
26 connect: () => void;
27 disconnect: () => void;
28 client: Ref<StreamerbotClient | undefined>;
29};
30
31export function useStreamerbot(options: UseStreamerbotOptions): UseStreamerbotReturn {
32 const _options = computed<Partial<StreamerbotClientOptions>>(() =>
33 typeof options === 'function' ? options() : toValue(options),
34 );
35 const _optionsChanged = ref(false);
36 watch(_options, () => (_optionsChanged.value = true), { deep: true, immediate: true });
37
38 const data = ref();
39 const status = ref<WebSocketStatus>('CLOSED');
40 const error = ref<string>();
41 const clientRef = ref<StreamerbotClient>();
42
43 function onConnect(data: StreamerbotInfo) {
44 status.value = 'OPEN';
45 error.value = undefined;
46 let cb = toValue(_options.value).onConnect;
47 cb?.(data);
48 }
49
50 function onDisconnect() {
51 status.value = 'CLOSED';
52 let cb = toValue(_options.value).onDisconnect;
53 cb?.();
54 }
55
56 function onError(err: Error) {
57 error.value = err?.message ?? 'Unkown Error';
58 status.value = 'CLOSED';
59 let cb = toValue(_options.value).onError;
60 cb?.(err);
61 }
62
63 function onData(payload: any) {
64 data.value = payload;
65 let cb = toValue(_options.value).onData;
66 cb?.(payload);
67 }
68
69 async function connect() {
70 await disconnect();
71 status.value = 'CONNECTING';
72 if (_optionsChanged.value) {
73 await _init(true);
74 } else {
75 await clientRef.value?.connect();
76 }
77 }
78

Callers 1

Calls 2

_initFunction · 0.85
disconnectFunction · 0.85

Tested by

no test coverage detected