@quenty/brio 3.5.2 → 3.5.3-canary.241.8dba9c1.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.5.3-canary.241.8dba9c1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/brio@3.5.2...@quenty/brio@3.5.3-canary.241.8dba9c1.0) (2022-01-03)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Fix moonwave documentation ([8dba9c1](https://github.com/Quenty/NevermoreEngine/commit/8dba9c1e474a8f8c5de373dc04e3c876e21b034b))
12
+
13
+
14
+ ### Features
15
+
16
+ * Deprecate old brio commands and add new utility functions, including: ([4cb10f2](https://github.com/Quenty/NevermoreEngine/commit/4cb10f2033d10052b0542f54ab53cbf871897a03))
17
+
18
+
19
+
20
+
21
+
6
22
  ## [3.5.2](https://github.com/Quenty/NevermoreEngine/compare/@quenty/brio@3.5.1...@quenty/brio@3.5.2) (2021-12-30)
7
23
 
8
24
  **Note:** Version bump only for package @quenty/brio
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/brio",
3
- "version": "3.5.2",
3
+ "version": "3.5.3-canary.241.8dba9c1.0",
4
4
  "description": "Brios wrap an object and either are alive or dead",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -26,12 +26,12 @@
26
26
  "Quenty"
27
27
  ],
28
28
  "dependencies": {
29
- "@quenty/loader": "^3.1.2",
30
- "@quenty/maid": "^2.0.2",
31
- "@quenty/rx": "^3.5.2"
29
+ "@quenty/loader": "3.1.3-canary.241.8dba9c1.0",
30
+ "@quenty/maid": "2.0.2",
31
+ "@quenty/rx": "3.5.3-canary.241.8dba9c1.0"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"
35
35
  },
36
- "gitHead": "4afdd64f4ff6bc3b4b4c0ca7a31e839196f2411e"
36
+ "gitHead": "8dba9c1e474a8f8c5de373dc04e3c876e21b034b"
37
37
  }
@@ -80,7 +80,6 @@ end
80
80
  --[=[
81
81
  Constructs a new Brio.
82
82
 
83
-
84
83
  ```lua
85
84
  local brio = Brio.new("a", "b")
86
85
  print(brio:GetValue()) --> a b
@@ -95,6 +94,22 @@ function Brio.new(...) -- Wrap
95
94
  }, Brio)
96
95
  end
97
96
 
97
+ --[=[
98
+ Constructs a new brio that will cleanup afer the set amount of time
99
+
100
+ @since 3.6.0
101
+ @param time number
102
+ @param ... any -- Brio values
103
+ @return Brio
104
+ ]=]
105
+ function Brio.delayed(time, ...)
106
+ local brio = Brio.new(...)
107
+ task.delay(time, function()
108
+ brio:Kill()
109
+ end)
110
+ return brio
111
+ end
112
+
98
113
  --[=[
99
114
  Gets a signal that will fire when the Brio dies. If the brio is already dead
100
115
  calling this method will error.
@@ -214,6 +229,18 @@ function Brio:GetValue()
214
229
  return unpack(self._values, 1, self._values.n)
215
230
  end
216
231
 
232
+ --[=[
233
+ Returns the packed values from table.pack() format
234
+
235
+ @since 3.6.0
236
+ @return { n: number, ... T }
237
+ ]=]
238
+ function Brio:GetPackedValues()
239
+ assert(self._values, "Brio is dead")
240
+
241
+ return self._values
242
+ end
243
+
217
244
  --[=[
218
245
  Kills the Brio.
219
246
 
@@ -126,6 +126,31 @@ function BrioUtils.first(brios, ...)
126
126
  return topBrio
127
127
  end
128
128
 
129
+ --[=[
130
+ Clones a brio, such that it may be killed without affecting the original
131
+ brio.
132
+
133
+ @since 3.6.0
134
+ @param brio Brio<T>
135
+ @param ... U
136
+ @return Brio<U>
137
+ ]=]
138
+ function BrioUtils.withOtherValues(brio, ...)
139
+ assert(brio, "Bad brio")
140
+
141
+ if brio:IsDead() then
142
+ return Brio.DEAD
143
+ end
144
+
145
+ local newBrio = Brio.new(...)
146
+
147
+ newBrio:ToMaid():GiveTask(brio:GetDiedSignal():Connect(function()
148
+ newBrio:Kill()
149
+ end))
150
+
151
+ return newBrio
152
+ end
153
+
129
154
  --[=[
130
155
  Makes a brio that is limited by the lifetime of its parent (but could be shorter)
131
156
  and has the new values given.
@@ -163,6 +188,44 @@ function BrioUtils.extend(brio, ...)
163
188
  return newBrio
164
189
  end
165
190
 
191
+ --[=[
192
+ Makes a brio that is limited by the lifetime of its parent (but could be shorter)
193
+ and has the new values given at the beginning of the result
194
+
195
+ @since 3.6.0
196
+ @param brio Brio<U>
197
+ @param ... T
198
+ @return Brio<T>
199
+ ]=]
200
+ function BrioUtils.prepend(brio, ...)
201
+ if brio:IsDead() then
202
+ return Brio.DEAD
203
+ end
204
+
205
+ local values = brio._values
206
+ local current = {}
207
+ local otherValues = table.pack(...)
208
+ for i=1, otherValues.n do
209
+ current[i] = otherValues[i]
210
+ end
211
+ for i=1, values.n do
212
+ current[otherValues.n+i] = values[i]
213
+ end
214
+
215
+ local maid = Maid.new()
216
+ local newBrio = Brio.new(unpack(current, 1, values.n + otherValues.n))
217
+
218
+ maid:GiveTask(brio:GetDiedSignal():Connect(function()
219
+ newBrio:Kill()
220
+ end))
221
+
222
+ maid:GiveTask(newBrio:GetDiedSignal():Connect(function()
223
+ maid:DoCleaning()
224
+ end))
225
+
226
+ return newBrio
227
+ end
228
+
166
229
  --[=[
167
230
  Merges the existing brio value with the other brio
168
231
 
@@ -284,10 +284,11 @@ end
284
284
  Unpacks the brio, and then repacks it. Ignored items
285
285
  still invalidate the previous brio
286
286
 
287
+ @since 3.6.0
287
288
  @param predicate (T) -> boolean
288
289
  @return (source: Observable<Brio<T>>) -> Observable<Brio<T>>
289
290
  ]=]
290
- function RxBrioUtils.filter(predicate)
291
+ function RxBrioUtils.where(predicate)
291
292
  assert(type(predicate) == "function", "Bad predicate")
292
293
 
293
294
  return function(source)
@@ -321,16 +322,30 @@ function RxBrioUtils.filter(predicate)
321
322
  end
322
323
  end
323
324
 
325
+ --[=[
326
+ Same as [RxBrioUtils.where]. Here to keep backwards compatability.
327
+
328
+ @deprecated 3.6.0 -- This method does not wrap the resulting value in a Brio, which can sometimes lead to leaks.
329
+ @function filter
330
+ @param predicate (T) -> boolean
331
+ @return (source: Observable<Brio<T>>) -> Observable<Brio<T>>
332
+ @within RxBrioUtils
333
+ ]=]
334
+ RxBrioUtils.filter = RxBrioUtils.where
335
+
324
336
  --[=[
325
337
  Flattens all the brios in one brio and combines them. Note that this method leads to
326
338
  gaps in the lifetime of the brio.
327
339
 
340
+ @deprecated 3.6.0 -- This method does not wrap the resulting value in a Brio, which can sometimes lead to leaks.
328
341
  @param observables { [any]: Observable<Brio<T>> | Observable<T> | T }
329
342
  @return Observable<Brio<{ [any]: T }>>
330
343
  ]=]
331
344
  function RxBrioUtils.combineLatest(observables)
332
345
  assert(type(observables) == "table", "Bad observables")
333
346
 
347
+ warn("[RxBrioUtils.combineLatest] - Deprecated since 3.6.0. Use RxBrioUtils.flatCombineLatest")
348
+
334
349
  return Rx.combineLatest(observables)
335
350
  :Pipe({
336
351
  Rx.map(BrioUtils.flatten);
@@ -339,29 +354,70 @@ function RxBrioUtils.combineLatest(observables)
339
354
  end
340
355
 
341
356
  --[=[
342
- Flat map equivalent for brios
357
+ Flat map equivalent for brios. The resulting observables will
358
+ be disconnected at the end of the brio.
343
359
 
360
+ @deprecated 3.6.0 -- This method does not wrap the resulting value in a Brio, which can sometimes lead to leaks.
344
361
  @param project (value: TBrio) -> TProject
345
- @param resultSelector ((value: TProject) -> TResult)?
346
- @return (source: Observable<Brio<TBrio>> -> Observable<Brio<TResult>>)
362
+ @param resultSelector ((initial TBrio, value: TProject) -> TResult)?
363
+ @return (source: Observable<Brio<TBrio>> -> Observable<TResult>)
347
364
  ]=]
348
365
  function RxBrioUtils.flatMap(project, resultSelector)
349
366
  assert(type(project) == "function", "Bad project")
350
367
 
368
+ warn("[RxBrioUtils.flatMap] - Deprecated since 3.6.0. Use RxBrioUtils.flatMapBrio")
369
+
351
370
  return Rx.flatMap(RxBrioUtils.mapBrio(project), resultSelector)
352
371
  end
353
372
 
354
373
  --[=[
355
- Switch map but for Brio.
374
+ Flat map equivalent for brios. The resulting observables will
375
+ be disconnected at the end of the brio.
376
+
377
+ Like [RxBrioUtils.flatMap], but emitted values are wrapped in brios.
378
+ The lifetime of this brio is limited by the lifetime of the
379
+ input brios, which are unwrapped and repackaged.
356
380
 
381
+ @since 3.6.0
382
+ @param project (value: TBrio) -> TProject | Brio<TProject>
383
+ @return (source: Observable<Brio<TBrio>> -> Observable<Brio<TResult>>)
384
+ ]=]
385
+ function RxBrioUtils.flatMapBrio(project)
386
+ return Rx.flatMap(RxBrioUtils.mapBrioBrio(project))
387
+ end
388
+
389
+ --[=[
390
+ Switch map but for brios. The resulting observable will be
391
+ disconnected on the end of the brio's life.
392
+
393
+ @deprecated 3.6.0 -- This method does not wrap the resulting value in a Brio, which can sometimes lead to leaks.
357
394
  @param project (value: TBrio) -> TProject
358
- @param resultSelector ((value: TProject) -> TResult)?
395
+ @return (source: Observable<Brio<TBrio>>) -> Observable<TResult>
396
+ ]=]
397
+ function RxBrioUtils.switchMap(project)
398
+ assert(type(project) == "function", "Bad project")
399
+
400
+ warn("[RxBrioUtils.switchMap] - Deprecated since 3.6.0. Use RxBrioUtils.switchMapBrio")
401
+
402
+ return Rx.switchMap(RxBrioUtils.mapBrio(project))
403
+ end
404
+
405
+ --[=[
406
+ Switch map but for brios. The resulting observable will be
407
+ disconnected on the end of the brio's life.
408
+
409
+ Like [RxBrioUtils.switchMap] but emitted values are wrapped in brios.
410
+ The lifetime of this brio is limited by the lifetime of the
411
+ input brios, which are unwrapped and repackaged.
412
+
413
+ @since 3.6.0
414
+ @param project (value: TBrio) -> TProject | Brio<TProject>
359
415
  @return (source: Observable<Brio<TBrio>>) -> Observable<Brio<TResult>>
360
416
  ]=]
361
- function RxBrioUtils.switchMap(project, resultSelector)
417
+ function RxBrioUtils.switchMapBrio(project)
362
418
  assert(type(project) == "function", "Bad project")
363
419
 
364
- return Rx.switchMap(RxBrioUtils.mapBrio(project), resultSelector)
420
+ return Rx.switchMap(RxBrioUtils.mapBrioBrio(project))
365
421
  end
366
422
 
367
423
  --[=[
@@ -378,8 +434,9 @@ end
378
434
  With this method we are able to do this, as we'll re-emit a table with all resoruces
379
435
  except the invalidated one.
380
436
 
437
+ @since 3.6.0
381
438
  @param observables { [any]: Observable<Brio<T>> | Observable<T> | T }
382
- @return Observable<Brio<{ [any]: T }>>
439
+ @return Observable<{ [any]: T? }>
383
440
  ]=]
384
441
  function RxBrioUtils.flatCombineLatest(observables)
385
442
  assert(type(observables) == "table", "Bad observables")
@@ -400,12 +457,15 @@ end
400
457
  Takes in a brio and returns an observable that emits the brio, and then completes
401
458
  on death.
402
459
 
460
+ @deprecated 3.6.0 -- This method does not wrap the resulting value in a Brio, which can sometimes lead to leaks.
403
461
  @param project (value: TBrio) -> TProject
404
- @return (brio<TBrio>) -> Brio<TProject>
462
+ @return (brio<TBrio>) -> TProject
405
463
  ]=]
406
464
  function RxBrioUtils.mapBrio(project)
407
465
  assert(type(project) == "function", "Bad project")
408
466
 
467
+ warn("[RxBrioUtils.mapBrio] - Deprecated since 3.6.0. Use RxBrioUtils.mapBrioBrio")
468
+
409
469
  return function(brio)
410
470
  assert(Brio.isBrio(brio), "Not a brio")
411
471
 
@@ -420,6 +480,151 @@ function RxBrioUtils.mapBrio(project)
420
480
  end
421
481
  end
422
482
 
483
+ --[=[
484
+ Prepends the value onto the emitted brio
485
+ @since 3.6.0
486
+ @param ... T
487
+ @return (source: Observable<Brio<U>>) -> Observable<Brio<U | T>>
488
+ ]=]
489
+ function RxBrioUtils.prepend(...)
490
+ local args = table.pack(...)
491
+
492
+ return Rx.map(function(brio)
493
+ assert(Brio.isBrio(brio), "Bad brio")
494
+
495
+ return BrioUtils.prepend(brio, table.unpack(args, 1, args.n))
496
+ end)
497
+ end
498
+
499
+ --[=[
500
+ Extends the value onto the emitted brio
501
+ @since 3.6.0
502
+ @param ... T
503
+ @return (source: Observable<Brio<U>>) -> Observable<Brio<U | T>>
504
+ ]=]
505
+ function RxBrioUtils.extend(...)
506
+ local args = table.pack(...)
507
+
508
+ return Rx.map(function(brio)
509
+ assert(Brio.isBrio(brio), "Bad brio")
510
+
511
+ return BrioUtils.extend(brio, table.unpack(args, 1, args.n))
512
+ end)
513
+ end
514
+
515
+ --[=[
516
+ Maps the input brios to the output observables
517
+ @since 3.6.0
518
+ @param project project (Brio<T> | T) -> Brio<U> | U
519
+ @return (source: Observable<Brio<T> | T>) -> Observable<Brio<U>>
520
+ ]=]
521
+ function RxBrioUtils.map(project)
522
+ return Rx.map(function(...)
523
+ local n = select("#", ...)
524
+ local brios = {}
525
+ local args
526
+
527
+ if n == 1 then
528
+ if Brio.isBrio(...) then
529
+ table.insert(brios, (...))
530
+ args = (...):GetPackedValues()
531
+ else
532
+ args = {[1] = ...}
533
+ end
534
+ else
535
+ args = {}
536
+ for index, item in pairs({...}) do
537
+ if Brio.isBrio(item) then
538
+ table.insert(brios, item)
539
+ args[index] = item:GetValue() -- we lose data here, but I think this is fine
540
+ else
541
+ args[index] = item
542
+ end
543
+ end
544
+ args.n = n
545
+ end
546
+
547
+ local results = table.pack(project(table.unpack(args, 1, args.n)))
548
+ if results.n == 1 then
549
+ if Brio.isBrio(results[1]) then
550
+ table.insert(brios, results[1])
551
+ return BrioUtils.first(brios, results:GetValue())
552
+ else
553
+ return BrioUtils.withOtherValues(brios, results[1])
554
+ end
555
+ else
556
+ local transformedResults = {}
557
+ for i=1, results.n do
558
+ local item = results[i]
559
+ if Brio.isBrio(item) then
560
+ table.insert(brios, item) -- add all subsequent brios into this table...
561
+ transformedResults[i] = item:GetValue()
562
+ else
563
+ transformedResults[i] = item
564
+ end
565
+ end
566
+
567
+ return BrioUtils.first(brios, table.unpack(transformedResults, 1, transformedResults.n))
568
+ end
569
+ end)
570
+ end
571
+
572
+ function RxBrioUtils._mapResult(brio)
573
+ return function(...)
574
+ local n = select("#", ...)
575
+ if n == 0 then
576
+ return BrioUtils.withOtherValues(brio)
577
+ elseif n == 1 then
578
+ if Brio.isBrio(...) then
579
+ return BrioUtils.first({brio, (...)}, (...):GetValue())
580
+ else
581
+ return BrioUtils.withOtherValues(brio, ...)
582
+ end
583
+ else
584
+ local brios = { brio }
585
+ local args = {}
586
+
587
+ for index, item in pairs({...}) do
588
+ if Brio.isBrio(item) then
589
+ table.insert(brios, item)
590
+ args[index] = item:GetValue() -- we lose data here, but I think this is fine
591
+ else
592
+ args[index] = item
593
+ end
594
+ end
595
+
596
+ return BrioUtils.first(brios, unpack(args, 1, n))
597
+ end
598
+ end
599
+ end
600
+
601
+ --[=[
602
+ Takes in a brio and returns an observable that emits the brio, and then completes
603
+ on death.
604
+
605
+ @since 3.6.0
606
+ @param project (value: TBrio) -> TProject | Brio<TProject>
607
+ @return (brio<TBrio>) -> Brio<TProject>
608
+ ]=]
609
+ function RxBrioUtils.mapBrioBrio(project)
610
+ assert(type(project) == "function", "Bad project")
611
+
612
+ return function(brio)
613
+ assert(Brio.isBrio(brio), "Not a brio")
614
+
615
+ if brio:IsDead() then
616
+ return Rx.EMPTY
617
+ end
618
+
619
+ local observable = project(brio:GetValue())
620
+ assert(Observable.isObservable(observable), "Not an observable")
621
+
622
+ return RxBrioUtils.completeOnDeath(brio, observable):Pipe({
623
+ Rx.map(RxBrioUtils._mapResult(brio))
624
+ })
625
+ end
626
+ end
627
+
423
628
  --[=[
424
629
  Transforms the brio into an observable that emits the initial value of the brio, and then another value on death
425
630
  @param brio Brio<T> | T
@@ -508,4 +713,17 @@ function RxBrioUtils.onlyLastBrioSurvives()
508
713
  end
509
714
  end
510
715
 
716
+ --[=[
717
+ Switches the result to a brio, and ensures only the last brio lives.
718
+
719
+ @since 3.6.0
720
+ @function switchToBrio
721
+ @return (source: Observable<T>) -> Observable<Brio<T>>
722
+ @within RxBrioUtils
723
+ ]=]
724
+ RxBrioUtils.switchToBrio = Rx.pipe({
725
+ RxBrioUtils.toBrio();
726
+ RxBrioUtils.onlyLastBrioSurvives();
727
+ })
728
+
511
729
  return RxBrioUtils