aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/labs/observe/observationset.js
blob: 892c3c6cf1d07970ebb4580e87acccf00adc3bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview A set of observations. This set provides a convenient
 * means of observing many observables at once.
 *
 * This is similar in purpose to {@code goog.events.EventHandler}.
 *
 */

goog.provide('goog.labs.observe.ObservationSet');

goog.require('goog.array');
goog.require('goog.labs.observe.Observer');



/**
 * A set of observations. An observation is defined by an observable
 * and an observer. The set keeps track of observations and
 * allows their removal.
 * @param {!Object=} opt_defaultScope Optional function scope to use
 *     when using {@code observeWithFunction} and
 *     {@code unobserveWithFunction}.
 * @constructor
 */
goog.labs.observe.ObservationSet = function(opt_defaultScope) {
  /**
   * @type {!Array.<!goog.labs.observe.ObservationSet.Observation_>}
   * @private
   */
  this.storedObservations_ = [];

  /**
   * @type {!Object|undefined}
   * @private
   */
  this.defaultScope_ = opt_defaultScope;
};


/**
 * Observes the given observer on the observable.
 * @param {!goog.labs.observe.Observable} observable The observable to
 *     observe on.
 * @param {!goog.labs.observe.Observer} observer The observer.
 * @return {boolean} True if the observer is successfully registered.
 */
goog.labs.observe.ObservationSet.prototype.observe = function(
    observable, observer) {
  var success = observable.observe(observer);
  if (success) {
    this.storedObservations_.push(
        new goog.labs.observe.ObservationSet.Observation_(
            observable, observer));
  }
  return success;
};


/**
 * Observes the given function on the observable.
 * @param {!goog.labs.observe.Observable} observable The observable to
 *     observe on.
 * @param {function(!goog.labs.observe.Notice)} fn The handler function.
 * @param {!Object=} opt_scope Optional scope.
 * @return {goog.labs.observe.Observer} The registered observer object.
 *     If the observer is not successfully registered, this will be null.
 */
goog.labs.observe.ObservationSet.prototype.observeWithFunction = function(
    observable, fn, opt_scope) {
  var observer = goog.labs.observe.Observer.fromFunction(
      fn, opt_scope || this.defaultScope_);
  if (this.observe(observable, observer)) {
    return observer;
  }
  return null;
};


/**
 * Unobserves the given observer from the observable.
 * @param {!goog.labs.observe.Observable} observable The observable to
 *     unobserve from.
 * @param {!goog.labs.observe.Observer} observer The observer.
 * @return {boolean} True if the observer is successfully removed.
 */
goog.labs.observe.ObservationSet.prototype.unobserve = function(
    observable, observer) {
  var removed = goog.array.removeIf(
      this.storedObservations_, function(o) {
        return o.observable == observable &&
            goog.labs.observe.Observer.equals(o.observer, observer);
      });

  if (removed) {
    observable.unobserve(observer);
  }
  return removed;
};


/**
 * Unobserves the given function from the observable.
 * @param {!goog.labs.observe.Observable} observable The observable to
 *     unobserve from.
 * @param {function(!goog.labs.observe.Notice)} fn The handler function.
 * @param {!Object=} opt_scope Optional scope.
 * @return {boolean} True if the observer is successfully removed.
 */
goog.labs.observe.ObservationSet.prototype.unobserveWithFunction = function(
    observable, fn, opt_scope) {
  var observer = goog.labs.observe.Observer.fromFunction(
      fn, opt_scope || this.defaultScope_);
  return this.unobserve(observable, observer);
};


/**
 * Removes all observations registered through this set.
 */
goog.labs.observe.ObservationSet.prototype.removeAll = function() {
  goog.array.forEach(this.storedObservations_, function(observation) {
    var observable = observation.observable;
    var observer = observation.observer;
    observable.unobserve(observer);
  });
};



/**
 * A representation of an observation, which is defined uniquely by
 * the observable and observer.
 * @param {!goog.labs.observe.Observable} observable The observable.
 * @param {!goog.labs.observe.Observer} observer The observer.
 * @constructor
 * @private
 */
goog.labs.observe.ObservationSet.Observation_ = function(
    observable, observer) {
  this.observable = observable;
  this.observer = observer;
};