forked from thinknetica/workshop_hotwire
-
Notifications
You must be signed in to change notification settings - Fork 0
Station listeners counter #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
foxy-eyed
wants to merge
3
commits into
day-3
Choose a base branch
from
feature/listeners-counter
base: day-3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| class StationListenersChannel < Turbo::StreamsChannel | ||
| include ActionView::RecordIdentifier | ||
|
|
||
| after_subscribe :track_listener | ||
| after_unsubscribe :untrack_listener | ||
|
|
||
| def track_listener | ||
| StationTracker.track_listener(station) | ||
| broadcast_counter_update | ||
| end | ||
|
|
||
| def untrack_listener | ||
| StationTracker.untrack_listener(station) | ||
| broadcast_counter_update | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def station | ||
| @station ||= LiveStation.find(params[:station_id]) | ||
| end | ||
|
|
||
| def broadcast_counter_update | ||
| counter = StationTracker.current_listeners(station) | ||
|
|
||
| Turbo::StreamsChannel.broadcast_update_to( | ||
| verified_stream_name_from_params, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А можно было тут просто передать station? А то сейчас какие внутренности вылезли наружу |
||
| target: dom_id(station, "listeners_counter"), | ||
| content: counter | ||
| ) | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module StationTracker | ||
| module_function | ||
|
|
||
| def track_listener(station) | ||
| store.increment(station.id) | ||
| end | ||
|
|
||
| def untrack_listener(station) | ||
| store.decrement(station.id) | ||
| end | ||
|
|
||
| def current_listeners(station) | ||
| store.get(station.id) | ||
| end | ||
|
|
||
| def store | ||
| @store ||= Store.new(key_prefix: "station_") | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| module StationTracker | ||
| class Store | ||
| attr_reader :expiry, :key_prefix | ||
|
|
||
| def initialize(key_prefix:, expiry: 60 * 60 * 24) | ||
| @key_prefix = key_prefix | ||
| @expiry = expiry | ||
| end | ||
|
|
||
| def increment(key) | ||
| cache.increment(cache_key(key), 1) | ||
| end | ||
|
|
||
| def decrement(key) | ||
| cache.decrement(cache_key(key), 1) | ||
| end | ||
|
|
||
| def get(key) | ||
| cache.get(cache_key(key)).to_i | ||
| end | ||
|
|
||
| def reset(key) | ||
| cache.delete(cache_key(key)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def cache | ||
| @cache ||= Litecache.new(expiry: expiry) | ||
| end | ||
|
|
||
| def cache_key(key) | ||
| [key_prefix, key].join("_") | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6"> | ||
| <path fill-rule="evenodd" d="M18.685 19.097A9.723 9.723 0 0021.75 12c0-5.385-4.365-9.75-9.75-9.75S2.25 6.615 2.25 12a9.723 9.723 0 003.065 7.097A9.716 9.716 0 0012 21.75a9.716 9.716 0 006.685-2.653zm-12.54-1.285A7.486 7.486 0 0112 15a7.486 7.486 0 015.855 2.812A8.224 8.224 0 0112 20.25a8.224 8.224 0 01-5.855-2.438zM15.75 9a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" clip-rule="evenodd" /> | ||
| </svg> |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы
StationTrackerповысил до модели (связанной с LiveStation) и присоединил к станции, чтобы получилось что-то вроде:Так сказать, ближе
к землек Rails Way.