-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.js
32 lines (31 loc) · 1.1 KB
/
next.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
import { createAsyncThunk } from '@reduxjs/toolkit'
import { removeNonJson, forceQuantized } from './utilsMelody'
import { sequencesMatch, sequencesIdentical } from './compare'
import { BLANK } from './melodies'
export const nextMelody = createAsyncThunk(
'meme/next',
async ({
skipDuplicates = true,
direction
} = {}, { dispatch, getState }) => {
const { memes, interpolate: i } = getState()
const size = i.melodies.length
const width = Math.ceil(Math.sqrt(size))
const currentMelody = i.melodies[i.current]
direction = direction || i.direction || [0, 1]
const next = (cur, [xdir, ydir]) => cur + xdir + (width * ydir)
let nextMelody = currentMelody
let newCurrent = i.current
// don't allow same melody twice
while (sequencesIdentical(currentMelody, nextMelody)) { // true first time
newCurrent = (2 * size + next(newCurrent, direction)) % size
nextMelody = i.melodies[newCurrent] ?? BLANK
}
// console.log("next melody",newCurrent,nextMelody)
return {
nextMelody: nextMelody ?? BLANK,
newCurrent,
direction
}
}, {}
)