MCPcopy
hub / github.com/tc39/proposal-observable / Subscription

Function Subscription

src/Observable.js:75–126  ·  view source on GitHub ↗
(observer, subscriber)

Source from the content-addressed store, hash-verified

73}
74
75function Subscription(observer, subscriber) {
76 // Assert: subscriber is callable
77 // The observer must be an object
78 this._cleanup = undefined;
79 this._observer = observer;
80
81 // If the observer has a start method, call it with the subscription object
82 try {
83 let start = getMethod(observer, "start");
84
85 if (start) {
86 start.call(observer, this);
87 }
88 }
89 catch(e) {
90 // HostReportErrors(e);
91 }
92
93 // If the observer has unsubscribed from the start method, exit
94 if (subscriptionClosed(this))
95 return;
96
97 observer = new SubscriptionObserver(this);
98
99 try {
100
101 // Call the subscriber function
102 let cleanup = subscriber.call(undefined, observer);
103
104 // The return value must be undefined, null, a subscription object, or a function
105 if (cleanup != null) {
106 if (typeof cleanup.unsubscribe === "function")
107 cleanup = cleanupFromSubscription(cleanup);
108 else if (typeof cleanup !== "function")
109 throw new TypeError(cleanup + " is not a function");
110
111 this._cleanup = cleanup;
112 }
113
114 } catch (e) {
115
116 // If an error occurs during startup, then send the error
117 // to the observer.
118 observer.error(e);
119 return;
120 }
121
122 // If the stream is already finished, then perform cleanup
123 if (subscriptionClosed(this)) {
124 cleanupSubscription(this);
125 }
126}
127
128Subscription.prototype = nonEnum({
129 get closed() { return subscriptionClosed(this) },

Callers

nothing calls this directly

Calls 4

getMethodFunction · 0.85
subscriptionClosedFunction · 0.85
cleanupFromSubscriptionFunction · 0.85
cleanupSubscriptionFunction · 0.85

Tested by

no test coverage detected