Coder Social home page Coder Social logo

Comments (3)

gitgrimbo avatar gitgrimbo commented on August 22, 2024

Here is the patch I made to my local copy (grimbo == me), though I'm sure someone could implement it better!

publish():

    publish: function( topic ) {
        var args = slice.call( arguments, 1 ),
            subscription,
            length,
            i = 0,
            ret;

        if ( !subscriptions[ topic ] ) {
            return true;
        }

        for ( length = subscriptions[ topic ].length; i < length; i++ ) {
            subscription = subscriptions[ topic ][ i ];

            // grimbo - added null check
            if (null !== subscription) {
                ret = subscription.callback.apply( subscription.context, args );
            }

            if ( ret === false ) {
                break;
            }
        }

        // grimbo - cull nulls in case a subscription unsubscribed itself (or
        // another subscription) for this topic
        removeNullSubscriptions(topic);

        return ret !== false;
    },

removeNullSubscriptions():

// grimbo - New function to cull null subscriptions
function removeNullSubscriptions(topic) {
    if ( !subscriptions[ topic ] ) {
        return;
    }
    var i = 0,
        subscription;
    while (i < subscriptions[ topic ].length) {
        subscription = subscriptions[ topic ][ i ];
        if (null === subscription) {
            subscriptions[ topic ].splice( i, 1 );
            continue;
        }
        i++;
    }
}

unsubscribe():

    unsubscribe: function( topic, callback ) {
        if ( !subscriptions[ topic ] ) {
            return;
        }

        var length = subscriptions[ topic ].length,
            i = 0,
            // grimbo
            subscription;

        for ( ; i < length; i++ ) {
            // grimbo - test for null
            subscription = subscriptions[ topic ][ i ];
            if ( null !== subscription && subscription.callback === callback ) {
                //subscriptions[ topic ].splice( i, 1 );
                // grimbo - we can't remove because it upsets the publish loop,
                // so just set to null in case unsubscribe is called from within publish (via a subscription)
                subscriptions[ topic ][ i ] = null;
                break;
            }
        }
    }

from amplify.

warmrobot avatar warmrobot commented on August 22, 2024

Why don't you give an option to unsubscribe all callbacks of the topic (like in jquery core )? amplify.unsubscribe("items.loaded") for example.

from amplify.

scottgonzalez avatar scottgonzalez commented on August 22, 2024

That's completely unrelated to this issue.

from amplify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.