Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/atom-solid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
coverage/
*.tsbuildinfo
node_modules/
.ultra.cache.json
.DS_Store
tmp/
build/
dist/
.direnv/

# files
/src/tsconfig.json
/dist

3 changes: 3 additions & 0 deletions packages/atom-solid/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js
*.ts

2 changes: 2 additions & 0 deletions packages/atom-solid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Changelog

22 changes: 22 additions & 0 deletions packages/atom-solid/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2023-present The Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4 changes: 4 additions & 0 deletions packages/atom-solid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# `@effect-atom/atom-solid`

SolidJS bindings for `@effect-atom/atom`.

25 changes: 25 additions & 0 deletions packages/atom-solid/docgen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"exclude": ["src/internal/**/*.ts"],
"theme": "mikearnaldi/just-the-docs",
"parseCompilerOptions": {
"noEmit": true,
"strict": true,
"target": "es2015",
"lib": ["es2015"],
"paths": {
"@effect-atom/atom": ["./src/index.ts"],
"@effect-atom/atom/*": ["./src/*"]
}
},
"examplesCompilerOptions": {
"noEmit": true,
"strict": true,
"target": "es2015",
"lib": ["es2015"],
"paths": {
"@effect-atom/atom": ["./src/index.ts"],
"@effect-atom/atom/*": ["./src/*"]
}
}
}

37 changes: 37 additions & 0 deletions packages/atom-solid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@effect-atom/atom-solid",
"version": "0.4.5",
"description": "Reactive toolkit for Effect",
"type": "module",
"publishConfig": {
"access": "public",
"directory": "dist"
},
"repository": {
"type": "git",
"url": "https://github.com/tim-smart/effect-atom.git"
},
"homepage": "https://github.com/tim-smart/effect-atom",
"scripts": {
"build": "pnpm build-esm && pnpm build-cjs && pnpm build-annotate && build-utils pack-v2",
"build-esm": "tsc -b tsconfig.build.json",
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
"build-annotate": "babel build --plugins annotate-pure-calls --out-dir build --source-maps"
},
"keywords": [],
"author": "Effect contributors",
"license": "MIT",
"sideEffects": [],
"devDependencies": {
"effect": "^3.19.0",
"solid-js": "^1.9.0"
},
"peerDependencies": {
"effect": "^3.19",
"solid-js": ">=1 <2"
},
"dependencies": {
"@effect-atom/atom": "workspace:^"
}
}

201 changes: 201 additions & 0 deletions packages/atom-solid/src/Hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* @since 1.0.0
*/
import * as Atom from "@effect-atom/atom/Atom"
import type * as AtomRef from "@effect-atom/atom/AtomRef"
import * as Registry from "@effect-atom/atom/Registry"
import type * as Result from "@effect-atom/atom/Result"
import * as Cause from "effect/Cause"
import * as Effect from "effect/Effect"
import * as Exit from "effect/Exit"
import { globalValue } from "effect/GlobalValue"
import type { Accessor } from "solid-js"
import { createSignal, onCleanup, useContext } from "solid-js"
import { RegistryContext } from "./RegistryContext.js"

const initialValuesSet = globalValue(
"@effect-atom/atom-solid/initialValuesSet",
() => new WeakMap<Registry.Registry, WeakSet<Atom.Atom<any>>>()
)

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomInitialValues = (initialValues: Iterable<readonly [Atom.Atom<any>, any]>): void => {
const registry = useContext(RegistryContext)
let set = initialValuesSet.get(registry)
if (set === undefined) {
set = new WeakSet()
initialValuesSet.set(registry, set)
}
for (const [atom, value] of initialValues) {
if (!set.has(atom)) {
set.add(atom)
;(registry as any).ensureNode(atom).setValue(value)
}
}
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomValue: {
<A>(atom: Atom.Atom<A>): Accessor<A>
<A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>
} = <A>(atom: Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
const registry = useContext(RegistryContext)
return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom)
}

function createAtomAccessor<A>(registry: Registry.Registry, atom: Atom.Atom<A>): Accessor<A> {
const [value, setValue] = createSignal<A>(registry.get(atom))
onCleanup(registry.subscribe(atom, setValue as any))
return value
}

function mountAtom<A>(registry: Registry.Registry, atom: Atom.Atom<A>): void {
onCleanup(registry.mount(atom))
}

