Skip to content

Commit

Permalink
feat: Renamed "effectSync()" to "syncEffect()"
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Oct 29, 2023
1 parent 587ab2e commit 03528b9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
15 changes: 7 additions & 8 deletions benchmarks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ main();
async function main() {
const bench = new Bench();

bench.add('DEV: compute + effectSync', () => {
return createComputeTest(DEV_CORE, true);
bench.add('DEV: compute + syncEffect', () => {
return createComputeTest(DEV_CORE, DEV_CORE.syncEffect);
});

bench.add('DEV: compute + effect ', () => {
return createComputeTest(DEV_CORE, false);
return createComputeTest(DEV_CORE, DEV_CORE.effect);
});

bench.add('REF: compute + effectSync', () => {
return createComputeTest(REF_CORE, true);
bench.add('REF: compute + syncEffect', () => {
return createComputeTest(REF_CORE, REF_CORE.effectSync);
});

bench.add('REF: compute + effect ', () => {
return createComputeTest(REF_CORE, false);
return createComputeTest(REF_CORE, REF_CORE.effect);
});

await bench.run();
Expand All @@ -36,10 +36,9 @@ async function main() {

function createComputeTest(
core: typeof DEV_CORE | typeof REF_CORE,
sync: boolean,
effectFactory: DEV_CORE.EffectFn,
) {
const { signal, compute } = core;
const effectFactory = sync ? core.effectSync : core.effect;

const entry = signal(0);

Expand Down
2 changes: 1 addition & 1 deletion src/core/_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type {
EffectCleanupFn,
EffectCleanupRegisterFn,
} from './effect';
export { effect, effectSync } from './effect';
export { effect, syncEffect } from './effect';

export { runEffects } from './runtime';

Expand Down
6 changes: 3 additions & 3 deletions src/core/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

import { defaultEquals, Signal } from './common';
import { compute, ComputedImpl } from './compute';
import { effect, effectSync } from './effect';
import { effect, syncEffect } from './effect';
import { SIGNAL_RUNTIME } from './runtime';
import { signal } from './signal';
import { createSignalSubject } from './signalSubject';
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('compute()', () => {

const results: number[] = [];

effectSync(() => results.push(h()));
syncEffect(() => results.push(h()));

entry.set(1);
entry.set(2);
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('compute()', () => {
const b = compute(() => a() + 1);

const results: number[] = [];
const fx = effectSync(() => results.push(b()));
const fx = syncEffect(() => results.push(b()));
source.set(1);
source.set(2);
fx.destroy();
Expand Down
10 changes: 5 additions & 5 deletions src/core/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { flushMicrotasks } from '../test/testUtils';

import { action } from './action';
import { compute } from './compute';
import { effect, effectSync } from './effect';
import { effect, syncEffect } from './effect';
import { signal } from './signal';

describe('effect()', () => {
Expand Down Expand Up @@ -89,12 +89,12 @@ describe('effect()', () => {
});
});

describe('effectSync()', () => {
describe('syncEffect()', () => {
it('should subscribe to an action', () => {
const emitter = action<number>();

let result = 0;
const fx = effectSync(emitter, (value) => (result = value));
const fx = syncEffect(emitter, (value) => (result = value));
expect(result).toBe(0);

emitter(1);
Expand All @@ -110,7 +110,7 @@ describe('effectSync()', () => {
const b = signal(2);

let result = 0;
const fx = effectSync(
const fx = syncEffect(
compute(() => a() + b()),
(value) => (result = value),
);
Expand All @@ -129,7 +129,7 @@ describe('effectSync()', () => {
const b = signal(2);

let result = 0;
const fx = effectSync(() => (result = a() + b()));
const fx = syncEffect(() => (result = a() + b()));
expect(result).toBe(3);

a.set(2);
Expand Down
2 changes: 1 addition & 1 deletion src/core/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const effect: EffectFn = <
);
};

export const effectSync: EffectFn = <
export const syncEffect: EffectFn = <
T,
Source extends Action<T> | Signal<T> | SideEffectFn,
Callback extends Source extends Action<T>
Expand Down
2 changes: 1 addition & 1 deletion src/core/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Scope', () => {
const source = signal<number>(1);
const results: number[] = [];

scope.effectSync(() => results.push(source() * 3));
scope.syncEffect(() => results.push(source() * 3));
source.set(2);
scope.destroy();
source.set(3);
Expand Down
10 changes: 5 additions & 5 deletions src/core/scope.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { action } from './action';
import { effect, EffectFn, effectSync } from './effect';
import { effect, EffectFn, syncEffect } from './effect';
import { signal } from './signal';

export interface Unsubscribable {
Expand Down Expand Up @@ -27,7 +27,7 @@ export type Scope = Readonly<
action: typeof action;
signal: typeof signal;
effect: EffectFn;
effectSync: EffectFn;
syncEffect: EffectFn;
}
>;

Expand Down Expand Up @@ -120,8 +120,8 @@ class ScopeImpl implements Scope {
return result;
}

effectSync(...args: Parameters<EffectFn>) {
const result = effectSync(...args);
syncEffect(...args: Parameters<EffectFn>) {
const result = syncEffect(...args);
this.add(result);
return result;
}
Expand All @@ -142,6 +142,6 @@ export function createScope(): Scope {
action: scope.action.bind(scope),
signal: scope.signal.bind(scope),
effect: scope.effect.bind(scope),
effectSync: scope.effectSync.bind(scope),
syncEffect: scope.syncEffect.bind(scope),
};
}
4 changes: 2 additions & 2 deletions src/rxjs/toObservable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable, share, shareReplay, skip } from 'rxjs';

import { Signal } from '../core/common';
import { effect, effectSync } from '../core/effect';
import { effect, syncEffect } from '../core/effect';
import { SIGNAL_RUNTIME } from '../core/runtime';

export type ToObservableOptions = {
Expand All @@ -19,7 +19,7 @@ export function toObservable<T>(
options?: ToObservableOptions,
): Observable<T> {
const observable = new Observable<T>((subscriber) => {
const effectFn = options?.sync ? effectSync : effect;
const effectFn = options?.sync ? syncEffect : effect;
const scheduler = options?.sync
? SIGNAL_RUNTIME.syncScheduler
: SIGNAL_RUNTIME.asyncScheduler;
Expand Down

0 comments on commit 03528b9

Please sign in to comment.