Skip to content

Commit

Permalink
docs: update signal.go.gotmpl
Browse files Browse the repository at this point in the history
  • Loading branch information
NateScarlet committed Jun 12, 2020
1 parent bd3a7b7 commit b34e750
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions templates/signal.go.gotmpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
{{- /*
Accept data:
Type: type without import path, can be slice or pointer.
Type: type without import path, can be slice or pointer.
TypeImport: type import path.
Package: package name.
*/ -}}

{{- if hasPrefix "*" .Type -}}
{{- $_ := set . "IsPtr" "true" -}}
{{- $_ := set . "ValueType" (substr 1 -1 .Type) -}}
{{- else if .IsInterface -}}
{{- $_ := set . "ValueType" .Type -}}
{{- else -}}
{{- $_ := set . "ValueType" .Type -}}
{{- $type := .Type }}
{{- $isSlice := hasPrefix "[]" $type }}
{{- if $isSlice }}
{{- $type = substr 2 -1 $type }}
{{- end }}
{{- $isPointer := hasPrefix "*" $type }}
{{- if $isPointer }}
{{- $type = substr 1 -1 $type }}
{{- end }}
{{- if .TypeImport }}
{{- $type = printf "pkg1.%s" $type }}
{{- end }}
{{- if $isPointer }}
{{- $type = printf "*%s" $type }}
{{- end}}
{{- if $isSlice }}
{{- $type = printf "[]%s" $type }}
{{- end -}}

// Code generated from signal.go.gotmpl, DO NOT EDIT.
Expand All @@ -19,17 +31,21 @@ package {{.Package}}
import (
"sync"
"context"
{{- with .TypeImport}}

pkg1 "{{.}}"
{{- end}}
)

// Signal that emit {{.Type}} to receivers.
// Signal that emit {{ $type }} to receivers.
type Signal struct {
mu sync.RWMutex
m map[chan<- {{ .ValueType }}]bool
sideEffects []func(context.Context, {{.Type}}) error
m map[chan<- {{ $type }}]bool
sideEffects []func(context.Context, {{ $type }}) error
}

// Emit send object to every receivers. error when hook function errors.
func (s *Signal) Emit(ctx context.Context, o {{ .Type }}) error {
func (s *Signal) Emit(ctx context.Context, o {{ $type }}) error {
s.mu.RLock()
defer s.mu.RUnlock()

Expand All @@ -40,21 +56,16 @@ func (s *Signal) Emit(ctx context.Context, o {{ .Type }}) error {
}
}

{{ if .IsPtr }}
v := *o
{{- else }}
v := o
{{- end }}
for c, block := range s.m {
if block {
select {
case c <- v:
case c <- o:
case <- ctx.Done():
return ctx.Err()
}
} else {
select {
case c <- v:
case c <- o:
case <- ctx.Done():
return ctx.Err()
default:
Expand All @@ -65,30 +76,30 @@ func (s *Signal) Emit(ctx context.Context, o {{ .Type }}) error {
return nil
}

func (s *Signal) addReceiver(c chan<- {{ .ValueType }}, block bool) {
func (s *Signal) addReceiver(c chan<- {{ $type }}, block bool) {
s.mu.Lock()
defer s.mu.Unlock()

if s.m == nil {
s.m = make(map[chan<- {{ .ValueType }}]bool)
s.m = make(map[chan<- {{ $type }}]bool)
}
s.m[c] = block
}

// Notify add channel to receivers. Emit will wait when channel is blocked.
// It is the caller's responsibility to Stop notify before channel close.
func (s *Signal) Notify(c chan<- {{ .ValueType }}) {
func (s *Signal) Notify(c chan<- {{ $type }}) {
s.addReceiver(c, true)
}

// TryNotify add channel to receivers. Emit will skip when channel is blocked.
// It is the caller's responsibility to Stop notify before channel close.
func (s *Signal) TryNotify(c chan<- {{ .ValueType }}) {
func (s *Signal) TryNotify(c chan<- {{ $type }}) {
s.addReceiver(c, false)
}

// Stop remove channel from receivers.
func (s *Signal) Stop(c chan<- {{ .ValueType }}) {
func (s *Signal) Stop(c chan<- {{ $type }}) {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -97,7 +108,7 @@ func (s *Signal) Stop(c chan<- {{ .ValueType }}) {

// Connect a side effect function to signal.
// it will be called before send each object to channels.
func (s *Signal) Connect(fn func(context.Context, {{.Type}}) error) {
func (s *Signal) Connect(fn func(context.Context, {{ $type }}) error) {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down

0 comments on commit b34e750

Please sign in to comment.