Skip to content

Instantly share code, notes, and snippets.

@killwing
Created November 19, 2012 08:00
Show Gist options
  • Save killwing/4109513 to your computer and use it in GitHub Desktop.
Save killwing/4109513 to your computer and use it in GitHub Desktop.
[Registry] a simple registry without moving elements
#!/usr/local/bin/node
var Registry = function() {
this.r = [null];
};
Registry.prototype.register = function(object) {
var id = this.r[0];
if (id === null) {
this.r.push(object);
return this.r.length - 1;
} else {
this.r[0] = this.r[id];
this.r[id] = object;
return id;
}
};
Registry.prototype.unregister = function(id) {
this.r[id] = this.r[0];
this.r[0] = id;
};
var reg = new Registry();
reg.register('module1');
var id = reg.register('module2');
reg.register('module3');
reg.unregister(id);
reg.register('new_module2');
console.log(reg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment