{"id":13896553,"url":"https://github.com/rxi/coil","last_synced_at":"2025-05-06T08:31:12.803Z","repository":{"id":19747449,"uuid":"23004285","full_name":"rxi/coil","owner":"rxi","description":"A tiny cooperative threading module for Lua","archived":false,"fork":false,"pushed_at":"2021-11-07T07:03:05.000Z","size":3,"stargazers_count":77,"open_issues_count":0,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-30T18:05:48.741Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rxi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-15T21:54:55.000Z","updated_at":"2025-02-16T16:44:23.000Z","dependencies_parsed_at":"2022-07-23T13:30:53.047Z","dependency_job_id":null,"html_url":"https://github.com/rxi/coil","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxi%2Fcoil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxi%2Fcoil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxi%2Fcoil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxi%2Fcoil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rxi","download_url":"https://codeload.github.com/rxi/coil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252648575,"owners_count":21782405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T18:03:00.097Z","updated_at":"2025-05-06T08:31:12.531Z","avatar_url":"https://github.com/rxi.png","language":"Lua","funding_links":[],"categories":["Lua","Resources"],"sub_categories":["Multitasking"],"readme":"# coil\nA tiny cooperative threading module for Lua. Coil is based around coroutines,\nallowing many cooperative threads to run simultaneously.\n\n## Usage\nThe [coil.lua](coil.lua) file should be dropped into an existing project and\nrequired by it.\n```lua\ncoil = require \"coil\"\n```\nAt the start of each frame `coil.update()` should be called and given the delta\ntime since the last call as its argument.\n```lua\ncoil.update(deltatime)\n```\nCoil refers to each cooperative thread as a *task*; new tasks can be created by\nusing the `coil.add()` function.\n```lua\n-- prints the word \"hello\" each second for 5 seconds\ncoil.add(function()\n  for i = 1, 5 do\n    print(\"hello\")\n    coil.wait(1)\n  end\nend)\n```\n\n### coil.wait()\nAs tasks are cooperative, they must be manually yielded to allow other parts of\nthe program to continue. This is done by calling the `coil.wait()` function.\nThe function can be used to wait for different events before continuing.\n\n* If wait is called with a number as its argument then it waits for that number\n  of seconds before resuming.\n* If wait is called with a callback created by coil.callback() it will wait\n  until that callback function is called before resuming; it will return the\n  arguments passed to the callback when it was called.\n* If wait is called with no arguments it waits until the next frame\n  (`coil.update()` call) before resuming; it returns the time that has passed\n  since the last frame.\n\n### coil.callback()\nThe `coil.callback()` function can be used to create a special callback which,\nif passed to the `coil.wait()` function, will cause the task to wait until the\ncallback is called.\n```lua\n-- Prints \"Hello world\" when `cb()` is called.\ncoil.add(function()\n  cb = coil.callback()\n  coil.wait(cb)\n  print(\"Hello world\")\nend)\n```\n\n### coil.update(dt)\nThis should be called at the start of each frame. Updates all the tasks,\nrunning those which aren't waiting or paused. `dt` should be the amount of time\nin seconds which has passed since the function was last called.\n\n### coil.add(fn)\nAdds a new task, the task will begin running on the next call to\n`coil.update()`.\n```lua\n-- prints \"hello world\" every 2 seconds\ncoil.add(function()\n  while 1 do\n    print(\"hello world\")\n    coil.wait(2)\n  end\nend)\n```\n\n## Stopping a task\nA task can be stopped and removed at any point by calling its `:stop()` method.\nTo do this the task must be assigned to a variable when it is created.\n```lua\n-- Adds a new task\nlocal t = coil.add(function()\n  coil.wait(1)\n  print(\"hello\")\nend)\n\n-- Removes the task before it has a chance to run\nt:stop()\n```\n\n## Groups\nCoil provides the ability to create task groups; these are objects which can\nhave tasks added to them, and which are in charge of updating and handling\ntheir contained tasks. A group is created by calling the `coil.group()`\nfunction.\n```lua\nlocal group = coil.group()\n```\n\nOnce a group is created it acts independently of the `coil` object, and must\nbe updated each frame using its own update method.\n```lua\ngroup:update(deltatime)\n```\n\nTo add a task to a group, the group's `add()` method should be used.\n```lua\ngroup:add(function()\n  coil.wait(10)\n  print(\"10 seconds have passed\")\nend)\n```\n\nA good example of where groups are useful is for games where you may have a set\nof tasks which effect objects in the game world and which you want to pause\nwhen the game is paused.  A group's tasks can be paused by simply neglecting\nto call its `update()` method; when a group is destroyed its tasks are also\ndestroyed.\n\n\n## License\nThis library is free software; you can redistribute it and/or modify it under\nthe terms of the MIT license. See [LICENSE](LICENSE) for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frxi%2Fcoil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frxi%2Fcoil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frxi%2Fcoil/lists"}