Browse Source

:art: localStorage 支持在多界面实例间同步 https://github.com/siyuan-note/siyuan/issues/6965

Liang Ding 2 years ago
parent
commit
853ba5be3a
5 changed files with 38 additions and 10 deletions
  1. 6 1
      kernel/api/storage.go
  2. 1 1
      kernel/go.mod
  3. 2 2
      kernel/go.sum
  4. 7 6
      kernel/util/result.go
  5. 22 0
      kernel/util/websocket.go

+ 6 - 1
kernel/api/storage.go

@@ -188,11 +188,16 @@ func setLocalStorage(c *gin.Context) {
 	}
 	}
 
 
 	val := arg["val"].(interface{})
 	val := arg["val"].(interface{})
-
 	err := model.SetLocalStorage(val)
 	err := model.SetLocalStorage(val)
 	if nil != err {
 	if nil != err {
 		ret.Code = -1
 		ret.Code = -1
 		ret.Msg = err.Error()
 		ret.Msg = err.Error()
 		return
 		return
 	}
 	}
+
+	app := arg["app"].(string)
+	evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcastMainExcludeSelfApp, util.PushModeBroadcastMainExcludeSelfApp)
+	evt.AppId = app
+	evt.Data = val
+	util.PushEvent(evt)
 }
 }

+ 1 - 1
kernel/go.mod

@@ -39,7 +39,7 @@ require (
 	github.com/patrickmn/go-cache v2.1.0+incompatible
 	github.com/patrickmn/go-cache v2.1.0+incompatible
 	github.com/radovskyb/watcher v1.0.7
 	github.com/radovskyb/watcher v1.0.7
 	github.com/shirou/gopsutil/v3 v3.22.11
 	github.com/shirou/gopsutil/v3 v3.22.11
-	github.com/siyuan-note/dejavu v0.0.0-20230101033600-8b9f3e6befc0
+	github.com/siyuan-note/dejavu v0.0.0-20230101054002-870267a37485
 	github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
 	github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
 	github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da
 	github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da
 	github.com/siyuan-note/filelock v0.0.0-20221117095924-e1947438a35e
 	github.com/siyuan-note/filelock v0.0.0-20221117095924-e1947438a35e

+ 2 - 2
kernel/go.sum

@@ -374,8 +374,8 @@ github.com/shurcooL/reactions v0.0.0-20181006231557-f2e0b4ca5b82/go.mod h1:TCR1l
 github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
 github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
 github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
 github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
-github.com/siyuan-note/dejavu v0.0.0-20230101033600-8b9f3e6befc0 h1:iIpzMneefDIHZk3xyXuDSxgz4ZsIvXCgB37LwdpnpBA=
-github.com/siyuan-note/dejavu v0.0.0-20230101033600-8b9f3e6befc0/go.mod h1:gTazhE8f5TuknSodwQukv7GHy+qR1crX5SCxARrTXZU=
+github.com/siyuan-note/dejavu v0.0.0-20230101054002-870267a37485 h1:oqHz0bGugvtUc9sl6YEru8jSA1YNHhMnPcvHLF/RuyM=
+github.com/siyuan-note/dejavu v0.0.0-20230101054002-870267a37485/go.mod h1:gTazhE8f5TuknSodwQukv7GHy+qR1crX5SCxARrTXZU=
 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE=
 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE=
 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
 github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90=
 github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90=

+ 7 - 6
kernel/util/result.go

@@ -24,12 +24,13 @@ import (
 type PushMode int
 type PushMode int
 
 
 const (
 const (
-	PushModeBroadcast               PushMode = 0  // 所有应用所有会话广播
-	PushModeSingleSelf              PushMode = 1  // 自我应用会话单播
-	PushModeBroadcastExcludeSelf    PushMode = 2  // 非自我会话广播
-	PushModeBroadcastExcludeSelfApp PushMode = 4  // 非自我应用所有会话广播
-	PushModeBroadcastApp            PushMode = 5  // 单个应用内所有会话广播
-	PushModeNone                    PushMode = 10 // 不进行 reload
+	PushModeBroadcast                   PushMode = 0  // 所有应用所有会话广播
+	PushModeSingleSelf                  PushMode = 1  // 自我应用会话单播
+	PushModeBroadcastExcludeSelf        PushMode = 2  // 非自我会话广播
+	PushModeBroadcastExcludeSelfApp     PushMode = 4  // 非自我应用所有会话广播
+	PushModeBroadcastApp                PushMode = 5  // 单个应用内所有会话广播
+	PushModeBroadcastMainExcludeSelfApp PushMode = 6  // 非自我应用主会话广播
+	PushModeNone                        PushMode = 10 // 不进行 reload
 )
 )
 
 
 type Result struct {
 type Result struct {

+ 22 - 0
kernel/util/websocket.go

@@ -233,6 +233,8 @@ func PushEvent(event *Result) {
 		broadcastOtherApps(msg, event.AppId)
 		broadcastOtherApps(msg, event.AppId)
 	case PushModeBroadcastApp:
 	case PushModeBroadcastApp:
 		broadcastApp(msg, event.AppId)
 		broadcastApp(msg, event.AppId)
+	case PushModeBroadcastMainExcludeSelfApp:
+		broadcastOtherAppMains(msg, event.AppId)
 	case PushModeNone:
 	case PushModeNone:
 	}
 	}
 }
 }
@@ -282,6 +284,26 @@ func broadcastOtherApps(msg []byte, excludeApp string) {
 	})
 	})
 }
 }
 
 
+func broadcastOtherAppMains(msg []byte, excludeApp string) {
+	sessions.Range(func(key, value interface{}) bool {
+		appSessions := value.(*sync.Map)
+		appSessions.Range(func(key, value interface{}) bool {
+			session := value.(*melody.Session)
+			if app, _ := session.Get("app"); app == excludeApp {
+				return true
+			}
+
+			if t, ok := session.Get("type"); ok && "main" != t {
+				return true
+			}
+
+			session.Write(msg)
+			return true
+		})
+		return true
+	})
+}
+
 func broadcastApp(msg []byte, app string) {
 func broadcastApp(msg []byte, app string) {
 	sessions.Range(func(key, value interface{}) bool {
 	sessions.Range(func(key, value interface{}) bool {
 		appSessions := value.(*sync.Map)
 		appSessions := value.(*sync.Map)