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,47 @@ 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+
73+ --[=[
74+ Promises a graceful close session notification to other sessions.
75+
76+ @param placeId number
77+ @param jobId string
78+ @param sessionId string
79+ @return Promise<()>
80+ ]=]
6381function DataStoreMessageHelper .PromiseCloseSessionGraceful (
6482 self : DataStoreMessageHelper ,
83+ placeId : number ,
84+ jobId : string ,
6585 sessionId : string
6686): Promise .Promise < ()>
6787 local promise : any = self ._sessionClosedNotifications [sessionId ]
@@ -84,16 +104,27 @@ function DataStoreMessageHelper.PromiseCloseSessionGraceful(
84104 end ))
85105 end )
86106
87- return self :PromiseMessage ( sessionId , {
107+ return self :PromiseSendSessionMessage ( placeId , jobId , sessionId , {
88108 type = " close-session" ,
89109 requesterSessionId = self ._dataStore :GetSessionId (),
90110 }):Then (function ()
91111 return promise
92112 end )
93113end
94114
95- function DataStoreMessageHelper .PromiseMessage (
115+ --[=[
116+ Promises sending a message to another DataStore session.
117+
118+ @param placeId number
119+ @param jobId string
120+ @param sessionId string
121+ @param message DataStoreMessage
122+ @return Promise<()>
123+ ]=]
124+ function DataStoreMessageHelper .PromiseSendSessionMessage (
96125 self : DataStoreMessageHelper ,
126+ placeId : number ,
127+ jobId : string ,
97128 sessionId : string ,
98129 message : DataStoreMessage
99130): Promise .Promise < ()>
@@ -103,14 +134,15 @@ function DataStoreMessageHelper.PromiseMessage(
103134 print (" [DataStoreMessageHelper] - Sending message:" , MessagingServiceUtils .toHumanReadable (message ))
104135 end
105136
106- return self ._maid :GivePromise (MessagingServiceUtils .promisePublish (self :_getActiveStoreTopic (sessionId ), message ))
137+ local topic = self :_getActiveStoreTopic (sessionId )
138+ return self ._maid :GivePromise (self ._placeMessagingService :SendMessage (placeId , jobId , topic , message ))
107139end
108140
109141function DataStoreMessageHelper ._handleSubscription (
110142 self : DataStoreMessageHelper ,
111- subscriptionData : MessagingServiceUtils.SubscriptionData
112- )
113- local data = subscriptionData . Data
143+ data : any ,
144+ metadata : PlaceMessagingService.PlacePacketMetadata
145+ ): ()
114146 if type (data ) ~= " table" or type (data .type ) ~= " string" then
115147 warn (` [DataStoreMessageHelper] - Received malformed message: {MessagingServiceUtils.toHumanReadable(data)}` )
116148 return
@@ -129,13 +161,10 @@ function DataStoreMessageHelper._handleSubscription(
129161
130162 closeSessionPromise :Then (function ()
131163 -- 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- )
164+ self ._placeMessagingService :SendMessageToAddress (metadata .from , topic , {
165+ type = " close-session-complete" ,
166+ senderId = senderId ,
167+ })
139168 end )
140169 end
141170 elseif data .type == " close-session-complete" then
0 commit comments