-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservable.ts
More file actions
35 lines (35 loc) · 854 Bytes
/
observable.ts
File metadata and controls
35 lines (35 loc) · 854 Bytes
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
class Subject<T>{
private state$:T | any;
private callbacks:any[];
constructor(_state?:T){
this.state$=_state || undefined;
this.callbacks=[];
}
subscribe(callback:(value:T | any)=>void){
this.callbacks.push(callback);
}
next(_value:T){
this.state$=_value;
this.callbacks.forEach(call=>{
call(this.state$);
})
}
}
class Observable<T>{
private state$:T | any;
private callback:(res:any)=>void;
constructor(_state?:T){
this.state$=_state || undefined;
this.callback=()=>0;
}
subscribe(callback:(value:T | any)=>void){
this.callback=callback;
}
next(_value:T){
this.state$=_value;
this.callback(this.state$);
}
map(callback:(value:T)=>any):T{
return callback(this.state$);
}
}