'use strict'
class ObjectRecorder {
constructor() {
this.clear()
}
clear() {
this.map = new WeakMap()
this.count = 0
this.recording = true
}
stop() {
this.recording = false
}
check(obj) {
const val = this.map.get(obj)
if (val) {
if (val.length > 1) {
if (val[0] || this.recording) {
return val[1]
}
val[0] = true
return ObjectRecorder.FIRST
}
if (!this.recording) {
return ObjectRecorder.NEVER
}
val.push(this.count++)
return val[1]
}
if (!this.recording) {
throw new Error('New object detected when not recording')
}
this.map.set(obj, [false])
return ObjectRecorder.NEVER
}
}
ObjectRecorder.NEVER = -1
ObjectRecorder.FIRST = -2
module.exports = ObjectRecorder