Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Stops all threads in the current buffer.
# @example
# load "~/sp-stop-util.rb"
# stop_current_buffer
# @note This method is using the Sonic Pi's internal methods and variables.
# SonicPi::RuntimeMethods#__current_job_info, __stop_job, __current_job_id
# SonicPi::Runtime#user_jobs, job_subthreads
def stop_current_buffer
current_job_info = __current_job_info
current_workspace_name = current_job_info[:workspace] || ''
puts "current workspace name: #{current_workspace_name}"
# find sub threads job id
current_buffer_job_ids = Set.new
@user_jobs.each_id do |job_id|
puts "job_id: #{job_id}"
subthreads = @job_subthreads[job_id]
next unless subthreads
subthreads.each do |st|
info = st.thread_variable_get :sonic_pi_spider_job_info || {}
puts "info: #{info}"
workspace = info[:workspace]
puts "workspace name: #{workspace}"
current_buffer_job_ids.add job_id if workspace == current_workspace_name
end
end
# kill sub threads
current_buffer_job_ids.each do |job_id|
__stop_job job_id
end
# kill current thread at last
__stop_job __current_job_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment