Skip to content

Commit 76ed26e

Browse files
committed
Merge pull request DefinitelyTyped#5764 from herrmanno/master
Added observe-js definition
2 parents 42aea6d + 135ca5e commit 76ed26e

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed

observe-js/observe-js-test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/// <reference path="observe-js.d.ts" />
2+
3+
module observejs {
4+
5+
function Test_PathObserver() {
6+
var obj = { foo: { bar: 'baz' } };
7+
var defaultValue = 42;
8+
var observer = new PathObserver(obj, 'foo.bar', defaultValue);
9+
observer.open(function(newValue, oldValue) {
10+
// respond to obj.foo.bar having changed value.
11+
});
12+
}
13+
14+
15+
function Test_ArrayObserver() {
16+
var arr = [0, 1, 2, 4];
17+
var observer = new ArrayObserver(arr);
18+
observer.open(function(splices) {
19+
// respond to changes to the elements of arr.
20+
splices.forEach(function(splice) {
21+
splice.index; // the index position that the change occurred.
22+
splice.removed; // an array of values representing the sequence of removed elements
23+
splice.addedCount; // the number of elements which were inserted.
24+
});
25+
});
26+
}
27+
28+
function Test_ObejctObserver() {
29+
var myObj = { id: 1, foo: 'bar' };
30+
var observer = new ObjectObserver(myObj);
31+
observer.open(function(added, removed, changed, getOldValueFn) {
32+
// respond to changes to the obj.
33+
Object.keys(added).forEach(function(property) {
34+
property; // a property which has been been added to obj
35+
added[property]; // its value
36+
});
37+
Object.keys(removed).forEach(function(property) {
38+
property; // a property which has been been removed from obj
39+
getOldValueFn(property); // its old value
40+
});
41+
Object.keys(changed).forEach(function(property) {
42+
property; // a property on obj which has changed value.
43+
changed[property]; // its value
44+
getOldValueFn(property); // its old value
45+
});
46+
});
47+
}
48+
49+
function Test_CompounObserver() {
50+
var obj = {
51+
a: 1,
52+
b: 2,
53+
};
54+
55+
var otherObj = { c: 3 };
56+
57+
var observer = new CompoundObserver();
58+
observer.addPath(obj, 'a');
59+
observer.addObserver(new PathObserver(obj, 'b'));
60+
observer.addPath(otherObj, 'c');
61+
var logTemplate = 'The %sth value before & after:';
62+
observer.open(function(newValues, oldValues) {
63+
// Use for-in to iterate which values have changed.
64+
for (var i in oldValues) {
65+
console.log(logTemplate, i, oldValues[i], newValues[i]);
66+
}
67+
});
68+
}
69+
70+
function Test_ObserverTransform_1() {
71+
var obj = { value: 10 };
72+
var observer = new PathObserver(obj, 'value');
73+
function getValue(value:any) { return value * 2 };
74+
function setValue(value:any) { return value / 2 };
75+
76+
var transform = new ObserverTransform(observer, getValue, setValue);
77+
78+
// returns 20.
79+
transform.open(function(newValue, oldValue) {
80+
console.log('new: ' + newValue + ', old: ' + oldValue);
81+
});
82+
83+
obj.value = 20;
84+
transform.deliver(); // 'new: 40, old: 20'
85+
transform.setValue(4); // obj.value === 2;
86+
}
87+
88+
function Test_ObserverTransform_2() {
89+
var obj = { a: 1, b: 2, c: 3 };
90+
var observer = new CompoundObserver();
91+
observer.addPath(obj, 'a');
92+
observer.addPath(obj, 'b');
93+
observer.addPath(obj, 'c');
94+
var transform = new ObserverTransform(observer, function(values) {
95+
var value = 0;
96+
for (var i = 0; i < values.length; i++)
97+
value += values[i]
98+
return value;
99+
});
100+
101+
// returns 6.
102+
transform.open(function(newValue, oldValue) {
103+
console.log('new: ' + newValue + ', old: ' + oldValue);
104+
});
105+
106+
obj.a = 2;
107+
obj.c = 10;
108+
transform.deliver(); // 'new: 14, old: 6'
109+
}
110+
111+
}

observe-js/observe-js.d.ts

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
// Type definitions for observe-js v0.5.5
2+
// Project: https://github.com/Polymer/observe-js
3+
// Definitions by: Oliver Herrmann <https://github.com/herrmanno/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module observejs {
7+
8+
/*----------------------
9+
Observable
10+
----------------------*/
11+
12+
interface Observable {
13+
/**
14+
* Begins observation.
15+
* @param onChange the function that gets invoked if a change is detected
16+
* @param the target of observation
17+
*/
18+
open(onChange:(newValue:any, oldValue:any)=>any, receiver?:any):void
19+
20+
/**
21+
* Report any changes now (does nothing if there are no changes to report).
22+
*/
23+
deliver(): void
24+
25+
/**
26+
* If there are changes to report, ignore them. Returns the current value of the observation.
27+
*/
28+
discardChanges():void
29+
30+
/**
31+
* Ends observation. Frees resources and drops references to observed objects.
32+
*/
33+
close():void
34+
}
35+
36+
37+
/*----------------------
38+
PathObserver
39+
----------------------*/
40+
41+
interface PathObserver_static {
42+
/**
43+
* Constructor
44+
* @param receiver the target for observation
45+
* @param path specifies the paht to observe. If path === '' the receiver itself gets observed.
46+
* @param defaultValue the defaultValue
47+
*/
48+
new(receiver:any, path:string, defaultValue?:any): PathObserver_instance
49+
}
50+
51+
interface PathObserver_instance extends Observable {
52+
/**
53+
* sets the observed value without notifying about the change.
54+
* @param value the value to set
55+
*/
56+
setValue(value:any): void
57+
}
58+
59+
/**
60+
* Observes a "value-at-a-path" from a given object:
61+
*/
62+
var PathObserver: PathObserver_static
63+
64+
65+
/*----------------------
66+
ArrayObserver
67+
----------------------*/
68+
69+
interface splice {
70+
71+
/**
72+
* the index position that the change occured
73+
*/
74+
index:number
75+
76+
/**
77+
* an array of values representing the sequence of removed elements
78+
*/
79+
removed: Array<any>
80+
81+
/**
82+
* the number of element which were inserted
83+
*/
84+
addedCount:number
85+
}
86+
87+
interface ArrayObserver_static {
88+
89+
/**
90+
* Constructor
91+
* @param receiver the target for observation
92+
*/
93+
new(receiver:Array<any>): ArrayObserver_instance
94+
95+
/**
96+
* transforms a copy of an old state of an array into a copy of its current state.
97+
* @param previous array of old state
98+
* @param current array of current state
99+
* @param splices splices to apply
100+
*/
101+
applySplices(previous:Array<any>, current:Array<any>, splices:Array<splice>):void
102+
}
103+
104+
interface ArrayObserver_instance extends Observable {
105+
open(onChange:(splices:Array<splice>)=>any):void
106+
}
107+
108+
/**
109+
* ArrayObserver observes the index-positions of an Array and reports changes as the minimal set of "splices" which would have had the same effect.
110+
*/
111+
var ArrayObserver: ArrayObserver_static
112+
113+
114+
/*----------------------
115+
ObjectObserver
116+
----------------------*/
117+
118+
interface Properties {
119+
[key:string]:any
120+
}
121+
122+
interface ObjectObserver_static {
123+
124+
/**
125+
* Constructor
126+
* @param receiver the target for observation
127+
*/
128+
new(receiver:any): ObjectObserver_instance
129+
}
130+
131+
interface ObjectObserver_instance extends Observable {
132+
open(onChange:(added:Properties, removed:Properties, changed:Properties, getOldValueFn:(property:string)=>any)=>any):void
133+
}
134+
135+
/**
136+
* Observes the set of own-properties of an object and their values
137+
*/
138+
var ObjectObserver: ObjectObserver_static
139+
140+
141+
/*----------------------
142+
CompounObserver
143+
----------------------*/
144+
145+
interface CompoundObserver_static {
146+
147+
/**
148+
* Constructor
149+
*/
150+
new(): CompoundObserver_instance
151+
}
152+
153+
interface CompoundObserver_instance extends Observable {
154+
open(onChange:(newValues:Array<any>, oldValue:Array<any>)=>any):void
155+
156+
/**
157+
* Adds the receivers property at the specified path to the list of observables.
158+
* @param receiver the target for observation
159+
* @param path specifies the paht to observe. If path === '' the receiver itself gets observed.
160+
*/
161+
addPath(receiver:any, path:string):void
162+
163+
/**
164+
* Adds an Observer to the list of observables.
165+
*/
166+
addObserver(observer:Observable):void
167+
168+
}
169+
170+
/**
171+
* CompoundObserver allows simultaneous observation of multiple paths and/or Observables.
172+
*/
173+
var CompoundObserver: CompoundObserver_static
174+
175+
176+
177+
/*----------------------
178+
ObserverTransform
179+
----------------------*/
180+
181+
interface ObserverTransform_static {
182+
183+
/**
184+
* Constructor
185+
* @param observer the observer to transform
186+
* @param getValue function that proxys getting a value
187+
* @param setValue function that proxys setting a value
188+
*/
189+
new(observer:Observable, getValue:(value:any)=>any, setValue:(value:any)=>any): ObserverTransform_instance
190+
191+
/**
192+
* Constructor
193+
* @param observer the observer to transform
194+
* @param valueFn function that gets invoked with all observed values. May return a single new value.
195+
*/
196+
new(observer:Observable, valueFn:(values:Array<any>)=>any): ObserverTransform_instance
197+
}
198+
199+
interface ObserverTransform_instance extends Observable {
200+
/**
201+
* sets the observed value without notifying about the change.
202+
* @param value the value to set
203+
*/
204+
setValue(value:any): void
205+
}
206+
207+
/**
208+
* CompoundObserver allows simultaneous observation of multiple paths and/or Observables.
209+
*/
210+
var ObserverTransform: ObserverTransform_static
211+
212+
213+
/*----------------------
214+
Path
215+
----------------------*/
216+
217+
interface Path {
218+
219+
/**
220+
* Returns the current value of the path from the provided object. If eval() is available,
221+
* a compiled getter will be used for better performance. Like PathObserver above, undefined
222+
* is returned unless you provide an overriding defaultValue.
223+
*/
224+
getValueFrom(object:any, defaultValue:any): any
225+
226+
/**
227+
* Attempts to set the value of the path from the provided object. Returns true IFF the path
228+
* was reachable and set.
229+
*/
230+
getValueFrom(object:any, newValue:any): any
231+
}
232+
}
233+
234+
declare module "observejs" {
235+
var PathObserver: typeof observejs.PathObserver;
236+
var ArrayObserver: typeof observejs.ArrayObserver;
237+
var ObjectObserver: typeof observejs.ObjectObserver;
238+
var CompoundObserver: typeof observejs.CompoundObserver;
239+
var ObserverTransform: typeof observejs.ObserverTransform;
240+
var Path: observejs.Path;
241+
242+
export {
243+
PathObserver,
244+
ArrayObserver,
245+
ObjectObserver,
246+
CompoundObserver,
247+
ObserverTransform,
248+
Path
249+
};
250+
}

0 commit comments

Comments
 (0)