Skip to content

Commit

Permalink
feat: Release effectOn & prepare for v6.0.0 (#813)
Browse files Browse the repository at this point in the history
* feat: Stabilize `effectOn` & prepare for 6.0.0

- Refactors `unstable_effectOn` -> `effectOn`
- Updated docs
- Bumps version to `6.0.0`

todo:
- [ ] Publish a copy of existing docs to `https://easy-peasy-v5.vercel.app/`

* docs: remove last reference to "unstable"

---------

Co-authored-by: Peter Weinberg <[email protected]>
  • Loading branch information
jmyrland and no-stack-dub-sack authored Apr 18, 2023
1 parent 90db988 commit fcb53b9
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 138 deletions.
2 changes: 1 addition & 1 deletion examples/kanban/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type-check": "tsc"
},
"dependencies": {
"easy-peasy": "5.1.0",
"easy-peasy": "6.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"easy-peasy": "5.2.0",
"easy-peasy": "6.0.0",
"next": "13.2.4",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/reduxtagram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@types/react-transition-group": "^4.4.5",
"easy-peasy": "^5.1.0",
"easy-peasy": "6.0.0",
"nanoid": "^4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type-check": "tsc"
},
"dependencies": {
"easy-peasy": "5.1.0",
"easy-peasy": "6.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type ActionListenerTypes = ActionOn<any, any> | ThunkOn<any, any, any>;
type ActionTypes =
| ActionEmitterTypes
| ActionListenerTypes
| Unstable_EffectOn<any, any, any>;
| EffectOn<any, any, any>;

interface ActionCreator<Payload = void> {
(payload: Payload): void;
Expand Down Expand Up @@ -698,7 +698,7 @@ export function computed<

// #region EffectOn

export interface Unstable_EffectOn<
export interface EffectOn<
Model extends object = {},
StoreModel extends object = {},
Injections = any,
Expand All @@ -717,7 +717,7 @@ type Change<Resolvers extends StateResolvers<any, any>> = {

export type Dispose = () => any;

export function unstable_effectOn<
export function effectOn<
Model extends object = {},
StoreModel extends object = {},
Injections = any,
Expand All @@ -732,7 +732,7 @@ export function unstable_effectOn<
change: Change<Resolvers>,
helpers: Helpers<Model, StoreModel, Injections>,
) => undefined | void | Dispose,
): Unstable_EffectOn<Model, StoreModel, Injections>;
): EffectOn<Model, StoreModel, Injections>;

// #endregion

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-peasy",
"version": "5.2.1-alpha.0",
"version": "6.0.0",
"description": "Vegetarian friendly state for React",
"license": "MIT",
"main": "dist/index.cjs.js",
Expand Down
8 changes: 4 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export const actionOn = (targetResolver, fn, config) => ({
[actionOnSymbol]: true,
fn,
targetResolver,
config
config,
});

export const action = (fn, config) => ({
[actionSymbol]: true,
fn,
config
config,
});

const defaultStateResolvers = [(state) => state];
Expand All @@ -47,7 +47,7 @@ export const computed = (fnOrStateResolvers, fn) => {
};
};

