From ff6d1469b8f96bd0b937703ea4b4906cf0969e7b Mon Sep 17 00:00:00 2001 From: harveyshao Date: Mon, 25 Jul 2022 18:12:32 +0800 Subject: [PATCH] opt some --- base/mdb/hash.go | 13 ++++++++----- core/code/webpack.go | 34 +++++++++++++++++++++++++++++----- core/code/xterm.go | 6 ++++-- lock.go | 23 +++++++---------------- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/base/mdb/hash.go b/base/mdb/hash.go index 480f235d..4ce26d1f 100644 --- a/base/mdb/hash.go +++ b/base/mdb/hash.go @@ -282,11 +282,14 @@ func HashImport(m *ice.Message, arg ...ice.Any) *ice.Message { func HashCache(m *ice.Message, h string, add func() ice.Any) ice.Any { defer m.Lock()() - if add != nil && m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache")) == nil { - m.Debug("add cache %s:%s", m.PrefixKey(), kit.Keys(HASH, h, "_cache")) - m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache"), add()) // 添加 + p := m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache")) + if pp, ok := p.(ice.Map); ok && len(pp) == 0 { + p = nil } - m.Debug("get cache %s:%s", m.PrefixKey(), kit.Keys(HASH, h, "_cache")) - return m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache")) // 读取 + if add != nil && p == nil { + p = add() + m.Confv(m.PrefixKey(), kit.Keys(HASH, h, "_cache"), p) // 添加 + } + return p } diff --git a/core/code/webpack.go b/core/code/webpack.go index 9ff715ec..89660d96 100644 --- a/core/code/webpack.go +++ b/core/code/webpack.go @@ -37,6 +37,7 @@ func _webpack_cache(m *ice.Message, dir string, write bool) { js, _, e := kit.Create(path.Join(dir, PAGE_CACHE_JS)) m.Assert(e) defer js.Close() + defer fmt.Fprintln(js, `_can_name = ""`) defer _webpack_can(m) if !write { @@ -59,6 +60,7 @@ func _webpack_cache(m *ice.Message, dir string, write bool) { for _, k := range []string{LIB, PANEL, PLUGIN} { m.Cmd(nfs.DIR, k).Tables(func(value ice.Maps) { if kit.Ext(value[nfs.PATH]) == CSS { + fmt.Fprintln(css, kit.Format("/* %s */", path.Join(ice.PS, value[nfs.PATH]))) fmt.Fprintln(css, m.Cmdx(nfs.CAT, value[nfs.PATH])) fmt.Fprintln(js, `Volcanos.meta.cache["`+path.Join(ice.PS, value[nfs.PATH])+`"] = []`) } @@ -77,7 +79,26 @@ func _webpack_cache(m *ice.Message, dir string, write bool) { fmt.Fprintln(js, `_can_name = "`+path.Join(ice.PS, k)+`"`) fmt.Fprintln(js, m.Cmdx(nfs.CAT, k)) } - fmt.Fprintln(js, `_can_name = ""`) + + m.Cmd(mdb.SELECT, m.PrefixKey(), "", mdb.HASH, ice.OptionFields(nfs.PATH)).Tables(func(value ice.Maps) { + defer fmt.Fprintln(js) + + p := value[nfs.PATH] + switch kit.Ext(p) { + case nfs.CSS: + fmt.Fprintln(css, kit.Format("/* %s */", path.Join("/require/node_modules/", p))) + fmt.Fprintln(css, m.Cmdx(nfs.CAT, path.Join("node_modules", p))) + fmt.Fprintln(js, `Volcanos.meta.cache["`+path.Join("/require/node_modules/", p)+`"] = []`) + return + case nfs.JS: + default: + p = p + "/lib/" + p + ".js" + } + + fmt.Fprintln(js, `_can_name = "`+path.Join("/require/node_modules/", p)+`"`) + fmt.Fprintln(js, m.Cmdx(nfs.CAT, path.Join("node_modules", p))) + fmt.Fprintln(js, `Volcanos.meta.cache["`+path.Join("/require/node_modules/", p)+`"] = []`) + }) } func _webpack_build(m *ice.Message, file string) { if f, _, e := kit.Create(kit.Keys(file, JS)); m.Assert(e) { @@ -134,11 +155,14 @@ const DEVPACK = "devpack" const WEBPACK = "webpack" func init() { - Index.Merge(&ice.Context{Commands: ice.Commands{ - WEBPACK: {Name: "webpack path auto create remove", Help: "打包", Actions: ice.Actions{ + Index.MergeCommands(ice.Commands{ + WEBPACK: {Name: "webpack path auto create remove", Help: "打包", Actions: ice.MergeAction(ice.Actions{ mdb.CREATE: {Name: "create", Help: "创建", Hand: func(m *ice.Message, arg ...string) { _webpack_cache(m.Spawn(), _volcanos(m), true) }}, + mdb.INSERT: {Name: "insert", Help: "添加", Hand: func(m *ice.Message, arg ...string) { + m.Cmd(mdb.INSERT, m.PrefixKey(), "", mdb.HASH, nfs.PATH, arg[0]) + }}, mdb.REMOVE: {Name: "remove", Help: "删除", Hand: func(m *ice.Message, arg ...string) { _webpack_cache(m.Spawn(), _volcanos(m), false) }}, @@ -151,12 +175,12 @@ func init() { _webpack_cache(m.Spawn(), _volcanos(m), true) _webpack_build(m, _publish(m, WEBPACK, m.Option(mdb.NAME))) }}, - }, Hand: func(m *ice.Message, arg ...string) { + }, mdb.HashAction(mdb.SHORT, nfs.PATH)), Hand: func(m *ice.Message, arg ...string) { m.Option(nfs.DIR_DEEP, true) m.Option(nfs.DIR_TYPE, nfs.CAT) m.OptionFields(nfs.DIR_WEB_FIELDS) m.Cmdy(nfs.DIR, _volcanos(m, PAGE)) m.Cmdy(nfs.DIR, _publish(m, WEBPACK)) }}, - }}) + }) } diff --git a/core/code/xterm.go b/core/code/xterm.go index 8e37c131..7ec46d7c 100644 --- a/core/code/xterm.go +++ b/core/code/xterm.go @@ -23,9 +23,11 @@ func _xterm_socket(m *ice.Message, h, t string) { } func _xterm_get(m *ice.Message, h string, must bool) (f *os.File) { f, _ = mdb.HashCache(m, h, func() ice.Any { + m.Debug("what %v", must) if !must { return nil } + cmd := exec.Command(cli.SystemFind(m, kit.Select("sh", m.Option(mdb.TYPE)))) cmd.Env = append(os.Environ(), "TERM=xterm") m.Option(mdb.HASH, h) @@ -47,6 +49,7 @@ func _xterm_get(m *ice.Message, h string, must bool) (f *os.File) { }) return tty }).(*os.File) + m.Debug("what %v", f) return } @@ -107,8 +110,7 @@ func init() { } }}, }, mdb.HashAction(mdb.FIELD, "time,hash,type,name,text,extra"), ctx.CmdAction()), Hand: func(m *ice.Message, arg ...string) { - mdb.HashSelect(m, kit.Slice(arg, 0, 1)...) - m.DisplayLocal("") + mdb.HashSelect(m, kit.Slice(arg, 0, 1)...).DisplayLocal("") }}, }) } diff --git a/lock.go b/lock.go index 21d6ba66..d9f76ae3 100644 --- a/lock.go +++ b/lock.go @@ -9,10 +9,11 @@ import ( var lock = map[string]*sync.RWMutex{} var _lock = sync.Mutex{} -func (m *Message) _lock(key string) (*sync.RWMutex, string) { +func (m *Message) _lock(key string) *sync.RWMutex { if key == "" { key = m.PrefixKey() } + _lock.Lock() defer _lock.Unlock() @@ -21,25 +22,15 @@ func (m *Message) _lock(key string) (*sync.RWMutex, string) { l = &sync.RWMutex{} lock[key] = l } - return l, key + return l } func (m *Message) Lock(arg ...Any) func() { - l, key := m._lock(kit.Keys(arg...)) - m.Debug("before lock %v", key) + l := m._lock(kit.Keys(arg...)) l.Lock() - m.Debug("success lock %v", key) - return func() { - l.Unlock() - m.Debug("success unlock %v", key) - } + return func() { l.Unlock() } } func (m *Message) RLock(arg ...Any) func() { - l, key := m._lock(kit.Keys(arg...)) - m.Debug("before rlock %v", key) + l := m._lock(kit.Keys(arg...)) l.RLock() - m.Debug("success rlock %v", key) - return func() { - l.RUnlock() - m.Debug("success runlock %v", key) - } + return func() { l.RUnlock() } }