Created
November 27, 2025 01:33
-
-
Save havenwood/5fb551ccf2795e21582f95d32ffc0995 to your computer and use it in GitHub Desktop.
at_exit with priorities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Curtain | |
| @hooks = [] | |
| @order = 0 | |
| @mutex = Mutex.new | |
| class << self | |
| attr_reader :order | |
| def register(group:, priority: 0, &block) | |
| @mutex.synchronize do | |
| @hooks << {priority:, group:, order:, block:} | |
| @order += 1 | |
| end | |
| end | |
| def call | |
| @mutex.synchronize do | |
| @hooks.sort_by! { |hook| hook.values_at(:priority, :group, :order) } | |
| @hooks.each { |hook| hook[:block].call } | |
| end | |
| end | |
| end | |
| at_exit { Curtain.call } | |
| end | |
| module Kernel | |
| def at_exit(...) = Curtain.register(...) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment