summaryrefslogtreecommitdiffstats
path: root/src/animation/Queue.js
blob: b4e27229a4d39c13df39604a35c08c740ac4311c (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
export default class Queue {

  constructor () {

    this._first = null
    this._last = null

  }

  push ( value ) {

    // An item stores an id and the provided value
    var item = value.next ? value : { value: value, next: null, prev: null }

    // Deal with the queue being empty or populated
    if ( this._last ) {

      item.prev = this._last
      this._last.next = item
      this._last = item

    } else {

      this._last = item
      this._first = item

    }

    // Update the length and return the current item
    return item

  }

  shift () {

    // Check if we have a value
    var remove = this._first
    if ( !remove ) return null

    // If we do, remove it and relink things
    this._first = remove.next
    if ( this._first ) this._first.prev = null
    this._last = this._first ? this._last : null
    return remove.value

  }

  // Shows us the first item in the list
  first () {

    return this._first && this._first.value

  }

  // Shows us the last item in the list
  last () {

    return this._last && this._last.value

  }

  // Removes the item that was returned from the push
  remove ( item ) {

    // Relink the previous item
    if ( item.prev ) item.prev.next = item.next
    if ( item.next ) item.next.prev = item.prev
    if ( item === this._last ) this._last = item.prev
    if ( item === this._first ) this._first = item.next

    // Invalidate item
    item.prev = null
    item.next = null

  }

}