I had one annoying bug that I hadn't got to the bottom of before yesterday's 1.0 release. I even added a bunch of unit tests to make sure Holster was working as I expected. The previous versions of the unit tests looked like this:
test("get unknown keys in for loop callbacks null", (t, done) => {
for (let i = 0; i < 5; i++) {
holster.get("unknown" + i, data => {
assert.equal(data, null)
if (i === 4) done()
})
}
})
The idea is that you should be able to make as many requests as you like and Holster will manage the context of each request and map it to the right callback. Can you see the problem with the test though? The loop will finish before the callbacks are called, so the condition will match and done() will be called regardless of if the callbacks were each called correctly. In fact only one was being called but the tests still passed.
They now look like this:
test("get unknown keys in for loop callbacks null", (t, done) => {
let count = 0
for (let i = 0; i < 5; i++) {
holster.get("unknown" + i, data => {
assert.equal(data, null)
count++
})
}
setTimeout(() => {
if (count === 5) done()
}, 200)
})
Which makes sure each was called correctly. Holster wasn't able to pass this version of the test because the context id was being dropped. The id is passed to the API as a parameter that I thought would form part of the closure, but it seems the callback function itself is created before that can happen. The answer was to pass the id to a function that then returns a new callback, which then appears to be called correctly.
This was enough of a problem in the app I'm working on for a 1.0.1 bug fix release.
Holster 1.0.1
The idea is that you should be able to make as many requests as you like and Holster will manage the context of each request and map it to the right callback. Can you see the problem with the test though? The loop will finish before the callbacks are called, so the condition will match and
done()
will be called regardless of if the callbacks were each called correctly. In fact only one was being called but the tests still passed.They now look like this:
Which makes sure each was called correctly. Holster wasn't able to pass this version of the test because the context id was being dropped. The id is passed to the API as a parameter that I thought would form part of the closure, but it seems the callback function itself is created before that can happen. The answer was to pass the id to a function that then returns a new callback, which then appears to be called correctly.
This was enough of a problem in the app I'm working on for a 1.0.1 bug fix release.