Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: channel and multi-concurrent task join support for the plugin system #2210

Merged
merged 2 commits into from
Jan 16, 2025

Conversation

sxyazi
Copy link
Owner

@sxyazi sxyazi commented Jan 16, 2025

Two new APIs added:

  • ya.chan() - Create a channel
  • ya.join() - Join multi-concurrent tasks

For example:

local keys = {
  { on = "q", run = "quit" },
  { on = "k", run = "up" },
  { on = "j", run = "down" },
  { on = "h", run = "left" },
  { on = "l", run = "right" },
}

local tx, rx = ya.chan("mpsc")

function producer()
  while true do
    local cand = self.keys[ya.which { cands = self.keys, silent = true }]
    if cand then
      tx:send(cand.run)
      if cand.run == "quit" then
        break
      end
    end
  end
end

function consumer()
  repeat
    local run = rx:recv()
    if run == "quit" then
      break
    elseif run == "up" then
      ya.dbg("up")
    elseif run == "down" then
      ya.dbg("down")
    elseif run == "left" then
      ya.dbg("left")
    elseif run == "right" then
      ya.dbg("right")
    end
  until not run
end

ya.join(producer, consumer)

@sxyazi sxyazi merged commit 6c94227 into main Jan 16, 2025
6 checks passed
@sxyazi sxyazi deleted the pr-08cf26f7 branch January 16, 2025 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant