{"id":333580,"date":"2022-05-24T15:01:13","date_gmt":"2022-05-24T15:01:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=333580"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=333580","title":{"rendered":"<span>\u041d\u0435 \u043e\u0442\u043f\u0438\u0441\u0430\u043b\u0441\u044f \u2014 \u0431\u0435\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0441\u0442\u0430\u043b\u0441\u044f<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h3>\n<p>\u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043f\u0440\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0441\u043e\u0431\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0432 \u0440\u0430\u0437\u044b \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u2014 RxJS. \u0412 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u2014 \u043e\u0442\u043f\u0438\u0441\u043a\u0438.<\/p>\n<p>\u0414\u0430, \u0434\u0430, \u044d\u0442\u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0443\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c, \u0438 \u044d\u0442\u043e \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0442\u0435\u0447\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0431 \u044d\u0442\u043e\u043c \u0434\u0430\u043b\u0435\u0435.<\/p>\n<h3>\u041a\u0435\u0439\u0441 \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0431\u044d\u043a \u0438 \u043f\u043e\u043a\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/h3>\n<h4>\u0417\u0430\u0434\u0430\u0447\u0430<\/h4>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u043a\u0443\u0440\u0441\u044b \u0432\u0430\u043b\u044e\u0442. \u041a\u043b\u0438\u0435\u043d\u0442 \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0431\u044d\u043a\u0443 \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0431-\u0441\u043e\u043a\u0435\u0442\u044b, \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0431\u044d\u043a\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>\u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c RxJS \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 Angular \u0438 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043c \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u043a \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u0443\u0440\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0440\u0430\u043d\u0434\u043e\u043c\u0430\u0439\u0437\u0435\u0440\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ fake-currency.function.ts  import { interval } from 'rxjs'; import { map } from 'rxjs\/operators';  \/\/ \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0430\u043d\u0434\u043e\u043c\u0430\u0439\u0437\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0434\u0430\u0451\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 function getRandomByLimits(min: number, max: number) {   return Math.round(Math.random() * (max - min) + min); }  \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043e\u0442\u0434\u0430\u044e\u0449\u0430\u044f \u0444\u0435\u0439\u043a\u043e\u0432\u044b\u0439 \u043a\u0443\u0440\u0441 export function fakeCurrency(time: number) {   const minLimit = 65;   const maxLimit = 78;   return interval(time).pipe(map(() => getRandomByLimits(minLimit, maxLimit))); }<\/code><\/pre>\n<p>\u0420\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0435 \u043a\u0443\u0440\u0441\u044b \u0432\u0430\u043b\u044e\u0442 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor() {     fakeCurrency(500).subscribe((num) => {       this.num = num;     });   } }<\/code><\/pre>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d:<\/p>\n<pre><code class=\"xml\">&lt;!-- my-component.component.html --> &lt;p>Random exchange rate: {{ num }}&lt;\/p><\/code><\/pre>\n<p><strong>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e!!! ???<\/strong><br \/>\u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u0432\u0430\u043b\u044e\u0442\u044b \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443!!!<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438!)<\/p>\n<h4>\u041e \u043a\u0430\u043a\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0434\u0451\u0442 \u0440\u0435\u0447\u044c?<\/h4>\n<p>\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0441 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043c\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e, \u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>fakeCurrency<\/code> \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443.<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500).subscribe((num) => {       console.log(`ID: ${id}`, num);       this.num = num;     });   } }<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e00\/a21\/d94\/e00a21d94d762ec2390e2d89f6b0775c.gif\" width=\"600\" height=\"463\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e00\/a21\/d94\/e00a21d94d762ec2390e2d89f6b0775c.gif\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u2014 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0443\u0440\u0441\u0430 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043d\u0435\u0442, \u043d\u043e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u0441\u044b\u043f\u0430\u0442\u044c\u0441\u044f \u043b\u043e\u0433\u0438. \u0415\u0441\u043b\u0438 \u044e\u0437\u0435\u0440 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438, \u0442\u043e \u0442\u0430\u043a\u0430\u044f \u0443\u0442\u0435\u0447\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0434\u0438\u043a\u0438\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u0430 \u043d\u0430 \u0435\u0433\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.<\/p>\n<h4>\u041a\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e?<\/h4>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f!)<\/p>\n<p>\u0418 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043e\u0442\u043f\u0438\u0441\u043a\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e.<\/p>\n<h4>Async Pipe<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439. \u041d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { Observable } from 'rxjs'; import { tap } from 'rxjs\/operators';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num$: Observable&lt;number>;    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     this.num$ = fakeCurrency(500)       .pipe(         tap((num) => console.log(`ID: ${id}`, num))       );   } }<\/code><\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u043d\u0430\u0448 \u0448\u0430\u0431\u043b\u043e\u043d:<\/p>\n<pre><code class=\"xml\">&lt;!-- my-component.component.html --> &lt;p>Random exchange rate: {{ num$ | async }}&lt;\/p><\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3b9\/1e0\/2cf\/3b91e02cf9b360a04436e27b8b34ae3c.gif\" width=\"600\" height=\"463\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3b9\/1e0\/2cf\/3b91e02cf9b360a04436e27b8b34ae3c.gif\"\/><figcaption><\/figcaption><\/figure>\n<h4>Destroy Subject Pattern<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043d\u0430\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043d\u0443\u0436\u043d\u043e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u043c <strong>Destroy Subject<\/strong> \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c \u043e\u0442\u043f\u0438\u0441\u043a\u0438 <code>takeUntil<\/code>:<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { takeUntil } from 'rxjs\/operators';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent implements OnDestroy {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;   destroy$ = new Subject();    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500)       \/\/ \u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0435\u043c\u0443 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a       .pipe(takeUntil(this.destroy$))       .subscribe((num) => {         console.log(`ID: ${id}`, num);         this.num = num;       });   }      ngOnDestroy() {     \/\/ \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a     \/\/ \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 takeUntil \u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     this.destroy$.next();     this.destroy$.complete();   } }<\/code><\/pre>\n<h4>\u041c\u043e\u0436\u0435\u0442 \u0435\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u0435\u0435?<\/h4>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c!)<\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e Subject \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u2014 \u0442\u0435 \u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f. (\u041d\u0430\u0433\u043b\u043e \u0431\u0435\u0440\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/taiga-ui.dev\/services\/destroy-service\" rel=\"noopener noreferrer nofollow\">taiga-ui<\/a>)<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>ReplaySubject<\/code> \u0438 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>OnDestroy<\/code> \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n<pre><code class=\"javascript\">\/\/ destroy.service.ts import { Injectable, OnDestroy } from '@angular\/core'; import { ReplaySubject } from 'rxjs';  @Injectable() export class DestroyService extends ReplaySubject&lt;void> implements OnDestroy {   constructor() {     super();   }    ngOnDestroy() {     this.next();     this.complete();   } }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e <strong>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e<\/strong> \u0437\u0430\u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u0442\u044c <code>DestroyService<\/code>, \u0441 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u043d\u0433\u043e\u043c \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u0442\u0430\u043a\u043e\u0435 \u043d\u0435 \u043f\u0440\u043e\u043a\u0430\u0442\u0438\u0442:<\/p>\n<pre><code>\/\/ my-component.component.ts  import { Component, Inject } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { takeUntil } from 'rxjs\/operators'; import { DestroyService } from '..\/destroy.service';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'],   \/\/ \u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441   providers: [DestroyService] }) export class MemoryLeakComponent implements OnDestroy {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor(@Inject(DestroyService) private destroy$: Observable&lt;void>) {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500)       \/\/ \u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a       .pipe(takeUntil(destroy$))       .subscribe((num) => {         console.log(`ID: ${id}`, num);         this.num = num;       });   } }<\/code><\/pre>\n<p><strong>\u041f\u043e\u0434\u044b\u0442\u043e\u0436\u0438\u043c:<\/strong><\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u0447\u0442\u0438 \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f (\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: \u043f\u043e\u0442\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e);<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>async pipe<\/strong>, \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b <code>take*<\/code>;<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451, \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/stackblitz.com\/edit\/angular-ivy-y31c5g\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \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\/667504\/\"> https:\/\/habr.com\/ru\/post\/667504\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h3>\n<p>\u0420\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043f\u0440\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0441\u043e\u0431\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0432 \u0440\u0430\u0437\u044b \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u2014 RxJS. \u0412 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u2014 \u043e\u0442\u043f\u0438\u0441\u043a\u0438.<\/p>\n<p>\u0414\u0430, \u0434\u0430, \u044d\u0442\u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0443\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c, \u0438 \u044d\u0442\u043e \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0442\u0435\u0447\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0431 \u044d\u0442\u043e\u043c \u0434\u0430\u043b\u0435\u0435.<\/p>\n<h3>\u041a\u0435\u0439\u0441 \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0431\u044d\u043a \u0438 \u043f\u043e\u043a\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/h3>\n<h4>\u0417\u0430\u0434\u0430\u0447\u0430<\/h4>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u043a\u0443\u0440\u0441\u044b \u0432\u0430\u043b\u044e\u0442. \u041a\u043b\u0438\u0435\u043d\u0442 \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0431\u044d\u043a\u0443 \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0431-\u0441\u043e\u043a\u0435\u0442\u044b, \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u043e\u0442 \u0431\u044d\u043a\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.<\/p>\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>\u0411\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c RxJS \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 Angular \u0438 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043c \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u043a \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u0443\u0440\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0440\u0430\u043d\u0434\u043e\u043c\u0430\u0439\u0437\u0435\u0440\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ fake-currency.function.ts  import { interval } from 'rxjs'; import { map } from 'rxjs\/operators';  \/\/ \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0430\u043d\u0434\u043e\u043c\u0430\u0439\u0437\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0434\u0430\u0451\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 function getRandomByLimits(min: number, max: number) {   return Math.round(Math.random() * (max - min) + min); }  \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u043e\u0442\u0434\u0430\u044e\u0449\u0430\u044f \u0444\u0435\u0439\u043a\u043e\u0432\u044b\u0439 \u043a\u0443\u0440\u0441 export function fakeCurrency(time: number) {   const minLimit = 65;   const maxLimit = 78;   return interval(time).pipe(map(() => getRandomByLimits(minLimit, maxLimit))); }<\/code><\/pre>\n<p>\u0420\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0435 \u043a\u0443\u0440\u0441\u044b \u0432\u0430\u043b\u044e\u0442 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0430\u0448\u0435\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b.<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor() {     fakeCurrency(500).subscribe((num) => {       this.num = num;     });   } }<\/code><\/pre>\n<p>\u0428\u0430\u0431\u043b\u043e\u043d:<\/p>\n<pre><code class=\"xml\">&lt;!-- my-component.component.html --> &lt;p>Random exchange rate: {{ num }}&lt;\/p><\/code><\/pre>\n<p><strong>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e!!! ???<\/strong><br \/>\u0423 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u043a\u0443\u0440\u0441 \u0432\u0430\u043b\u044e\u0442\u044b \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443!!!<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e \u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438!)<\/p>\n<h4>\u041e \u043a\u0430\u043a\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0434\u0451\u0442 \u0440\u0435\u0447\u044c?<\/h4>\n<p>\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0441 \u044d\u0442\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043c\u0441\u044f \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e, \u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>fakeCurrency<\/code> \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443.<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500).subscribe((num) => {       console.log(`ID: ${id}`, num);       this.num = num;     });   } }<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u2014 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0443\u0440\u0441\u0430 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043d\u0435\u0442, \u043d\u043e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u0441\u044b\u043f\u0430\u0442\u044c\u0441\u044f \u043b\u043e\u0433\u0438. \u0415\u0441\u043b\u0438 \u044e\u0437\u0435\u0440 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438, \u0442\u043e \u0442\u0430\u043a\u0430\u044f \u0443\u0442\u0435\u0447\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0434\u0438\u043a\u0438\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u0430 \u043d\u0430 \u0435\u0433\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.<\/p>\n<h4>\u041a\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e?<\/h4>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f!)<\/p>\n<p>\u0418 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043e\u0442\u043f\u0438\u0441\u043a\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e.<\/p>\n<h4>Async Pipe<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439. \u041d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { Observable } from 'rxjs'; import { tap } from 'rxjs\/operators';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num$: Observable&lt;number>;    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     this.num$ = fakeCurrency(500)       .pipe(         tap((num) => console.log(`ID: ${id}`, num))       );   } }<\/code><\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u043d\u0430\u0448 \u0448\u0430\u0431\u043b\u043e\u043d:<\/p>\n<pre><code class=\"xml\">&lt;!-- my-component.component.html --> &lt;p>Random exchange rate: {{ num$ | async }}&lt;\/p><\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h4>Destroy Subject Pattern<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043d\u0430\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043d\u0443\u0436\u043d\u043e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u043c <strong>Destroy Subject<\/strong> \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c \u043e\u0442\u043f\u0438\u0441\u043a\u0438 <code>takeUntil<\/code>:<\/p>\n<pre><code class=\"javascript\">\/\/ my-component.component.ts  import { Component } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { takeUntil } from 'rxjs\/operators';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'], }) export class MemoryLeakComponent implements OnDestroy {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;   destroy$ = new Subject();    constructor() {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500)       \/\/ \u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0435\u043c\u0443 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a       .pipe(takeUntil(this.destroy$))       .subscribe((num) => {         console.log(`ID: ${id}`, num);         this.num = num;       });   }      ngOnDestroy() {     \/\/ \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a     \/\/ \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 takeUntil \u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     this.destroy$.next();     this.destroy$.complete();   } }<\/code><\/pre>\n<h4>\u041c\u043e\u0436\u0435\u0442 \u0435\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u0435\u0435?<\/h4>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c!)<\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e Subject \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u2014 \u0442\u0435 \u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f. (\u041d\u0430\u0433\u043b\u043e \u0431\u0435\u0440\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/taiga-ui.dev\/services\/destroy-service\" rel=\"noopener noreferrer nofollow\">taiga-ui<\/a>)<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>ReplaySubject<\/code> \u0438 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>OnDestroy<\/code> \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n<pre><code class=\"javascript\">\/\/ destroy.service.ts import { Injectable, OnDestroy } from '@angular\/core'; import { ReplaySubject } from 'rxjs';  @Injectable() export class DestroyService extends ReplaySubject&lt;void> implements OnDestroy {   constructor() {     super();   }    ngOnDestroy() {     this.next();     this.complete();   } }<\/code><\/pre>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e <strong>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e<\/strong> \u0437\u0430\u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u0442\u044c <code>DestroyService<\/code>, \u0441 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u043d\u0433\u043e\u043c \u0432 \u043c\u043e\u0434\u0443\u043b\u044c \u0442\u0430\u043a\u043e\u0435 \u043d\u0435 \u043f\u0440\u043e\u043a\u0430\u0442\u0438\u0442:<\/p>\n<pre><code>\/\/ my-component.component.ts  import { Component, Inject } from '@angular\/core'; import { fakeCurrency } from '..\/fake-currency.function'; import { takeUntil } from 'rxjs\/operators'; import { DestroyService } from '..\/destroy.service';  @Component({   selector: 'my-component',   templateUrl: '.\/my-component.component.html',   styleUrls: ['.\/my-component.component.css'],   \/\/ \u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441   providers: [DestroyService] }) export class MemoryLeakComponent implements OnDestroy {   \/\/ \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435   num: number;    constructor(@Inject(DestroyService) private destroy$: Observable&lt;void>) {     \/\/ \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 id     const id = Math.floor(Math.random() * 100000);     fakeCurrency(500)       \/\/ \u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a       .pipe(takeUntil(destroy$))       .subscribe((num) => {         console.log(`ID: ${id}`, num);         this.num = num;       });   } }<\/code><\/pre>\n<p><strong>\u041f\u043e\u0434\u044b\u0442\u043e\u0436\u0438\u043c:<\/strong><\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u0447\u0442\u0438 \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f (\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: \u043f\u043e\u0442\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e);<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>async pipe<\/strong>, \u0435\u0441\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b <code>take*<\/code>;<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451, \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/stackblitz.com\/edit\/angular-ivy-y31c5g\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \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\/667504\/\"> https:\/\/habr.com\/ru\/post\/667504\/<\/a><br \/><\/br><\/br><\/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-333580","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/333580","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=333580"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/333580\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=333580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=333580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=333580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}