import { Atom, atom, PrimitiveAtom } from "jotai"; import { molecule } from "../molecule"; import { createScope } from "../scope"; import { ScopeTuple } from "../types"; import { Molecule, use } from ".."; export type BaseAtoms = { nameAtom: PrimitiveAtom; }; export const exampleMol = molecule(() => { return { nameAtom: atom(`${Math.random()}`), }; }); const UnrelatedScope = createScope(1); export const unrelatedScope1: ScopeTuple = [UnrelatedScope, 1]; export const UserScope = createScope("bob@example.com"); export const user1Scope: ScopeTuple = [UserScope, "one@example.com"]; export const user2Scope: ScopeTuple = [UserScope, "two@example.com"]; export const CompanyScope = createScope("example.com"); export const company1Scope: ScopeTuple = [ CompanyScope, "one.example.com", ]; export const company2Scope: ScopeTuple = [ CompanyScope, "two.example.com", ]; export const userMolecule = molecule((mol, scope) => { const userId = scope(UserScope); const company = mol(companyMolecule); const userNameAtom = atom(userId + " name"); const userCountryAtom = atom(userId + " country"); const groupAtom = atom((get) => { return userId + " in " + get(company.companyNameAtom); }); return { userId, userCountryAtom, userNameAtom, groupAtom, company: company.company, }; }); export const companyMolecule = molecule((_, getScope) => { const company = getScope(CompanyScope); const companyNameAtom = atom(company.toUpperCase()); return { company, companyNameAtom, }; }); type Config = { example: Atom; }; export const ConfigMolecule = molecule(() => { return { example: atom("example"), }; }); export const ConfigScope = createScope | 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 Config; const derivedAtom = atom((get) => get(config.example).toUpperCase()); return { ...config, derivedAtom, }; });