# Copyright 2005-2007 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ pragma.syntax("0.9") pragma.enable("call-pattern") def ALL :DeepFrozen := EIO.getALL() def terminating(chunk) as DeepFrozen { return chunk == null || Ref.isBroken(chunk) } def join implements DeepFrozen, ExitViaHere { /** Transfers the elements of an InStream to an OutStream. Acts immediately if possible; for non-immediate effects, use join <- (...). The return value will be resolved, to the terminator of the input stream, as soon as no more elements will be delivered to the OutStream; at this time, the output stream will also be terminated. */ to run(input, output) { def loop() { while (true) { def chunk := input.takeAtMost(ALL) def resolver := output.reserve() if (Ref.isNear(resolver)) { resolver.resolve(chunk) } else { resolver <- resolve(chunk) } if (Ref.isNear(chunk)) { if (terminating(chunk)) { return chunk } } else { return when (chunk) -> { if (terminating(chunk)) { chunk } else { loop() } } } } } return loop() } }