@rbxts/yield-for-tree

Yield until all members of a specified tree exist, then dot access!

Usage no npm install needed!

<script type="module">
  import rbxtsYieldForTree from 'https://cdn.skypack.dev/@rbxts/yield-for-tree';
</script>

README

yield-for-tree

Returns a yieldForTree function, which can be used like so:

Demo

yieldForTree(game.GetService("Workspace"), {
    $className: "Workspace",

    SpawnLocation: {
        $className: "SpawnLocation",

        Decal: {
            $className: "Decal",

            Value: {
                $className: "IntValue",
                Folder: "Folder",
            },
        },
    },
} as const).then(workspace => {
    print(++workspace.SpawnLocation.Decal.Value.Value);
    const count = workspace.SpawnLocation.Decal.Value.Clone();
    count.Parent = workspace;

    // Members are preserved by the `Clone` method!
    print((count.Folder.Name = "Soup"), ++count.Value);
});

The first parameter must be an Instance (or extend from it). The second parameter must be an object tree similar to ones considered valid by Rojo. Every tree must have a $className member, and can have any number of keys which represent the name of a child instance, which should have a corresponding value which is this same kind of tree. There is also a shorthand syntax, seen above with Folder: "Folder", which is equivalent to Folder: { $className: "Folder" }.

Note: Currently, the as const may be necessary to preserve the true type of the object tree. Your types will not work if you do not use the tree object in-line or declare it with as const after it.

This library also exports EvaluateInstanceTree if you want to use it for your own nefarious purposes.

Note: This library doesn't mutate/modify anything, it only yields until the tree is fully detected.

Also see validate-tree