Skip to content

Commit

Permalink
Renaming internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Jan 24, 2022
1 parent 7e6c33d commit 0b7f58d
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getContext(): Context {
return currentContext;
}

export function runWith<T>(context: Context, fn: () => T): T {
export function runWithContext<T>(context: Context, fn: () => T): T {
const oldContext = { ...currentContext };

if ("reactionId" in context) currentContext.reactionId = context.reactionId;
Expand All @@ -35,7 +35,7 @@ export function runUpdate<T>(context: Context, fn: () => T): T {
if (context.disposerId) flushDisposer(context.disposerId);

try {
return runWith(context, fn);
return runWithContext(context, fn);
} catch (error) {
if (context.disposerId) flushDisposer(context.disposerId);

Expand Down
9 changes: 6 additions & 3 deletions src/core/reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ const reactionsQueue = new Map<ReactionId, Task | number>();
let taskPriority: Priority = Priority.NormalPriority;
let scheduled = false;

export function withPriority(priority: Priority, fn?: () => any) {
export function runWithPriority<T>(
priority: Priority,
fn?: () => T
): T | undefined {
const prev = taskPriority;

try {
taskPriority = priority;
fn?.();
return fn?.();
} finally {
taskPriority = prev;
}
Expand Down Expand Up @@ -64,7 +67,7 @@ export function scheduleReactions(reactionsIds: Array<ReactionId>): void {
requestCallback(
() => {
reactionsQueue.delete(reactionId);
withPriority(task, getComputation(reactionId));
runWithPriority(task, getComputation(reactionId));
},
{ timeout: LOW_PRIORITY_TIMEOUT }
)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/concurrently.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Priority, withPriority } from "../core/reactor";
import { Priority, runWithPriority } from "../core/reactor";

export function concurrently(fn: () => any) {
return withPriority(Priority.LowPriority, fn);
return runWithPriority(Priority.LowPriority, fn);
}
4 changes: 2 additions & 2 deletions src/utils/freeze.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runWith } from "../core/context";
import { runWithContext } from "../core/context";

export function freeze<T>(fn: () => T): T {
return runWith({ reactionId: undefined }, fn);
return runWithContext({ reactionId: undefined }, fn);
}
6 changes: 4 additions & 2 deletions src/utils/root.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runWith } from "../core/context";
import { runWithContext } from "../core/context";
import { createDisposer, flushDisposer } from "../core/disposer";

import type { Disposable } from "../core/disposer";
Expand All @@ -7,5 +7,7 @@ export function root<T>(fn: (disposer: Disposable) => T): T {
const disposerId = createDisposer();
const disposer = () => flushDisposer(disposerId);

return runWith({ disposerId, reactionId: undefined }, () => fn(disposer));
return runWithContext({ disposerId, reactionId: undefined }, () =>
fn(disposer)
);
}
6 changes: 3 additions & 3 deletions tests/core/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, vi, expect } from "vitest";
import { runWith, getContext } from "../../src/core/context";
import { runWithContext, getContext } from "../../src/core/context";
import { createValue } from "../../src/reactive/value";
import { reactive } from "../../src/utils/reactive";
import { root } from "../../src/utils/root";
Expand Down Expand Up @@ -92,7 +92,7 @@ describe("freeze", () => {

expect(getContext().reactionId).toBeUndefined();

runWith({ disposerId: undefined, reactionId }, () => {
runWithContext({ disposerId: undefined, reactionId }, () => {
expect(getContext().reactionId).toBe(reactionId);

freeze(() => {
Expand All @@ -111,7 +111,7 @@ describe("freeze", () => {

expect(getContext().disposerId).toBeUndefined();

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
freeze(() => {
onCleanup(cleanupMock);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/core/disposer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
flushDisposer,
onCleanup,
} from "../../src/core/disposer";
import { runWith } from "../../src/core/context";
import { runWithContext } from "../../src/core/context";
import { root } from "../../src/utils/root";

vi.useFakeTimers();
Expand All @@ -14,7 +14,7 @@ describe("onCleanup", () => {
const disposerId = createDisposer();
const cleanupMock = vi.fn();

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
onCleanup(cleanupMock);
});

Expand Down
6 changes: 3 additions & 3 deletions tests/reactive/memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
flushDisposer,
onCleanup,
} from "../../src/core/disposer";
import { runWith } from "../../src/core/context";
import { runWithContext } from "../../src/core/context";
import { root } from "../../src";

vi.useFakeTimers();
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("createMemo", () => {
const disposerId = createDisposer();
const spy = vi.fn();

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
expect(spy.mock.calls.length).toBe(0);

const getMemo = createMemo(() => {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe("createMemo", () => {
const [getAtom] = createValue(1);
const disposerId = createDisposer();

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
const getMemo = createMemo(() => {
onCleanup(spy);
getAtom();
Expand Down
16 changes: 8 additions & 8 deletions tests/reactive/value.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, vi, expect } from "vitest";
import { runWith } from "../../src/core/context";
import { runWithContext } from "../../src/core/context";
import { createValue } from "../../src/reactive/value";
import { reactive } from "../../src/utils/reactive";
import { root } from "../../src/utils/root";
Expand Down Expand Up @@ -29,7 +29,7 @@ describe("createValue", () => {
const reactionId = createReaction(spy);
const [getAtom, setAtom] = createValue(false);

runWith({ disposerId: undefined, reactionId }, () => getAtom());
runWithContext({ disposerId: undefined, reactionId }, () => getAtom());

expect(spy.mock.calls.length).toBe(0);
expect(getAtom()).toBe(false);
Expand All @@ -49,7 +49,7 @@ describe("createValue", () => {
const reactionId = createReaction(spy);
const [getAtom, setAtom] = createValue(true, false);

runWith({ disposerId: undefined, reactionId }, () => getAtom());
runWithContext({ disposerId: undefined, reactionId }, () => getAtom());

expect(spy.mock.calls.length).toBe(0);

Expand All @@ -68,7 +68,7 @@ describe("createValue", () => {
const [getAtom, setAtom] = createValue(false);

root(() => {
runWith({ disposerId: undefined, reactionId }, () => getAtom());
runWithContext({ disposerId: undefined, reactionId }, () => getAtom());

expect(spy.mock.calls.length).toBe(0);
expect(getAtom()).toBe(false);
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("createValue", () => {
const [getAtom, setAtom] = createValue([1], (a, b) => a[0] === b[0]);

root(() => {
runWith({ disposerId: undefined, reactionId }, () => getAtom());
runWithContext({ disposerId: undefined, reactionId }, () => getAtom());

expect(spy.mock.calls.length).toBe(0);

Expand All @@ -122,7 +122,7 @@ describe("createValue", () => {
const [getAtom, setAtom] = createValue(false);
const disposerId = createDisposer();

runWith({ disposerId, reactionId }, () => getAtom());
runWithContext({ disposerId, reactionId }, () => getAtom());

setAtom(true);

Expand All @@ -147,7 +147,7 @@ describe("createValue", () => {
const [getAtom, setAtom] = createValue(0);
const disposerId = createDisposer();

runWith({ disposerId, reactionId }, () => setAtom(getAtom() + 1));
runWithContext({ disposerId, reactionId }, () => setAtom(getAtom() + 1));

expect(spy.mock.calls.length).toBe(0);
expect(getAtom()).toBe(1);
Expand Down Expand Up @@ -227,7 +227,7 @@ describe("createValue", () => {
spy();
});

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
reactive(() => {
getData();
spy2();
Expand Down
6 changes: 3 additions & 3 deletions tests/utils/reactive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
onCleanup,
} from "../../src/core/disposer";
import { reactive } from "../../src/utils/reactive";
import { runWith } from "../../src/core/context";
import { runWithContext } from "../../src/core/context";
import { createValue } from "../../src/reactive/value";

vi.useFakeTimers();
Expand All @@ -17,7 +17,7 @@ describe("reactive", () => {
const reactionSpy = vi.fn();
const cleanupSpy = vi.fn();

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
reactive(() => {
onCleanup(cleanupSpy);
reactionSpy();
Expand Down Expand Up @@ -126,7 +126,7 @@ describe("reactive", () => {
const disposerId = createDisposer();
const [getAtom, setAtom] = createValue(false);

runWith({ disposerId, reactionId: undefined }, () => {
runWithContext({ disposerId, reactionId: undefined }, () => {
reactive(() => {
getAtom();
reactionSpy();
Expand Down

0 comments on commit 0b7f58d

Please sign in to comment.