local E=(script:FindFirstAncestor("\067\109\100\114\065\100\100\105\116\105\111\110\115")).Packages local Y=require(E.Util)local O={}O.__index=O function O.new(E,x)return setmetatable({Name=E,History={},_historyLimit=x or 500;Subscribers={};_restrictors={};Event=Y.Event.new()},O)end function O.AddRestrictor(Y,E)table.insert(Y._restrictors,E)end function O.CanPlayerAccess(Y,E)for Y,O in ipairs(Y._restrictors)do if not O(E)then return false end end return true end function O.AddSubscriber(Y,E)Y:RemoveSubscriber(E)table.insert(Y.Subscribers,E)end function O.RemoveSubscriber(Y,E)local O=table.find(Y.Subscribers,E)if O then table.remove(Y.Subscribers,O)end end function O.Push(Y,E)Y.Event:fire(E)table.insert(Y.History,E)if#Y.History>Y._historyLimit then for E=1,#Y.History-Y._historyLimit,1 do table.remove(Y.History,1)end end end return O
local Packages = script:FindFirstAncestor("CmdrAdditions").Packages

local Util = require(Packages.Util)

local Stream = {}
Stream.__index = Stream

function Stream.new(name, historyLimit)
	return setmetatable({
		Name = name,

		History = {},
		_historyLimit = historyLimit or 500,
		
		Subscribers = {},
		_restrictors = {},

		Event = Util.Event.new()
	}, Stream)
end

function Stream:AddRestrictor(restrictor)
	table.insert(self._restrictors, restrictor)
end

function Stream:CanPlayerAccess(player)
	for _, restrictor in ipairs(self._restrictors) do
		if not restrictor(player) then
			return false
		end
	end

	return true
end

function Stream:AddSubscriber(player)
	self:RemoveSubscriber(player)
	table.insert(self.Subscribers, player)
end

function Stream:RemoveSubscriber(player)
	local index = table.find(self.Subscribers, player)
	if index then
		table.remove(self.Subscribers, index)
	end
end

function Stream:Push(event)
	self.Event:fire(event)

	table.insert(self.History, event)
	
	if #self.History > self._historyLimit then
		for _ = 1, #self.History - self._historyLimit do
			table.remove(self.History, 1)
		end
	end
end

return Stream