diff --git a/src/MarcSync/MarcSyncv0.5.lua b/src/MarcSync/MarcSyncv0.5.lua index 82235cd..7ed2028 100644 --- a/src/MarcSync/MarcSyncv0.5.lua +++ b/src/MarcSync/MarcSyncv0.5.lua @@ -56,16 +56,16 @@ end local marcsync = {} marcsync.__index = marcsync marcsync.request = {} -function marcsync.new(token: string) +function marcsync.new(token: string):typeof(marcsync) if not token then warn("Token not provided while creating a new MarcSync Object.") end if not tokens[token] then warn("Token provided for creating a new MarcSync Object not Found in Token Table, using it as token instead.") else token = tokens[token] end local self = setmetatable({}, marcsync) self._token = token self.request._parent = self self.request.version._parent = self.request - self.request.database._parent = self.request self.request.collection._parent = self.request self.utils._parent = self + self.events = require(script.Parent.Objects.SubscriptionManager)._new(token) return self end @@ -160,25 +160,7 @@ function marcsync.request.collection:get(collectionName: string):typeof(require( if not collectionName then error("No CollectionName Provided") end return require(script.Parent.Objects.Collection)._new(collectionName, self._parent._parent._token) end -marcsync.request.database = {} -function marcsync.request.database:fetch(databaseId: number):DefaultResult - if typeof(self) ~= "table" then error("Please use : instead of .") end - if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end - self._parent._parent:_checkInstallation() - if not databaseId then error("No DatabaseId Provided") end - local result; - local success, Error = pcall(function() result = HttpService:RequestAsync({Url = "https://api.marcthedev.it/marcsync/v0/database/"..databaseId, Headers = {["Authorization"]=self._parent._parent._token}}) end) - return self._parent._parent:_requestHandler(result, Error) -end -function marcsync.request.database:delete(databaseId: number):DefaultResult - if typeof(self) ~= "table" then error("Please use : instead of .") end - if not self._parent then error("[MarcSync] Please set a Token before using MarcSync.") end - self._parent._parent:_checkInstallation() - if not databaseId then error("No DatabaseId Provided") end - local result; - local success, Error = pcall(function() result = HttpService:RequestAsync({Url = "https://api.marcthedev.it/marcsync/v0/database/"..databaseId, Headers = {["Authorization"]=self._parent._parent._token}}) end) - return self._parent._parent:_requestHandler(result, Error) -end +marcsync.events = require(script.Parent.Objects.SubscriptionManager) marcsync.utils = {} function marcsync.utils.safeRequest(method: Function, arguments: {}) --_checkInstallation() diff --git a/src/MarcSync/Objects/SubscriptionManager.lua b/src/MarcSync/Objects/SubscriptionManager.lua new file mode 100644 index 0000000..143f299 --- /dev/null +++ b/src/MarcSync/Objects/SubscriptionManager.lua @@ -0,0 +1,158 @@ +local MessagingService = game:GetService("MessagingService") +local HttpService = game:GetService("HttpService") + +type CollectionCreatedResponse = { + DatabaseId: string, + CollectionName: string, + Collection: typeof(require(script.Parent.Collection)) +} + +type CollectionUpdatedResponse = { + DatabaseId: string, + oldCollectionName: string, + newCollectionName: string, + Collection: typeof(require(script.Parent.Collection)) +} + +type CollectionDeletedResponse = { + DatabaseId: string, + CollectionName: string +} + +type DocumentCreatedResponse = { + DatabaseId: string, + CollectionName: string, + DocumentId: string, + Entry: typeof(require(script.Parent.Entry)) +} +type DocumentUpdatedResponse = { + DatabaseId: string, + CollectionName: string, + DocumentId: string, + oldEntry: typeof(require(script.Parent.Entry)), + newEntry: typeof(require(script.Parent.Entry)) +} + +type DocumentDeletedResponse = { + DatabaseId: string, + CollectionName: string, + DocumentId: string, + Entry: typeof(require(script.Parent.Entry)) +} + +local subscriptionManager = {} +subscriptionManager.__index = subscriptionManager + +function subscriptionManager:onCollectionCreated(callback: (CollectionCreatedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncCollectionCreated", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + callback({ + DatabaseId = messageTable[1], + CollectionName = messageTable[2].CollectionName, + Collection = require(script.Parent.Collection)._new(messageTable[2].CollectionName, self._token) + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager:onCollectionDeleted(callback: (CollectionDeletedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncCollectionDeleted", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + callback({ + DatabaseId = messageTable[1], + CollectionName = messageTable[2].CollectionName + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager:onCollectionUpdated(callback: (CollectionUpdatedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncCollectionUpdated", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + callback({ + DatabaseId = messageTable[1], + oldCollectionName = messageTable[2].oldCollectionName, + newCollectionName = messageTable[2].newCollectionName, + Collection = require(script.Parent.Collection)._new(messageTable[2].newCollectionName, self._token) + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager:onDocumentCreated(callback: (DocumentCreatedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncDocumentCreated", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + messageTable[2].DocumentValues = HttpService:JSONDecode(messageTable[2].DocumentValues) + messageTable[2].DocumentValues._id = messageTable[2].DocumentId + callback({ + DatabaseId = messageTable[1], + CollectionName = messageTable[2].CollectionName, + DocumentId = messageTable[2].DocumentId, + Entry = require(script.Parent.Entry)._new(messageTable[2].CollectionName, messageTable[2].DocumentValues, self._token) + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager:onDocumentUpdated(callback: (DocumentUpdatedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncDocumentUpdated", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + messageTable[2].oldDocumentValues = HttpService:JSONDecode(messageTable[2].oldDocumentValues) + messageTable[2].oldDocumentValues._id = messageTable[2].DocumentId + messageTable[2].newDocumentValues = HttpService:JSONDecode(messageTable[2].newDocumentValues) + messageTable[2].newDocumentValues._id = messageTable[2].DocumentId + callback({ + DatabaseId = messageTable[1], + CollectionName = messageTable[2].CollectionName, + DocumentId = messageTable[2].DocumentId, + oldEntry = require(script.Parent.Entry)._new(messageTable[2].CollectionName, messageTable[2].oldDocumentValues, self._token), + newEntry = require(script.Parent.Entry)._new(messageTable[2].CollectionName, messageTable[2].newDocumentValues, self._token) + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager:onDocumentDeleted(callback: (DocumentDeletedResponse) -> ()):RBXScriptConnection + if typeof(self) ~= "table" then error("Please use : instead of .") end + if not self._token then error("[MarcSync: SubscriptionManager] Invalid Object created or trying to access an destroied object.") end + local connection = MessagingService:SubscribeAsync("00MarcsyncDocumentDeleted", function(message) + local messageTable = HttpService:JSONDecode(message.Data) + messageTable[2].DocumentValues = HttpService:JSONDecode(messageTable[2].DocumentValues) + messageTable[2].DocumentValues._id = messageTable[2].DocumentId + callback({ + DatabaseId = messageTable[1], + CollectionName = messageTable[2].CollectionName, + DocumentId = messageTable[2].DocumentId, + Entry = require(script.Parent.Entry)._new(messageTable[2].CollectionName, messageTable[2].DocumentValues, self._token) + }) + end) + return self._createRBXScriptConnection(connection) +end + +function subscriptionManager._createRBXScriptConnection(subscription: RBXScriptConnection):RBXScriptConnection + local connection = {} + connection.Connected = true + connection.Disconnect = function() + subscription:Disconnect() + connection.Connected = false + end + return connection +end + +function subscriptionManager._new(_token: string) + local self = setmetatable({}, subscriptionManager) + self._token = _token + return self +end + +return subscriptionManager