function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
registry: Registry.Registry,
atom: Atom.Writable<R, W>,
options?: {
readonly mode?: ([R] extends [Result.Result<any, any>] ? Mode : "value") | undefined
}
): "promise" extends Mode ? (
(value: W) => Promise<Result.Result.Success<R>>
) :
"promiseExit" extends Mode ? (
(value: W) => Promise<Exit.Exit<Result.Result.Success<R>, Result.Result.Failure<R>>>
) :
((value: W | ((value: R) => W)) => void)
{
if (options?.mode === "promise" || options?.mode === "promiseExit") {
return ((value: W) => {
registry.set(atom, value)
const promise = Effect.runPromiseExit(
Registry.getResult(registry, atom as Atom.Atom<Result.Result<any, any>>, { suspendOnWaiting: true })
)
return options!.mode === "promise" ? promise.then(flattenExit) : promise
}) as any
}
return ((value: W | ((value: R) => W)) => {
registry.set(atom, typeof value === "function" ? (value as any)(registry.get(atom)) : value)
}) as any
}

const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => {
if (Exit.isSuccess(exit)) return exit.value
throw Cause.squash(exit.cause)
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomMount = <A>(atom: Atom.Atom<A>): void => {
const registry = useContext(RegistryContext)
mountAtom(registry, atom)
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomSet = <
R,
W,
Mode extends "value" | "promise" | "promiseExit" = never
>(
atom: Atom.Writable<R, W>,
options?: {
readonly mode?: ([R] extends [Result.Result<any, any>] ? Mode : "value") | undefined
}
): "promise" extends Mode ? (
(value: W) => Promise<Result.Result.Success<R>>
) :
"promiseExit" extends Mode ? (
(value: W) => Promise<Exit.Exit<Result.Result.Success<R>, Result.Result.Failure<R>>>
) :
((value: W | ((value: R) => W)) => void) =>
{
const registry = useContext(RegistryContext)
mountAtom(registry, atom)
return setAtom(registry, atom, options)
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomRefresh = <A>(atom: Atom.Atom<A>): () => void => {
const registry = useContext(RegistryContext)
mountAtom(registry, atom)
return () => registry.refresh(atom)
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(
atom: Atom.Writable<R, W>,
options?: {
readonly mode?: ([R] extends [Result.Result<any, any>] ? Mode : "value") | undefined
}
): readonly [
value: Accessor<R>,
write: "promise" extends Mode ? (
(value: W) => Promise<Result.Result.Success<R>>
) :
"promiseExit" extends Mode ? (
(value: W) => Promise<Exit.Exit<Result.Result.Success<R>, Result.Result.Failure<R>>>
) :
((value: W | ((value: R) => W)) => void)
] => {
const registry = useContext(RegistryContext)
return [
createAtomAccessor(registry, atom),
setAtom(registry, atom, options)
] as const
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomSubscribe = <A>(
atom: Atom.Atom<A>,
f: (_: A) => void,
options?: { readonly immediate?: boolean }
): void => {
const registry = useContext(RegistryContext)
onCleanup(registry.subscribe(atom, f, options))
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomRef = <A>(ref: AtomRef.ReadonlyRef<A>): Accessor<A> => {
const [value, setValue] = createSignal(ref.value)
onCleanup(ref.subscribe(setValue))
return value
}

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomRefProp = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): AtomRef.AtomRef<A[K]> =>
ref.prop(prop)

/**
* @since 1.0.0
* @category hooks
*/
export const useAtomRefPropValue = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
useAtomRef(useAtomRefProp(ref, prop))
39 changes: 39 additions & 0 deletions packages/atom-solid/src/RegistryContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @since 1.0.0
*/
import type * as Atom from "@effect-atom/atom/Atom"
import * as Registry from "@effect-atom/atom/Registry"
import type { JSX } from "solid-js"
import { createComponent, createContext, onCleanup } from "solid-js"

/**
* @since 1.0.0
* @category context
*/
export const RegistryContext = createContext<Registry.Registry>(Registry.make())

/**
* @since 1.0.0
* @category context
*/
export const RegistryProvider = (options: {
readonly children?: JSX.Element | undefined
readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any]> | undefined
readonly scheduleTask?: ((f: () => void) => void) | undefined
readonly timeoutResolution?: number | undefined
readonly defaultIdleTTL?: number | undefined
}) => {
const registry = Registry.make({
scheduleTask: options.scheduleTask,
initialValues: options.initialValues,
timeoutResolution: options.timeoutResolution,
defaultIdleTTL: options.defaultIdleTTL
})
onCleanup(() => registry.dispose())
return createComponent(RegistryContext.Provider, {
value: registry,
get children() {
return options.children
}
})
}
Loading