-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs.js
141 lines (122 loc) · 3.71 KB
/
obs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// @ title OBS
//.
//. Contains functions for connecting to an OBS websocket,
//. observing OBS events as well as sending commands to the OBS
//. websocket.
//.
//. ```js
//. const OBS = require('obs.remote.kefir/lib/obs');
//. ```
const U = require('karet.util');
const R = require('ramda');
const L = require('partial.lenses');
const { curry2, curry3, flip } = require('sanctuary');
const $ = require('sanctuary-def');
const { createSocket, listenTo, send3 } = require('./socket');
const T = require('./types');
const { camelCaseKebab } = require('./util');
const {
def,
WebSocket: WebSocketT,
KefirObservable: KefirObservableT
} = T;
const listenFor = flip(listenTo);
const camelCaseKeys = L.modify(L.keys, camelCaseKebab);
const isEventMessage = R.has('update-type');
//. #### Sessions
//# connect :: String -> WebSocket
const connect =
def('connect',
{},
[$.String, WebSocketT],
url => createSocket(url, undefined));
//
//# listen_ :: (WebSocket, (Object -> Object)) -> Observable
//.
//. Listen to events from the given websocket, and uptionally apply
//. a custom transforming function on the result.
//.
//. This is the uncurried and unchecked version of the function.
const listen_ = (socket, fn = camelCaseKeys) =>
U.seq(socket,
listenFor('message'),
U.skipUnless(isEventMessage),
U.lift1(fn));
//# listen :: WebSocket -> Observable
//.
//. Create an Observable from any events OBS emits.
//.
//. All objects will be rewritten so that all `kebab-case` keys
//. will be transformed into `camelCase` keys.
//.
//. If you want to override the default transforming function,
//. use [`listenWithTransformer`](#listenWithTransformer) instead.
//.
//. Usually one creates an Observable from all events, and create
//. new Observables by filtering the events by their event name.
//. These can then be used on their own or by combining them
//. to ensure some computation or action is taken when both events
//. have occurred.
//.
//. ```js
//. const obsEvents = listen(ws);
//.
//. obsEvents.filter(R.whereEq({ updateType: 'transition-begin' }))
//. .onValue(e => {
//. // Do something when transition begins
//. });
//.
//. obsEvents.filter(R.whereEq({ updateType: 'preview-scene-changed' }))
//. .onValue(e => {
//. // Do something when the preview scene is changed
//. });
//. ```
const listen =
def('listen',
{},
[WebSocketT, KefirObservableT],
socket => listen_(socket));
//# listenWithTransformer :: WebSocket -> (Object -> Object) -> Observable
//.
//. Create an Observable from any events OBS emits, and transform the event
//. with the given function.
const listenWithTransformer =
def('listenWithTransformer',
{},
[WebSocketT, $.AnyFunction, KefirObservableT],
curry2(listen_));
//
//# command_ :: (String, Object, WebSocket) -> Observable
//.
//. Send a command with optional arguments to the given websocket.
//. Uncurried.
const command_ = (type, args, ws) => send3(type, args, ws);
//# command :: String -> WebSocket -> Observable
//.
//. Send a command without arguments to the given websocket.
//. Curried, takes two arguments.
const command =
def('command',
{},
[$.String, WebSocketT, KefirObservableT],
(type, ws) => command_(type, undefined, ws));
//# commandWithArgs :: String -> Object -> WebSocket -> Observable
//.
//. Send a command with arguments to the given websocket.
//. Curried, takes three arguments.
const commandWithArgs =
def('commandWithArgs',
{},
[$.String, $.Object, WebSocketT, KefirObservableT],
curry3(command_));
//
//
module.exports = {
connect,
listen_,
listen,
listenWithTransformer,
command_,
command,
commandWithArgs,
};