]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/node_modules/event-kit/node_modules/grim/node_modules/emissary/lib/behavior.js
863c2f21e910d1423c8c19c3f77df9adc49f64d5
[rbdr/dotfiles] / atom / packages / vim-mode / node_modules / event-kit / node_modules / grim / node_modules / emissary / lib / behavior.js
1 (function() {
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; },
5 __slice = [].slice;
6
7 isEqual = require('underscore-plus').isEqual;
8
9 PropertyAccessors = require('property-accessors');
10
11 Signal = require('./signal');
12
13 module.exports = Behavior = (function(_super) {
14 __extends(Behavior, _super);
15
16 PropertyAccessors.includeInto(Behavior);
17
18 function 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();
23 }
24 Behavior.__super__.constructor.call(this, subscribeCallback = args.shift());
25 }
26
27 Behavior.prototype.retained = function() {
28 var _this = this;
29 this.subscribe(this, 'value-internal', function(value) {
30 return _this.value = value;
31 });
32 this.subscribe(this, 'value-subscription-added', function(handler) {
33 return handler(_this.value);
34 });
35 return typeof this.subscribeCallback === "function" ? this.subscribeCallback() : void 0;
36 };
37
38 Behavior.prototype.emit = function() {
39 var args, name;
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)));
43 }
44 return Behavior.__super__.emit.apply(this, arguments);
45 };
46
47 Behavior.prototype.getValue = function() {
48 if (!(this.retainCount > 0)) {
49 throw new Error("Subscribe to or retain this behavior before calling getValue");
50 }
51 return this.value;
52 };
53
54 Behavior.prototype.and = function(right) {
55 return helpers.combine(this, right, (function(leftValue, rightValue) {
56 return leftValue && rightValue;
57 })).distinctUntilChanged();
58 };
59
60 Behavior.prototype.or = function(right) {
61 return helpers.combine(this, right, (function(leftValue, rightValue) {
62 return leftValue || rightValue;
63 })).distinctUntilChanged();
64 };
65
66 Behavior.prototype.toBehavior = function() {
67 return this;
68 };
69
70 Behavior.prototype.lazyAccessor('changes', function() {
71 var source;
72 source = this;
73 return new Signal(function() {
74 var gotFirst,
75 _this = this;
76 gotFirst = false;
77 return this.subscribe(source, 'value', function() {
78 var metadata, value;
79 value = arguments[0], metadata = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
80 if (gotFirst) {
81 _this.emitValue.apply(_this, [value].concat(__slice.call(metadata)));
82 }
83 return gotFirst = true;
84 });
85 });
86 });
87
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);
94 });
95 }
96 predicate = predicateOrTargetValue;
97 return this.map(function(value) {
98 return !!predicate(value);
99 }).distinctUntilChanged().changes;
100 };
101
102 Behavior.prototype.becomesLessThan = function(targetValue) {
103 return this.becomes(function(value) {
104 return value < targetValue;
105 });
106 };
107
108 Behavior.prototype.becomesGreaterThan = function(targetValue) {
109 return this.becomes(function(value) {
110 return value > targetValue;
111 });
112 };
113
114 return Behavior;
115
116 })(Signal);
117
118 helpers = require('./helpers');
119
120 }).call(this);