Skip to content

Instantly share code, notes, and snippets.

@jcmoore
Last active February 25, 2017 20:13
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 jcmoore/cab5bcabbe9ec3172b50dc145ec3b970 to your computer and use it in GitHub Desktop.
Save jcmoore/cab5bcabbe9ec3172b50dc145ec3b970 to your computer and use it in GitHub Desktop.
managable.js -- primitives for facilitating "reference counted" resources
function managableFactory () {
var _broken = Promise.resolve(null);
function Managable () {
}
Managable.prototype.rref = function () {
return null;
};
Managable.prototype.rretain = function () {
return this;
};
Managable.prototype.rrelease = function () {
return this;
};
Managable.prototype.rreclaimed = function () {
return _broken;
};
var _unobservable = new Managable();
var _invalid = new Helper(0|!!"unobservable");
_invalid.retainCount = 0;
function _noop () {}
function Helper (unobservable) {
var abandon;
this.retainCount = 1;
this.thenable = unobservable ?
_broken : new Promise(function (resolve) { abandon = resolve; });
this.abandon = unobservable ?
_noop : abandon;
}
return function managedFactory (silent, minimal, unsafe, loud) {
var _safe = unsafe ? null : new WeakMap();
var Managed = unsafe && loud ? function () {
this[unsafe] = new Helper(0|!"unobservable");
} : unsafe ? function () {
this[unsafe] = 1;
} : minimal ? function Managed () {
_safe.set(this, 1);
} : silent ? function Managed () {
_safe.set(this, new Helper(0|!!"unobservable"));
} : function Managed () {
_safe.set(this, new Helper(0|!"unobservable"));
};
Managed.prototype = new Managable();
Managed.prototype.rref = unsafe && loud ? function nested () {
return ((this[unsafe] || _invalid).retainCount > 0) ? this : null;
} : unsafe ? function unrestricted () {
return (0|this[unsafe] > 0) ? this : null;
} : minimal ? function helpless () {
var _retainCount = 0|_safe.get(this);
return (_retainCount > 0) ? this : null;
} : function helpful () {
var _helper = _safe.get(this) || _invalid;
return (_helper.retainCount > 0) ? this : null;
};
Managed.prototype.rretain = unsafe && loud ? function nested () {
var _helper = this[unsafe] || _invalid;
if (_helper.retainCount > 0) _helper.retainCount++;
return this;
} : unsafe ? function unrestricted () {
var _retainCount = 0|this[unsafe];
if (_retainCount > 0) this[unsafe] = _retainCount + 1;
return this;
} : minimal ? function helpless () {
var _retainCount = 0|_safe.get(this);
if (_retainCount > 0) _safe.set(++_retainCount);
return this;
} : function helpful () {
var _helper = _safe.get(this) || _invalid;
if (_helper.retainCount > 0) _helper.retainCount++;
return this;
};
Managed.prototype.rrelease = unsafe && loud ? function nested () {
var _helper = this[unsafe] || _invalid;
if (_helper.retainCount > 0 && --_helper.retainCount < 1) (0, _helper.abandon)(this);
return this;
} : unsafe ? function unrestricted () {
var _retainCount = 0|this[unsafe];
if (_retainCount > 0) this[unsafe] = _retainCount - 1;
return this;
} : minimal ? function helpless () {
var _retainCount = 0|_safe.get(this);
if (_retainCount > 0) _safe.set(--_retainCount);
return this;
} : function helpful () {
var _helper = _safe.get(this) || _invalid;
if (_helper.retainCount > 0 && --_helper.retainCount < 1) (0, _helper.abandon)(this);
return this;
};
Managed.prototype.rreclaimed = unsafe && loud ? function nested () {
return (this[unsafe] || _invalid).thenable;
} : unsafe ? function unrestricted () {
return _broken;
} : minimal ? function helpless () {
return _broken;
} : function helpful () {
var _helper = _safe.get(this) || _invalid;
return _helper.thenable;
};
return Managed;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment