Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use a molecule from a scope #63

Open
loganvolkers opened this issue May 27, 2024 · 1 comment
Open

Can't use a molecule from a scope #63

loganvolkers opened this issue May 27, 2024 · 1 comment

Comments

@loganvolkers
Copy link
Member

Given that you have LibraryMolecule that is relying on ConfigScope to set up how the molecule should be used:

import { Atom, atom } from "jotai";
import { molecule, createScope, Molecule, use } from "../../vanilla";

export type ExampleConfig = {
  example: Atom<string>;
};

export const ConfigScope = createScope<Molecule<ExampleConfig> | undefined>(
  undefined,
);

export const LibaryMolecule = molecule(() => {
  const configMol = use(ConfigScope);
  if (!configMol)
    throw new Error("This molecule requires ConfigScope to function!");

  const config = use(configMol) as ExampleConfig;
  const derivedAtom = atom((get) => get(config.example).toUpperCase());

  return {
    ...config,
    derivedAtom,
  };
});

And you have two different configs for scope:

export const ConfigAMolecule = molecule<ExampleConfig>(() => {
  return {
    example: atom("Config A"),
  };
});

const ScopeB = createScope("Longer tree");
export const ConfigBMolecule = molecule<ExampleConfig>(() => {
  // This creates a different path for the dependencies of `LibraryMolecule`
  // And can throw errors
  use(ScopeB);
  return {
    example: atom("Config B"),
  };
});

When you use LibraryMolecule with ConfigScope set to ConfigAMolecule
Then it works just fine
But you use LibraryMolecule with ConfigScope set to ConfigBMolecule
And it will throw a Molecule is using conditional dependencies. This is not supported. exception

This is a new limitation in Bunshi 2.1 as part of the introduction of molecule lifecycles.

@loganvolkers
Copy link
Member Author

Conditional dependencies started throwing error in Bunshi 2.1 with the introduction of lifecycle hooks because it became difficult to know how to unmount dependencies when a scope was released.

The un-intended side-effect of that was disabling this use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant