Observe - Easily observe objects that are not yet streamed in

Observe

Make streaming objects/values easier with Observe by simply observing them using a path.
Utilizes Dumpster & Promise

You can use Observe in the following ways.

Download From Toolbox

OR

Wally Dependency

Observe = “kinqandi/observe@1.0.3”
Source

Functions

--@return Promise
.Get(object: Instance | table, path: string, timeout: number?, customPathSeperator: string?)

--@return Promise
.PrimaryPart(object: Instance | table, path: string, timeout: number?, customPathSeperator: string?)

--@return Promise
.Descendant(parentObject: Instance, objectName: string, timeout: number?, className: string?)

--@return Promise
.Bulk(...) 

Notes:

  • Observe(...) = Observe.Get(...)
  • If no timeout is passed, it will keep on observing forever.
  • To cancel an Observer you can simply call :cancel()
  • You can change the path format by passing a customPathSeperator parameter.

If you find this useful / have feedback / recommendations let me know!

Tutorials & Examples

.Get

-- Let's say I want to observe for a spawn location with this hiearchy
-- workspace.MyBase.SpawnLocation
-- I can do the following:
Observe(workspace, "MyBase.SpawnLocation"):andThen(function(mySpawnLocation)
	warn(mySpawnLocation) --Hooray, we got our spawn location!
end)
--we are passing nil for timeout for this example, but changing the path seperator to /
Observe(workspace, "MyBase/SpawnLocation", nil, "/"):andThen(function(mySpawnLocation)
	warn(mySpawnLocation) --Hooray, we got our spawn location!
end)
--In this example the timeout is set to 10 seconds, 
-- meaning that after 10 seconds if workspace.MyBase.SpawnLocation is not found
-- it will reject it.
Observe(workspace, "MyBase/SpawnLocation", 10, "/"):andThen(function(mySpawnLocation)
	warn(mySpawnLocation) --Hooray, we got our spawn location!
end):catch(function(errorMessage)
	warn(errorMessage) -- oh no, the observer failed!
end)
-- Let's say we have a table that holds some references.
-- In this example I want to retrieve the "Display" part of my atm that is referenced in this table
-- 2 seconds after this piece of code runs
local t = {
	cash = 10,
}

--But I want to observe the "Display" part of atm1, I can do the following:
Observe(t, "atms.atm1.Display"):andThen(function(display)
	warn("Got the display board!")
end)

task.delay(2, function()
	t.atms = {
		atm1 = workspace.ATM1,
	}
end)

Now let’s say I have a table and I am trying to stream for items inside of said table
I can easily do so by the following:

local someTable = {}

--Will begin observing for "Currency.Cash" inside someTable.
Observe(someTable, "Currency.Cash"):andThen(function(cash)
	warn("Got Cash: ", cash)
end)

--Let's wait 2 seconds before we assign the Currency table to "someTable"
task.delay(2, function()
	someTable.Currency = {
		Cash = 10
	}
end)

.PrimaryPart

Now let’s say we want to observe for a primary part of a certain model. In this example, we will observe for the humanoid root part of my character.

Observe.PrimaryPart(workspace, "KinqAndi"):andThen(function(humanoidRootPart)
	warn(humanoidRootPart) -- woohoo hrp was streamed in!
end) 

.Descendant

--this will observe the entire workspace and every descendant matching the name "SomePartInWorkspace"
Observe.Descendant(workspace, "SomePartInWorkspace"):andThen(function(thePart)
	warn(thePart) -- Got SomePartInWorkspace
end) 

--same as above except this observer has a timeout of 10 seconds
-- and only will hunt for the ClassName of "Part" that matches
-- the name of "SomePartInWorkspace"
Observe.Descendant(workspace, "SomePartInWorkspace", 10, "Part"):andThen(function(thePart)
	warn(thePart) -- Got SomePartInWorkspace
end) 

.Bulk

local baseplateObserver = Observe(workspace, "Baseplate")
local partAObserver = Observer(workspace, "PartA")
local partBObserver = Observer(workspace, "PartB")

Observer.Bulk(baseplateObserver, partAObserver, partBObserver):andThen(function(basePlate, partA, partB)
	warn(basePlate, partA, partB) -- all of the observers have been resolved!
end)

Thank you for checking it out!

5 Likes

Wow this is super epic. I can’t wait to use this and my favorite package dumpster to totally streamline my development experience!

2 Likes