mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Ignore repeated PresenceChannel leave/join calls (#19638)
If a consumer is calling leave or join on a channel repeatedly, that should not trigger additional HTTP requests
This commit is contained in:
parent
ebe8b868bf
commit
e70ed31a45
@ -66,14 +66,18 @@ class PresenceChannel extends EmberObject.extend(Evented) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.setProperties({ activeOptions });
|
this.setProperties({ activeOptions });
|
||||||
await this.presenceService._enter(this);
|
if (!this.present) {
|
||||||
this.set("present", true);
|
await this.presenceService._enter(this);
|
||||||
|
this.set("present", true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark the current user as leaving this channel
|
// Mark the current user as leaving this channel
|
||||||
async leave() {
|
async leave() {
|
||||||
await this.presenceService._leave(this);
|
if (this.present) {
|
||||||
this.set("present", false);
|
await this.presenceService._leave(this);
|
||||||
|
this.set("present", false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async subscribe(initialData = null) {
|
async subscribe(initialData = null) {
|
||||||
|
@ -292,6 +292,39 @@ acceptance("Presence - Entering and Leaving", function (needs) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("join should be a no-op if already present", async function (assert) {
|
||||||
|
const presenceService = this.container.lookup("service:presence");
|
||||||
|
const channel = presenceService.getChannel("/test/ch1");
|
||||||
|
|
||||||
|
await channel.enter();
|
||||||
|
assert.strictEqual(requests.length, 1, "updated the server for enter");
|
||||||
|
|
||||||
|
await channel.enter();
|
||||||
|
assert.strictEqual(
|
||||||
|
requests.length,
|
||||||
|
1,
|
||||||
|
"does not update the server unnecessarily"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("leave should be a no-op if not present", async function (assert) {
|
||||||
|
const presenceService = this.container.lookup("service:presence");
|
||||||
|
const channel = presenceService.getChannel("/test/ch1");
|
||||||
|
|
||||||
|
await channel.enter();
|
||||||
|
assert.strictEqual(requests.length, 1, "updated the server for enter");
|
||||||
|
|
||||||
|
await channel.leave();
|
||||||
|
assert.strictEqual(requests.length, 2, "updated the server for leave");
|
||||||
|
|
||||||
|
await channel.leave();
|
||||||
|
assert.strictEqual(
|
||||||
|
requests.length,
|
||||||
|
2,
|
||||||
|
"did not update the server unnecessarily"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("raises an error when entering a non-existent channel", async function (assert) {
|
test("raises an error when entering a non-existent channel", async function (assert) {
|
||||||
const presenceService = this.container.lookup("service:presence");
|
const presenceService = this.container.lookup("service:presence");
|
||||||
const channel = presenceService.getChannel("/blah/does-not-exist");
|
const channel = presenceService.getChannel("/blah/does-not-exist");
|
||||||
|
Loading…
Reference in New Issue
Block a user