]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/node_modules/grim/node_modules/emissary/lib/behavior.js
863c2f21e910d1423c8c19c3f77df9adc49f64d5
2 var Behavior
, PropertyAccessors
, Signal
, helpers
, isEqual
,
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
; },
7 isEqual
= require('underscore-plus').isEqual
;
9 PropertyAccessors
= require('property-accessors');
11 Signal
= require('./signal');
13 module
.exports
= Behavior
= (function(_super
) {
14 __extends(Behavior
, _super
);
16 PropertyAccessors
.includeInto(Behavior
);
19 var args
, subscribeCallback
, _ref
;
20 args
= 1 <= arguments
.length
? __slice
.call(arguments
, 0) : [];
21 if (typeof ((_ref
= args
[0]) != null ? _ref
.call : void 0) !== 'function') {
22 this.value
= args
.shift();
24 Behavior
.__super__
.constructor.call(this, subscribeCallback
= args
.shift());
27 Behavior
.prototype.retained = function() {
29 this.subscribe(this, 'value-internal', function(value
) {
30 return _this
.value
= value
;
32 this.subscribe(this, 'value-subscription-added', function(handler
) {
33 return handler(_this
.value
);
35 return typeof this.subscribeCallback
=== "function" ? this.subscribeCallback() : void 0;
38 Behavior
.prototype.emit = function() {
40 name
= arguments
[0], args
= 2 <= arguments
.length
? __slice
.call(arguments
, 1) : [];
41 if (name
=== 'value') {
42 this.emit
.apply(this, ['value-internal'].concat(__slice
.call(args
)));
44 return Behavior
.__super__
.emit
.apply(this, arguments
);
47 Behavior
.prototype.getValue = function() {
48 if (!(this.retainCount
> 0)) {
49 throw new Error("Subscribe to or retain this behavior before calling getValue");
54 Behavior
.prototype.and = function(right
) {
55 return helpers
.combine(this, right
, (function(leftValue
, rightValue
) {
56 return leftValue
&& rightValue
;
57 })).distinctUntilChanged();
60 Behavior
.prototype.or = function(right
) {
61 return helpers
.combine(this, right
, (function(leftValue
, rightValue
) {
62 return leftValue
|| rightValue
;
63 })).distinctUntilChanged();
66 Behavior
.prototype.toBehavior = function() {
70 Behavior
.prototype.lazyAccessor('changes', function() {
73 return new Signal(function() {
77 return this.subscribe(source
, 'value', function() {
79 value
= arguments
[0], metadata
= 2 <= arguments
.length
? __slice
.call(arguments
, 1) : [];
81 _this
.emitValue
.apply(_this
, [value
].concat(__slice
.call(metadata
)));
83 return gotFirst
= true;
88 Behavior
.prototype.becomes = function(predicateOrTargetValue
) {
89 var predicate
, targetValue
;
90 if (typeof predicateOrTargetValue
!== 'function') {
91 targetValue
= predicateOrTargetValue
;
92 return this.becomes(function(value
) {
93 return isEqual(value
, targetValue
);
96 predicate
= predicateOrTargetValue
;
97 return this.map(function(value
) {
98 return !!predicate(value
);
99 }).distinctUntilChanged().changes
;
102 Behavior
.prototype.becomesLessThan = function(targetValue
) {
103 return this.becomes(function(value
) {
104 return value
< targetValue
;
108 Behavior
.prototype.becomesGreaterThan = function(targetValue
) {
109 return this.becomes(function(value
) {
110 return value
> targetValue
;
118 helpers
= require('./helpers');