Skip to content

Commit 58770cc

Browse files
committed
Use JSON to communicate with highlights
This will allow us to use multiple threads by waiting for the results from the correct file to be returned, and not just using the result returned most recently.
1 parent 7ab0b2a commit 58770cc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

highlights/highlight-filename-from-stdin.coffee

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ stdin = process.openStdin()
1111
stdin.setEncoding 'utf8'
1212
mystderr = process.stderr
1313
mystdout = process.stdout
14-
process_file = (full_path) ->
14+
process_file = (given_path) ->
15+
full_path = path.resolve given_path
1516
fs.readFile full_path, 'utf8', (read_err, file_str) ->
1617
if read_err
1718
console.error read_err
@@ -20,8 +21,11 @@ process_file = (full_path) ->
2021
if hl_err
2122
console.error hl_err
2223
else
23-
mystdout.write(html + '\n')
24+
obj = {}
25+
obj.file = full_path
26+
obj.html = html
27+
mystdout.write(JSON.stringify(obj) + '\n' )
2428

2529

2630
stdin.on 'data', (input) ->
27-
process_file path.resolve input.trim()
31+
process_file input.trim()

htmlify.p6

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use Perl6::TypeGraph::Viz;
2828
use Perl6::Documentable::Registry;
2929
use Pod::Convenience;
3030
use Pod::Htmlify;
31+
use JSON::Fast;
3132

3233
&spurt.wrap(sub (|c){
3334
state %seen-paths;
@@ -153,7 +154,7 @@ sub MAIN(
153154
# Then they can be copied to doc/Programs.
154155
if $use-highlights and $async {
155156
$proc = Proc::Async.new('./highlights/node_modules/coffee-script/bin/coffee', './highlights/highlight-filename-from-stdin.coffee', :r, :w);
156-
$supply = $proc.stdout.lines.Channel;
157+
$supply = $proc.stdout.lines;
157158
$supply2 = $proc.stderr.tap( { .say } );
158159

159160
$prom = $proc.start;
@@ -1023,8 +1024,17 @@ sub highlight-code-blocks(:$use-inline-python = True, :$use-highlights = False)
10231024
my $thing;
10241025
if $use-highlights {
10251026
if $async {
1027+
my $promise = Promise.new;
1028+
my $tap = $supply.tap( -> $json {
1029+
my $parsed-json = from-json($json);
1030+
if $parsed-json<file> eq $tmp_fname {
1031+
$promise.keep($parsed-json<html>);
1032+
$tap.close();
1033+
}
1034+
} );
10261035
$proc.say($tmp_fname);
1027-
$thing = $supply.receive;
1036+
await $promise;
1037+
$thing = $promise.result;
10281038
}
10291039
else {
10301040
$command = ./highlights/node_modules/coffee-script/bin/coffee ./highlights/highlight-file.coffee "$tmp_fname";

0 commit comments

Comments
 (0)