11-- !strict
2-
32--[=[
43 @class DataStoreMessageHelper
54]=]
@@ -8,8 +7,10 @@ local require = require(script.Parent.loader).load(script)
87
98local BaseObject = require (" BaseObject" )
109local MessagingServiceUtils = require (" MessagingServiceUtils" )
10+ local PlaceMessagingService = require (" PlaceMessagingService" )
1111local Promise = require (" Promise" )
1212local PromiseMaidUtils = require (" PromiseMaidUtils" )
13+ local ServiceBag = require (" ServiceBag" )
1314
1415local DEBUG_LOG = false
1516
@@ -21,6 +22,8 @@ export type DataStoreMessageHelper =
2122 typeof (setmetatable (
2223 {} :: {
2324 _dataStore : any ,
25+ _serviceBag : ServiceBag .ServiceBag ,
26+ _placeMessagingService : PlaceMessagingService .PlaceMessagingService ,
2427 _sessionClosedNotifications : { [string ]: Promise .Promise < ()> },
2528 },
2629 {} :: typeof ({ __index = DataStoreMessageHelper })
@@ -38,30 +41,39 @@ export type CloseSessionComplete = {
3841
3942export type DataStoreMessage = CloseSessionRequest | CloseSessionComplete
4043
41- function DataStoreMessageHelper .new (dataStore : any ): DataStoreMessageHelper
44+ function DataStoreMessageHelper .new (serviceBag : ServiceBag.ServiceBag , dataStore : any ): DataStoreMessageHelper
4245 local self : DataStoreMessageHelper = setmetatable (BaseObject .new () :: any , DataStoreMessageHelper )
4346
47+ self ._serviceBag = assert (serviceBag , " No serviceBag" )
48+ self ._placeMessagingService = self ._serviceBag :GetService (PlaceMessagingService :: any )
49+
4450 self ._dataStore = assert (dataStore , " No dataStore" )
4551 self ._sessionClosedNotifications = {}
4652
47- -- Explicitly don't give to maid so wecan disconnect the subscription before destroying
48- MessagingServiceUtils .promiseSubscribe (self :_getActiveStoreTopic (self ._dataStore :GetSessionId ()), function (data )
49- self :_handleSubscription (data )
50- end ):Then (function (subscription )
51- if not self .Destroy then
52- subscription :Disconnect ()
53- end
54-
55- self ._maid :GiveTask (function ()
56- subscription :Disconnect ()
57- end )
58- end )
53+ self ._maid :GiveTask (
54+ self ._placeMessagingService
55+ :ObserveMessages (self :_getActiveStoreTopic (self ._dataStore :GetSessionId ()))
56+ :Subscribe (function (data , metadata )
57+ self :_handleSubscription (data , metadata )
58+ end )
59+ )
5960
6061 return self
6162end
6263
64+ --[=[
65+ Returns the service bag used by this helper. (Used for comparison)
66+
67+ @return ServiceBag
68+ ]=]
69+ function DataStoreMessageHelper .GetServiceBag (self : DataStoreMessageHelper ): ServiceBag .ServiceBag
70+ return self ._serviceBag
71+ end
72+
6373function DataStoreMessageHelper .PromiseCloseSessionGraceful (
6474 self : DataStoreMessageHelper ,
75+ placeId : number ,
76+ jobId : string ,
6577 sessionId : string
6678): Promise .Promise < ()>
6779 local promise : any = self ._sessionClosedNotifications [sessionId ]
@@ -84,7 +96,7 @@ function DataStoreMessageHelper.PromiseCloseSessionGraceful(
8496 end ))
8597 end )
8698
87- return self :PromiseMessage (sessionId , {
99+ return self :PromiseMessage (placeId , jobId , sessionId , {
88100 type = " close-session" ,
89101 requesterSessionId = self ._dataStore :GetSessionId (),
90102 }):Then (function ()
94106
95107function DataStoreMessageHelper .PromiseMessage (
96108 self : DataStoreMessageHelper ,
109+ placeId : number ,
110+ jobId : string ,
97111 sessionId : string ,
98112 message : DataStoreMessage
99113): Promise .Promise < ()>
@@ -103,14 +117,15 @@ function DataStoreMessageHelper.PromiseMessage(
103117 print (" [DataStoreMessageHelper] - Sending message:" , MessagingServiceUtils .toHumanReadable (message ))
104118 end
105119
106- return self ._maid :GivePromise (MessagingServiceUtils .promisePublish (self :_getActiveStoreTopic (sessionId ), message ))
120+ local topic = self :_getActiveStoreTopic (sessionId )
121+ return self ._maid :GivePromise (self ._placeMessagingService :SendMessage (placeId , jobId , topic , message ))
107122end
108123
109124function DataStoreMessageHelper ._handleSubscription (
110125 self : DataStoreMessageHelper ,
111- subscriptionData : MessagingServiceUtils.SubscriptionData
112- )
113- local data = subscriptionData . Data
126+ data : any ,
127+ metadata : PlaceMessagingService.PlacePacketMetadata
128+ ): ()
114129 if type (data ) ~= " table" or type (data .type ) ~= " string" then
115130 warn (` [DataStoreMessageHelper] - Received malformed message: {MessagingServiceUtils.toHumanReadable(data)}` )
116131 return
@@ -129,13 +144,10 @@ function DataStoreMessageHelper._handleSubscription(
129144
130145 closeSessionPromise :Then (function ()
131146 -- We could have GCed by now, but try to send off a notification to the requester
132- MessagingServiceUtils .promisePublish (
133- topic ,
134- {
135- type = " close-session-complete" ,
136- senderId = senderId ,
137- } :: CloseSessionComplete
138- )
147+ self ._placeMessagingService :SendMessageToAddress (metadata .from , topic , {
148+ type = " close-session-complete" ,
149+ senderId = senderId ,
150+ })
139151 end )
140152 end
141153 elseif data .type == " close-session-complete" then
0 commit comments