GithubHelp home page GithubHelp logo

ddavness / roblox-object-event Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 193 KB

Roblox-Like events for custom OOP implementations, without resorting to BindableEvent. Targeting readability.

License: MIT License

Lua 100.00%
roblox roblox-ts rbxts oop event

roblox-object-event's Introduction

@rbxts/object-event

npm version

This NPM Package for Roblox-TS allows developers to implement custom Events for custom classes, without resorting to BindableEvents.

This should be particularly useful for projects that heavily rely on the OOP paradigm for libraries and other game components.

The module can be used both in pure Lua(u) scripts (via Rojo or any other way) or with Roblox-TS (includes compile-time type-checking).

Example

/////// Module.ts ///////

import ObjectEvent from "@rbxts/object-event"

const event = new ObjectEvent<[number, String, Vector3]>()

event.Connect((id, msg, position) => {
    // We can safely assume that:
    // id is a number
    // msg is a string
    // position is a Vector3
})

export event


/////// Waiter.server.ts ///////

import {event} from "./Module"

event.Fire(10, "oof", new Vector3(1, 2, 3))  // all good
event.Fire(10, "oof")                        // will not compile!

export {}

To allow all kinds of arguments, of any number:

let event = new ObjectEvent<[...unknown]>()

ObjectEvent API

  • Connect(f) - f is a function that takes the arguments typed accordingly and returns void. Returns an ObjectEventConnection
  • Wait() - yields the thread until the event is fired. Returns the values typed accordingly.
  • Fire(...) - fires the event. Arguments must be the same number and type of the event.
  • Event - an RBXScriptSignal-like interface supporting the Connect() and Wait(), just in case you prefer to use it as a BindableEvent.
  • SubscribedConnections - an array of ObjectEventConnections with all the connections currently listening to the event.

ObjectEventConnection API

  • Disconnect() - disconnects from the event
  • Reconnect() - reverts a disconnection
  • IsConnected() - returns true if the connection is listening to the event (this is, not Disconnect()'ed), false otherwise.
  • Event - the ObjectEvent associated with this connection
  • Listener - the function associated with this connection

Current Features:

  • Standard Roblox Event Feature Set:
    • Connecting to an event;
    • Disconnecting a connection;
    • Yielding a thread until the event is fired;
    • Passing arguments through the functions;
  • Custom features:
    • Reconnecting a disconnected connection without having to make a new one (ability to reuse connections).
    • Reading all active connections for a given event.

Goals:

  • Abstraction from internals;
  • Roblox-like syntax while keeping a minimum of decency for TypeScript syntax;
  • Code readability for OOP implementations.
  • Complement Roblox's implementation with extra (useful) features.

roblox-object-event's People

Contributors

ashleydavies avatar ddavness avatar tacheometry avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

ashleydavies

roblox-object-event's Issues

Event Type checking

In the future, we should be able to allow developers to enforce a specific type when declaring an event:

let event = new ObjectEvent<funcType>() // Is this valid?

So that functions being connected to said event must to take the argument types defined within the event:

let event = new ObjectEvent<(a: number, b: number) => void>()

// ERROR!
event.Connect(function(a: string) {
    print(a)
})

So the tackling goes as following:

  • Verify whether this is possible;
  • Investigate the syntax for this;
  • Implement!

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.