#! /bin/env tclsh

package require http

package require {ycl coro relay}
namespace import [yclprefix]::coro::relay

namespace eval doc {}

coroutine geturl apply [list {} {
	while 1 {
		relay accept {deliver url}
		# Because this coroutine uses [yield] directly, [accept] only 1 order
		# should be made to it .
		coroutine geturl_[info cmdcount] apply [list {deliver url} {
			http::geturl $url -command [info coroutine]
			set token [yield]
			{*}$deliver [http::data $token]
		} [namespace current]] $deliver $url
	}
} [namespace current]]


variable doc::main {
	description {
		Retrieve each URL specified on the command line and print a dictionary
		mapping thee URL to its contents .
	}
}
coroutine main apply [list {argv0 argv} {
	set results {}
	foreach url $argv {
		dict set orders [relay order 0 geturl $url] $url
	}
	foreach arg $argv {
		set result [relay receive]
		lappend results [dict get $orders [relay last]] $result
	}
	puts $results
	exit 0
}] $argv0 $argv

vwait forever
