@quenty/rx 4.1.1-canary.256.edbbcfc.0 → 5.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.
package/CHANGELOG.md CHANGED
@@ -3,7 +3,20 @@
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
- ## [4.1.1-canary.256.edbbcfc.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/rx@4.1.0...@quenty/rx@4.1.1-canary.256.edbbcfc.0) (2022-03-27)
6
+ # [5.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/rx@4.2.0...@quenty/rx@5.0.0) (2022-05-21)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add ObservableSubscriptionTable ([43152b8](https://github.com/Quenty/NevermoreEngine/commit/43152b876dda2f752c457eef8932bed27170ac9a))
12
+ * Add Rx.skip(toSkip) and make promises autocancel ([69e13f6](https://github.com/Quenty/NevermoreEngine/commit/69e13f6f368f238e64868eecd358f6a6aff76e02))
13
+ * Add Subscription:Disconnect() ([89d1218](https://github.com/Quenty/NevermoreEngine/commit/89d1218c8c7b5d5988d272561c2011df0e58a75c))
14
+
15
+
16
+
17
+
18
+
19
+ # [4.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/rx@4.1.0...@quenty/rx@4.2.0) (2022-03-27)
7
20
 
8
21
 
9
22
  ### Features
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2014-2021 Quenty
3
+ Copyright (c) 2014-2022 Quenty
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/rx",
3
- "version": "4.1.1-canary.256.edbbcfc.0",
3
+ "version": "5.0.0",
4
4
  "description": "Quenty's reactive library for Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -27,17 +27,18 @@
27
27
  "Quenty"
28
28
  ],
29
29
  "dependencies": {
30
- "@quenty/cancellabledelay": "3.1.1-canary.256.edbbcfc.0",
31
- "@quenty/loader": "4.0.1-canary.256.edbbcfc.0",
32
- "@quenty/maid": "2.2.1-canary.256.edbbcfc.0",
33
- "@quenty/promise": "4.1.1-canary.256.edbbcfc.0",
34
- "@quenty/signal": "2.1.1-canary.256.edbbcfc.0",
35
- "@quenty/symbol": "2.0.2-canary.256.edbbcfc.0",
36
- "@quenty/table": "2.1.2-canary.256.edbbcfc.0",
37
- "@quenty/throttle": "4.0.1-canary.256.edbbcfc.0"
30
+ "@quenty/cancellabledelay": "^3.2.0",
31
+ "@quenty/canceltoken": "^5.0.0",
32
+ "@quenty/loader": "^5.0.0",
33
+ "@quenty/maid": "^2.3.0",
34
+ "@quenty/promise": "^5.0.0",
35
+ "@quenty/signal": "^2.2.0",
36
+ "@quenty/symbol": "^2.1.0",
37
+ "@quenty/table": "^3.0.0",
38
+ "@quenty/throttle": "^5.0.0"
38
39
  },
39
40
  "publishConfig": {
40
41
  "access": "public"
41
42
  },
42
- "gitHead": "edbbcfc38516772a791d50dc43cd6b304ffc4aff"
43
+ "gitHead": "9f7eaea7543c33c89d2e32c38491b13f9271f4f7"
43
44
  }
@@ -0,0 +1,102 @@
1
+ --[=[
2
+ This allows the storage of subscriptions for keys, such that something
3
+ can subscribe onto a key, and events can be invoked onto keys.
4
+ @class ObservableSubscriptionTable
5
+ ]=]
6
+
7
+ local require = require(script.Parent.loader).load(script)
8
+
9
+ local Observable = require("Observable")
10
+
11
+ local ObservableSubscriptionTable = {}
12
+ ObservableSubscriptionTable.ClassName = "ObservableSubscriptionTable"
13
+ ObservableSubscriptionTable.__index = ObservableSubscriptionTable
14
+
15
+ function ObservableSubscriptionTable.new()
16
+ local self = setmetatable({}, ObservableSubscriptionTable)
17
+
18
+ self._subMap = {} -- { TKey: Subscription<TEmit> }
19
+
20
+ return self
21
+ end
22
+
23
+ --[=[
24
+ Fires for the current key the given value
25
+ @param key TKey
26
+ @param ... TEmit
27
+ ]=]
28
+ function ObservableSubscriptionTable:Fire(key, ...)
29
+ assert(key ~= nil, "Bad key")
30
+
31
+ local subs = self._subMap[key]
32
+ if not subs then
33
+ return
34
+ end
35
+
36
+ -- Make a copy so we don't have to worry about our last changing
37
+ for _, sub in pairs(table.clone(subs)) do
38
+ task.spawn(sub.Fire, sub, ...)
39
+ end
40
+ end
41
+
42
+ --[=[
43
+ Observes for the key
44
+ @param key TKey
45
+ @return Observable<TEmit>
46
+ ]=]
47
+ function ObservableSubscriptionTable:Observe(key)
48
+ assert(key ~= nil, "Bad key")
49
+
50
+ return Observable.new(function(sub)
51
+ if not self._subMap[key] then
52
+ self._subMap[key] = { sub }
53
+ else
54
+ table.insert(self._subMap[key], sub)
55
+ end
56
+
57
+ return function()
58
+ if not self._subMap[key] then
59
+ return
60
+ end
61
+
62
+ local current = self._subMap[key]
63
+ local index = table.find(current, sub)
64
+ if not index then
65
+ return
66
+ end
67
+
68
+ table.remove(current, index)
69
+ if #current == 0 then
70
+ self._subMap[key] = nil
71
+ end
72
+
73
+ -- Complete the subscription
74
+ if sub:IsPending() then
75
+ task.spawn(function()
76
+ sub:Complete()
77
+ end)
78
+ end
79
+ end
80
+ end)
81
+ end
82
+
83
+ --[=[
84
+ Completes all subscriptions and removes them from the list.
85
+ ]=]
86
+ function ObservableSubscriptionTable:Destroy()
87
+ while next(self._subMap) do
88
+ local key, list = next(self._subMap)
89
+ self._subMap[key] = nil
90
+
91
+ for _, sub in pairs(list) do
92
+ if sub:IsPending() then
93
+ task.spawn(function()
94
+ sub:Complete()
95
+ end)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+
102
+ return ObservableSubscriptionTable
package/src/Shared/Rx.lua CHANGED
@@ -19,6 +19,7 @@ local Symbol = require("Symbol")
19
19
  local Table = require("Table")
20
20
  local ThrottledFunction = require("ThrottledFunction")
21
21
  local cancellableDelay = require("cancellableDelay")
22
+ local CancelToken = require("CancelToken")
22
23
 
23
24
  local UNSET_VALUE = Symbol.named("unsetValue")
24
25
 
@@ -128,18 +129,27 @@ end
128
129
  function Rx.toPromise(observable, cancelToken)
129
130
  local maid = Maid.new()
130
131
 
131
- local promise = Promise.new(function(resolve, reject)
132
+ local newCancelToken = CancelToken.new(function(cancel)
133
+ maid:GiveTask(cancel)
132
134
  if cancelToken then
133
135
  if cancelToken:IsCancelled() then
134
- reject()
135
- return
136
+ cancel()
137
+ else
138
+ maid:GiveTask(cancelToken.Cancelled:Connect(cancel))
136
139
  end
140
+ end
141
+ end)
137
142
 
138
- maid:GiveTask(cancelToken.Cancelled:Connect(function()
139
- reject()
140
- end))
143
+ local promise = Promise.new(function(resolve, reject)
144
+ if newCancelToken:IsCancelled() then
145
+ reject()
146
+ return
141
147
  end
142
148
 
149
+ maid:GiveTask(newCancelToken.Cancelled:Connect(function()
150
+ reject()
151
+ end))
152
+
143
153
  maid:GiveTask(observable:Subscribe(resolve, reject, reject))
144
154
  end)
145
155
 
@@ -1170,6 +1180,38 @@ function Rx.take(number)
1170
1180
  end
1171
1181
  end
1172
1182
 
1183
+ --[=[
1184
+ Takes n entries and then completes the observation.
1185
+
1186
+ https://rxjs.dev/api/operators/take
1187
+ @param toSkip number
1188
+ @return (source: Observable<T>) -> Observable<T>
1189
+ ]=]
1190
+ function Rx.skip(toSkip)
1191
+ assert(type(toSkip) == "number", "Bad toSkip")
1192
+ assert(toSkip > 0, "Bad toSkip")
1193
+
1194
+ return function(source)
1195
+ assert(Observable.isObservable(source), "Bad observable")
1196
+
1197
+ return Observable.new(function(sub)
1198
+ local skipped = 0
1199
+ local maid = Maid.new()
1200
+
1201
+ maid:GiveTask(source:Subscribe(function(...)
1202
+ if skipped <= toSkip then
1203
+ skipped = skipped + 1
1204
+ return
1205
+ end
1206
+
1207
+ sub:Fire(...)
1208
+ end, sub:GetFailComplete()))
1209
+
1210
+ return maid
1211
+ end)
1212
+ end
1213
+ end
1214
+
1173
1215
  --[=[
1174
1216
  Defers the subscription and creation of the observable until the
1175
1217
  actual subscription of the observable.
@@ -207,4 +207,11 @@ function Subscription:Destroy()
207
207
  self:_doCleanup()
208
208
  end
209
209
 
210
+ --[=[
211
+ Alias for [Subscription.Destroy].
212
+ ]=]
213
+ function Subscription:Disconnect()
214
+ self:Destroy()
215
+ end
216
+
210
217
  return Subscription