aboutsummaryrefslogtreecommitdiffstats
path: root/src/memory.js
blob: def59690c689815eac88f3254a9dc66f2a308e39 (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
import {registerMethods} from './methods.js'
import {registerConstructor} from './methods.js'

export const name = 'Memory'

export function setup (node) {
  this._memory = {}
}

// Remember arbitrary data
export function remember (k, v) {
  // remember every item in an object individually
  if (typeof arguments[0] === 'object') {
    for (var key in k) {
      this.remember(key, k[key])
    }
  } else if (arguments.length === 1) {
    // retrieve memory
    return this.memory()[k]
  } else {
    // store memory
    this.memory()[k] = v
  }

  return this
}

  // Erase a given memory
export function forget () {
  if (arguments.length === 0) {
    this._memory = {}
  } else {
    for (var i = arguments.length - 1; i >= 0; i--) {
      delete this.memory()[arguments[i]]
    }
  }
  return this
}

  // Initialize or return local memory object
export function memory () {
  return this._memory
}

registerMethods('Element', {remember, forget, memory})
registerConstructor('Memory', setup)