function DataStore.Load(m,f,G)return(m:_promiseLoad()):Then(function(q)return m:_afterLoadGetAndApplyStagedData(f,q,G)end)end function DataStore._saveData(G,f)local m=Maid.new()local q=Promise.new()q:Resolve(m:GivePromise(DataStorePromises.updateAsync(G._robloxDataStore,G._key,function(G)if q:IsRejected()then return nil end G=f:WriteMerge(G or{})assert(G~=DataStoreDeleteToken,"\067\097\110\110\111\116 \100\101\108\101\116\101 \102\114\111\109 \085\112\100\097\116\101\065\115\121\110\099")if DEBUG_WRITING then print("\091\068\097\116\097\083\116\111\114\101\093 - \087\114\105\116\105\110\103",(game:GetService("\072\116\116\112\083\101\114\118\105\099\101")):JSONEncode(G))end return G end,function(f)warn("\091\068\097\116\097\083\116\111\114\101\093 - \070\097\105\108\101\100 \116\111 \085\112\100\097\116\101\065\115\121\110\099 \100\097\116\097",f)return Promise.rejected(f)end)))G._maid._saveMaid=m if G.Saving.Destroy then G.Saving:Fire(q)end return q end function DataStore._promiseLoad(f)if f._loadPromise then return f._loadPromise end f._loadPromise=f._maid:GivePromise((DataStorePromises.getAsync(f._robloxDataStore,f._key)):Then(function(f)if f==nil then return{}elseif type(f)=="\116\097\098\108\101"then return f else return Promise.rejected("\070\097\105\108\101\100 \116\111 \108\111\097\100 \100\097\116\097\046 \087\114\111\110\103 \116\121\112\101 \'"..(type(f).."\'"))end end,function(f)warn("\091\068\097\116\097\083\116\111\114\101\093 - \070\097\105\108\101\100 \116\111 \071\101\116\065\115\121\110\099 \100\097\116\097",f)return Promise.rejected(f)end))return f._loadPromise end return DataStore
--[=[
	Loads data. This returns the originally loaded data.
	@param keyName string
	@param defaultValue any?
	@return any?
]=]
function DataStore:Load(keyName, defaultValue)
	return self:_promiseLoad():Then(function(data)
		return self:_afterLoadGetAndApplyStagedData(keyName, data, defaultValue)
	end)
end

function DataStore:_saveData(writer)
	local maid = Maid.new()

	local promise = Promise.new()
	promise:Resolve(maid:GivePromise(DataStorePromises.updateAsync(self._robloxDataStore, self._key, function(data)
		if promise:IsRejected() then
			-- Cancel if we have another request
			return nil
		end

		data = writer:WriteMerge(data or {})
		assert(data ~= DataStoreDeleteToken, "Cannot delete from UpdateAsync")

		if DEBUG_WRITING then
			print("[DataStore] - Writing", game:GetService("HttpService"):JSONEncode(data))
		end

		return data
	end, function(err)
		-- Might be caused by Maid rejecting state
		warn("[DataStore] - Failed to UpdateAsync data", err)
		return Promise.rejected(err)
	end)))

	self._maid._saveMaid = maid

	if self.Saving.Destroy then
		self.Saving:Fire(promise)
	end

	return promise
end

function DataStore:_promiseLoad()
	if self._loadPromise then
		return self._loadPromise
	end

	self._loadPromise =
		self._maid:GivePromise(DataStorePromises.getAsync(self._robloxDataStore, self._key):Then(function(data)
			if data == nil then
				return {}
			elseif type(data) == "table" then
				return data
			else
				return Promise.rejected("Failed to load data. Wrong type '" .. type(data) .. "'")
			end
		end, function(err)
			-- Log:
			warn("[DataStore] - Failed to GetAsync data", err)
			return Promise.rejected(err)
		end))

	return self._loadPromise
end

return DataStore