export function unstable_effectOn(dependencyResolvers, fn) {
export function effectOn(dependencyResolvers, fn) {
return {
[effectOnSymbol]: true,
dependencyResolvers,
Expand Down Expand Up @@ -82,5 +82,5 @@ export const thunk = (fn) => ({
export const reducer = (fn, config) => ({
[reducerSymbol]: true,
fn,
config
config,
});
56 changes: 25 additions & 31 deletions tests/effect-on.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createStore,
action,
unstable_effectOn,
effectOn,
thunk,
thunkOn,
actionOn,
Expand Down Expand Up @@ -36,7 +36,7 @@ test('fires when the dependencies change', () => {
addTodo: action((state, payload) => {
state.todos.push(payload);
}),
onTodosChanged: unstable_effectOn(
onTodosChanged: effectOn(
[(state) => state.todos, (state) => state.foo],
() => {
fired = true;
Expand All @@ -45,7 +45,7 @@ test('fires when the dependencies change', () => {
});

// ACT
store.getActions().addTodo('add unstable_effectOn api');
store.getActions().addTodo('add effectOn api');

// ASSERT
expect(fired).toBe(true);
Expand All @@ -69,7 +69,7 @@ test('does not fire when the dependencies have not changed', () => {
setFoo: action((state, payload) => {
state.foo = payload;
}),
onTodosChanged: unstable_effectOn([(state) => state.todos], () => {
onTodosChanged: effectOn([(state) => state.todos], () => {
fired = true;
}),
});
Expand All @@ -89,7 +89,7 @@ test('fires when store dependency changes', () => {
setFired: action((state, payload) => {
state.fired = payload;
}),
onTodosChanged: unstable_effectOn(
onTodosChanged: effectOn(
[(state, storeState) => storeState.todos],
(actions) => {
actions.setFired(true);
Expand Down Expand Up @@ -123,7 +123,7 @@ test('it receives the local actions', () => {
setFired: action((state, payload) => {
state.fired = payload;
}),
onTodosChanged: unstable_effectOn([(state) => state.todos], (actions) => {
onTodosChanged: effectOn([(state) => state.todos], (actions) => {
actions.setFired(true);
}),
});
Expand All @@ -144,7 +144,7 @@ test('it receives the local actions for nested model', () => {
nested: {
fired: false,
todos: [],
onTodosChanged: unstable_effectOn([(state) => state.todos], (actions) => {
onTodosChanged: effectOn([(state) => state.todos], (actions) => {
actions.setFired(true);
}),
addTodo: action((state, payload) => {
Expand All @@ -153,7 +153,7 @@ test('it receives the local actions for nested model', () => {
setFired: action((state, payload) => {
state.fired = payload;
}),
}
},
});

// ASSERT
Expand Down Expand Up @@ -184,12 +184,9 @@ test('change argument is as expected', () => {
actions.setTwo(target.payload);
},
),
onStateChanged: unstable_effectOn(
[(state) => state.two],
(actions, change) => {
actualChange = change;
},
),
onStateChanged: effectOn([(state) => state.two], (actions, change) => {
actualChange = change;
}),
});

// ACT
Expand All @@ -216,7 +213,7 @@ test('getState is exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, { getState }) => {
actualState = getState();
Expand Down Expand Up @@ -245,7 +242,7 @@ test('getStoreState is exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, { getStoreState }) => {
actualStoreState = getStoreState();
Expand Down Expand Up @@ -276,7 +273,7 @@ test('meta values are exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, { meta }) => {
actualMeta = meta;
Expand Down Expand Up @@ -308,7 +305,7 @@ test('injections are exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, helpers) => {
actualInjections = helpers.injections;
Expand Down Expand Up @@ -338,7 +335,7 @@ test('dispatch is exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, helpers) => {
actualDispatch = helpers.dispatch;
Expand All @@ -363,7 +360,7 @@ test('getStoreActions are exposed in helpers', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
(actions, change, helpers) => {
helpers.getStoreActions().nested.setString('three');
Expand Down Expand Up @@ -391,12 +388,9 @@ test('dispatches actions to represent a succeeded effect', () => {
setNumber: action((state, payload) => {
state.number = payload;
}),
onStateChanged: unstable_effectOn(
[(state) => state.string],
(actions) => {
actions.setNumber(2);
},
),
onStateChanged: effectOn([(state) => state.string], (actions) => {
actions.setNumber(2);
}),
},
};
const trackActions = trackActionsMiddleware();
Expand Down Expand Up @@ -448,7 +442,7 @@ describe('errors', () => {
doAsync: thunk(() => {
throw err;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[(state) => state.string],
async (actions, change) => {
await actions.doAsync(change.action.payload);
Expand Down Expand Up @@ -504,7 +498,7 @@ test('effects cannot be targetted by actionOn', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn([(state) => state.string], () => {
onStateChanged: effectOn([(state) => state.string], () => {
// do nothing
}),
invalidActionOn: actionOn(
Expand Down Expand Up @@ -535,7 +529,7 @@ test('effects cannot be targetted by thunkOn', async () => {
setString: action((state, payload) => {
state.string = payload;
}),
onStateChanged: unstable_effectOn([(state) => state.string], () => {
onStateChanged: effectOn([(state) => state.string], () => {
// do nothing
}),
invalidThunkOn: actionOn(
Expand Down Expand Up @@ -567,7 +561,7 @@ test('synchronous effect with synchronous dispose executes as expected', () => {
setFoo: action((state, payload) => {
state.foo = payload;
}),
onFooChange: unstable_effectOn([(state) => state.foo], () => {
onFooChange: effectOn([(state) => state.foo], () => {
executionId += 1;
executions.push({ id: executionId, type: 'effect' });
return () => {
Expand Down Expand Up @@ -601,7 +595,7 @@ test('synchronous effect with asynchronous dispose executes as expected', async
setFoo: action((state, payload) => {
state.foo = payload;
}),
onFooChange: unstable_effectOn([(state) => state.foo], () => {
onFooChange: effectOn([(state) => state.foo], () => {
executionId += 1;
const id = executionId;
executions.push({ id, type: 'effect' });
Expand Down
12 changes: 3 additions & 9 deletions tests/typescript/effect-on.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
createStore,
Unstable_EffectOn,
unstable_effectOn,
Action,
action,
} from 'easy-peasy';
import { createStore, EffectOn, effectOn, Action, action } from 'easy-peasy';

interface Injections {
doSomething: () => Promise<void>;
Expand All @@ -14,7 +8,7 @@ interface TodosModel {
items: { id: number; text: string }[];
foo: string;
setFoo: Action<TodosModel, string>;
onStateChanged: Unstable_EffectOn<TodosModel, StoreModel, Injections>;
onStateChanged: EffectOn<TodosModel, StoreModel, Injections>;
}

interface StoreModel {
Expand All @@ -28,7 +22,7 @@ const todosModel: TodosModel = {
setFoo: action((state, payload) => {
state.foo = payload;
}),
onStateChanged: unstable_effectOn(
onStateChanged: effectOn(
[
(state) => state.items,
(state) => state.foo,
Expand Down
15 changes: 6 additions & 9 deletions tests/typescript/external-type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
Computed,
thunk,
Thunk,
unstable_effectOn,
Unstable_EffectOn,
effectOn,
EffectOn,
} from 'easy-peasy';

type MyModelEffectOn = Unstable_EffectOn<MyModel>;
type MyModelEffectOn = EffectOn<MyModel>;
type MyModelAction<TPayload = void> = Action<MyModel, TPayload>;
type MyModelComputed<TResult> = Computed<MyModel, TResult>;
type MyModelThunk<TPayload = undefined, TResult = any> = Thunk<
Expand Down Expand Up @@ -57,10 +57,7 @@ const myModel: MyModel = {
return true;
}),

myEffectOn: unstable_effectOn(
[(state) => state.myState.value],
(actions, change) => {
// do something
},
),
myEffectOn: effectOn([(state) => state.myState.value], (actions, change) => {
// do something
}),
};
Loading

1 comment on commit fcb53b9

@vercel
Copy link

@vercel vercel bot commented on fcb53b9 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

easy-peasy – ./

easy-peasy-ctrlplusb.vercel.app
easy-peasy-git-master-ctrlplusb.vercel.app
easy-peasy.dev

Please sign in to comment.