Skip to content

Commit

Permalink
Refactor the Queue class
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarrough committed May 3, 2020
1 parent 90e0665 commit 6141a66
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 53 deletions.
17 changes: 9 additions & 8 deletions public/game/action-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ export default function () {
const future = new Queue()
const past = new Queue()

// Enqueued items are added to the "future" list.
// Enqueued items are added to the "future" list. An action looks like this:
// {type: 'dealDamage', amount: 7, ... }
function enqueue(action) {
future.addToTop({action})
future.enqueue({action})
}

// Once dequeued, they end up in the "past".
// Returns a new state.
// Deqeueing means running the oldest action in the queue on a game state.
// The action is then moved to the "past". Returns the next state.
function dequeue(state) {
const {action} = future.takeFromBottom() || {}
const {action} = future.dequeue() || {}
let nextState
if (!action) return
try {
nextState = actions[action.type](state, action)
} catch (err) {
throw new Error(err)
}
past.addToTop({state, action})
past.enqueue({state, action})
return nextState
}

// Undo the latest action from the passt.
// Returns an object with the most recently run action and how the state looked before.
function undo() {
return this.past.takeFromTop()
return this.past.list.pop()
}

return {
Expand Down
17 changes: 5 additions & 12 deletions public/game/queue.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
// A queue is a list of objects that are inserted and removed first-in-first-out (FIFO).
export default class Queue {
constructor(items = []) {
this.list = items
}
// Add to the front
addToTop(item) {
// Enqueue and add an item at the end of the queue.
enqueue(item) {
this.list.push(item)
}
// Add to the end
addToBottom(item) {
this.list.unshift(item)
}
// Removes and returns the last/newest item
takeFromTop() {
return this.list.pop()
}
// Removes and returns the first/oldest item
takeFromBottom() {
// Dequeue and remove an item at the front of the queue.
dequeue() {
return this.list.shift()
}
}
2 changes: 1 addition & 1 deletion public/ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class App extends Component {
}
}
undo() {
const prev = this.am.past.takeFromTop()
const prev = this.am.past.pop()
if (!prev) return
this.setState(prev.state)
}
Expand Down
41 changes: 9 additions & 32 deletions tests/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,18 @@ import Queue from '../public/game/queue.js'
test('can add and run through queue', (t) => {
const q = new Queue()
t.is(q.list.length, 0)
q.addToTop('a')
q.enqueue('a')
t.is(q.list.length, 1)
q.addToTop('b')
q.addToTop('c')
q.addToTop('d')
q.enqueue('b')
q.enqueue('c')
q.enqueue('d')
t.is(q.list.length, 4)
const first = q.takeFromBottom()
const first = q.dequeue()
t.is(q.list.length, 3)
const second = q.takeFromTop()
const third = q.takeFromBottom()
const second = q.dequeue()
const third = q.dequeue()
t.is(q.list.length, 1)
t.is(first, 'a')
t.is(second, 'd')
t.is(third, 'b')
t.is(second, 'b')
t.is(third, 'c')
})

// https://immerjs.github.io/immer/docs/patches
// https://medium.com/@mweststrate/distributing-state-changes-using-snapshots-patches-and-actions-part-2-2f50d8363988
// const changes = []
// const inverseChanges = []

// const handleUndo = (patches, inversePatches) => {
// changes.push(...patches)
// inverseChanges.push(...inversePatches)
// }

// const handleUndo = (patch, inversePatches) => {
// this.undo.push(inversePatches)
// }

// state = applyPatches(state, changes)
// state = applyPatches(state, inverseChanges)

// handleUndo = () => {
// const undo = this.undo.pop()
// if (!undo) return
// this.setState(applyPatches(this.state, undo))
// }

0 comments on commit 6141a66

Please sign in to comment.