{"id":"sayhisam1/rewire","name":"rewire","scope":"sayhisam1","platform":"roblox","description":"An open-sourced library for Hot Reloading code in Roblox","version":"0.3.0","latest":"0.3.0","versions":["0.1.0","0.1.1","0.2.0","0.3.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"52b6e228412f3d0d34deb4f09d7072eeda9530514c2b076857a801c162cb5ee5","likes":0,"downloads":0,"install":"forest install sayhisam1/rewire","url":"https://forest.dev/p/roblox/sayhisam1/rewire","files":"https://api.forest.dev/ai/package/roblox/sayhisam1/rewire/files","readme":"Rewire is a Roblox library that makes adding HotReload functionality easy.\n\n**What's Hot Reloading?**\nHot Reloading means changing the behavior of your game immediately when some code is edited. This means you can write code _while your game is running in studio play mode_ and see updates happen in realtime, without having to stop and start the running session.\n\nHere is an example of HotReloading used to edit Roact UI while the game is running:\nhttps://user-images.githubusercontent.com/6133296/161100007-9e6616f1-01ca-4d1d-9812-270fbc238433.mp4\n\n**How to use it?**\n\n1. Create a new HotReloader object:\n\n```lua\nlocal Rewire = require(WHEREVER_REWIRE_IS)\nlocal reloader = Rewire.HotReloader.new()\n```\n\n2. Listen to a modulescript for which you want to support HotReloading\n\n```lua\n\nlocal requiredModule = nil\n\nreloader:listen(WHICHEVER_MODULE,\nfunction(module:ModuleScript)\n   -- callback invoked immediately upon listening, and whenever the module in question updates\n   -- this could include requiring the module and changing a global reference\n   requiredModule = require(module)\nend,\nfunction(module:ModuleScript)\n   -- here you put cleanup code that needs to happen before the next invocation of the callback\n   -- this could be destroying objects that need to be destroyed, or unmounting a Roact handle\nend)\n\n-- since the HotReloader doesn't yield on first invocation, requiredModule is guaranteed to be non-nil by this point\n```\n\nRewire currently only listens to updates in Studio - on live servers, it just fires the callback once and returns.\n\n**Some additional functionality**\nAs of version 0.3.0, Rewire now passes along a Context value to the callbacks. This allows callbacks to behave differently based on the types of reloading. The context parameter is structured as follows:\n\n```lua\ntype Context = {\n\toriginalModule: ModuleScript, -- a pointer to the original module that was listened to\n\tisReloading: boolean, -- is true if the callback was invoked while the module was reloading (instead of module removed or during the first call to :listen)\n}\n```\n\n**How does this even work?**\n\nRewire [listens to changes on ModuleScripts](src/HotReloader.lua) to decide when to reload. Rewire then creates a clone of the ModuleScript in question - this is needed since Roblox currently caches ModuleScript sources while the game is running, so if we didn't clone then `require` wouldn't return the results of the changed code.\nFor convenience, Rewire tags all created clones [with a CollectionService tag](src/Constants.lua). This tag can be accessed as follows:\n\n```lua\nRewire.CollectionServiceTag\n```\n\nYou can use this tag in upstream code to ignore Rewire created modules (e.g. in `ChildAdded` or `ChildRemoved` events)\n","readmeTruncated":false}