Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Streaming template provider #505

Merged
merged 15 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/animationprovider/src/Shared/AnimationProvider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ local require = require(script.Parent.loader).load(script)

local TaggedTemplateProvider = require("TaggedTemplateProvider")

return TaggedTemplateProvider.new("AnimationContainer")
return TaggedTemplateProvider.new(script.Name, "AnimationContainer")
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ local require = require(script.Parent.loader).load(script)

local TemplateProvider = require("TemplateProvider")

return TemplateProvider.new(script)
return TemplateProvider.new(script.Name, script)
41 changes: 40 additions & 1 deletion src/loader/src/Replication/Replicator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ function Replicator:_doStandardReplication(maid, replicator, child, copy)
assert(typeof(child) == "Instance", "Bad child")

self:_setupAttributeReplication(maid, child, copy)
self:_setupTagReplication(maid, child, copy)
self:_setupNameReplication(maid, child, copy)
self:_setupParentReplication(maid, copy)
self:_setupReference(maid, child, copy)
Expand Down Expand Up @@ -467,6 +468,44 @@ function Replicator:_setupParentReplication(maid, copy)
copy.Parent = self._target.Value
end

--[[
Sets up tag replication explicitly.

@param maid Maid
@param child Instance
@param copy Instance
]]
function Replicator:_setupTagReplication(maid, child, copy)
assert(Maid.isMaid(maid), "Bad maid")
assert(typeof(child) == "Instance", "Bad child")
assert(typeof(copy) == "Instance", "Bad copy")

for _, tag in pairs(child:GetTags()) do
copy:AddTag(tag)
end

maid:GiveTask(child.Changed:Connect(function(property)
if property == "Tags" then
local ourTagSet = {}
for _, tag in pairs(copy:GetTags()) do
ourTagSet[tag] = true
end

for _, tag in pairs(child:GetTags()) do
if not ourTagSet[tag] then
copy:AddTag(tag)
end

ourTagSet[tag] = nil
end

for tag, _ in pairs(ourTagSet) do
copy:RemoveTag(tag)
end
end
end))
end

