mcollina / reusify

Reuse objects and functions with style
MIT License
162 stars 18 forks source link

Should state be maintained after the release of the object? #6

Closed brunoscopelliti closed 7 years ago

brunoscopelliti commented 7 years ago

The value of o1, and o2 in the snippet below does not match my expectation.

test('Question 1', function (t) {
  function MyObject () {
    this.foobar = null
    this.next = null
  }

  var instance = reusify(MyObject)

  var obj = instance.get()
  obj.foobar = true

  instance.release(obj)

  var o1 = instance.get()
  // MyObject {foobar: null, next: null}

  var o2 = instance.get()
  // MyObject {foobar: true, next: null}
})

I would have found more reasonable to have o1.foobar = true and o2.foobar = null. Am I missing something, or is this a bug?

I've seen in middie that the hanlder restores the state before releasing the object. Do I need to manually restore the state as well?

mcollina commented 7 years ago

Yes, you need to restore the state yourself. Would you like to send a PR to update the docs?

brunoscopelliti commented 7 years ago

To be fair, the README already mentions the fact that state should be reset on the consumer side... I was not sure this is the done thing to do, cause in the benchmark state is never reset. I'll fix the benchmark, and try to make more clear the expectation. Thanks for the clarification.

mcollina commented 7 years ago

Thanks!!