Skip to content

A simple implementation of a lazy loading wrapper for any type of value.

License

Notifications You must be signed in to change notification settings

Bytebit-Org/roblox-Lazy

Repository files navigation

Lazy

CI status PRs Welcome License: MIT Discord server

Lazy is a simple implementation of a lazy loading wrapper for any type of value.

Installation

roblox-ts

Simply install to your roblox-ts project as follows:

npm i @rbxts/lazy

Wally

Wally users can install this package by adding the following line to their Wally.toml under [dependencies]:

Lazy = "bytebit/[email protected]"

Then just run wally install.

From model file

Model files are uploaded to every release as .rbxmx files. You can download the file from the Releases page and load it into your project however you see fit.

From model asset

New versions of the asset are uploaded with every release. The asset can be added to your Roblox Inventory and then inserted into your Place via Toolbox by getting it here.

Documentation

Documentation can be found here, is included in the TypeScript files directly, and was generated using TypeDoc.

Example

In this example, the Lazy class will be used to wait to load a reference to something in the world named the Objective, and it will do so by waiting for a RemoteEvent to be fired from the server saying it is ready.

roblox-ts example
import { ILazy, Lazy } from "@rbxts/lazy";
import { ReplicatedService } from "@rbxts/services";

type Objective = {}; // some type for the objective

declare const loadObjective: () => Objective; // some function that loads the objective and returns it

export class ObjectiveLoader {
  private objectiveLazyLoader: ILazy<Objective>;

  public constructor() {
    this.objectiveLazyLoader = new Lazy(loadObjective);

    this.waitForServerToSayObjectiveIsReady();
  }

  private waitForServerToSayObjectiveIsReady() {
    const remoteEvent = ReplicatedStorage.WaitForChild("ObjectiveReadiedRemoteEvent");
    assert(remoteEvent.IsA("RemoteEvent"));

    remoteEvent.OnClientEvent.Connect(() => {
      objectiveLazyLoader.getValue();
    });
  }
}
Luau example
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Lazy = require(path.to.modules["lazy"]).Lazy

function loadObjective()
  -- some function that loads the objective and returns it 
end

local ObjectiveLoader = {}
ObjectiveLoader.__index = ObjectiveLoader

function new()
  local self = {}
  setmetatable(self, ObjectiveLoader)

  self._objectiveLazyLoader = Lazy.new(loadObjective)

  _waitForServerToSayObjectiveIsReady(self)

  return self
end

function _waitForServerToSayObjectiveIsReady(self)
    local remoteEvent = ReplicatedStorage:WaitForChild("ObjectiveReadiedRemoteEvent")
    assert(remoteEvent:IsA("RemoteEvent"))

    remoteEvent.OnClientEvent:Connect(function()
      objectiveLazyLoader:getValue()
    end)
end

return {
  new = new
}

About

A simple implementation of a lazy loading wrapper for any type of value.

Resources

License

Stars

Watchers

Forks

Packages

No packages published