@kokimoki/app 0.6.6 → 0.6.8

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.
@@ -18,7 +18,7 @@ export declare class KokimokiClient<StatelessDataT = any, ClientContextT = any>
18
18
  private _providers;
19
19
  private _serverTimeOffset;
20
20
  private _clientContext?;
21
- private _synced;
21
+ private _connected;
22
22
  private _lastPongAt;
23
23
  constructor(host: string, appId: string, code?: string);
24
24
  get id(): string;
@@ -13,7 +13,7 @@ export class KokimokiClient extends EventEmitter {
13
13
  _providers = new Map();
14
14
  _serverTimeOffset = 0;
15
15
  _clientContext;
16
- _synced = false;
16
+ _connected = false;
17
17
  _lastPongAt = 0;
18
18
  constructor(host, appId, code = "") {
19
19
  super();
@@ -81,9 +81,12 @@ export class KokimokiClient extends EventEmitter {
81
81
  });
82
82
  // Ping interval
83
83
  setInterval(() => {
84
- this.checkConnectionState();
85
84
  this._providers.forEach((provider) => provider.sendStateless("ping"));
86
85
  }, 5000);
86
+ // Connection state interval
87
+ setInterval(() => {
88
+ this.checkConnectionState();
89
+ }, 1000);
87
90
  // Check initial connected state
88
91
  this.receivePong();
89
92
  }
@@ -95,24 +98,20 @@ export class KokimokiClient extends EventEmitter {
95
98
  this.checkConnectionState();
96
99
  }
97
100
  checkConnectionState() {
98
- const pongReceived = Date.now() - this._lastPongAt > 6000;
99
- if (!pongReceived && this._synced) {
101
+ const connected = this._providers.size === 0 || Date.now() - this._lastPongAt < 6000;
102
+ if (connected && !this._connected) {
103
+ this._connected = true;
104
+ this.emit("connected");
105
+ }
106
+ else if (!connected && this._connected) {
107
+ this._connected = false;
108
+ this.emit("disconnected");
100
109
  // Reset connections to providers
101
110
  this._providers.forEach(async (provider) => {
102
111
  provider.disconnect();
103
112
  await provider.connect();
104
113
  });
105
114
  }
106
- const synced = pongReceived &&
107
- !Array.from(this._providers.values()).some((provider) => provider.status !== "connected" || !provider.synced);
108
- if (synced && !this._synced) {
109
- this._synced = true;
110
- this.emit("connected");
111
- }
112
- else if (!synced && this._synced) {
113
- this._synced = false;
114
- this.emit("disconnected");
115
- }
116
115
  }
117
116
  // Realtime database
118
117
  async setProvider(name, store) {
@@ -128,7 +127,7 @@ export class KokimokiClient extends EventEmitter {
128
127
  // Handle incoming stateless messages
129
128
  provider.on("stateless", (e) => {
130
129
  if (e.payload === "pong") {
131
- this._lastPongAt = Date.now();
130
+ this.receivePong();
132
131
  return;
133
132
  }
134
133
  const payload = JSON.parse(e.payload);
@@ -142,21 +141,9 @@ export class KokimokiClient extends EventEmitter {
142
141
  };
143
142
  provider.on("synced", handler);
144
143
  });
144
+ this._lastPongAt = Date.now();
145
145
  this._providers.set(name, provider);
146
146
  this.checkConnectionState();
147
- // Handle connection state changes
148
- provider.on("disconnect", async () => {
149
- // KokimokiClient is considered disconnected when any single provider is disconnected
150
- this.checkConnectionState();
151
- // Attempt to reconnect immediately with retries
152
- await provider.connect();
153
- });
154
- provider.on("connect", () => {
155
- this.checkConnectionState();
156
- });
157
- provider.on("synced", () => {
158
- this.checkConnectionState();
159
- });
160
147
  }
161
148
  removeProvider(name) {
162
149
  const provider = this._providers.get(name);
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const KOKIMOKI_APP_VERSION = "0.6.6";
1
+ export declare const KOKIMOKI_APP_VERSION = "0.6.8";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const KOKIMOKI_APP_VERSION = "0.6.6";
1
+ export const KOKIMOKI_APP_VERSION = "0.6.8";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokimoki/app",
3
- "version": "0.6.6",
3
+ "version": "0.6.8",
4
4
  "type": "module",
5
5
  "description": "Kokimoki app",
6
6
  "main": "dist/index.js",