Skip to content

Commit

Permalink
feat: improve action api
Browse files Browse the repository at this point in the history
  • Loading branch information
kepta committed Oct 8, 2023
1 parent 8cffe2b commit 5ea36c7
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 185 deletions.
12 changes: 3 additions & 9 deletions misc/__test__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ import {
} from '../type-helpers';

const dep1Key = createKey('dep1Key', []);
const dep1Slice = dep1Key.slice({
fields: {},
});
const dep1Slice = dep1Key.slice({});

const dep2Key = createKey('dep2Key', []);
const dep2Slice = dep2Key.slice({
fields: {},
});
const dep2Slice = dep2Key.slice({});

const key = createKey('myKey', [dep1Slice, dep2Slice]);

const field1 = key.field(1);

const mySlice = key.slice({
fields: {},
});
const mySlice = key.slice({});

const store = createStore({
slices: [dep1Slice, dep2Slice, mySlice],
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('actions', () => {
const counterNegative = key.field(-1);

const counterSlice = key.slice({
fields: { counter, counterNegative },
counter,
counterNegative,
});

function increment() {
Expand Down Expand Up @@ -186,7 +187,7 @@ describe('actions', () => {
});
}
const baseSlice = key.slice({
fields: { base },
base,
});

test('should work', () => {
Expand Down
24 changes: 6 additions & 18 deletions packages/core/src/__tests__/store-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ const sliceOneKey = createKey('sliceOne', []);
const keyOne = sliceOneKey.field('valueOne');

const sliceOne = sliceOneKey.slice({
fields: {
keyOne,
},
keyOne,
});

const sliceTwoKey = createKey('sliceTwo', []);
const keyTwo = sliceTwoKey.field('valueTwo');

const sliceTwo = sliceTwoKey.slice({
fields: {
keyTwo,
},
keyTwo,
});

const updateKeyOneSliceOne = (val: string) => {
Expand Down Expand Up @@ -104,9 +100,7 @@ describe('StoreState Slice and Transaction Operations', () => {
const immutableField = immutableSliceKey.field(fixedState);

const immutableSlice = immutableSliceKey.slice({
fields: {
immutableField: immutableField,
},
immutableField: immutableField,
});

function nonMutatingAction(inputNumber: number) {
Expand Down Expand Up @@ -138,9 +132,7 @@ describe('StoreState Slice and Transaction Operations', () => {
const myField = mySliceKey.field(fixedState);

const mySlice = mySliceKey.slice({
fields: {
myField: myField,
},
myField: myField,
});

function nonMutatingAction(inputNumber: number) {
Expand Down Expand Up @@ -177,18 +169,14 @@ describe('StoreState Slice and Transaction Operations', () => {
const counterA = sliceAKey.field(1);

const sliceA = sliceAKey.slice({
fields: {
counter: counterA,
},
counter: counterA,
});

const sliceBKey = createKey('sliceB', [sliceA]);
const counterB = sliceBKey.field(1);

const sliceB = sliceBKey.slice({
fields: {
counter: counterB,
},
counter: counterB,
});

function actionIncrementCounterA() {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/__tests__/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const key = createKey('mySliceName', []);
const counter = key.field(0);
const counterNegative = key.field(-1);

const counterSlice = key.slice({
fields: { counter, counterNegative },
});
const increment = () => {
return counter.update((val) => val + 1);
};
const counterSlice = key.slice({ counter, counterNegative, increment });

afterEach(() => {
testCleanup();
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/effect/__tests__/effect-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ const setup = () => {
const sliceAKey = createKey('slice1', []);
const fooField = sliceAKey.field('bar');
const sliceA = sliceAKey.slice({
fields: {
foo: fooField,
},
foo: fooField,
});

const sliceBKey = createKey('slice2', []);
const sliceBField = sliceBKey.field('bar');
const sliceBOtherField = sliceBKey.field('bizz');
const sliceB = sliceBKey.slice({
fields: {
sliceBField: sliceBField,
sliceBOtherField: sliceBOtherField,
},
sliceBField: sliceBField,
sliceBOtherField: sliceBOtherField,
});

const store = createStore({
Expand Down
18 changes: 6 additions & 12 deletions packages/core/src/effect/__tests__/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ const sliceAField1 = sliceAKey.field('value:sliceAField1');
const sliceAField2 = sliceAKey.field('value:sliceAField2');

const sliceA = sliceAKey.slice({
fields: {
sliceAField1,
sliceAField2,
},
sliceAField1,
sliceAField2,
});

const sliceBKey = createKey('sliceB', []);
const sliceBField1 = sliceBKey.field('value:sliceBField1');

const sliceB = sliceBKey.slice({
fields: {
sliceBField1,
},
sliceBField1,
});

const sliceCDepBKey = createKey('sliceCDepB', [sliceB]);
Expand All @@ -61,11 +57,9 @@ const sliceCDepBSelector2 = sliceCDepBKey.derive((state) => {
});

const sliceCDepB = sliceCDepBKey.slice({
fields: {
sliceCDepBField,
sliceCDepBSelector1,
sliceCDepBSelector2,
},
sliceCDepBField,
sliceCDepBSelector1,
sliceCDepBSelector2,
});

describe('effect with store', () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/effect/__tests__/ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ const sliceAField1 = sliceAKey.field('value:sliceAField1');
const sliceAField2 = sliceAKey.field('value:sliceAField2');

const sliceA = sliceAKey.slice({
fields: {
sliceAField1,
sliceAField2,
},
sliceAField1,
sliceAField2,
});

const sliceBKey = createKey('sliceB', []);
const sliceBField1 = sliceBKey.field('value:sliceBField1');

const sliceB = sliceBKey.slice({
fields: {
sliceBField1,
},
sliceBField1,
});

beforeEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/helpers/__tests__/validations.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { describe, expect, test } from '@jest/globals';
import { AnySlice } from '../../slice/slice';
import {
circularCheck,
findDuplications,
validateSlices,
} from '../validations';
import { Slice } from '../../slice/slice';

const createSlice = ({ sliceId, dependencies }: any): AnySlice => {
const createSlice = ({ sliceId, dependencies }: any): Slice => {
return {
sliceId,
dependencies,
} as AnySlice;
} as Slice;
};

describe('Slice validation', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/helpers/validations.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AnySlice } from '../slice/slice';
import { Slice } from '../slice/slice';

export function validateSlices(slices: AnySlice[]) {
export function validateSlices(slices: Slice[]) {
checkUniqDependency(slices);
checkUniqueSliceId(slices);
checkDependencyOrder(slices);
circularCheck(slices);
}

function checkUniqDependency(slices: AnySlice[]) {
function checkUniqDependency(slices: Slice[]) {
for (const slice of slices) {
const dependencies = slice.dependencies;

Expand All @@ -23,7 +23,7 @@ function checkUniqDependency(slices: AnySlice[]) {
}
}

export function checkUniqueSliceId(slices: AnySlice[]) {
export function checkUniqueSliceId(slices: Slice[]) {
const dups = checkUnique(slices.map((s) => s.sliceId));

if (dups) {
Expand Down Expand Up @@ -56,11 +56,11 @@ export function findDuplications<T>(arr: T[]): T[] {
return [...dupes];
}

export function circularCheck(slices: AnySlice[]) {
export function circularCheck(slices: Slice[]) {
const stack = new Set<string>();
const visited = new Set<string>();

const checkCycle = (slice: AnySlice): boolean => {
const checkCycle = (slice: Slice): boolean => {
const sliceId = slice.sliceId;

if (stack.has(sliceId)) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export function circularCheck(slices: AnySlice[]) {
}
}

function checkDependencyOrder(slices: AnySlice[]) {
function checkDependencyOrder(slices: Slice[]) {
let seenSliceIds = new Set<string>();
for (const slice of slices) {
const dependencies = slice.dependencies;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ export { createStore } from './store';
export { DerivedField, StateField } from './slice/field';
export { ref } from './effect/ref';
export { Slice } from './slice/slice';

export type { BaseField } from './slice/field';
export type { Effect, EffectStore, EffectScheduler } from './effect/effect';
export type { IfSubset } from './types';
export type { Key } from './slice/key';
export type { Store } from './store';
export type { StoreState } from './store-state';

// for internal packages only
export type {
InferSliceFieldState as _InferSliceFieldState,
ExposedSliceFieldNames as _ExposedSliceFieldNames,
} from './slice/slice';
export type { AnyExternal as _AnyExternal } from './slice/key';
22 changes: 7 additions & 15 deletions packages/core/src/slice/__tests__/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { testCleanup } from '../../helpers/test-cleanup';
import { createKey } from '../key';
import { createStore } from '../../store';
import { AnySlice, Slice } from '../slice';
import { Slice } from '../slice';
import { StoreState } from '../../store-state';
import { Transaction } from '../../transaction';
import { expectType } from '../../types';
Expand All @@ -18,7 +18,7 @@ beforeEach(() => {
testCleanup();
});

type GetStoreStateFromSliceName<TSlice extends AnySlice> = TSlice extends Slice<
type GetStoreStateFromSliceName<TSlice extends Slice> = TSlice extends Slice<
any,
infer TSliceName,
any
Expand All @@ -30,9 +30,7 @@ describe('internal fields', () => {
test('internal field should be updated', () => {
const key = createKey('mySliceName', []);
const counter = key.field(0);
const counterSlice = key.slice({
fields: {},
});
const counterSlice = key.slice({});

function updateCounter(state: number) {
return counter.update(state + 1);
Expand Down Expand Up @@ -67,10 +65,8 @@ describe('internal fields', () => {
});

const counterSlice = key.slice({
fields: {
myName,
externalDerivedOnCounter,
},
myName,
externalDerivedOnCounter,
});

function updateCounter() {
Expand Down Expand Up @@ -169,17 +165,13 @@ describe('internal fields', () => {
const mySliceKey = createKey('mySlice', []);
const aField = mySliceKey.field(1);
const mySlice = mySliceKey.slice({
fields: {
a: aField,
},
a: aField,
});

const mySlice2Key = createKey('mySlice2', [mySlice]);
const aField2 = mySlice2Key.field(1);
const mySlice2 = mySlice2Key.slice({
fields: {
a: aField2,
},
a: aField2,
});

// type checks
Expand Down
Loading

0 comments on commit 5ea36c7

Please sign in to comment.