Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:20:08+00:00 2026-06-11T17:20:08+00:00

I am having a strange issue here I hope you all can help with.

  • 0

I am having a strange issue here I hope you all can help with.

Project Details

I am working on a simple pub/sub implementation for a larger application that includes a pubsub.subscribe_once() method. This method enables the creation of one-off subscriptions, meaning that a generic subscription is created, and then once the correct “publish” event fires and the subscription callback is run, the subscription deletes itself.

subscribe_once: function(topic, func) {
    var sub = pubsub.subscribe(topic, func),
        old_func = sub.func;

    // rewrite our subscription's method to remove itself after invocation
    sub.func = function() {
        // call the original function
        old_func.apply(this);
        // remove subscription from topic
        pubsub.unsubscribe(sub);
    };

    return sub;
}

Problem

I seem to be having some kind of issue with the memory flow of this process. (In order to best understand the following explanation I suggest you walk through the jsfiddle demo below as I go.) I create a subscribe_once(‘someevent’) subscription, and then fire publish(‘someevent’). What you would expect to see when the publish method is invoked is that the topics hashtable contains a “someevent” key, which references an array of Subscription objects. And in fact, if you reference topics[“someevent”], you see an array with a single Subscription. If, however, you reference topics you see the “someevent” key, but the array is empty!

By commenting out pubsub.unsubscribe(sub); the problem is eliminated, even though this function does not appear to be fired until after we run console.log(topics).

Further, this does not seem to be an issue with the way a given browser “threads” console.log; try console.log(topics, topics[topic], topics, topics[topic]) and you get the same result.

Demo: http://jsfiddle.net/4Ab6c/

Any help would be greatly appreciated! Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:20:10+00:00Added an answer on June 11, 2026 at 5:20 pm

    I’m still looking for some documentation to back me up, but I suspect the object display in the console is performing lazy evaluation on your topics object. I added console.log(topics) to the subscribe method after sub is pushed onto the array and I get the same result as your ‘but not here’ log. When I wrap the final line of your fiddle pubsub.publish('someevent') in setTimeout and I get the Object tree open before the publish callback runs, then it shows the subscription in the array and it stays that way even after the callback runs. If I don’t open the object tree in the console before the callback runs then I see the empty array.

    I will keep searching for a blog post or something that confirms lazy evaluation is occurring.

    Just in case I haven’t made it obvious enough, by lazy I mean, the console isn’t gathering the details of the object until the tree view is clicked open in the console.

    I am working in Chrome.

    UPDATE

    I have found similar behavior on Firefox as well. Firefox recognizes that there is one object in the array but if you don’t drill down into the array before the publish even fires then the drill-down on the array will be empty.

    I updated the fiddle from your comment:

    http://jsfiddle.net/4Ab6c/2/

    Please try this:

    • Run the fiddle and expand the object tree for the first console.log before the publish event fires, I set it to a five second timeout but you could make it longer or shorter depending on how quickly you can get down to the console and click the inspector open.

    • You should see the subscribe object in the array as expected.

    • Clear the console and run the fiddle again. This time do not open the object inspector until after the publish event has fired and all the code is done running.

    • Now when you open the object inspector of the first console.log you should not see the subscription event in the array.

    UPDATE 2
    Here is a much simpler fiddle that exhibits the same behavior:

    http://jsfiddle.net/4Ab6c/3/

    If you expand first before second shows up then you will get foo: bar. If you expand first after second shows up you will get foo: baz.

    UPDATE 3

    And, voila, another SO question seconds the motion on lazy evaluation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a strange issue that I can't seem to resolve. Here is
I'm having an excessively strange issue. I'm working on a project and it's being
I'm having a strange issue here. I have a For loop inside a sub,
Hey, I'm having a strange issue. I'm wondering if anyone can help me make
I'm having a strange issue here, i have this bit of code: <?php if
I'm having a strange issue with RedirectToAction in MVC 3.0. Here is the code
I'm having a strange issue that I can't seem to figure out. I tried
Ok I got a strange issue that I hope someone could help with I
I'm having a strange issue that I haven't encountered before. I'm working on the
I'm having a very strange issue, it looks like my application can't create file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.