-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve sockets implementation to not be simple pooling
- Loading branch information
Showing
3 changed files
with
33 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,24 @@ | ||
import json | ||
|
||
from channels.generic.websocket import AsyncWebsocketConsumer | ||
from channels.db import database_sync_to_async | ||
|
||
from core.models import Task, Client, Project | ||
|
||
|
||
class UpdateConsumer(AsyncWebsocketConsumer): | ||
# TODO: This really should use channel layers and not something as janky as | ||
# storing everything currently avaliable on connection and double check on | ||
# polling. That defeats the purpose of using websockets. | ||
""" | ||
Goes with our core signal that, on model update, sends a message to all | ||
channels in the group to sync the UI. | ||
""" | ||
|
||
async def connect(self): | ||
await self.accept() | ||
await self.channel_layer.group_add( | ||
'sync_clients', self.channel_name) | ||
|
||
async def disconnect(self, close_code): | ||
await self.channel_layer.group_discard( | ||
'sync_clients', self.channel_name) | ||
|
||
async def receive(self, text_data): | ||
async def sync_clients_save(self, event): | ||
await self.send(json.dumps({ | ||
'tasks': await self.get_task_count(), | ||
'clients': await self.get_client_count(), | ||
'projects': await self.get_project_count(), | ||
'model': event['model'], | ||
})) | ||
|
||
@database_sync_to_async | ||
def get_task_count(self): | ||
return Task.objects.count() | ||
|
||
@database_sync_to_async | ||
def get_client_count(self): | ||
return Client.objects.filter(archive=False).count() | ||
|
||
@database_sync_to_async | ||
def get_project_count(self): | ||
return Project.objects.filter(archive=False).count() |