-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
1069 lines (768 loc) · 23.7 KB
/
init.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--!nonstrict
--> Services
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
--> Includes
local Promise = require(script.Parent.Promise)
local Signal = require(script.Parent.Signal)
local MockStore = require(script.MockStore)
local Keep = require(script.Keep)
--> Structure
--[=[
@class Store
@server
A store is a class that holds inner savable objects, Keep(s), from a datastore (DataStoreService:GetDataStore())
]=]
local Store = {
LoadMethods = {
ForceLoad = "ForceLoad",
-- maybe add "Ignore"?
Cancel = "Cancel",
},
mockStore = false, -- Enabled when DataStoreService is not available (Studio)
_saveInterval = 30,
_storeQueue = {}, -- Stores that are currently loaded in the save cycle
assumeDeadLock = 10 * 60, -- how long without updates to assume the session is dead
-- according to clv2, os.time is synced roblox responded in a bug report. I don't see why it would in the first place anyways
ServiceDone = false, -- is shutting down?
CriticalState = false, -- closet thing to tracking if they are down, will be set to true after many failed requests
_criticalStateThreshold = 5, -- how many failed requests before we assume they are down
CriticalStateSignal = Signal.new(), -- fires when we enter critical state
IssueSignal = Signal.new(), -- fires when we have an issue (issue logging)
_issueQueue = {}, -- queue of issues to keep track of if CriticalState should activate
_maxIssueTime = 60, -- how long to keep issues 'valid' in the queue
}
Store.__index = Store
Keep.assumeDeadLock = Store.assumeDeadLock
local GlobalUpdates = {}
GlobalUpdates.__index = GlobalUpdates
--> Types
--[=[
@type StoreInfo {Name: string, Scope: string?}
@within Store
Table format for a store's info in ```:GetStore()```
]=]
export type StoreInfo = {
Name: string,
Scope: string?,
}
type MockStore = MockStore.MockStore
export type Promise = typeof(Promise.new(function() end))
--[=[
@type Store {Mock: MockStore, LoadKeep: (string, unreleasedHandler?) -> Promise<Keep>, ViewKeep: (string) -> Promise<Keep>, PreSave: (({any}) -> {any}) -> nil, PreLoad: (({any}) -> {any}) -> nil, PostGlobalUpdate: (string, (GlobalUpdates) -> nil) -> Promise<void>, IssueSignal: Signal, CriticalStateSignal: Signal, CriticalState: boolean}
@within Store
Stores are used to load and save Keeps from a ```DataStoreService:GetDataStore()```
]=]
--[=[
@prop Wrapper {}
@within Store
Wrapper functions that are inheritted by Keeps when they are loaded
:::info
Any wrapper changes post ```:GetStore()``` will not apply to that store but the next one.
:::info
]=]
--[=[
@prop Mock MockStore
@within Store
A mock store that mirrors the real store, but doesn't save data
]=]
--[=[
@prop IssueSignal Signal
@within Store
Fired when an issue occurs, like a failed request
```lua
keepStore.IssueSignal:Connect(function(err)
print("Issue!", err)
end)
```
]=]
--[=[
@prop CriticalStateSignal Signal
@within Store
Fired when the store enters critical state. After it has failed many requests and maybe dangerous to proceed with purchases or other important actions
```lua
keepStore.CriticalStateSignal:Connect(function()
print("Critical State!")
end)
```
]=]
--[=[
@prop CriticalState boolean
@within Store
Whether the store is in critical state or not. See ```CriticalStateSignal```
```lua
if keepStore.CriticalState then
warn("Critical State!")
return
end
-- process purchase
```
]=]
--[=[
@prop validate (any) -> true | (false & string)
@within Store
Used to validate data before saving. Ex. type guards
```lua
keepStore.validate = function(data)
for key, value in data do
local dataTempVersion = dataTemplate[key]
if typeof(data[key]) ~= typeof(dataTempVersion) then
return false, "Invalid type for key " .. key
end
end
return true
end
```
]=]
export type Store = typeof(Store) & {
_store_info: StoreInfo,
_data_template: any,
_store: DataStore?,
_mock_store: MockStore?,
_mock: boolean,
_keeps: { [string]: Keep.Keep },
Wrapper: { [string]: (any) -> any },
}
export type GlobalUpdates = typeof(setmetatable({}, GlobalUpdates))
--[=[
@type unreleasedHandler (Keep.ActiveSession) -> "ForceLoad" | "Cancel"
@within Store
Used to determine how to handle an session locked Keep
### Default: "ForceLoad"
### "ForceLoad"
Steals the lock, releasing the previous session. It can take up to around 2 auto save cycles (1 on session that is requesting and 1 on session that already owns the lock) to release previous session and save new one if session is locked and up to around 10 minutes if session is in dead lock
### "Cancel"
Cancels the load of the Keep
]=]
export type unreleasedHandler = (Keep.Session) -> string -- use a function for any purposes, logging, whitelist only certain places, etc
--> Private Variables
local Keeps = {} -- queues to save
local JobID = game.JobId
local PlaceID = game.PlaceId
local saveCycle = 0 -- total heartbeat dt
--> Private Functions
local function len(tbl: { [any]: any })
local count = 0
for _ in tbl do
count += 1
end
return count
end
local function deepCopy(tbl: { [any]: any })
local copy = {}
for key, value in pairs(tbl) do
if type(value) == "table" then
copy[key] = deepCopy(value)
else
copy[key] = value
end
end
return copy
end
local function canLoad(keep: Keep.KeepStruct)
--[[
return not keep.MetaData
or not keep.MetaData.ActiveSession -- no active session, so we can load (most likely a new Keep)
or keep.MetaData.ActiveSession.PlaceID == PlaceID and keep.MetaData.ActiveSession.JobID == JobID
or os.time() - keep.MetaData.LastUpdate < Store.assumeDeadLock
]]
if not keep.MetaData then
return true
end
if not keep.MetaData.ActiveSession then
return true
end
if keep.MetaData.ActiveSession.PlaceID == PlaceID and keep.MetaData.ActiveSession.JobID == JobID then
return true
end
if os.time() - keep.MetaData.LastUpdate > Store.assumeDeadLock then
return true
end
return false
end
local function createMockStore(storeInfo: StoreInfo, dataTemplate) -- complete mirror of real stores, minus mock related data as we are in a mock store
return setmetatable({
_store_info = storeInfo,
_data_template = dataTemplate,
_store = MockStore.new(),
_mock = true,
_keeps = {},
_cachedKeepPromises = {},
Wrapper = require(script.Wrapper),
validate = function()
return true
end,
}, Store)
end
local function releaseKeepInternally(keep: Keep.Keep)
Keeps[keep:Identify()] = nil
local keepStore = keep._keep_store
keepStore._cachedKeepPromises[keep:Identify()] = nil
keep.Releasing:Destroy()
keep.Saving:Destroy()
keep.Overwritten:Destroy()
end
local function saveKeep(keep: Keep.Keep, release: boolean): Promise
if keep._released then
releaseKeepInternally(keep)
return Promise.resolve()
end
release = release or false
local operation
if release then
operation = keep.Release
else
operation = keep.Save
end
local savingState = operation(keep)
:andThen(function()
keep._last_save = os.clock()
end)
:catch(function(err)
local keepStore = keep._keep_store
keepStore._processError(err, 1)
end)
return savingState
end
--[[
Future idea: I doubt it is needed so it may just throttle speed.
local function getRequestBudget(keep)
return keep._store:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync)
end
]]
--> Public Functions
local mockStoreCheck = Promise.new(function(resolve)
if game.GameId == 0 then
print("[DataKeep] Local file, using mock store")
return resolve(false)
end
local success, message = pcall(function()
DataStoreService:GetDataStore("__LiveCheck"):SetAsync("__LiveCheck", os.time())
end)
if message then
if string.find(message, "ConnectFail", 1, true) then
warn("[DataKeep] No internet connection, using mock store")
end
if string.find(message, "403", 1, true) or string.find(message, "must publish", 1, true) then
print("[DataKeep] Datastores are not available, using mock store")
else
print("[DataKeep] Datastores are available, using real store")
end
end
return resolve(success)
end):andThen(function(isLive)
Store.mockStore = if not Store.ServiceDone then not isLive else true -- check for Store.ServiceDone to prevent loading keeps during BindToClose()
end)
--[=[
@function GetStore
@within Store
@param storeInfo StoreInfo | string
@param dataTemplate any
@return Promise<Store>
Loads a store from a ```DataStoreService:GetDataStore()``` and returns a Store object
```lua
local keepStore = DataKeep.GetStore("TestStore", {
Test = "Hello World!",
}):expect()
```
]=]
function Store.GetStore(storeInfo: StoreInfo | string, dataTemplate): Promise
local info: StoreInfo
if type(storeInfo) == "string" then
info = {
Name = storeInfo,
Scope = nil,
}
else
info = storeInfo
end
local id = `{info.Name}{info.Scope or ""}`
if Store._storeQueue[id] then
return Promise.resolve(Store._storeQueue[id])
end
return mockStoreCheck:andThen(function()
local self = setmetatable({
_store_info = info,
_data_template = dataTemplate,
_store = if Store.mockStore then MockStore.new() else DataStoreService:GetDataStore(info.Name, info.Scope), -- this always returns even with datastores down, so only way of tracking is via failed requests
Mock = createMockStore(info, dataTemplate), -- revealed to api
_mock = if Store.mockStore then true else false, -- studio only/datastores not available
_cachedKeepPromises = {},
validate = function()
return true
end,
Wrapper = require(script.Wrapper),
}, Store)
Store._storeQueue[id] = self._store
local function processError(err, priority: number)
Store.IssueSignal:Fire(err)
priority = priority or 1
-- priorities:
-- 0: no issue signal, warn
-- 1: warn
-- 2: error issue signal
if priority > 1 then
error(`[DataKeep] {err}`)
else
warn(`[DataKeep] {err}`)
end
local clock = os.clock()
if priority ~= 0 then
table.insert(Store._issueQueue, clock)
end
if Store._issueQueue[Store._criticalStateThreshold + 1] then
table.remove(Store._issueQueue, Store._criticalStateThreshold + 1)
end
local issueCount = 0
for _, issueTime in ipairs(Store._issueQueue) do
if clock - issueTime < Store._maxIssueTime then
issueCount += 1
end
end
if issueCount >= Store._criticalStateThreshold then
Store.CriticalState = true
Store.CriticalStateSignal:Fire()
end
end
self._processError = processError
self.Mock._processError = processError
return Promise.resolve(self)
end)
end
--[=[
@method LoadKeep
@within Store
@param key string
@param unreleasedHandler unreleasedHandler?
@return Promise<Keep>
Loads a Keep from the store and returns a Keep object
```lua
keepStore:LoadKeep(`Player_{player.UserId}`, function()
return DataKeep.LoadMethods.ForceLoad
end)):andThen(function(keep)
print(`Loaded {keep:Identify()}!`)
end)
```
:::info
Stores can be loaded multiple times as they are cached, that way you can call ```:LoadKeep()``` and get the same cached Keeps
:::info
]=]
function Store:LoadKeep(key: string, unreleasedHandler: unreleasedHandler?): Promise
local store = self._store
if unreleasedHandler == nil then
unreleasedHandler = function(_)
return Store.LoadMethods.ForceLoad
end
end
if type(unreleasedHandler) ~= "function" then
error("[DataKeep] unreleasedHandler must be a function")
end
local id = `{self._store_info.Name}/{self._store_info.Scope or ""}{self._store_info.Scope and "/" or ""}{key}`
if self._mock then
print(`[DataKeep] Using mock store on {id}`)
end
if Keeps[id] then -- TODO: check if got rejected before returning cache
return Promise.resolve(Keeps[id])
elseif self._cachedKeepPromises[id] and self._cachedKeepPromises[id].Status ~= Promise.Status.Rejected and self._cachedKeepPromises[id].Status ~= Promise.Status.Cancelled then
return self._cachedKeepPromises[id]
end
local promise = Promise.new(function(resolve, reject)
local keep: Keep.KeepStruct = store:GetAsync(key) or {} -- support versions
local forceLoad = nil
if not canLoad(keep) and keep.MetaData.ActiveSession then
local loadMethod = unreleasedHandler(keep.MetaData.ActiveSession)
if not Store.LoadMethods[loadMethod] then
warn(`[DataKeep] unreleasedHandler returned an invalid value, defaulting to {Store.LoadMethods.ForceLoad}`) -- TODO: Custom Error Class to fire to IssueSignal
loadMethod = Store.LoadMethods.ForceLoad
end
if loadMethod == Store.LoadMethods.Cancel then
reject(nil) -- should this return an error object?
return
end
if loadMethod == Store.LoadMethods.ForceLoad then
forceLoad = { PlaceID = PlaceID, JobID = JobID }
end
end
if keep.Data and len(keep.Data) > 0 and self._preLoad then
keep.Data = self._preLoad(deepCopy(keep.Data))
end
local keepClass = Keep.new(keep, self._data_template) -- why does typing break here? no idea.
keepClass._store = store -- mock store or real store
keepClass._key = key
keepClass._store_info.Name = self._store_info.Name
keepClass._store_info.Scope = self._store_info.Scope or ""
keepClass._keep_store = self
keepClass.MetaData.ForceLoad = forceLoad
keepClass.MetaData.LoadCount = (keepClass.MetaData.LoadCount or 0) + 1
self._storeQueue[key] = keepClass
saveKeep(keepClass, false)
Keeps[keepClass:Identify()] = keepClass
self._cachedKeepPromises[id] = nil
for functionName, func in self.Wrapper do
keepClass[functionName] = function(...)
return func(...)
end
end
resolve(keepClass)
end)
self._cachedKeepPromises[id] = promise
return promise
end
--[=[
@method ViewKeep
@within Store
@param key string
@param version string?
@return Promise<Keep>
Loads a Keep from the store and returns a Keep object, but doesn't save it
View only Keeps have the same functions as normal Keeps, but can not operate on data
```lua
keepStore:ViewKeep(`Player_{player.UserId}`):andThen(function(viewOnlyKeep)
print(`Viewing {viewOnlyKeep:Identify()}!`)
end)
```
]=]
function Store:ViewKeep(key: string, version: string?): Promise
return Promise.new(function(resolve)
local id = `{self._store_info.Name}/{self._store_info.Scope or ""}{self._store_info.Scope and "/" or ""}{key}`
local isFoundLoadedKeep = false
if Keeps[id] and not Keeps[id]._released then
isFoundLoadedKeep = true
end
local data
if not isFoundLoadedKeep then
data = self._store:GetAsync(key, version) or {}
else
data = { -- copy only data and basic metaData
Data = deepCopy(Keeps[id].Data),
MetaData = {
Created = Keeps[id].MetaData.Created,
LastUpdate = Keeps[id].MetaData.LastUpdate,
LoadCount = Keeps[id].MetaData.LoadCount,
},
UserIds = deepCopy(Keeps[id].UserIds),
}
end
if data.Data and len(data.Data) > 0 and self._preLoad then
data.Data = self._preLoad(deepCopy(data.Data))
end
local keepObject = Keep.new(data, self._data_template)
keepObject._view_only = true
keepObject._released = true -- incase they call :Release() and it tries to save
keepObject._store = self._store -- mock store or real store
keepObject._key = key
keepObject._store_info.Name = self._store_info.Name
keepObject._store_info.Scope = self._store_info.Scope or ""
keepObject._keep_store = self
for functionName, func in self.Wrapper do -- attach wrapper functions
keepObject[functionName] = function(...)
return func(...)
end
end
return resolve(keepObject)
end)
end
--[=[
@method PreSave
@within Store
@param callback ({ any }) -> { any: any }
Runs before saving a Keep, allowing you to modify the data before, like compressing data
:::caution
Functions **must** return a new data table. Failure to do so will result in data loss.
:::caution
:::warning
```:PreSave()``` can only be set once
:::warning
Compression example:
```lua
keepStore:PreSave(function(data)
local newData = {}
for key, value in data do
newData[key] = HttpService:JSONEncode(value)
end
return newData
end)
```
]=]
function Store:PreSave(callback: ({ any }) -> { any: any })
assert(self._preSave == nil, "PreSave can only be set once")
assert(callback and type(callback) == "function", "Callback must be a function")
self._preSave = callback
end
--[=[
@method PreLoad
@within Store
@param callback ({ any }) -> { any: any }
Runs before loading a Keep, allowing you to modify the data before, like decompressing compressed data
:::caution
Functions **must** return a new data table. Failure to do so will result in data loss.
:::caution
:::warning
```:PreLoad()``` can only be set once
:::warning
Decompression example:
```lua
keepStore:PreLoad(function(data)
local newData = {}
for key, value in data do
newData[key] = HttpService:JSONDecode(value)
end
return newData
end)
```
]=]
function Store:PreLoad(callback: ({ any }) -> { any: any })
assert(self._preLoad == nil, "PreLoad can only be set once")
assert(callback and type(callback) == "function", "Callback must be a function")
self._preLoad = callback
end
--[=[
@method PostGlobalUpdate
@within Store
@param key string
@param updateHandler (GlobalUpdates) -> nil
@return Promise<void>
Posts a global update to a Keep
```updateHandler``` reveals globalUpdates to the API
```lua
keepStore:PostGlobalUpdate(`Player_{player.UserId}`, function(globalUpdates)
globalUpdates:AddGlobalUpdate({
Hello = "World!",
}):andThen(function(updateId)
print("Added Global Update!")
end)
end)
```
]=]
function Store:PostGlobalUpdate(key: string, updateHandler: (GlobalUpdates) -> nil) -- gets passed add, lock & change functions
return Promise.new(function(resolve)
if Store.ServiceDone then
error("[DataKeep] Server is closing, can't post global update")
end
local id = `{self._store_info.Name}/{self._store_info.Scope or ""}{self._store_info.Scope and "/" or ""}{key}`
local keep = Keeps[id]
if not keep then
keep = self:ViewKeep(key):expect()
keep._global_updates_only = true
end
local globalUpdateObject = {
_updates = keep.GlobalUpdates,
_pending_removal = keep._pending_global_lock_removes,
_view_only = keep._view_only,
_global_updates_only = keep._global_updates_only,
}
setmetatable(globalUpdateObject, GlobalUpdates)
updateHandler(globalUpdateObject)
if not keep:IsActive() then
keep:Release()
end
keep.MetaData.LoadCount = (keep.MetaData.LoadCount or 0) + 1
return resolve()
end)
end
--> Global Updates
--[=[
@class GlobalUpdates
@server
Used to add, lock and change global updates
Revealed through ```PostGlobalUpdate```
]=]
--[=[
@type GlobalID number
@within GlobalUpdates
Used to identify a global update
]=]
--[=[
@method AddGlobalUpdate
@within GlobalUpdates
@param globalData {}
@return Promise<GlobalID>
Adds a global update to the Keep
```lua
globalUpdates:AddGlobalUpdate({
Hello = "World!",
}):andThen(function(updateId)
print("Added Global Update!")
end)
```
]=]
function GlobalUpdates:AddGlobalUpdate(globalData: {})
return Promise.new(function(resolve, reject)
if Store.ServiceDone then
return reject()
end
if self._view_only and not self._global_updates_only then -- shouldn't happen, fail safe for anyone trying to break the API
error("[DataKeep] Can't add global update to a view only Keep")
return reject()
end
local globalUpdates = self._updates
local updateId: number = globalUpdates.ID
updateId += 1
globalUpdates.ID = updateId
table.insert(globalUpdates.Updates, {
ID = updateId,
Locked = false,
Data = globalData,
})
return resolve(updateId)
end)
end
--[=[
@method GetActiveUpdates
@within GlobalUpdates
@return {GlobalUpdate}
Returns all **active** global updates
```lua
local updates = globalUpdates:GetActiveUpdates()
for _, update in ipairs(updates) do
print(update.Data)
end
```
]=]
function GlobalUpdates:GetActiveUpdates()
if Store.ServiceDone then
warn("[DataKeep] Server is closing, can't get active updates") -- maybe shouldn't error incase they don't :catch()?
end
if self._view_only and not self._global_updates_only then
error("[DataKeep] Can't get active updates from a view only Keep")
return {}
end
local globalUpdates = self._updates
local updates = {}
for _, update in ipairs(globalUpdates.Updates) do
if not update.Locked then
table.insert(updates, update)
end
end
return updates
end
--[=[
@method RemoveActiveUpdate
@within GlobalUpdates
@param updateId GlobalID
@return Promise<void>
Removes an active global update
```lua
local updates = globalUpdates:GetActiveUpdates()
for _, update in ipairs(updates) do
globalUpdates:RemoveActiveUpdate(update.ID):andThen(function()
print("Removed Global Update!")
end)
end
```
]=]
function GlobalUpdates:RemoveActiveUpdate(updateId: number)
return Promise.new(function(resolve, reject)
if Store.ServiceDone then
return reject()
end
if self._view_only and not self._global_updates_only then
error("[DataKeep] Can't remove active update from a view only Keep")
return {}
end
local globalUpdates = self._updates
if globalUpdates.ID < updateId then
return reject()
end
local globalUpdateIndex = nil
for i = 1, #globalUpdates.Updates do
if globalUpdates.Updates[i].ID == updateId and not globalUpdates.Updates[i].ID then
globalUpdateIndex = i
break
end
end
if globalUpdateIndex == nil then
return reject()
end
if globalUpdates.Updates[globalUpdateIndex].Locked then
error("[DataKeep] Can't remove active update on a locked update")
return reject()
end
table.remove(globalUpdates.Updates, globalUpdateIndex) -- instantly removes internally, unlike locked updates. this is because locked updates can still be deleted mid-processing
return resolve()
end)
end
--[=[
@method ChangeActiveUpdate
@within GlobalUpdates
@param updateId GlobalID
@param globalData {}
@return Promise<void>
Change an **active** global update's data to the new data.
Useful for stacking updates to save space for Keeps that maybe receiving lots of globals. Ex. A YouTuber receiving gifts
]=]
function GlobalUpdates:ChangeActiveUpdate(updateId: number, globalData: {}): Promise
return Promise.new(function(resolve, reject)
if Store.ServiceDone then
return reject()
end
if self._view_only and not self._global_updates_only then
error("[DataKeep] Can't change active update from a view only Keep")
return {}
end
local globalUpdates = self._updates
if globalUpdates.ID < updateId then
return reject()
end
for _, update in ipairs(globalUpdates.Updates) do
if update.ID == updateId and not update.Locked then
update.Data = globalData
return resolve()
end
end
return reject()
end)
end
local saveLoop
game:BindToClose(function()
Store.ServiceDone = true