-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d7a703
commit 7a72e3b
Showing
5 changed files
with
82 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package pubsub | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ItsNotGoodName/reciva-web-remote/core/state" | ||
) | ||
|
||
type FragmentPub struct { | ||
subsMu sync.Mutex | ||
subs map[*FragmentSub]string | ||
} | ||
|
||
func NewFragmentPub() *FragmentPub { | ||
return &FragmentPub{ | ||
subsMu: sync.Mutex{}, | ||
subs: make(map[*FragmentSub]string), | ||
} | ||
} | ||
|
||
func (p *FragmentPub) Unsubscribe(sub *FragmentSub) { | ||
p.subsMu.Lock() | ||
delete(p.subs, sub) | ||
sub.close() | ||
p.subsMu.Unlock() | ||
} | ||
|
||
func (p *FragmentPub) Subscribe(buffer int, uuid string) *FragmentSub { | ||
sub := newFragmentSub(buffer) | ||
|
||
p.subsMu.Lock() | ||
p.subs[sub] = uuid | ||
p.subsMu.Unlock() | ||
|
||
return sub | ||
} | ||
|
||
func (p *FragmentPub) Publish(frag state.Fragment) { | ||
p.subsMu.Lock() | ||
for sub, uuid := range p.subs { | ||
if uuid == frag.UUID || uuid == "" { | ||
if !sub.send(&frag) { | ||
delete(p.subs, sub) | ||
sub.close() | ||
} | ||
} | ||
} | ||
p.subsMu.Unlock() | ||
} | ||
|
||
type FragmentSub struct { | ||
channel chan *state.Fragment | ||
open bool | ||
} | ||
|
||
func newFragmentSub(buffer int) *FragmentSub { | ||
return &FragmentSub{ | ||
channel: make(chan *state.Fragment, buffer), | ||
open: true, | ||
} | ||
} | ||
|
||
func (s *FragmentSub) Channel() <-chan *state.Fragment { | ||
return s.channel | ||
} | ||
|
||
func (s *FragmentSub) send(f *state.Fragment) bool { | ||
select { | ||
case s.channel <- f: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func (s *FragmentSub) close() { | ||
if s.open { | ||
close(s.channel) | ||
s.open = false | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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