christopher commited on
Commit
b04a46f
·
1 Parent(s): 9157c4e

Update static/app.js

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