Skip to content

Commit 7f3b8f9

Browse files
committed
test: add missing tests
1 parent 746a142 commit 7f3b8f9

20 files changed

Lines changed: 123 additions & 44 deletions

src/collections/reactiveArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { batchFlush, batchStart, batchTasks } from "../batch";
22
import { type RemoveListener } from "../event";
3+
import { type ReadableProvider, type OwnedWritable, type Readable } from "../interface";
34
import { writable } from "../readable";
4-
import { type ReadableProvider, type OwnedWritable, type Readable } from "../typings";
55
import { strictEqual } from "../utils";
66
import { onDisposeValue, type OnDisposeValue } from "./utils";
77

src/collections/reactiveMap.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { batchFlush, batchStart, type BatchTask, batchTasks } from "../batch";
22
import { type EventObject, on, type RemoveListener, send, size } from "../event";
3+
import { type ReadableProvider, type OwnedWritable, type Readable } from "../interface";
34
import { writable } from "../readable";
4-
import { type ReadableProvider, type OwnedWritable, type Readable } from "../typings";
55
import { strictEqual } from "../utils";
66
import { onDisposeValue, type OnDisposeValue } from "./utils";
77

@@ -37,6 +37,7 @@ export class OwnedReactiveMap<K, V> extends Map<K, V> implements ReadableProvide
3737
batchTask_: () => {
3838
if (this._onChanged_ && size(this._onChanged_)) {
3939
const { upsert_, delete_ } = this._onChanged_;
40+
/** c8 ignore else -- @preserve */
4041
if (upsert_.size > 0 || delete_.size > 0) {
4142
const changedData = {
4243
upsert: [...upsert_],
@@ -141,17 +142,17 @@ export class OwnedReactiveMap<K, V> extends Map<K, V> implements ReadableProvide
141142
public override clear(): void {
142143
if (this.size) {
143144
const isBatchTop = batchStart();
144-
if (this.onDisposeValue_ || this._onChanged_) {
145-
for (const [key, value] of this) {
146-
if (this.onDisposeValue_) {
147-
this.onDisposeValue_.delete_.add(value);
148-
batchTasks.add(this.onDisposeValue_);
149-
}
150-
if (this._onChanged_) {
151-
this._onChanged_.delete_.add(key);
152-
this._onChanged_.upsert_.delete(key);
153-
batchTasks.add(this._onChanged_);
154-
}
145+
if (this.onDisposeValue_) {
146+
for (const value of this.values()) {
147+
this.onDisposeValue_.delete_.add(value);
148+
batchTasks.add(this.onDisposeValue_);
149+
}
150+
}
151+
if (this._onChanged_) {
152+
for (const key of this.keys()) {
153+
this._onChanged_.delete_.add(key);
154+
this._onChanged_.upsert_.delete(key);
155+
batchTasks.add(this._onChanged_);
155156
}
156157
}
157158
super.clear();

src/collections/reactiveSet.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { batchFlush, batchStart, type BatchTask, batchTasks } from "../batch";
22
import { type EventObject, on, type RemoveListener, send, size } from "../event";
3+
import { type ReadableProvider, type OwnedWritable, type Readable } from "../interface";
34
import { writable } from "../readable";
4-
import { type ReadableProvider, type OwnedWritable, type Readable } from "../typings";
55
import { onDisposeValue, type OnDisposeValue } from "./utils";
66

77
export interface ReactiveSetChanged<V> {
@@ -36,6 +36,7 @@ export class OwnedReactiveSet<V> extends Set<V> implements ReadableProvider<Read
3636
batchTask_: () => {
3737
if (this._onChanged_ && size(this._onChanged_)) {
3838
const { upsert_, delete_ } = this._onChanged_;
39+
/** c8 ignore else -- @preserve */
3940
if (upsert_.size > 0 || delete_.size > 0) {
4041
const changedData = {
4142
upsert: [...upsert_],
@@ -136,17 +137,17 @@ export class OwnedReactiveSet<V> extends Set<V> implements ReadableProvider<Read
136137
public override clear(): void {
137138
if (this.size) {
138139
const isBatchTop = batchStart();
139-
if (this.onDisposeValue_ || this._onChanged_) {
140+
if (this.onDisposeValue_) {
140141
for (const value of this) {
141-
if (this.onDisposeValue_) {
142-
this.onDisposeValue_.delete_.add(value);
143-
batchTasks.add(this.onDisposeValue_);
144-
}
145-
if (this._onChanged_) {
146-
this._onChanged_.delete_.add(value);
147-
this._onChanged_.upsert_.delete(value);
148-
batchTasks.add(this._onChanged_);
149-
}
142+
this.onDisposeValue_.delete_.add(value);
143+
batchTasks.add(this.onDisposeValue_);
144+
}
145+
}
146+
if (this._onChanged_) {
147+
for (const value of this) {
148+
this._onChanged_.delete_.add(value);
149+
this._onChanged_.upsert_.delete(value);
150+
batchTasks.add(this._onChanged_);
150151
}
151152
}
152153
super.clear();

src/combine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { compute } from "./compute";
2-
import { type Config, type OwnedReadable, type ReadableLike } from "./typings";
2+
import { type Config, type OwnedReadable, type ReadableLike } from "./interface";
33

44
export type MapReadablesToValues<TDepValues extends readonly ReadableLike[]> = {
55
[K in keyof TDepValues]: TDepValues[K] extends ReadableLike<infer V> ? V : never;

src/compute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { type ReadableLike, type Config, type Get, type OwnedReadable } from "./interface";
12
import { ReadableImpl } from "./readable";
2-
import { type ReadableLike, type Config, type Get, type OwnedReadable } from "./typings";
33
import { getReadable } from "./utils";
44

55
export interface ComputeFn<TValue = any> {
@@ -49,6 +49,7 @@ export const compute = <TValue>(fn: ComputeFn<TValue>, config?: Config<TValue>):
4949
try {
5050
return fn(get);
5151
} finally {
52+
/** c8 ignore else -- @preserve */
5253
if (isFirst) {
5354
running = false;
5455
}

src/debug/trace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ComputeFn } from "../compute";
2-
import { type ReadableProvider, type Get, type Readable } from "../typings";
2+
import { type ReadableProvider, type Get, type Readable } from "../interface";
33
import { isReadable, isWritable } from "../utils";
44
import { type WatchEffect } from "../watch";
55

src/derive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { compute } from "./compute";
2-
import { type ReadableLike, type Config, type OwnedReadable } from "./typings";
2+
import { type ReadableLike, type Config, type OwnedReadable } from "./interface";
33

44
export interface Derive {
55
/**
@@ -29,7 +29,7 @@ export interface Derive {
2929
* which makes it easier to reuse functions that are not aware of the reactive system.
3030
*
3131
* @param dep - The {@link ReadableLike} to derive from.
32-
* @param transform - A pure function that takes an input value and returns a new value.
32+
* @param transform - Optional pure function that takes an input value and returns a new value.
3333
* @param config - Optional custom {@link Config}.
3434
* @returns A {@link OwnedReadable} with transformed value from the given {@link ReadableLike}.
3535
*/

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type {
1515
SetValue,
1616
Version,
1717
Unwrap,
18-
} from "./typings";
18+
} from "./interface";
1919

2020
export { type Listener, type RemoveListener } from "./event";
2121

@@ -66,6 +66,7 @@ export {
6666
type ReadonlyReactiveArray,
6767
} from "./collections/reactiveArray";
6868

69+
/** c8 ignore else -- @preserve */
6970
if (process.env.NODE_ENV !== "production") {
7071
/* @__PURE__ */ customFormatter();
7172
}

src/typings.ts renamed to src/interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export interface Readable<TValue = any> {
5353
* @internal
5454
*/
5555
deps_?: Map<Readable, Version>;
56+
/**
57+
* Indicates whether the Readable has been disposed.
58+
*/
59+
readonly disposed: boolean;
5660
/**
5761
* Current value of the $.
5862
*/

src/react/useDerive.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export interface UseDerive {
3030
/**
3131
* Derive a new {@link Readable} with transformed value from the given {@link ReadableLike}.
3232
* @param dep - The {@link ReadableLike} to derive from.
33-
* @param transform A pure function that takes an input value and returns a new value.
34-
* @param config custom config for the derived {@link Readable}.
33+
* @param transform - A pure function that takes an input value and returns a new value.
34+
* @param config - Optional custom {@link Config}.
3535
* @returns A {@link Readable} with transformed value from the given {@link ReadableLike}.
3636
*/
3737
<TDepValue, TValue>(
@@ -42,8 +42,8 @@ export interface UseDerive {
4242
/**
4343
* Derive a new {@link Readable} with transformed value from the given {@link ReadableLike}.
4444
* @param dep - The {@link ReadableLike} to derive from, or a non-Readable value that will be returned as-is.
45-
* @param transform A pure function that takes an input value and returns a new value.
46-
* @param config custom config for the derived {@link Readable}.
45+
* @param transform - A pure function that takes an input value and returns a new value.
46+
* @param config - Optional custom {@link Config}.
4747
* @returns A {@link Readable} with transformed value from the given {@link ReadableLike}, or `dep` itself if `dep` is not a {@link ReadableLike}.
4848
*/
4949
<TDepValue, TValue, U>(
@@ -57,7 +57,11 @@ export interface UseDerive {
5757
* Derive a new {@link Readable} from the given {@link ReadableLike}.
5858
*
5959
* Note that changes to `transform` and `config` will not trigger re-derivation, and `useDerive` always uses the latest `transform` and `config` in the derivation.
60-
* In other words, no extra care is needed to use this in React components. All args will be updated properly.
60+
*
61+
* @param dep - The {@link ReadableLike} to derive from, or a non-Readable value that will be returned as-is.
62+
* @param transform - Optional pure function that takes an input value and returns a new value.
63+
* @param config - Optional custom {@link Config}.
64+
* @returns A {@link Readable} with transformed value from the given {@link ReadableLike}, or `dep` itself if `dep` is not a {@link ReadableLike}.
6165
*
6266
* @example
6367
* ```tsx

0 commit comments

Comments
 (0)