]> git.r.bdr.sh - rbdr/dotfiles/blob
4cb92d8936cda81318ae7e870acd6665818840c3
[rbdr/dotfiles] /
1 (function() {
2 var Mixin, PropertyAccessors, WeakMap, _ref, _ref1,
3 __hasProp = {}.hasOwnProperty,
4 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
5
6 Mixin = require('mixto');
7
8 WeakMap = (_ref = global.WeakMap) != null ? _ref : require('es6-weak-map');
9
10 module.exports = PropertyAccessors = (function(_super) {
11 __extends(PropertyAccessors, _super);
12
13 function PropertyAccessors() {
14 _ref1 = PropertyAccessors.__super__.constructor.apply(this, arguments);
15 return _ref1;
16 }
17
18 PropertyAccessors.prototype.accessor = function(name, definition) {
19 if (typeof definition === 'function') {
20 definition = {
21 get: definition
22 };
23 }
24 return Object.defineProperty(this, name, definition);
25 };
26
27 PropertyAccessors.prototype.advisedAccessor = function(name, definition) {
28 var getAdvice, setAdvice, values;
29 if (typeof definition === 'function') {
30 getAdvice = definition;
31 } else {
32 getAdvice = definition.get;
33 setAdvice = definition.set;
34 }
35 values = new WeakMap;
36 return this.accessor(name, {
37 get: function() {
38 if (getAdvice != null) {
39 getAdvice.call(this);
40 }
41 return values.get(this);
42 },
43 set: function(newValue) {
44 if (setAdvice != null) {
45 setAdvice.call(this, newValue, values.get(this));
46 }
47 return values.set(this, newValue);
48 }
49 });
50 };
51
52 PropertyAccessors.prototype.lazyAccessor = function(name, definition) {
53 var values;
54 values = new WeakMap;
55 return this.accessor(name, {
56 get: function() {
57 if (values.has(this)) {
58 return values.get(this);
59 } else {
60 values.set(this, definition.call(this));
61 return values.get(this);
62 }
63 },
64 set: function(value) {
65 return values.set(this, value);
66 }
67 });
68 };
69
70 return PropertyAccessors;
71
72 })(Mixin);
73
74 }).call(this);