--[[
Sets up the object value replication to point towards new values.

Expand Down Expand Up @@ -528,7 +567,7 @@ end

function Replicator:_setupAttributeReplication(maid, child, copy)
for key, value in pairs(child:GetAttributes()) do
child:SetAttribute(key, value)
copy:SetAttribute(key, value)
end

maid:GiveTask(child.AttributeChanged:Connect(function(attribute)
Expand Down
6 changes: 5 additions & 1 deletion src/loader/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ function Loader:_findDependency(request)
-- Just standard dependency search
local foundBackup = DependencyUtils.findDependency(self._packages, request, self._replicationType)
if foundBackup then
warn(string.format("[Loader] - Failed to find package %q in package tracker of root %s\n%s", request, self._packages:GetFullName(), debug.traceback()))
if packageTracker then
warn(string.format("[Loader] - No package tracker for root %s (while loading %s)\n%s", self._packages:GetFullName(), request, debug.traceback()))
else
warn(string.format("[Loader] - Failed to find package %q in package tracker of root %s\n%s", request, self._packages:GetFullName(), debug.traceback()))
end

-- Ensure hoarcekat story has a link to use
-- TODO: Maybe add to global package cache instead...
Expand Down
28 changes: 18 additions & 10 deletions src/observablecollection/src/Shared/ObservableCountingMap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ end
]=]
function ObservableCountingMap:ObserveKeysList()
return self:_observeDerivedDataStructureFromKeys(function()
local list = {}
local list = table.create(self._totalKeyCountValue.Value)

for key, _ in pairs(self._map) do
table.insert(list, key)
Expand Down Expand Up @@ -184,22 +184,28 @@ end
function ObservableCountingMap:ObserveKeysBrio()
return Observable.new(function(sub)
local maid = Maid.new()
local keyMaid = maid:Add(Maid.new())

local function handleItem(key)
-- Happens upon key added re-entrance
if keyMaid[key] then
return
end

local brio = Brio.new(key)
maid[key] = brio
keyMaid[key] = brio
sub:Fire(brio)
end

for key, _ in pairs(self._map) do
handleItem(key)
end

maid:GiveTask(self.KeyAdded:Connect(handleItem))
maid:GiveTask(self.KeyRemoved:Connect(function(key)
maid[key] = nil
keyMaid[key] = nil
end))

for key, _ in pairs(self._map) do
handleItem(key)
end

self._maid[sub] = maid
maid:GiveTask(function()
self._maid[sub] = nil
Expand Down Expand Up @@ -286,8 +292,10 @@ function ObservableCountingMap:Add(key, amount)
return
end

if self._map[key] then
local newValue = self._map[key] + amount
local oldValue = self._map[key]

if oldValue then
local newValue = oldValue + amount
if newValue == 0 then
-- Remove item
self._map[key] = nil
Expand Down Expand Up @@ -363,7 +371,7 @@ end
@return { T }
]=]
function ObservableCountingMap:GetKeyList()
local list = {}
local list = table.create(self._totalKeyCountValue.Value)
for key, _ in pairs(self._map) do
table.insert(list, key)
end
Expand Down
5 changes: 5 additions & 0 deletions src/observablecollection/src/Shared/ObservableList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ end
Observes the current value at a given index. This can be useful for observing
the first entry, or matching stuff up to a given slot.

```
list:ObserveAtIndex(1):Subscribe(print) --> prints first item
list:ObserveAtIndex(-1):Subscribe(print) --> prints last item
```

@param indexToObserve number
@return Observable<T?>
]=]
Expand Down
68 changes: 68 additions & 0 deletions src/observablecollection/src/Shared/ObservableMapList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,54 @@ function ObservableMapList:Push(observeKey, entry)
return maid
end

--[=[
Gets the first item for the given key

@param key TKey
@return TValue | nil
]=]
function ObservableMapList:GetFirstItemForKey(key)
assert(key ~= nil, "Bad key")

local observableList = self:GetListForKey(key)
if not observableList then
return nil
end

return observableList:Get(1)
end

--[=[
Gets the item for the given key at the index

```
mapList:Push("fruits", "apple")
mapList:Push("fruits", "orange")
mapList:Push("fruits", "banana")

-- Print the last item
print(mapList:GetItemForKeyAtIndex("fruits", -1)) ==> banana
```

@param key TKey
@param index number
@return TValue | nil
]=]
function ObservableMapList:GetItemForKeyAtIndex(key, index)
assert(key ~= nil, "Bad key")
assert(type(index) == "number", "Bad index")

local observableList = self:GetListForKey(key)
if not observableList then
return nil
end

return observableList:Get(index)
end

--[=[
Gets how many lists exist

@return number
]=]
function ObservableMapList:GetListCount()
Expand All @@ -102,6 +148,7 @@ end

--[=[
Observes how many lists exist

@return Observable<number>
]=]
function ObservableMapList:ObserveListCount()
Expand Down Expand Up @@ -244,6 +291,27 @@ function ObservableMapList:GetListForKey(key)
return self._observableMapOfLists:Get(key)
end

--[=[
Gets a list of all of the entries at the given index, if it exists

@param index number
@return ObservableList<TValue>
]=]
function ObservableMapList:GetListOfValuesAtListIndex(index)
assert(type(index) == "number", "Bad index")

local list = table.create(self._observableMapOfLists:GetCount())

for _, observableList in pairs(self._observableMapOfLists:GetValueList()) do
local value = observableList:Get(index)
if value ~= nil then
table.insert(list, value)
end
end

return list
end

--[=[
Observes the observable list for the given key

Expand Down
2 changes: 1 addition & 1 deletion src/observablecollection/src/Shared/ObservableMapSet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ end
Gets a list of all keys.
@return { TKey }
]=]
function ObservableMapSet:ObserveKeyList()
function ObservableMapSet:GetKeyList()
return self._observableMapOfSets:GetKeyList()
end

Expand Down
12 changes: 9 additions & 3 deletions src/observablecollection/src/Shared/ObservableSet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,26 @@ function ObservableSet:ObserveItemsBrio()
local maid = Maid.new()

local function handleItem(item)
if maid[item] then
-- Happens when we're re-entrance
return
end

local brio = Brio.new(item)
maid[item] = brio
sub:Fire(brio)
end

for item, _ in pairs(self._set) do
handleItem(item)
end

maid:GiveTask(self.ItemAdded:Connect(handleItem))
maid:GiveTask(self.ItemRemoved:Connect(function(item)
maid[item] = nil
end))

for item, _ in pairs(self._set) do
handleItem(item)
end

self._maid[sub] = maid
maid:GiveTask(function()
self._maid[sub] = nil
Expand Down
Loading
Loading