1 | import { Subject } from '../Subject';
|
---|
2 | import { Subscription } from '../Subscription';
|
---|
3 | import { OuterSubscriber } from '../OuterSubscriber';
|
---|
4 | import { subscribeToResult } from '../util/subscribeToResult';
|
---|
5 | export function windowToggle(openings, closingSelector) {
|
---|
6 | return (source) => source.lift(new WindowToggleOperator(openings, closingSelector));
|
---|
7 | }
|
---|
8 | class WindowToggleOperator {
|
---|
9 | constructor(openings, closingSelector) {
|
---|
10 | this.openings = openings;
|
---|
11 | this.closingSelector = closingSelector;
|
---|
12 | }
|
---|
13 | call(subscriber, source) {
|
---|
14 | return source.subscribe(new WindowToggleSubscriber(subscriber, this.openings, this.closingSelector));
|
---|
15 | }
|
---|
16 | }
|
---|
17 | class WindowToggleSubscriber extends OuterSubscriber {
|
---|
18 | constructor(destination, openings, closingSelector) {
|
---|
19 | super(destination);
|
---|
20 | this.openings = openings;
|
---|
21 | this.closingSelector = closingSelector;
|
---|
22 | this.contexts = [];
|
---|
23 | this.add(this.openSubscription = subscribeToResult(this, openings, openings));
|
---|
24 | }
|
---|
25 | _next(value) {
|
---|
26 | const { contexts } = this;
|
---|
27 | if (contexts) {
|
---|
28 | const len = contexts.length;
|
---|
29 | for (let i = 0; i < len; i++) {
|
---|
30 | contexts[i].window.next(value);
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 | _error(err) {
|
---|
35 | const { contexts } = this;
|
---|
36 | this.contexts = null;
|
---|
37 | if (contexts) {
|
---|
38 | const len = contexts.length;
|
---|
39 | let index = -1;
|
---|
40 | while (++index < len) {
|
---|
41 | const context = contexts[index];
|
---|
42 | context.window.error(err);
|
---|
43 | context.subscription.unsubscribe();
|
---|
44 | }
|
---|
45 | }
|
---|
46 | super._error(err);
|
---|
47 | }
|
---|
48 | _complete() {
|
---|
49 | const { contexts } = this;
|
---|
50 | this.contexts = null;
|
---|
51 | if (contexts) {
|
---|
52 | const len = contexts.length;
|
---|
53 | let index = -1;
|
---|
54 | while (++index < len) {
|
---|
55 | const context = contexts[index];
|
---|
56 | context.window.complete();
|
---|
57 | context.subscription.unsubscribe();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | super._complete();
|
---|
61 | }
|
---|
62 | _unsubscribe() {
|
---|
63 | const { contexts } = this;
|
---|
64 | this.contexts = null;
|
---|
65 | if (contexts) {
|
---|
66 | const len = contexts.length;
|
---|
67 | let index = -1;
|
---|
68 | while (++index < len) {
|
---|
69 | const context = contexts[index];
|
---|
70 | context.window.unsubscribe();
|
---|
71 | context.subscription.unsubscribe();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
---|
76 | if (outerValue === this.openings) {
|
---|
77 | let closingNotifier;
|
---|
78 | try {
|
---|
79 | const { closingSelector } = this;
|
---|
80 | closingNotifier = closingSelector(innerValue);
|
---|
81 | }
|
---|
82 | catch (e) {
|
---|
83 | return this.error(e);
|
---|
84 | }
|
---|
85 | const window = new Subject();
|
---|
86 | const subscription = new Subscription();
|
---|
87 | const context = { window, subscription };
|
---|
88 | this.contexts.push(context);
|
---|
89 | const innerSubscription = subscribeToResult(this, closingNotifier, context);
|
---|
90 | if (innerSubscription.closed) {
|
---|
91 | this.closeWindow(this.contexts.length - 1);
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | innerSubscription.context = context;
|
---|
95 | subscription.add(innerSubscription);
|
---|
96 | }
|
---|
97 | this.destination.next(window);
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | this.closeWindow(this.contexts.indexOf(outerValue));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | notifyError(err) {
|
---|
104 | this.error(err);
|
---|
105 | }
|
---|
106 | notifyComplete(inner) {
|
---|
107 | if (inner !== this.openSubscription) {
|
---|
108 | this.closeWindow(this.contexts.indexOf(inner.context));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | closeWindow(index) {
|
---|
112 | if (index === -1) {
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | const { contexts } = this;
|
---|
116 | const context = contexts[index];
|
---|
117 | const { window, subscription } = context;
|
---|
118 | contexts.splice(index, 1);
|
---|
119 | window.complete();
|
---|
120 | subscription.unsubscribe();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | //# sourceMappingURL=windowToggle.js.map |
---|