christopher commited on
Commit
365937e
·
1 Parent(s): 69db3b2

Update static/app.js

Browse files
Files changed (1) hide show
  1. static/app.js +56 -8
static/app.js CHANGED
@@ -16,25 +16,73 @@ function updateInfo() {
16
  let pieces = Object.keys(pos).length;
17
  }
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  function search() {
20
  let pos = board.position();
21
  if (Object.keys(pos).length < 3) {
22
- document.getElementById("results").innerHTML =
23
- "<p>Add at least 3 pieces to search.</p>";
24
  return;
25
  }
 
 
26
  document.getElementById("results").innerHTML = "<p>Searching...</p>";
 
 
27
  fetch("/search", {
28
  method: "POST",
29
  headers: { "Content-Type": "application/json" },
30
  body: JSON.stringify({ fen: board.fen() }),
31
  })
32
- .then((r) => r.json())
33
- .then((data) => {
34
- allResults = data.results;
35
- window.searchTimeMs = data.time_ms;
36
- currentPage = 0;
37
- displayPage();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  });
39
  }
40
 
 
16
  let pieces = Object.keys(pos).length;
17
  }
18
 
19
+ // function search() {
20
+ // let pos = board.position();
21
+ // if (Object.keys(pos).length < 3) {
22
+ // document.getElementById("results").innerHTML =
23
+ // "<p>Add at least 3 pieces to search.</p>";
24
+ // return;
25
+ // }
26
+ // document.getElementById("results").innerHTML = "<p>Searching...</p>";
27
+ // fetch("/search", {
28
+ // method: "POST",
29
+ // headers: { "Content-Type": "application/json" },
30
+ // body: JSON.stringify({ fen: board.fen() }),
31
+ // })
32
+ // .then((r) => r.json())
33
+ // .then((data) => {
34
+ // allResults = data.results;
35
+ // window.searchTimeMs = data.time_ms;
36
+ // currentPage = 0;
37
+ // displayPage();
38
+ // });
39
+ // }
40
+
41
  function search() {
42
  let pos = board.position();
43
  if (Object.keys(pos).length < 3) {
44
+ document.getElementById("results").innerHTML = "<p>Add at least 3 pieces to search.</p>";
 
45
  return;
46
  }
47
+
48
+ allResults = [];
49
  document.getElementById("results").innerHTML = "<p>Searching...</p>";
50
+ const startTime = performance.now();
51
+
52
  fetch("/search", {
53
  method: "POST",
54
  headers: { "Content-Type": "application/json" },
55
  body: JSON.stringify({ fen: board.fen() }),
56
  })
57
+ .then(r => r.body.getReader())
58
+ .then(reader => {
59
+ const decoder = new TextDecoder();
60
+ let buffer = '';
61
+
62
+ function read() {
63
+ reader.read().then(({done, value}) => {
64
+ if (done) {
65
+ window.searchTimeMs = performance.now() - startTime;
66
+ currentPage = 0;
67
+ displayPage();
68
+ return;
69
+ }
70
+
71
+ buffer += decoder.decode(value, {stream: true});
72
+ const lines = buffer.split('\n');
73
+ buffer = lines.pop();
74
+
75
+ lines.forEach(line => {
76
+ if (line.trim()) {
77
+ allResults.push(JSON.parse(line));
78
+ if (allResults.length === 1 || allResults.length % 20 === 0) displayPage();
79
+ }
80
+ });
81
+
82
+ read();
83
+ });
84
+ }
85
+ read();
86
  });
87
  }
88