@instantdb/core 0.22.144 → 0.22.145

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/core",
3
- "version": "0.22.144",
3
+ "version": "0.22.145",
4
4
  "description": "Instant's core local abstraction",
5
5
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/core",
6
6
  "repository": {
@@ -53,7 +53,7 @@
53
53
  "dependencies": {
54
54
  "mutative": "^1.0.10",
55
55
  "uuid": "^11.1.0",
56
- "@instantdb/version": "0.22.144"
56
+ "@instantdb/version": "0.22.145"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "vitest",
package/src/Reactor.js CHANGED
@@ -39,6 +39,8 @@ import { InstantStream } from './Stream.ts';
39
39
  /** @typedef {import('./Connection.ts').EventSourceConstructor} EventSourceConstructor */
40
40
  /** @typedef {import('./reactorTypes.ts').QuerySub} QuerySub */
41
41
  /** @typedef {import('./reactorTypes.ts').QuerySubInStorage} QuerySubInStorage */
42
+ /** @typedef {import('./clientTypes.ts').User} User */
43
+ /** @typedef {import('./clientTypes.ts').AuthState} AuthState */
42
44
 
43
45
  export const STATUS = {
44
46
  CONNECTING: 'connecting',
@@ -320,7 +322,9 @@ export default class Reactor {
320
322
  this._broadcastChannel.addEventListener('message', async (e) => {
321
323
  try {
322
324
  if (e.data?.type === 'auth') {
323
- const res = await this.getCurrentUser();
325
+ const res = await this.getCurrentUser({
326
+ forceReadFromStorage: true,
327
+ });
324
328
  await this.updateUser(res.user).catch((error) => {
325
329
  this._log.error('[error] update user', error);
326
330
  });
@@ -1995,7 +1999,7 @@ export default class Reactor {
1995
1999
  if (error) {
1996
2000
  throw new InstantError('Could not get current user: ' + error.message);
1997
2001
  }
1998
- return user;
2002
+ return user ?? null;
1999
2003
  }
2000
2004
 
2001
2005
  subscribeConnectionStatus(cb) {
@@ -2049,26 +2053,37 @@ export default class Reactor {
2049
2053
  return this._currentUserCached;
2050
2054
  }
2051
2055
 
2052
- async _getCurrentUser() {
2056
+ /**
2057
+ * @param {{ forceReadFromStorage?: boolean }} [opts]
2058
+ * @returns {Promise<User | undefined>}
2059
+ */
2060
+ async _getCurrentUser(opts) {
2061
+ if (opts?.forceReadFromStorage) {
2062
+ await this.kv.unloadKey(currentUserKey);
2063
+ }
2053
2064
  const user = await this.kv.waitForKeyToLoad(currentUserKey);
2054
2065
  return typeof user === 'string' ? JSON.parse(user) : user;
2055
2066
  }
2056
2067
 
2057
- async getCurrentUser() {
2068
+ /**
2069
+ * @param {{ forceReadFromStorage?: boolean }} [opts]
2070
+ * @returns {Promise<AuthState>}
2071
+ */
2072
+ async getCurrentUser(opts) {
2058
2073
  const oauthResp = await this._waitForOAuthCallbackResponse();
2059
2074
  if (oauthResp?.error) {
2060
2075
  const errorV = { error: oauthResp.error, user: undefined };
2061
2076
  this._currentUserCached = { isLoading: false, ...errorV };
2062
- return errorV;
2077
+ return this._currentUserCached;
2063
2078
  }
2064
2079
  try {
2065
- const user = await this._getCurrentUser();
2080
+ const user = await this._getCurrentUser(opts);
2066
2081
  const userV = { user: user, error: undefined };
2067
2082
  this._currentUserCached = {
2068
2083
  isLoading: false,
2069
2084
  ...userV,
2070
2085
  };
2071
- return userV;
2086
+ return this._currentUserCached;
2072
2087
  } catch (e) {
2073
2088
  return {
2074
2089
  user: undefined,
@@ -2096,6 +2111,9 @@ export default class Reactor {
2096
2111
  await this.updateUser(newUser);
2097
2112
 
2098
2113
  try {
2114
+ // Make sure everything is written to storage before we tell the
2115
+ // other tab to refetch
2116
+ await this.kv.flush();
2099
2117
  this._broadcastChannel?.postMessage({ type: 'auth' });
2100
2118
  } catch (error) {
2101
2119
  console.error('Error posting message to broadcast channel', error);
@@ -2158,7 +2176,7 @@ export default class Reactor {
2158
2176
  appId: this.config.appId,
2159
2177
  email,
2160
2178
  code,
2161
- refreshToken: isGuest ? currentUser.user.refresh_token : undefined,
2179
+ refreshToken: isGuest ? currentUser?.user?.refresh_token : undefined,
2162
2180
  });
2163
2181
  await this.changeCurrentUser(res.user);
2164
2182
  return res;
@@ -2237,7 +2255,7 @@ export default class Reactor {
2237
2255
  appId: this.config.appId,
2238
2256
  code: code,
2239
2257
  codeVerifier,
2240
- refreshToken: isGuest ? currentUser.user.refresh_token : undefined,
2258
+ refreshToken: isGuest ? currentUser?.user?.refresh_token : undefined,
2241
2259
  });
2242
2260
  await this.changeCurrentUser(res.user);
2243
2261
  return res;