11import { assert } from '@ember/debug' ;
22import type { ElementDescriptor , ExtendedMethodDecorator } from '@ember/-internals/metal' ;
3- import {
4- isElementDescriptor ,
5- setClassicDecorator ,
6- hasListeners ,
7- addObserver ,
8- removeObserver ,
9- get ,
10- set ,
11- } from '@ember/-internals/metal' ;
3+ import { isElementDescriptor , setClassicDecorator , get , set } from '@ember/-internals/metal' ;
124import { getFactoryFor } from '@ember/-internals/container' ;
135import CoreObject from '@ember/object/core' ;
146import { peekMeta } from '@ember/-internals/meta' ;
@@ -24,10 +16,6 @@ export {
2416 trySet ,
2517} from '@ember/-internals/metal' ;
2618
27- type ObserverMethod < Target , Sender > =
28- | ( keyof Target & string )
29- | ( ( this : Target , sender : Sender , key : string , value : any , rev : number ) => void ) ;
30-
3119/**
3220@module @ember /object
3321*/
@@ -40,149 +28,6 @@ type ObserverMethod<Target, Sender> =
4028 @public
4129*/
4230class EmberObject extends CoreObject {
43- /**
44- Adds an observer on a property.
45-
46- This is the core method used to register an observer for a property.
47-
48- Once you call this method, any time the key's value is set, your observer
49- will be notified. Note that the observers are triggered any time the
50- value is set, regardless of whether it has actually changed. Your
51- observer should be prepared to handle that.
52-
53- There are two common invocation patterns for `.addObserver()`:
54-
55- - Passing two arguments:
56- - the name of the property to observe (as a string)
57- - the function to invoke (an actual function)
58- - Passing three arguments:
59- - the name of the property to observe (as a string)
60- - the target object (will be used to look up and invoke a
61- function on)
62- - the name of the function to invoke on the target object
63- (as a string).
64-
65- ```app/components/my-component.js
66- import Component from '@ember/component';
67-
68- export default Component.extend({
69- init() {
70- this._super(...arguments);
71-
72- // the following are equivalent:
73-
74- // using three arguments
75- this.addObserver('foo', this, 'fooDidChange');
76-
77- // using two arguments
78- this.addObserver('foo', (...args) => {
79- this.fooDidChange(...args);
80- });
81- },
82-
83- fooDidChange() {
84- // your custom logic code
85- }
86- });
87- ```
88-
89- ### Observer Methods
90-
91- Observer methods have the following signature:
92-
93- ```app/components/my-component.js
94- import Component from '@ember/component';
95-
96- export default Component.extend({
97- init() {
98- this._super(...arguments);
99- this.addObserver('foo', this, 'fooDidChange');
100- },
101-
102- fooDidChange(sender, key, value, rev) {
103- // your code
104- }
105- });
106- ```
107-
108- The `sender` is the object that changed. The `key` is the property that
109- changes. The `value` property is currently reserved and unused. The `rev`
110- is the last property revision of the object when it changed, which you can
111- use to detect if the key value has really changed or not.
112-
113- Usually you will not need the value or revision parameters at
114- the end. In this case, it is common to write observer methods that take
115- only a sender and key value as parameters or, if you aren't interested in
116- any of these values, to write an observer that has no parameters at all.
117-
118- @method addObserver
119- @param {String } key The key to observe
120- @param {Object } target The target object to invoke
121- @param {String|Function } method The method to invoke
122- @param {Boolean } sync Whether the observer is sync or not
123- @return {Observable }
124- @public
125- */
126- addObserver < Target extends object | Function | null > (
127- key : keyof this & string ,
128- target : Target ,
129- method : ObserverMethod < Target , this>
130- ) : this;
131- addObserver ( key : keyof this & string , method : ObserverMethod < this, this> ) : this;
132- addObserver < Target extends object | Function | null > (
133- key : string ,
134- target : Target ,
135- method ?: ObserverMethod < Target , this> ,
136- sync ?: boolean
137- ) {
138- addObserver ( this , key , target , method , sync ) ;
139- return this ;
140- }
141-
142- /**
143- Remove an observer you have previously registered on this object. Pass
144- the same key, target, and method you passed to `addObserver()` and your
145- target will no longer receive notifications.
146-
147- @method removeObserver
148- @param {String } key The key to observe
149- @param {Object } target The target object to invoke
150- @param {String|Function } method The method to invoke
151- @param {Boolean } sync Whether the observer is async or not
152- @return {Observable }
153- @public
154- */
155- removeObserver < Target extends object | Function | null > (
156- key : keyof this & string ,
157- target : Target ,
158- method : ObserverMethod < Target , this>
159- ) : this;
160- removeObserver ( key : keyof this & string , method : ObserverMethod < this, this> ) : this;
161- removeObserver < Target extends object | Function | null > (
162- key : string ,
163- target : Target ,
164- method ?: string | Function ,
165- sync ?: boolean
166- ) {
167- removeObserver ( this , key , target , method , sync ) ;
168- return this ;
169- }
170-
171- /**
172- Returns `true` if the object currently has observers registered for a
173- particular key. You can use this method to potentially defer performing
174- an expensive action until someone begins observing a particular property
175- on the object.
176-
177- @method hasObserverFor
178- @param {String } key Key to check
179- @return {Boolean }
180- @private
181- */
182- hasObserverFor ( key : string ) {
183- return hasListeners ( this , `${ key } :change` ) ;
184- }
185-
18631 // NOT TYPE SAFE!
18732 /**
18833 Set the value of a property to the current value plus some amount.
0 commit comments