###*
 * This is a example of how to use `kit.async` to deal with producer-consumer problem.
 *
 * Of cause, most time `kit.async` is used with a fixed array, this is a complex
 * usage of it.
###

nobone = require 'nobone'
{ kit } = nobone
{ _, Promise } = kit

tasks = ['http://www.baidu.com']
store = []

# Max running threads at the same time.
maxProducer = 5
maxConsumer = 2

launch = ->
	# The producer and the comsumer will create
	# a nearly infinity life circle.
	kit.async [
		kit.async maxProducer, producer, false
		kit.async maxConsumer, consumer, false
	]
	.catch (err) ->
		kit.err err.message

sleepAwhile = ->
	new Promise (resolve) ->
		setTimeout resolve, 200

# Producer will download a page and add it to the store.
producer = ->
	if tasks.length == 0
		# Nothing to work, sleep.
		return sleepAwhile()

	url = tasks.pop()

	kit.request(url).then (page) ->
		kit.log "Produce: #{url}"

		store.push page

# Comsumer will parse a page and find some urls in the page,
# then add the urls to the tasks.
consumer = ->
	if store.length == 0
		return sleepAwhile()

	page = store.pop()

	urls = []

	# Find url in page.
	page.replace /<a[\w\s]+href="(http.+?)"/g, (m, p) ->
		urls.push p

	# Randomly get 3 urls.
	urls = _.sample(urls, 3)

	tasks = tasks.concat urls

launch()
