Skip to content

Commit

Permalink
schedule tasks with react scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed May 8, 2024
1 parent 61053ec commit dc3dbee
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/seven-shirts-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@effect-rx/rx-react": patch
"@effect-rx/rx": patch
---

schedule tasks with react scheduler
7 changes: 5 additions & 2 deletions packages/rx-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
"sideEffects": false,
"devDependencies": {
"@types/react": "^18.3.1",
"@types/scheduler": "^0.23.0",
"effect": "^3.1.1",
"react": "^18.3.1"
"react": "^18.3.1",
"scheduler": "^0.23"
},
"peerDependencies": {
"effect": "^3.0.5",
"react": "^18"
"react": "^18",
"scheduler": "*"
},
"dependencies": {
"@effect-rx/rx": "workspace:^"
Expand Down
9 changes: 8 additions & 1 deletion packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Cause from "effect/Cause"
import type * as Exit from "effect/Exit"
import { globalValue } from "effect/GlobalValue"
import * as React from "react"
import * as Scheduler from "scheduler"

/**
* @since 1.0.0
Expand All @@ -31,11 +32,17 @@ export * as Rx from "@effect-rx/rx/Rx"
*/
export * as RxRef from "@effect-rx/rx/RxRef"

function scheduleTask(f: () => void): void {
Scheduler.unstable_scheduleCallback(Scheduler.unstable_LowPriority, f)
}

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

interface RxStore<A> {
readonly subscribe: (f: () => void) => () => void
Expand Down
6 changes: 5 additions & 1 deletion packages/rx/src/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ export interface Registry {
* @category constructors
*/
export const make: (
options?: { readonly initialValues: Iterable<readonly [Rx.Rx<any>, any]> } | undefined
options?: {
readonly initialValues?: Iterable<readonly [Rx.Rx<any>, any]> | undefined
readonly scheduleTask?: ((f: () => void) => void) | undefined
readonly timeoutResolution?: number | undefined
} | undefined
) => Registry = internal.make
16 changes: 12 additions & 4 deletions packages/rx/src/internal/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ export const TypeId: Registry.TypeId = Symbol.for("@effect-rx/rx/Registry") as R

/** @internal */
export const make = (options?: {
readonly initialValues: Iterable<readonly [Rx.Rx<any>, any]>
}): Registry.Registry => new RegistryImpl(options?.initialValues)
readonly initialValues?: Iterable<readonly [Rx.Rx<any>, any]> | undefined
readonly scheduleTask?: ((f: () => void) => void) | undefined
readonly timeoutResolution?: number | undefined
}): Registry.Registry =>
new RegistryImpl(
options?.initialValues,
options?.scheduleTask,
options?.timeoutResolution
)

class RegistryImpl implements Registry.Registry {
readonly [TypeId]: Registry.TypeId
constructor(
initialValues?: Iterable<readonly [Rx.Rx<any>, any]>,
readonly scheduleTask = queueMicrotask,
readonly timeoutResolution = 5000
) {
this[TypeId] = TypeId
Expand Down Expand Up @@ -104,7 +112,7 @@ class RegistryImpl implements Registry.Registry {
}

scheduleRxRemoval(rx: Rx.Rx<any>): void {
queueMicrotask(() => {
this.scheduleTask(() => {
const node = this.nodes.get(rx)
if (node !== undefined && node.canBeRemoved) {
this.removeNode(node)
Expand All @@ -113,7 +121,7 @@ class RegistryImpl implements Registry.Registry {
}

scheduleNodeRemoval(node: Node<any>): void {
queueMicrotask(() => {
this.scheduleTask(() => {
if (node.canBeRemoved) {
this.removeNode(node)
}
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc3dbee

Please sign in to comment.