Coder Social home page Coder Social logo

observe's Introduction

Why observe

Simple

Compared to many other reactive libraries, observe has a relatively small scope and straightforward API usage, allowing you to leverage it in your code with very minimal learning curve.

-- Make everything with the "Red" tag red.
Observe.Tagged("Red", function(instance)
    instance.Color = Color3.new(1, 0, 0)
end)

Reactive

Roblox is fundamentally built around instances with fixed lifetimes, and the events they emit. Observe complements this structure well by allowing your code to observe properties, and stop observing them when they are no longer relevant.

-- Start observing the name of workspace.Part
local stopObserving = Observe.Attribute(workspace.Part, "Name" function(name)
    print("The part's name is:", name)
end)

-- Stop the observation after 10 seconds.
task.delay(10, function()
    stopObserving()
end)

Cleaner Cleanup

In many cases, it is desirable to be able to have code happen when a specific observation ends as well. All observers in observe allow a cleanup function to be returned to be ran when the observation ends.

Observe.Players(function(player)
    print(player.Name, "is in the game!")

    return function()
        print(player.Name, "has left the game!")
    end
end)

Additionally, every observer returns a cleanup function, allowing the observer to stop all observations, and stop looking for new ones.

-- Make every child of the workspace blue
local stopObserving = Observe.Children(workspace, function(part)
    if not part:IsA("BasePart") then
        return
    end

    local previousColor = part.Color
    part.Color = Color3.new(0, 0, 0)

    -- Revert the color back when the part is removed from the observer
    return function()
        part.Color = previousColor
    end
end)

-- Stop the observer, reverting all parts back to their original color
task.delay(10, function()
    stopObserving()
end)

Type-safe

Observe's API is 100% statically typed using luau's type system.

Installation

Add the latest installation of observe to your wally.toml:

iter = "4812571/[email protected]

observe's People

Contributors

4812571 avatar hexcede avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.