Skip to content

Commit

Permalink
added a mutex around adding helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Mar 20, 2017
1 parent cc392a4 commit ce2727f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion helper_map.go
@@ -1,16 +1,22 @@
package plush

import "github.com/pkg/errors"
import (
"sync"

"github.com/pkg/errors"
)

// HelperMap holds onto helpers and validates they are properly formed.
type HelperMap struct {
helpers map[string]interface{}
moot *sync.Mutex
}

// NewHelperMap containing all of the "default" helpers from "plush.Helpers".
func NewHelperMap() (HelperMap, error) {
hm := HelperMap{
helpers: map[string]interface{}{},
moot: &sync.Mutex{},
}

err := hm.AddMany(Helpers.Helpers())
Expand All @@ -23,6 +29,8 @@ func NewHelperMap() (HelperMap, error) {
// Add a new helper to the map. New Helpers will be validated to ensure they
// meet the requirements for a helper:
func (h *HelperMap) Add(key string, helper interface{}) error {
h.moot.Lock()
defer h.moot.Unlock()
if h.helpers == nil {
h.helpers = map[string]interface{}{}
}
Expand Down
5 changes: 4 additions & 1 deletion helpers.go
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"reflect"
"strings"
"sync"

"github.com/gobuffalo/plush/ast"

Expand All @@ -18,7 +19,9 @@ import (
// Helpers contains all of the default helpers for
// These will be available to all templates. You should add
// any custom global helpers to this list.
var Helpers = HelperMap{}
var Helpers = HelperMap{
moot: &sync.Mutex{},
}

func init() {
Helpers.Add("json", toJSONHelper)
Expand Down

0 comments on commit ce2727f

Please sign in to comment.