|
| 1 | +--[=[ |
| 2 | + @class BindableEventWrapper |
| 3 | +
|
| 4 | + A wrapper around a [BindableEvent] that exposes a similar API to that of a normal signal library. |
| 5 | +
|
| 6 | + Please note that this has the same [limitations](https://create.roblox.com/docs/scripting/events/bindable#argument-limitations) as a regular BindableEvent. |
| 7 | +
|
| 8 | + ```lua |
| 9 | + local roundEnded = BindableEventWrapper.new() |
| 10 | +
|
| 11 | + foodEaten:Connect(function(winner) |
| 12 | + print(winner .. " won the round!") |
| 13 | + end) |
| 14 | +
|
| 15 | + foodEaten:Fire("Jerry") |
| 16 | + ``` |
| 17 | +]=] |
| 18 | +local BindableEventWrapper = {} |
| 19 | +BindableEventWrapper.__index = BindableEventWrapper |
| 20 | + |
| 21 | +type BindableEventWrapper = typeof(setmetatable({} :: {}, BindableEventWrapper)) |
| 22 | + |
| 23 | +--[=[ |
| 24 | + @prop _internal BindableEvent |
| 25 | + @private |
| 26 | + @within BindableEventWrapper |
| 27 | +
|
| 28 | + The underlying bindable event used by the class |
| 29 | +]=] |
| 30 | + |
| 31 | +--[=[ |
| 32 | + @within BindableEventWrapper |
| 33 | + @param event BindableEvent? -- If this is not nil, the wrapper will wrap the provided [BindableEvent] instead of creating a new one. |
| 34 | +
|
| 35 | + Instantiates a new BindableEventWrapper. |
| 36 | +]=] |
| 37 | +function BindableEventWrapper.new(event: BindableEvent?) |
| 38 | + assert( |
| 39 | + event == nil |
| 40 | + or (typeof(event) == "Instance" and event:IsA("BindableEvent")), |
| 41 | + "event must be nil or an instance of BindableEvent" |
| 42 | + ) |
| 43 | + |
| 44 | + if event == nil then |
| 45 | + event = Instance.new("BindableEvent") |
| 46 | + end |
| 47 | + |
| 48 | + local self = { |
| 49 | + _inner = event, |
| 50 | + } |
| 51 | + |
| 52 | + setmetatable(self, BindableEventWrapper) |
| 53 | + |
| 54 | + return self |
| 55 | +end |
| 56 | + |
| 57 | +--[=[ |
| 58 | + @within BindableEventWrapper |
| 59 | +
|
| 60 | + Fires the internal [BindableEvent]. |
| 61 | +
|
| 62 | + See [BindableEvent:Fire] |
| 63 | +]=] |
| 64 | +function BindableEventWrapper.Fire(self: BindableEventWrapper, ...: any) |
| 65 | + self._inner:Fire(...) |
| 66 | +end |
| 67 | + |
| 68 | +--[=[ |
| 69 | + @within BindableEventWrapper |
| 70 | +
|
| 71 | + Connects to the internal [BindableEvent's Event](https://create.roblox.com/docs/reference/engine/classes/BindableEvent#Event) and returns the [RBXScriptConnection](https://create.roblox.com/docs/reference/engine/datatypes/RBXScriptConnection) representing it. |
| 72 | +
|
| 73 | + See [RBXScriptSignal:Connect] |
| 74 | +]=] |
| 75 | +function BindableEventWrapper.Connect( |
| 76 | + self: BindableEventWrapper, |
| 77 | + callback: (...any) -> () |
| 78 | +): RBXScriptConnection |
| 79 | + return self._inner.Event:Connect(callback) |
| 80 | +end |
| 81 | + |
| 82 | +--[=[ |
| 83 | + @within BindableEventWrapper |
| 84 | +
|
| 85 | + See [RBXScriptSignal:ConnectParallel] |
| 86 | +]=] |
| 87 | +function BindableEventWrapper.ConnectParallel( |
| 88 | + self: BindableEventWrapper, |
| 89 | + callback: (...any) -> () |
| 90 | +): RBXScriptConnection |
| 91 | + return self._inner.Event:ConnectParallel(callback) |
| 92 | +end |
| 93 | + |
| 94 | +--[=[ |
| 95 | + @within BindableEventWrapper |
| 96 | +
|
| 97 | + Connects to the [internal BindableEvent's Event](https://create.roblox.com/docs/reference/engine/classes/BindableEvent#Event) such that the connection is disconnected after the first event is delivered. Returns the [RBXScriptConnection](https://create.roblox.com/docs/reference/engine/datatypes/RBXScriptConnection) representing the connection. |
| 98 | +
|
| 99 | + See [RBXScriptSignal:Once] |
| 100 | +]=] |
| 101 | +function BindableEventWrapper.Once( |
| 102 | + self: BindableEventWrapper, |
| 103 | + callback: (...any) -> () |
| 104 | +): RBXScriptConnection |
| 105 | + return self._inner.Event:Once(callback) |
| 106 | +end |
| 107 | + |
| 108 | +--[=[ |
| 109 | + @within BindableEventWrapper |
| 110 | + @yields |
| 111 | +
|
| 112 | + Yields until the internal [BindableEvent's Event](https://create.roblox.com/docs/reference/engine/classes/BindableEvent#Event) fires. Returns the arguments provided by the signal. |
| 113 | +
|
| 114 | + See [RBXScriptSignal:Wait] |
| 115 | +]=] |
| 116 | +function BindableEventWrapper.Wait(self: BindableEventWrapper): ...any |
| 117 | + return self._inner.Event:Wait() |
| 118 | +end |
| 119 | + |
| 120 | +return BindableEventWrapper |
0 commit comments