Skip to content

Instantly share code, notes, and snippets.

@dworznik

dworznik/id.js Secret

Created May 17, 2017 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dworznik/518ed1b943b6a1cf2fe55998e5089c02 to your computer and use it in GitHub Desktop.
Save dworznik/518ed1b943b6a1cf2fe55998e5089c02 to your computer and use it in GitHub Desktop.
/*======================================================================*\
ICBIaW50OiBtYWtlIHRoaXMgYXMgY2xvc2UgdG8gcHJvZHVjdGlvbi1yZWFkeSBzb3VyY2
UgY29kZSBhcyB5b3UgY2FuIQoKICBCb251cyBwb2ludHMgZm9yIHRlbGxpbmcgdXMgd2hh
dCB0aGlzIGRvZXMgaW4gcGxhaW4gdGVybXM6CgogICAgJycuam9pbihpdGVydG9vbHMuY2
hhaW4oKnppcChzWy0yOjotMl0sIHNbOjotMl0pKSk=
\*======================================================================*/
if (NAMESPACE == null
|| typeof (NAMESPACE) == 'undefined') {
NAMESPACE = {};
// Creates an object that allocates a new or references an
// existing very expensive resource associated with `id`
var resource = function (id) {
// Private data
var _all_ids = new Array();
var _closed = false;
var _id = id;
var _expensive_resource = null;
// Public data
var persona = {
};
// Public methods
var getExpensiveResource = function () {
return _expensive_resource;
}
persona.getExpensiveResource = getExpensiveResource;
var getId = function () {
return _id;
}
persona.getId = getId;
var close = function () {
delete _all_ids[_id];
this._closed = true;
}
persona.close = close;
// Private methods
function _lookupOrCreateExpensiveResourceById(id) {
_expensive_resource = _all_ids[id];
if (_expensive_resource == null) {
// Just pretend for the sake of this example
_expensive_resource = {
value: "I'm a very expensive resource associated with ID " + id
};
_all_ids[id] = _expensive_resource;
}
return _expensive_resource;
}
// Initialization
_expensive_resource = _lookupOrCreateExpensiveResourceById(id);
return persona;
}
NAMESPACE.resource = resource;
}
@wishtree-vindoria
Copy link

/======================================================================
ICBIaW50OiBtYWtlIHRoaXMgYXMgY2xvc2UgdG8gcHJvZHVjdGlvbi1yZWFkeSBzb3VyY2
UgY29kZSBhcyB5b3UgY2FuIQoKICBCb251cyBwb2ludHMgZm9yIHRlbGxpbmcgdXMgd2hh
dCB0aGlzIGRvZXMgaW4gcGxhaW4gdGVybXM6CgogICAgJycuam9pbihpdGVydG9vbHMuY2
hhaW4oKnppcChzWy0yOjotMl0sIHNbOjotMl0pKSk=
*======================================================================*/
var NAMESPACE;
if (NAMESPACE == null || typeof(NAMESPACE) == 'undefined') {

NAMESPACE = {};

// Creates an object that allocates a new or references an
// existing very expensive resource associated with `id`
var resource = function(id) {
    // Private data
    var _all_ids = new Array();
    var _closed = false;
    var _id = id;
    var _expensive_resource = null;

    // Public data
    var persona = {};

    // Private methods
    function _lookupOrCreateExpensiveResourceById(id) {
        _expensive_resource = _all_ids[id];
        if (_expensive_resource == null) {
            // Just pretend for the sake of this example
            _expensive_resource = {
                value: "I'm a very expensive resource associated with ID " + id
            };
            _all_ids[id] = _expensive_resource;
        }
        return _expensive_resource;
    }

    // Initialization
    _expensive_resource = _lookupOrCreateExpensiveResourceById(id);

    // Public methods
    var getExpensiveResource = function() {
        return _expensive_resource;
    };

    persona.getExpensiveResource = getExpensiveResource();
    var getId = function() {
        return _id;
    }

    persona.getId = getId();

    var close = function() {
        delete _all_ids[_id];
        this._closed = true;
        return _closed;
    };

    persona.close = close();
    return persona;
};
var rid;
NAMESPACE.resource = resource(rid);

}

@qirh
Copy link

qirh commented May 14, 2018

// First of all, I tested this code and in general, it works. I found the following error in NAMESPACE == null || typeof (NAMESPACE) == 'undefined' should be typeof (NAMESPACE) === 'undefined' || NAMESPACE === null in case NAMESPACE isn't defined it will error out instead of not entering the if block

// With that in mind, I think that the main problem with this code is that it is never deleting the expensive resource. The close function does not delete the resource from memory, unless that is done intentionally, it's bad practice and allocates too many resources.

@knowself
Copy link

No comment on this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment