@hypen-space/core 0.4.25 → 0.4.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,7 @@ export declare class RemoteServer {
46
46
  private nextClientId;
47
47
  private server;
48
48
  private sessionManager;
49
+ private _syncActions;
49
50
  /**
50
51
  * Set the module for this app
51
52
  */
@@ -78,6 +79,12 @@ export declare class RemoteServer {
78
79
  * Configure session management
79
80
  */
80
81
  session(config: SessionConfig): this;
82
+ /**
83
+ * Enable action synchronization across all connected clients.
84
+ * When enabled, an action dispatched by any client is also dispatched
85
+ * to every other client's engine, keeping all clients in sync.
86
+ */
87
+ syncActions(): this;
81
88
  /**
82
89
  * Register connection callback
83
90
  */
@@ -1637,6 +1637,7 @@ class RemoteServer {
1637
1637
  nextClientId = 1;
1638
1638
  server = null;
1639
1639
  sessionManager = null;
1640
+ _syncActions = false;
1640
1641
  module(name, module) {
1641
1642
  this._moduleName = name;
1642
1643
  this._module = module;
@@ -1658,6 +1659,10 @@ class RemoteServer {
1658
1659
  this._sessionConfig = config2;
1659
1660
  return this;
1660
1661
  }
1662
+ syncActions() {
1663
+ this._syncActions = true;
1664
+ return this;
1665
+ }
1661
1666
  onConnection(callback) {
1662
1667
  this._onConnectionCallbacks.push(callback);
1663
1668
  return this;
@@ -1773,7 +1778,8 @@ class RemoteServer {
1773
1778
  revision: 0,
1774
1779
  connectedAt,
1775
1780
  sessionId: "",
1776
- helloReceived: false
1781
+ helloReceived: false,
1782
+ stateSubscribed: false
1777
1783
  };
1778
1784
  this.clients.set(ws, clientData);
1779
1785
  clientData.helloTimeout = setTimeout(() => {
@@ -1879,19 +1885,39 @@ class RemoteServer {
1879
1885
  if (!data)
1880
1886
  return;
1881
1887
  data.revision++;
1882
- const message = {
1888
+ const patchMessage = {
1883
1889
  type: "patch",
1884
1890
  module: this._moduleName,
1885
1891
  patches,
1886
1892
  revision: data.revision
1887
1893
  };
1888
- ws.send(JSON.stringify(message));
1894
+ ws.send(JSON.stringify(patchMessage));
1895
+ if (data.stateSubscribed) {
1896
+ const stateMessage = {
1897
+ type: "stateUpdate",
1898
+ module: this._moduleName,
1899
+ state: data.moduleInstance.getState(),
1900
+ revision: data.revision
1901
+ };
1902
+ ws.send(JSON.stringify(stateMessage));
1903
+ }
1889
1904
  if (this.sessionManager?.getConcurrentPolicy() === "allow-multiple" && data.sessionId) {
1890
1905
  const connections = this.sessionManager.getConnections(data.sessionId);
1891
1906
  if (connections) {
1892
1907
  for (const conn of connections) {
1893
1908
  if (conn !== ws) {
1894
- conn.send(JSON.stringify(message));
1909
+ const otherWs = conn;
1910
+ const otherData = this.clients.get(otherWs);
1911
+ otherWs.send(JSON.stringify(patchMessage));
1912
+ if (otherData?.stateSubscribed) {
1913
+ const stateMessage = {
1914
+ type: "stateUpdate",
1915
+ module: this._moduleName,
1916
+ state: data.moduleInstance.getState(),
1917
+ revision: data.revision
1918
+ };
1919
+ otherWs.send(JSON.stringify(stateMessage));
1920
+ }
1895
1921
  }
1896
1922
  }
1897
1923
  }
@@ -1960,6 +1986,30 @@ class RemoteServer {
1960
1986
  case "dispatchAction": {
1961
1987
  const actionMsg = msg;
1962
1988
  clientData.engine.dispatchAction(actionMsg.action, actionMsg.payload);
1989
+ if (this._syncActions) {
1990
+ for (const [otherWs, otherClient] of this.clients) {
1991
+ if (otherWs === ws || !otherClient.helloReceived)
1992
+ continue;
1993
+ otherClient.engine.dispatchAction(actionMsg.action, actionMsg.payload);
1994
+ }
1995
+ }
1996
+ break;
1997
+ }
1998
+ case "updateState": {
1999
+ const stateMsg = msg;
2000
+ clientData.moduleInstance.updateState(stateMsg.state);
2001
+ if (this._syncActions) {
2002
+ for (const [otherWs, otherClient] of this.clients) {
2003
+ if (otherWs === ws || !otherClient.helloReceived)
2004
+ continue;
2005
+ otherClient.moduleInstance.updateState(stateMsg.state);
2006
+ }
2007
+ }
2008
+ break;
2009
+ }
2010
+ case "subscribeState": {
2011
+ clientData.stateSubscribed = true;
2012
+ log4.info(`Client ${clientData.id} subscribed to state updates`);
1963
2013
  break;
1964
2014
  }
1965
2015
  default:
@@ -2076,4 +2126,4 @@ export {
2076
2126
  RemoteServer
2077
2127
  };
2078
2128
 
2079
- //# debugId=204F87CAD3EAB92064756E2164756E21
2129
+ //# debugId=E039307432F8950364756E2164756E21