This commit is contained in:
Liang Ding 2022-05-26 15:18:53 +08:00
parent e650b8100c
commit f40ed985e1
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
1214 changed files with 345766 additions and 9 deletions

39
kernel/cmd/closews.go Normal file
View file

@ -0,0 +1,39 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"github.com/siyuan-note/siyuan/kernel/util"
)
type closews struct {
*BaseCmd
}
func (cmd *closews) Exec() {
id, _ := cmd.session.Get("id")
util.ClosePushChan(id.(string))
cmd.Push()
}
func (cmd *closews) Name() string {
return "closews"
}
func (cmd *closews) IsRead() bool {
return true
}

86
kernel/cmd/cmd.go Normal file
View file

@ -0,0 +1,86 @@
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"github.com/88250/melody"
"github.com/siyuan-note/siyuan/kernel/util"
)
type Cmd interface {
Name() string
IsRead() bool // 非读即写
Id() float64
Exec()
}
type BaseCmd struct {
id float64
param map[string]interface{}
session *melody.Session
PushPayload *util.Result
}
func (cmd *BaseCmd) Id() float64 {
return cmd.id
}
func (cmd *BaseCmd) Push() {
cmd.PushPayload.Callback = cmd.param["callback"]
appId, _ := cmd.session.Get("app")
cmd.PushPayload.AppId = appId.(string)
sid, _ := cmd.session.Get("id")
cmd.PushPayload.SessionId = sid.(string)
util.PushEvent(cmd.PushPayload)
}
func NewCommand(cmdStr string, cmdId float64, param map[string]interface{}, session *melody.Session) (ret Cmd) {
baseCmd := &BaseCmd{id: cmdId, param: param, session: session}
switch cmdStr {
case "closews":
ret = &closews{baseCmd}
}
if nil == ret {
return
}
pushMode := util.PushModeSingleSelf
if pushModeParam := param["pushMode"]; nil != pushModeParam {
pushMode = util.PushMode(pushModeParam.(float64))
}
reloadPushMode := util.PushModeSingleSelf
if reloadPushModeParam := param["reloadPushMode"]; nil != reloadPushModeParam {
reloadPushMode = util.PushMode(reloadPushModeParam.(float64))
}
baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode, reloadPushMode)
appId, _ := baseCmd.session.Get("app")
baseCmd.PushPayload.AppId = appId.(string)
sid, _ := baseCmd.session.Get("id")
baseCmd.PushPayload.SessionId = sid.(string)
return
}
func Exec(cmd Cmd) {
go func() {
//start := time.Now()
defer util.Recover()
cmd.Exec()
//end := time.Now()
//util.Logger.Infof("cmd [%s] exec consumed [%d]ms", cmd.Name(), end.Sub(start).Milliseconds())
}()
}