/
RxSelectionUtils.lua
213 lines (166 loc) · 4.59 KB
/
RxSelectionUtils.lua
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
--[=[
@class RxSelectionUtils
]=]
local Selection = game:GetService("Selection")
local require = require(script.Parent.loader).load(script)
local Observable = require("Observable")
local Maid = require("Maid")
local Brio = require("Brio")
local ValueObject = require("ValueObject")
local RxBrioUtils = require("RxBrioUtils")
local Set = require("Set")
local RxSelectionUtils = {}
--[=[
Observes first selection in the selection list which is of a class
```lua
RxSelectionUtils.observeFirstSelectionWhichIsA("BasePart"):Subscribe(function(part)
print("part", part)
end)
```
@param className string
@return Observable<Instance?>
]=]
function RxSelectionUtils.observeFirstSelectionWhichIsA(className)
assert(type(className) == "string", "Bad className")
return RxSelectionUtils.observeFirstSelection(function(inst)
return inst:IsA(className)
end)
end
--[=[
Observes first selection in the selection list which is of a class wrapped in a brio
```lua
RxSelectionUtils.observeFirstSelectionWhichIsA("BasePart"):Subscribe(function(brio)
if brio:IsDead() then
return
end
print("part", brio:GetValue())
end)
```
@param className string
@return Observable<Brio<Instance>>
]=]
function RxSelectionUtils.observeFirstSelectionWhichIsABrio(className)
assert(type(className) == "string", "Bad className")
return RxSelectionUtils.observeFirstSelectionBrio(function(inst)
return inst:IsA(className)
end)
end
--[=[
Observes first selection in the selection list which is an "Adornee"
@return Observable<Instance?>
]=]
function RxSelectionUtils.observeFirstAdornee()
return RxSelectionUtils.observeFirstSelection(function(inst)
return inst:IsA("BasePart") or inst:IsA("Model")
end)
end
--[=[
Observes selection in which are an "Adornee"
@return Observable<Brio<Instance>>
]=]
function RxSelectionUtils.observeAdorneesBrio()
return RxSelectionUtils.observeSelectionItemsBrio():Pipe({
RxBrioUtils.where(function(inst)
return inst:IsA("BasePart") or inst:IsA("Model")
end)
})
end
--[=[
Observes first selection which meets condition
```lua
RxSelectionUtils.observeFirstSelection(function(instance)
return instance:IsA("BasePart")
end):Subscribe(function(part)
print("part", part)
end)
```
@param where callback
@return Observable<Instance?>
]=]
function RxSelectionUtils.observeFirstSelection(where)
assert(type(where) == "function", "Bad where")
return Observable.new(function(sub)
local maid = Maid.new()
local current = ValueObject.new(nil)
maid:GiveTask(current)
local function handleSelectionChanged()
for _, item in pairs(Selection:Get()) do
if where(item) then
current.Value = item
return
end
end
current.Value = nil
end
maid:GiveTask(Selection.SelectionChanged:Connect(handleSelectionChanged))
handleSelectionChanged()
maid:GiveTask(current:Observe():Subscribe(function(value)
task.spawn(function()
sub:Fire(value)
end)
end))
return maid
end)
end
--[=[
Observes first selection which meets condition
@param where callback
@return Observable<Brio<Instance>>
]=]
function RxSelectionUtils.observeFirstSelectionBrio(where)
assert(type(where) == "function", "Bad where")
return RxSelectionUtils.observeFirstSelection(where):Pipe({
RxBrioUtils.toBrio();
RxBrioUtils.onlyLastBrioSurvives();
RxBrioUtils.where(function(value)
return value ~= nil
end)
})
end
--[=[
Observes the current selection table.
@return Observable<{ Instance }>
]=]
function RxSelectionUtils.observeSelectionList()
return Observable.new(function(sub)
local maid = Maid.new()
local current = Selection:Get()
maid:GiveTask(Selection.SelectionChanged:Connect(function()
sub:Fire(Selection:Get())
end))
sub:Fire(current)
return maid
end)
end
--[=[
Observes selection items by brio. De-duplicates changed events.
@return Observable<Brio<Instance>>
]=]
function RxSelectionUtils.observeSelectionItemsBrio()
return Observable.new(function(sub)
local maid = Maid.new()
local lastSet = {}
local function handleSelectionChanged()
local currentSet = Set.fromList(Selection:Get())
local toRemoveSet = Set.difference(lastSet, currentSet)
local toAddSet = Set.difference(currentSet, lastSet)
lastSet = currentSet
-- Remove first
for toRemove, _ in pairs(toRemoveSet) do
maid[toRemove] = nil
end
-- Then add
for toAdd, _ in pairs(toAddSet) do
local brio = Brio.new(toAdd)
maid[toAdd] = brio
task.spawn(function()
sub:Fire(brio)
end)
end
end
maid:GiveTask(Selection.SelectionChanged:Connect(handleSelectionChanged))
handleSelectionChanged()
return maid
end)
end
return RxSelectionUtils