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