-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
32 lines (28 loc) · 923 Bytes
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Makeshift state manager
*
* @template {T}
* @type {(value: T) => [() => T, (newValue: T) => void, (cb: (value: T) => void) => void]}
*/
const useState = value => {
const subscribers = [];
return [
function get() { return value; },
function set(newValue) {
if (newValue == value) return;
value = newValue;
subscribers.forEach(cb => cb(value));
},
function subscribe(cb) {
subscribers.push(cb);
cb(value);
}
]
};
export const [rows, setRows, subRows] = useState(20);
export const [cols, setCols, subCols] = useState(20);
const initialBoard = Array(rows()).fill(Array(cols()).fill(false)).map(x => [...x]);
export const [board, setBoard, subBoard] = useState(initialBoard);
// Random board start
const threshold = parseFloat(new URLSearchParams(location.search).get('amount')) || .5
setBoard(board().map(row => row.map(() => Math.random() < threshold)));