jazz-tools 0.10.14 → 0.10.15

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > jazz-tools@0.10.14 build /home/runner/_work/jazz/jazz/packages/jazz-tools
2
+ > jazz-tools@0.10.15 build /home/runner/_work/jazz/jazz/packages/jazz-tools
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: {"index":"src/index.ts","testing":"src/testing.ts"}
@@ -10,9 +10,9 @@
10
10
  CLI Cleaning output folder
11
11
  ESM Build start
12
12
  ESM dist/index.js 1.50 KB
13
- ESM dist/testing.js 6.28 KB
14
- ESM dist/chunk-5YDDEUNX.js 115.67 KB
13
+ ESM dist/testing.js 6.18 KB
14
+ ESM dist/chunk-5USJBXLW.js 116.50 KB
15
15
  ESM dist/index.js.map 259.00 B
16
- ESM dist/testing.js.map 12.41 KB
17
- ESM dist/chunk-5YDDEUNX.js.map 275.90 KB
18
- ESM ⚡️ Build success in 38ms
16
+ ESM dist/testing.js.map 12.24 KB
17
+ ESM dist/chunk-5USJBXLW.js.map 277.65 KB
18
+ ESM ⚡️ Build success in 59ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # jazz-tools
2
2
 
3
+ ## 0.10.15
4
+
5
+ ### Patch Changes
6
+
7
+ - 2f99de0: Avoid race conditions on context creation and run the anonymous migration only when the hook is provided
8
+ - Updated dependencies [f86e278]
9
+ - cojson@0.10.15
10
+
3
11
  ## 0.10.14
4
12
 
5
13
  ### Patch Changes
@@ -3591,7 +3591,7 @@ import { cojsonInternals as cojsonInternals4 } from "cojson";
3591
3591
  var JazzContextManager = class {
3592
3592
  constructor() {
3593
3593
  this.authSecretStorage = new AuthSecretStorage();
3594
- this.authenticating = false;
3594
+ this.keepContextOpen = false;
3595
3595
  this.logOut = async () => {
3596
3596
  if (!this.context || !this.props) {
3597
3597
  return;
@@ -3606,6 +3606,14 @@ var JazzContextManager = class {
3606
3606
  }
3607
3607
  this.context.done();
3608
3608
  };
3609
+ this.shouldMigrateAnonymousAccount = async () => {
3610
+ if (!this.props?.onAnonymousAccountDiscarded) {
3611
+ return false;
3612
+ }
3613
+ const prevCredentials = await this.authSecretStorage.get();
3614
+ const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3615
+ return wasAnonymous;
3616
+ };
3609
3617
  /**
3610
3618
  * Authenticates the user with the given credentials
3611
3619
  */
@@ -3614,13 +3622,12 @@ var JazzContextManager = class {
3614
3622
  throw new Error("Props required");
3615
3623
  }
3616
3624
  const prevContext = this.context;
3617
- const prevCredentials = await this.authSecretStorage.get();
3618
- const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3619
- this.authenticating = true;
3625
+ const migratingAnonymousAccount = await this.shouldMigrateAnonymousAccount();
3626
+ this.keepContextOpen = migratingAnonymousAccount;
3620
3627
  await this.createContext(this.props, { credentials }).finally(() => {
3621
- this.authenticating = false;
3628
+ this.keepContextOpen = false;
3622
3629
  });
3623
- if (wasAnonymous) {
3630
+ if (migratingAnonymousAccount) {
3624
3631
  await this.handleAnonymousAccountMigration(prevContext);
3625
3632
  }
3626
3633
  };
@@ -3629,18 +3636,17 @@ var JazzContextManager = class {
3629
3636
  throw new Error("Props required");
3630
3637
  }
3631
3638
  const prevContext = this.context;
3632
- const prevCredentials = await this.authSecretStorage.get();
3633
- const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3634
- this.authenticating = true;
3639
+ const migratingAnonymousAccount = await this.shouldMigrateAnonymousAccount();
3640
+ this.keepContextOpen = migratingAnonymousAccount;
3635
3641
  await this.createContext(this.props, {
3636
3642
  newAccountProps: {
3637
3643
  secret: accountSecret,
3638
3644
  creationProps
3639
3645
  }
3640
3646
  }).finally(() => {
3641
- this.authenticating = false;
3647
+ this.keepContextOpen = false;
3642
3648
  });
3643
- if (wasAnonymous) {
3649
+ if (migratingAnonymousAccount) {
3644
3650
  await this.handleAnonymousAccountMigration(prevContext);
3645
3651
  }
3646
3652
  if (this.context && "me" in this.context) {
@@ -3661,12 +3667,27 @@ var JazzContextManager = class {
3661
3667
  return new InMemoryKVStore();
3662
3668
  }
3663
3669
  async createContext(props, authProps) {
3670
+ this.props = props;
3671
+ const { promise, resolve } = createResolvablePromise();
3672
+ const prevPromise = this.contextPromise;
3673
+ this.contextPromise = promise;
3674
+ await prevPromise;
3675
+ try {
3676
+ const result = await this.getNewContext(props, authProps);
3677
+ await this.updateContext(props, result, authProps);
3678
+ resolve();
3679
+ } catch (error) {
3680
+ resolve();
3681
+ throw error;
3682
+ }
3683
+ }
3684
+ async getNewContext(props, authProps) {
3664
3685
  props;
3665
3686
  authProps;
3666
3687
  throw new Error("Not implemented");
3667
3688
  }
3668
3689
  async updateContext(props, context, authProps) {
3669
- if (!this.authenticating) {
3690
+ if (!this.keepContextOpen) {
3670
3691
  this.context?.done();
3671
3692
  }
3672
3693
  this.context = context;
@@ -3728,6 +3749,13 @@ var JazzContextManager = class {
3728
3749
  }
3729
3750
  }
3730
3751
  };
3752
+ function createResolvablePromise() {
3753
+ let resolve;
3754
+ const promise = new Promise((res) => {
3755
+ resolve = res;
3756
+ });
3757
+ return { promise, resolve };
3758
+ }
3731
3759
 
3732
3760
  // src/auth/DemoAuth.ts
3733
3761
  var DemoAuth = class {
@@ -4045,4 +4073,4 @@ export {
4045
4073
  consumeInviteLink
4046
4074
  };
4047
4075
  /* istanbul ignore file -- @preserve */
4048
- //# sourceMappingURL=chunk-5YDDEUNX.js.map
4076
+ //# sourceMappingURL=chunk-5USJBXLW.js.map