Skip to content

Commit

Permalink
add support for injecting initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 21, 2023
1 parent 169515b commit bcad9a7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-cycles-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

add support for injecting initial values
4 changes: 3 additions & 1 deletion docs/rx/Registry.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Added in v1.0.0
**Signature**

```ts
export declare const make: () => Registry
export declare const make: (
options?: { readonly initialValues: Iterable<readonly [Rx.Rx<any>, any]> } | undefined
) => Registry
```
Added in v1.0.0
Expand Down
48 changes: 32 additions & 16 deletions docs/rx/Rx.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Added in v1.0.0

<h2 class="text-delta">Table of contents</h2>

- [batching](#batching)
- [batch](#batch)
- [combinators](#combinators)
- [initialValue](#initialvalue)
- [keepAlive](#keepalive)
- [refreshable](#refreshable)
- [withLabel](#withlabel)
Expand Down Expand Up @@ -57,13 +60,36 @@ Added in v1.0.0
- [TypeId (type alias)](#typeid-type-alias)
- [WritableTypeId](#writabletypeid)
- [WritableTypeId (type alias)](#writabletypeid-type-alias)
- [utils](#utils)
- [batch](#batch)

---

# batching

## batch

**Signature**

```ts
export declare const batch: (f: () => void) => void
```
Added in v1.0.0
# combinators
## initialValue
**Signature**
```ts
export declare const initialValue: {
<A>(initialValue: A): (self: Rx<A>) => readonly [Rx<A>, A]
<A>(self: Rx<A>, initialValue: A): readonly [Rx<A>, A]
}
```
Added in v1.0.0
## keepAlive
**Signature**
Expand All @@ -89,8 +115,10 @@ Added in v1.0.0
**Signature**
```ts
export declare const withLabel: ((name: string) => <A extends Rx<any>>(self: A) => A) &
(<A extends Rx<any>>(self: A, name: string) => <A extends Rx<any>>(self: A) => A)
export declare const withLabel: {
(name: string): <A extends Rx<any>>(self: A) => A
<A extends Rx<any>>(self: A, name: string): <A extends Rx<any>>(self: A) => A
}
```
Added in v1.0.0
Expand Down Expand Up @@ -563,15 +591,3 @@ export type WritableTypeId = typeof WritableTypeId
```
Added in v1.0.0
# utils
## batch
**Signature**
```ts
export declare const batch: (f: () => void) => void
```
Added in v1.0.0
4 changes: 3 additions & 1 deletion packages/rx/src/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ export interface Registry {
* @since 1.0.0
* @category constructors
*/
export const make: () => Registry = internal.make
export const make: (
options?: { readonly initialValues: Iterable<readonly [Rx.Rx<any>, any]> } | undefined
) => Registry = internal.make
20 changes: 18 additions & 2 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,12 @@ export const refreshable = <T extends Rx<any>>(
* @since 1.0.0
* @category combinators
*/
export const withLabel = dual<
export const withLabel: {
(name: string): <A extends Rx<any>>(self: A) => A
<A extends Rx<any>>(self: A, name: string): <A extends Rx<any>>(self: A) => A
} = dual<
(name: string) => <A extends Rx<any>>(self: A) => A,
<A extends Rx<any>>(self: A, name: string) => <A extends Rx<any>>(self: A) => A
<A extends Rx<any>>(self: A, name: string) => A
>(2, (self, name) =>
Object.assign(Object.create(Object.getPrototypeOf(self)), {
...self,
Expand All @@ -727,5 +730,18 @@ export const withLabel = dual<

/**
* @since 1.0.0
* @category combinators
*/
export const initialValue: {
<A>(initialValue: A): (self: Rx<A>) => readonly [Rx<A>, A]
<A>(self: Rx<A>, initialValue: A): readonly [Rx<A>, A]
} = dual<
<A>(initialValue: A) => (self: Rx<A>) => readonly [Rx<A>, A],
<A>(self: Rx<A>, initialValue: A) => readonly [Rx<A>, A]
>(2, (self, initialValue) => [self, initialValue])

/**
* @since 1.0.0
* @category batching
*/
export const batch: (f: () => void) => void = internalRegistry.batch
11 changes: 9 additions & 2 deletions packages/rx/src/internal/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ function constListener(_: any) {}
export const TypeId: Registry.TypeId = Symbol.for("@effect-rx/rx/Registry") as Registry.TypeId

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

class RegistryImpl implements Registry.Registry {
readonly [TypeId]: Registry.TypeId
constructor() {
constructor(initialValues?: Iterable<readonly [Rx.Rx<any>, any]>) {
this[TypeId] = TypeId
if (initialValues !== undefined) {
for (const [rx, value] of initialValues) {
this.ensureNode(rx).setValue(value)
}
}
}

private readonly nodes = new Map<Rx.Rx<any>, Node<any>>()
Expand Down
12 changes: 12 additions & 0 deletions packages/rx/test/Rx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,18 @@ describe("Rx", () => {
expect(count).toEqual(1)
expect(r.get(derived)).toEqual("2b")
})

it("initialValues", async () => {
const state = Rx.state(0)
const r = Registry.make({
initialValues: [
Rx.initialValue(state, 10)
]
})
expect(r.get(state)).toEqual(10)
await new Promise((resolve) => resolve(null))
expect(r.get(state)).toEqual(0)
})
})

interface Counter {
Expand Down

0 comments on commit bcad9a7

Please sign in to comment.