<commit_before>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.util {

import com.threerings.util.Log;

import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.DObjectManager;
import com.threerings.presents.dobj.ObjectAccessError;
import com.threerings.presents.dobj.Subscriber;

/**
 * A class that safely handles the asynchronous subscription to a distributed object when it is not
 * know if the subscription will complete before the subscriber decides they no longer wish to be
 * subscribed.
 */
public class SafeSubscriber implements Subscriber
{
    /**
     * Creates a safe subscriber for the specified distributed object which will interact with the
     * supplied callback functions.
     *
     * @param onAvailable a function to be called when the object in question is available. Looks
     * like: function (obj :DObject) :void
     * @param onFailure a function to be called if the subscription request fails. Looks like:
     * function (oid :int, error :ObjectAccessError) :void
     */
    public function SafeSubscriber (oid :int, onAvailable :Function, onFailure :Function)
    {
        // make sure they're not fucking us around
        if (oid <= 0) {
            throw new ArgumentError("Invalid oid provided to safesub [oid=" + oid + "]");
        }
        if (onAvailable == null) {
            throw new ArgumentError("Null onAvailable provided to safesub [oid=" + oid + "]");
        }
        if (onFailure == null) {
            throw new ArgumentError("Null onFailure provided to safesub [oid=" + oid + "]");
        }

        _oid = oid;
        _onAvailable = onAvailable;
        _onFailure = onFailure;
    }

    /**
     * Returns true if we are currently subscribed to our object (or in the process of obtaining a
     * subscription).
     */
    public function isActive () :Boolean
    {
        return _active;
    }

    /**
     * Initiates the subscription process.
     */
    public function subscribe (omgr :DObjectManager) :void
    {
        if (_active) {
            log.warning("Active safesub asked to resubscribe " + this);
            return;
        }

        // note that we are now again in the "wishing to be subscribed" state
        _active = true;

        // make sure we dont have an object reference (which should be logically impossible)
        if (_object != null) {
            log.warning("Incroyable! A safesub has an object and was non-active!? " + this);
            // make do in the face of insanity
            _onAvailable(_object);
            return;
        }

        if (_pending) {<commit_msg>Expose the DObject here. Then the client code needn't keep both a subscriber and an object lying around.<commit_after>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.util {

import com.threerings.util.Log;

import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.DObjectManager;
import com.threerings.presents.dobj.ObjectAccessError;
import com.threerings.presents.dobj.Subscriber;

/**
 * A class that safely handles the asynchronous subscription to a distributed object when it is not
 * know if the subscription will complete before the subscriber decides they no longer wish to be
 * subscribed.
 */
public class SafeSubscriber implements Subscriber
{
    /**
     * Creates a safe subscriber for the specified distributed object which will interact with the
     * supplied callback functions.
     *
     * @param onAvailable a function to be called when the object in question is available. Looks
     * like: function (obj :DObject) :void
     * @param onFailure a function to be called if the subscription request fails. Looks like:
     * function (oid :int, error :ObjectAccessError) :void
     */
    public function SafeSubscriber (oid :int, onAvailable :Function, onFailure :Function)
    {
        // make sure they're not fucking us around
        if (oid <= 0) {
            throw new ArgumentError("Invalid oid provided to safesub [oid=" + oid + "]");
        }
        if (onAvailable == null) {
            throw new ArgumentError("Null onAvailable provided to safesub [oid=" + oid + "]");
        }
        if (onFailure == null) {
            throw new ArgumentError("Null onFailure provided to safesub [oid=" + oid + "]");
        }

        _oid = oid;
        _onAvailable = onAvailable;
        _onFailure = onFailure;
    }

    /**
     * Returns true if we are currently subscribed to our object (or in the process of obtaining a
     * subscription).
     */
    public function isActive () :Boolean
    {
        return _active;
    }

    /**
     * Gets the object we are subscribed to, or null if the subscription is not yet complete.
     */
    public function getObject () :DObject
    {
        return _object;
    }

    /**
     * Initiates the subscription process.
     */
    public function subscribe (omgr :DObjectManager) :void
    {
        if (_active) {
            log.warning("Active safesub asked to resubscribe " + this);
            return;
        }

        // note that we are now again in the "wishing to be subscribed" state
        _active = true;

        // make sure we dont have an object reference (which should be logically impossible)
        if (_object != null) {
            log.warning("Incroyable! A safesub has an object and was non-active!? " + this);
            // make do in the face of insanity
            _onAvailable(_object);
            return;
        }

        if (_pending) {
{'fraction_non_alphanumeric': 0.08349328214971209, 'fraction_numerical': 0.006854949273375377, 'mean_word_length': 2.6078140454995054, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2366939', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1896, 'n_words': 831}