-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbind.directive.ts
55 lines (47 loc) · 1.16 KB
/
bind.directive.ts
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
import { Directive, ElementRef, Inject, Input } from '@angular/core';
import { Observable, SubscriptionLike, isObservable } from 'rxjs';
import { AuxSubscriptions } from './subscriptions.model';
import { DebounceBinding } from '@deuso/aux';
@Directive({
selector: '[auxBind]'
})
export class AuxBindDirective extends AuxSubscriptions
{
private bindings = new Map<string,DebounceBinding>();
protected remove_binding(widget: any, name: string)
{
let binding = this.bindings.get(name);
if (binding)
{
this.bindings.delete(name);
binding.destroy();
}
}
protected install_binding(widget: any, name: string, b: any)
: SubscriptionLike
{
let binding = this.bindings.get(name);
if (!binding)
{
this.bindings.set(name, binding = new DebounceBinding(widget, name, 250));
}
if (isObservable(b))
{
const o = b as Observable<any>;
return o.subscribe((v) => {
binding.set(v);
});
}
else
{
binding.set(b);
// nothing was subscribed
return null;
}
}
@Input('auxBind')
set auxBind(bindings: {[name: string]: any}|null)
{
this.update(bindings);
}
}