Beta

Callback Set

Description
Loading description...
Fundamentals
View
AllIssues2QuestionsSuggestionsShow Resolved
  • Please sign in or sign up to leave a comment.
  • Voile Avatar

    The actual tests are seriously lacking, even a half-working solution will have a great chance to pass given some minimal fiddling.

    Also, needs random tests.

    • wthit56 Avatar

      Okay. Could you post a code block of an example of a non-solution, please, so I can get some ideas on what the problem is?

  • Voile Avatar

    Needs sample tests

  • computerguy103 Avatar

    I can't make heads or tails of this description. What's the original callback, how's it different from ones added by the add method, why does add return a trigger function but there's no trigger function for the original callback, is instance.count supposed to start at 0 and count the original callback plus each callback that you add, then count down each time a callback is triggered? And if you call resolve() then do the callbacks get triggered or not? The description just says the original oncomplete gets triggered, which was the original callback.

    And as I see it, count should be a getter for a private value that can't be changed outside of the class methods. instance.count = x should either throw an error or simply have no effect, because count is supposed to be counting something, and it should remain the correct count always.

    Code examples would help immensely, too.

    • wthit56 Avatar

      I've now added code examples. Hopefully they'll help others to figure things out.

      On your specific points:

      • The original callback will be called after all "added" callbacks have resolved. You effectively are making a queue of callbacks. That original callback will be called once the queue has been emptied, letting you use all the data retrieved, etc. after all the async work has been completed, and triggers triggered.
      • Callbacks "added" will be wrapped in a "trigger" function which is then returned. When "triggered", instance.resolve() is automatically called for you.
      • Your instance.count explanation is spot on.
      • instance.resolve() isn't normally called by the user, though it can be, if they wish. It is, however, automatically called by a trigger, after the relevant callback has been called.
      • When instance.count reaches zero as part of an instance.resolve() call, the instance.oncomplete callback is called, and the object wiped.
      • instance.count can be changed outside the callbacks, and outside of the control of the CallbackSet itself. Same goes for instance.oncomplete. This is to make the usage of the object as flexible as possible, which is also why instance.resolve can be called manually.

      I have made some changes to the description to try to make these things a little clearer. I hope my explanation above has helped you, though?

    • computerguy103 Avatar

      Okay, it's clearer now. The sample code you added to the description has CallbackSet which should be CreateCallbackSet but other than that it's pretty good.

      Do you have any tests that check to see if the callback functions are executed within the correct context?

      e.g.

      function create(secret) {
        return CreateCallbackSet(function () { console.log(secret); });
      }
      
      function add(set, secret) {
        return set.add(function () { console.log(secret); });
      }
      
      var set = create('phil');
      var t1 = add(set, 'fred');
      var t2 = add(set, 'mark');
      
      t2();  // should log 'mark'
      t1();  // should log 'fred', then execute oncomplete, which should log 'phil'
      

      If the callback functions aren't run from their original context, secret will be undefined.

    • wthit56 Avatar

      Great stuff. And typo fixed--thanks for that.

      I think you mean "closure"? And closures are implicitly preserved; there's no way of running a function without it's original closure, as the closure is lumped in with a function when it is created.

    • computerguy103 Avatar

      You're right. I guess there could be problems if the functions referred to this but I'm not coming up with any easy examples.

    • wthit56 Avatar

      The thing is, unless the API supports it (and this one doesn't), there is no way of guaranteeing a callback will have a particular context. So most callbacks just don't use a context, anyway.