source: trip-planner-front/node_modules/core-js/modules/esnext.observable.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.8 KB
Line 
1'use strict';
2// https://github.com/tc39/proposal-observable
3var $ = require('../internals/export');
4var DESCRIPTORS = require('../internals/descriptors');
5var setSpecies = require('../internals/set-species');
6var aFunction = require('../internals/a-function');
7var anObject = require('../internals/an-object');
8var isObject = require('../internals/is-object');
9var anInstance = require('../internals/an-instance');
10var defineProperty = require('../internals/object-define-property').f;
11var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
12var redefineAll = require('../internals/redefine-all');
13var getIterator = require('../internals/get-iterator');
14var iterate = require('../internals/iterate');
15var hostReportErrors = require('../internals/host-report-errors');
16var wellKnownSymbol = require('../internals/well-known-symbol');
17var InternalStateModule = require('../internals/internal-state');
18
19var OBSERVABLE = wellKnownSymbol('observable');
20var getInternalState = InternalStateModule.get;
21var setInternalState = InternalStateModule.set;
22
23var getMethod = function (fn) {
24 return fn == null ? undefined : aFunction(fn);
25};
26
27var cleanupSubscription = function (subscriptionState) {
28 var cleanup = subscriptionState.cleanup;
29 if (cleanup) {
30 subscriptionState.cleanup = undefined;
31 try {
32 cleanup();
33 } catch (error) {
34 hostReportErrors(error);
35 }
36 }
37};
38
39var subscriptionClosed = function (subscriptionState) {
40 return subscriptionState.observer === undefined;
41};
42
43var close = function (subscriptionState) {
44 var subscription = subscriptionState.facade;
45 if (!DESCRIPTORS) {
46 subscription.closed = true;
47 var subscriptionObserver = subscriptionState.subscriptionObserver;
48 if (subscriptionObserver) subscriptionObserver.closed = true;
49 } subscriptionState.observer = undefined;
50};
51
52var Subscription = function (observer, subscriber) {
53 var subscriptionState = setInternalState(this, {
54 cleanup: undefined,
55 observer: anObject(observer),
56 subscriptionObserver: undefined
57 });
58 var start;
59 if (!DESCRIPTORS) this.closed = false;
60 try {
61 if (start = getMethod(observer.start)) start.call(observer, this);
62 } catch (error) {
63 hostReportErrors(error);
64 }
65 if (subscriptionClosed(subscriptionState)) return;
66 var subscriptionObserver = subscriptionState.subscriptionObserver = new SubscriptionObserver(this);
67 try {
68 var cleanup = subscriber(subscriptionObserver);
69 var subscription = cleanup;
70 if (cleanup != null) subscriptionState.cleanup = typeof cleanup.unsubscribe === 'function'
71 ? function () { subscription.unsubscribe(); }
72 : aFunction(cleanup);
73 } catch (error) {
74 subscriptionObserver.error(error);
75 return;
76 } if (subscriptionClosed(subscriptionState)) cleanupSubscription(subscriptionState);
77};
78
79Subscription.prototype = redefineAll({}, {
80 unsubscribe: function unsubscribe() {
81 var subscriptionState = getInternalState(this);
82 if (!subscriptionClosed(subscriptionState)) {
83 close(subscriptionState);
84 cleanupSubscription(subscriptionState);
85 }
86 }
87});
88
89if (DESCRIPTORS) defineProperty(Subscription.prototype, 'closed', {
90 configurable: true,
91 get: function () {
92 return subscriptionClosed(getInternalState(this));
93 }
94});
95
96var SubscriptionObserver = function (subscription) {
97 setInternalState(this, { subscription: subscription });
98 if (!DESCRIPTORS) this.closed = false;
99};
100
101SubscriptionObserver.prototype = redefineAll({}, {
102 next: function next(value) {
103 var subscriptionState = getInternalState(getInternalState(this).subscription);
104 if (!subscriptionClosed(subscriptionState)) {
105 var observer = subscriptionState.observer;
106 try {
107 var nextMethod = getMethod(observer.next);
108 if (nextMethod) nextMethod.call(observer, value);
109 } catch (error) {
110 hostReportErrors(error);
111 }
112 }
113 },
114 error: function error(value) {
115 var subscriptionState = getInternalState(getInternalState(this).subscription);
116 if (!subscriptionClosed(subscriptionState)) {
117 var observer = subscriptionState.observer;
118 close(subscriptionState);
119 try {
120 var errorMethod = getMethod(observer.error);
121 if (errorMethod) errorMethod.call(observer, value);
122 else hostReportErrors(value);
123 } catch (err) {
124 hostReportErrors(err);
125 } cleanupSubscription(subscriptionState);
126 }
127 },
128 complete: function complete() {
129 var subscriptionState = getInternalState(getInternalState(this).subscription);
130 if (!subscriptionClosed(subscriptionState)) {
131 var observer = subscriptionState.observer;
132 close(subscriptionState);
133 try {
134 var completeMethod = getMethod(observer.complete);
135 if (completeMethod) completeMethod.call(observer);
136 } catch (error) {
137 hostReportErrors(error);
138 } cleanupSubscription(subscriptionState);
139 }
140 }
141});
142
143if (DESCRIPTORS) defineProperty(SubscriptionObserver.prototype, 'closed', {
144 configurable: true,
145 get: function () {
146 return subscriptionClosed(getInternalState(getInternalState(this).subscription));
147 }
148});
149
150var $Observable = function Observable(subscriber) {
151 anInstance(this, $Observable, 'Observable');
152 setInternalState(this, { subscriber: aFunction(subscriber) });
153};
154
155redefineAll($Observable.prototype, {
156 subscribe: function subscribe(observer) {
157 var length = arguments.length;
158 return new Subscription(typeof observer === 'function' ? {
159 next: observer,
160 error: length > 1 ? arguments[1] : undefined,
161 complete: length > 2 ? arguments[2] : undefined
162 } : isObject(observer) ? observer : {}, getInternalState(this).subscriber);
163 }
164});
165
166redefineAll($Observable, {
167 from: function from(x) {
168 var C = typeof this === 'function' ? this : $Observable;
169 var observableMethod = getMethod(anObject(x)[OBSERVABLE]);
170 if (observableMethod) {
171 var observable = anObject(observableMethod.call(x));
172 return observable.constructor === C ? observable : new C(function (observer) {
173 return observable.subscribe(observer);
174 });
175 }
176 var iterator = getIterator(x);
177 return new C(function (observer) {
178 iterate(iterator, function (it, stop) {
179 observer.next(it);
180 if (observer.closed) return stop();
181 }, { IS_ITERATOR: true, INTERRUPTED: true });
182 observer.complete();
183 });
184 },
185 of: function of() {
186 var C = typeof this === 'function' ? this : $Observable;
187 var length = arguments.length;
188 var items = new Array(length);
189 var index = 0;
190 while (index < length) items[index] = arguments[index++];
191 return new C(function (observer) {
192 for (var i = 0; i < length; i++) {
193 observer.next(items[i]);
194 if (observer.closed) return;
195 } observer.complete();
196 });
197 }
198});
199
200createNonEnumerableProperty($Observable.prototype, OBSERVABLE, function () { return this; });
201
202$({ global: true }, {
203 Observable: $Observable
204});
205
206setSpecies('Observable');
Note: See TracBrowser for help on using the repository browser.