function Store.new(P,z,k)assert(typeof(P)=="\102\117\110\099\116\105\111\110","\066\097\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \083\116\111\114\101\046\110\101\119\044 \101\120\112\101\099\116\101\100 \102\117\110\099\116\105\111\110\046")assert(k==nil or typeof(k)=="\116\097\098\108\101","\066\097\100 \097\114\103\117\109\101\110\116 \035\051 \116\111 \083\116\111\114\101\046\110\101\119\044 \101\120\112\101\099\116\101\100 \110\105\108 \111\114 \116\097\098\108\101\046")local B={}B._reducer=P B._state=P(z,{type="\064\064\073\078\073\084"})B._lastState=B._state B._mutatedSinceFlush=false B._connections={}B.changed=Signal.new()setmetatable(B,Store)local a=B._flushEvent:Connect(function()B:flush()end)table.insert(B._connections,a)if k then local P=B.dispatch local z=function(...)return P(B,...)end for P=#k,1,-1 do local a=k[P]z=a(z,B)end B.dispatch=function(P,...)return z(...)end end return B end

function Store.new(reducer, initialState, middlewares)
	assert(typeof(reducer) == "function", "Bad argument #1 to Store.new, expected function.")
	assert(middlewares == nil or typeof(middlewares) == "table", "Bad argument #3 to Store.new, expected nil or table.")

	local self = {}

	self._reducer = reducer
	self._state = reducer(initialState, {
		type = "@@INIT",
	})
	self._lastState = self._state

	self._mutatedSinceFlush = false
	self._connections = {}

	self.changed = Signal.new()

	setmetatable(self, Store)

	local connection = self._flushEvent:Connect(function()
		self:flush()
	end)
	table.insert(self._connections, connection)

	if middlewares then
		local unboundDispatch = self.dispatch
		local dispatch = function(...)
			return unboundDispatch(self, ...)
		end

		for i = #middlewares, 1, -1 do
			local middleware = middlewares[i]
			dispatch = middleware(dispatch, self)
		end

		self.dispatch = function(self, ...)
			return dispatch(...)
		end
	end

	return self
end
