{"id":300312,"date":"2020-03-18T15:00:07","date_gmt":"2020-03-18T15:00:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=300312"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=300312","title":{"rendered":"\u0417\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 defer"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/492948\/\">\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 <a href=\"https:\/\/netbasal.com\/creating-custom-operators-in-rxjs-32f052d69457\" rel=\"nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b RxJS. \u0421\u0435\u0439\u0447\u0430\u0441 \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u043c\u0430\u043b\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u2014 <code>defer<\/code> \u2014 \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0435\u0435 \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u043f\u043e\u0434 \u0438\u043c\u0435\u043d\u0435\u043c tapOnce<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">function tapOnce&lt;T&gt;(fn: Function): OperatorFunction&lt;T, T&gt; {   return function(source: Observable&lt;any&gt;) {     let run = false;     return source.pipe(       tap(() =&gt; {         if (!run) {           fn();           run = true;         }       })     );   }; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u043e\u0434 \u043f\u043e\u043d\u044f\u0442\u0435\u043d \u2014 <code>tap<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0444\u043b\u0430\u0433 <code>run<\/code> \u043d\u0443\u0436\u0435\u043d \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const source = interval(5000).pipe(   tapOnce(() =&gt; console.log('+') ));  source.subscribe(console.log);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043f\u043b\u044e\u0441\u0438\u043a \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u043c\u0438\u0442\u0435. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const source = interval(5000).pipe(   tapOnce(() =&gt; console.log('+') ));  source.subscribe(console.log); source.subscribe(console.log);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u2014 \u0442\u0430\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043b\u044e\u0441\u0438\u043a. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e \u043e\u0431\u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u043b\u0435\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0438 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0442\u0443 \u0436\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>run<\/code>. \u041d\u0443\u0436\u0435\u043d \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0442\u0441\u0440\u043e\u0447\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430 \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 \u043a\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0434\u043f\u0438\u0448\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043c\u043e\u0436\u0435\u0442 <code>defer<\/code><\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import { defer } from 'rxjs';  function tapOnce&lt;T&gt;(fn: Function): OperatorFunction&lt;T, T&gt; {   return function(source: Observable&lt;any&gt;) {     return defer(() =&gt; {       let run = false;       return source.pipe(         tap(() =&gt; {           if (!run) {             fn();             run = true;           }         })       );     });   }; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>defer<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u0435\u0440\u043d\u0443\u0442\u044c <a href=\"https:\/\/github.com\/ReactiveX\/rxjs\/blob\/7b2904d29697bea7661c189175fc2cb9e503e093\/src\/internal\/types.ts#L52\" rel=\"nofollow\"><code>ObservableInput<\/code><\/a>. \u041a\u043e\u0434 \u0432\u043d\u0443\u0442\u0440\u0438 <code>defer<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0435, \u0430 \u043d\u0435 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044e js, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0432\u043e\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043b\u0435\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u044e \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e <code>defer<\/code> \u0434\u043b\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">function defer(observableFactory: () =&gt; ObservableInput&lt;any&gt;) {   return new Observable(subscriber =&gt; {     const source = observableFactory();     return source.subscribe(subscriber);   }); }<\/code><\/pre>\n<p>  <\/p>\n<p><code>defer<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439-\u0444\u0430\u0431\u0440\u0438\u043a\u043e\u0439, \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a.<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0435\u0449\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u0435\u043d <code>defer<\/code>. \u0421\u043a\u0430\u0436\u0435\u043c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0430\u0434\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u043a\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const randNum = of(Math.random());  randNum.subscribe(console.log); randNum.subscribe(console.log);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e\u0436\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0435, \u0430 \u043d\u0435 \u043f\u0440\u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const randNum = defer(() =&gt; of(Math.random()));  randNum2.subscribe(console.log); randNum2.subscribe(console.log);  \/\/ The same concept as using a function const randNum = () =&gt; of(Math.random()); randNum2().subscribe(console.log); randNum2().subscribe(console.log);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u0440\u043e\u0447\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u043c\u0438\u0441\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">\/\/ This already executing regardless the numbers of handlers const promise = new Promise(resolve =&gt; {   resolve(); });  \/\/ Deferring the creation of the promise until someone subscribes const promiseDefered = defer(() =&gt; {   return new Promise(resolve =&gt; {     resolve();   }); });  promiseDefered.subscribe(console.log);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u043c\u0438\u0441\u044b \u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0447\u0438\u0441\u043b\u0430 \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u0435\u0439. \u041c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u043c\u0438\u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043d\u0430 \u043f\u043e\u0442\u043e\u043a (\u0442.\u0435. \u043b\u0435\u043d\u0438\u0432\u044b\u043c) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>defer<\/code>.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/492948\/\"> https:\/\/habr.com\/ru\/post\/492948\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/492948\/\">\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 <a href=\"https:\/\/netbasal.com\/creating-custom-operators-in-rxjs-32f052d69457\" rel=\"nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b RxJS. \u0421\u0435\u0439\u0447\u0430\u0441 \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u043c\u0430\u043b\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u2014 <code>defer<\/code> \u2014 \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-300312","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300312"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300312\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}