Class: Mayu::Resources::Registry
- Inherits:
-
Object
- Object
- Mayu::Resources::Registry
- Extended by:
- T::Sig
- Defined in:
- lib/mayu/resources/registry.rb
Constant Summary collapse
- EXTENSIONS =
T.let(["", ".rb", ".haml"].freeze, T::Array[String])
- MarshalFormat =
T.type_alias { [String, T::Hash[String, String]] }
Instance Attribute Summary collapse
-
#dependency_graph ⇒ Object
readonly
Returns the value of attribute dependency_graph.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_path(path) ⇒ Object
- #add_resource(path) ⇒ Object
- #dump ⇒ Object
- #generate_assets(asset_dir, concurrency:, forever:) ⇒ Object
-
#initialize(root:) ⇒ Registry
constructor
A new instance of Registry.
- #load_resource(path, source = "/") ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(args) ⇒ Object
- #mermaid_url ⇒ Object
- #reload_resource(path, visited: T::Set[String].new, add: false) ⇒ Object
- #unload_resource(path, visited: T::Set[String].new) ⇒ Object
- #wait_for_asset(filename, timeout: 2, task: Async::Task.current) ⇒ Object
Constructor Details
#initialize(root:) ⇒ Registry
Returns a new instance of Registry.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mayu/resources/registry.rb', line 27 def initialize(root:) @root = T.let(File.(root), String) @dependency_graph = T.let(DependencyGraph.new, DependencyGraph) @resolver = T.let( Resolver::Filesystem.new(@root, extensions: EXTENSIONS), Resolver::Base ) @assets = T.let(Assets.new, T.nilable(Assets)) end |
Instance Attribute Details
#dependency_graph ⇒ Object (readonly)
Returns the value of attribute dependency_graph.
24 25 26 |
# File 'lib/mayu/resources/registry.rb', line 24 def dependency_graph @dependency_graph end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
22 23 24 |
# File 'lib/mayu/resources/registry.rb', line 22 def root @root end |
Class Method Details
.load(dumped, root:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mayu/resources/registry.rb', line 39 def self.load(dumped, root:) MessagePack.unpack(dumped) => { type: "registry", data: String => data } registry = T.cast( Marshal.load( data, ->(obj) do obj.instance_variable_set(:@root, root) if obj.is_a?(Registry) obj end ), Registry ) registry end |
Instance Method Details
#absolute_path(path) ⇒ Object
89 90 91 |
# File 'lib/mayu/resources/registry.rb', line 89 def absolute_path(path) File.join(@root, File.(path, "/")) end |
#add_resource(path) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mayu/resources/registry.rb', line 155 def add_resource(path) if resource = @dependency_graph.get_resource(path) return resource end resource = Resource.new(registry: self, path:) @dependency_graph.add_node(resource.path, resource) resource.assets.each { |asset| @assets.add(asset) } if @assets Console.logger.info( self, "Loaded #{resource.type.name} from #{resource.path}" ) resource end |
#dump ⇒ Object
57 58 59 |
# File 'lib/mayu/resources/registry.rb', line 57 def dump MessagePack.pack(type: "registry", data: Marshal.dump(self)) end |
#generate_assets(asset_dir, concurrency:, forever:) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/mayu/resources/registry.rb', line 112 def generate_assets(asset_dir, concurrency:, forever:) if @assets @assets.run(asset_dir, concurrency:, forever:) else raise "Assets can't be generated in production" end end |
#load_resource(path, source = "/") ⇒ Object
149 150 151 152 |
# File 'lib/mayu/resources/registry.rb', line 149 def load_resource(path, source = "/") resolved_path = @resolver.resolve(path, source) add_resource(resolved_path) end |
#marshal_dump ⇒ Object
64 65 66 |
# File 'lib/mayu/resources/registry.rb', line 64 def marshal_dump [Marshal.dump(@dependency_graph), @resolver.resolved_paths] end |
#marshal_load(args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mayu/resources/registry.rb', line 69 def marshal_load(args) dependency_graph = T.cast( Marshal.load( args[0], ->(obj) do if obj.is_a?(Resource) obj.instance_variable_set(:@registry, self) end obj end ), DependencyGraph ) @dependency_graph = dependency_graph @resolver = Resolver::Static.new(args[1]) end |
#mermaid_url ⇒ Object
94 95 96 |
# File 'lib/mayu/resources/registry.rb', line 94 def mermaid_url @dependency_graph.to_mermaid_url end |
#reload_resource(path, visited: T::Set[String].new, add: false) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mayu/resources/registry.rb', line 123 def reload_resource(path, visited: T::Set[String].new, add: false) unless @dependency_graph.has_node?(path) add_resource(path) if add return end reload_resources( [path, *@dependency_graph.dependants_of(path)], visited: ) end |
#unload_resource(path, visited: T::Set[String].new) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mayu/resources/registry.rb', line 136 def unload_resource(path, visited: T::Set[String].new) return unless @dependency_graph.has_node?(path) dependants = @dependency_graph.dependants_of(path) Console.logger.info(self, "Unloading resource, #{path}") @dependency_graph.delete_node(path) reload_resources(dependants, visited:) end |
#wait_for_asset(filename, timeout: 2, task: Async::Task.current) ⇒ Object
99 100 101 102 103 |
# File 'lib/mayu/resources/registry.rb', line 99 def wait_for_asset(filename, timeout: 2, task: Async::Task.current) return unless @assets task.with_timeout(timeout) { @assets.wait_for(filename) } end |