9d7f8fd65eb064db799e07a8ea405a124a1195d1 — Stephen Paul Weber 3 months ago a776988
Do not start IO work until asked
2 files changed, 21 insertions(+), 3 deletions(-)

M lib/dhall/resolve.rb
M lib/dhall/util.rb
M lib/dhall/resolve.rb => lib/dhall/resolve.rb +3 -3
@@ 16,13 16,13 @@
 	module Resolvers
 		ReadPathSources = lambda do |sources|
 			sources.map do |source|
-				Promise.resolve(nil).then { source.pathname.binread }
+				Util::LazyPromise.new { source.pathname.binread }
 			end
 		end
 
 		ReadEnvironmentSources = lambda do |sources|
 			sources.map do |source|
-				Promise.resolve(nil).then do
+				Util::LazyPromise.new do
 					ENV.fetch(source.var) do
 						raise ImportFailedException, "No #{source}"
 					end


@@ 53,7 53,7 @@
 
 		ReadHttpSources = lambda do |sources, parent_origin|
 			sources.map do |source|
-				Promise.resolve(nil).then do
+				Util::LazyPromise.new do
 					PreflightCORS.call(source, parent_origin)
 					timeout = source.deadline.timeout
 					uri = source.uri

M lib/dhall/util.rb => lib/dhall/util.rb +18 -0
@@ 1,9 1,27 @@
 # frozen_string_literal: true
 
+require "promise"
 require "timeout"
 
 module Dhall
 	module Util
+		class LazyPromise < Promise
+			def initialize(&block)
+				super
+				@block = block
+			end
+
+			def subscribe(*args)
+				super
+
+				begin
+					fulfill(@block.call)
+				rescue => e
+					reject(e)
+				end
+			end
+		end
+
 		class AllOf
 			def initialize(*validators)
 				@validators = validators