多个主题和订阅或只有一个 - Angular 5
Multiple subjects and subscriptions or only one - Angular 5
现在我正在为我的应用程序编写前端代码。现在我有一个名为 socketio 的服务,它接收我的应用程序需要显示的数据。此外,该服务使用 Subjects 通过订阅将数据传递给组件。
是只有 1 个 subject/subscription 更好,它表示邮件中的用途,还是有多个 subjects/subscriptions,每个任务一个?
我对有关内存使用和处理时间的最佳方法感兴趣。
我的服务是这样的:
import { Injectable} from '@angular/core';
import { Subject } from 'rxjs/Subject';
import * as io from 'socket.io-client';
@Injectable()
export class SocketioService {
namespace = '/test';
socket: any;
positionsUpdate: Subject<object> = new Subject<object>(); // One subject
pingsUpdate: Subject<object> = new Subject<object>(); // Another subject
constructor() {
this.socket = io(location.protocol + '//' + 'localhost' + ':5000' + this.namespace);
const self = this;
this.socket.on('advisor_ping', function(msg) {
self.pingsUpdate.next(msg);
});
this.socket.on('advisor_position', function(msg) {
self.positionsUpdate.next(msg);
});
}
}
现在,如果我采用多学科方法,我最终会得到 9 个。
谢谢!
您总是希望订阅尽可能少,否则很容易忘记取消订阅等。您还可以通过异步管道将单个可观察对象绑定到模板,它会显示所有值。
所以你的代码示例看起来很不错
另请查看 Ben Lesh(rxjs 的经理)的这篇文章:https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87
现在我正在为我的应用程序编写前端代码。现在我有一个名为 socketio 的服务,它接收我的应用程序需要显示的数据。此外,该服务使用 Subjects 通过订阅将数据传递给组件。
是只有 1 个 subject/subscription 更好,它表示邮件中的用途,还是有多个 subjects/subscriptions,每个任务一个?
我对有关内存使用和处理时间的最佳方法感兴趣。
我的服务是这样的:
import { Injectable} from '@angular/core';
import { Subject } from 'rxjs/Subject';
import * as io from 'socket.io-client';
@Injectable()
export class SocketioService {
namespace = '/test';
socket: any;
positionsUpdate: Subject<object> = new Subject<object>(); // One subject
pingsUpdate: Subject<object> = new Subject<object>(); // Another subject
constructor() {
this.socket = io(location.protocol + '//' + 'localhost' + ':5000' + this.namespace);
const self = this;
this.socket.on('advisor_ping', function(msg) {
self.pingsUpdate.next(msg);
});
this.socket.on('advisor_position', function(msg) {
self.positionsUpdate.next(msg);
});
}
}
现在,如果我采用多学科方法,我最终会得到 9 个。
谢谢!
您总是希望订阅尽可能少,否则很容易忘记取消订阅等。您还可以通过异步管道将单个可观察对象绑定到模板,它会显示所有值。
所以你的代码示例看起来很不错
另请查看 Ben Lesh(rxjs 的经理)的这篇文章:https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87