Skip to content

Commit

Permalink
Allows path to be included in Class() cache method (dennwc#41)
Browse files Browse the repository at this point in the history
* Allows path to be included in Class() cache method

Resolves dennwc#40
  • Loading branch information
smoyer64 authored and Denys Smirnov committed May 27, 2019
1 parent 61179be commit 5d59134
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions js/js.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package js

import (
"strings"
"sync"
)

Expand Down Expand Up @@ -37,20 +38,24 @@ func Call(name string, args ...interface{}) Value {

// Class searches for a class in global scope.
// It caches results, so the lookup should be faster than calling Get.
func Class(class string) Value {
func Class(class string, path ...string) Value {
switch class {
case "Object":
return Value{object}
case "Array":
return Value{array}
}
key := class
if len(path) != 0 {
key += "." + strings.Join(path, ".")
}
mu.RLock()
v := classes[class]
v := classes[key]
mu.RUnlock()
if v.isZero() {
v = Get(class)
v = Get(class, path...)
mu.Lock()
classes[class] = v
classes[key] = v
mu.Unlock()
}
return v
Expand Down

0 comments on commit 5d59134

Please sign in to comment.