Skip to content

Commit

Permalink
feat: Updated API for the library. Introduced tooling for ViewControl…
Browse files Browse the repository at this point in the history
…lers with Ditox.js DI container.

* feat: Renamed `StateReader` to `StateQuery`

* feat: Refactored API, removed deprecated types and functions.

* feat: Replaced `StateDeclarationOptions` by `StoreOptions` type. Added `options` argument to `StateDeclaration.createStore()` factory.

* feat: Removed `StateDeclaration` and related utilities

* feat: Moved Queries to the top level

* feat: Action can be created with a custom operator for processing an event

* feat: Renamed `StoreOptions.stateComparator` to `StoreOptions.comparator`

* feat: Refactored Store Actions to Store Updates. Introduced `withStoreUpdates()`

* feat: Refactored Store Actions to Store Updates. Introduced `withStoreUpdates()`

* feat: Refactored StoreWithUpdates. Introduced `useStore()` hook.

* refactor: Removed `StoreUpdateRecord` and `StoreUpdater` types. Removed `createStoreUpdatesFactory()`.

* refactor: Introduced `EffectController` utility for building custom effects.

* feat: Introduced `pipeStore()` utility function to create a deferred or transformed view of a store.

* feat: Added tools for creating controllers and view controllers which are compatible with Ditox.js dependency container.

* chore: Updated dev dependencies. Fixed types. Fixed jest config.

* refactor: Renamed `scope.onDestroy()` back to `scope.add()`

* chore: Added 'pack' script

* fix: Stabilizing the library, adding tests

* fix: Stabilizing the library, adding tests

* fix: Stabilizing the library, adding tests

* fix: Stabilizing the library, adding tests

* refactor: Refactored controllers and view controllers

* feat: Added "declareStoreWithUpdates()" utility function

* test: Added a test

* refactor: Minor

* refactor: Reeverted to the previous way of passing parameters to a view controller from UI view.

* refactor: fixes
  • Loading branch information
mnasyrov committed Dec 20, 2022
1 parent 357c8d1 commit 7cffcd0
Show file tree
Hide file tree
Showing 59 changed files with 8,324 additions and 6,829 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
node_modules
npm-debug.log
Thumbs.db
/dist
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,31 @@ import {
Controller,
createAction,
createScope,
declareState,
declareStateUpdates,
EffectState,
StateMutation,
Query,
withStoreUpdates,
} from 'rx-effects';
import { delay, filter, map, mapTo, of } from 'rxjs';

// The state
type CartState = { orders: Array<string> };
type CartState = Readonly<{ orders: Array<string> }>;

// State mutation can be exported and tested separately
const addPizzaToCart =
(name: string): StateMutation<CartState> =>
(state) => ({ ...state, orders: [...state.orders, name] });
// Declare the initial state.
const CART_STATE: CartState = { orders: [] };

const removePizzaFromCart =
(name: string): StateMutation<CartState> =>
(state) => ({
// Declare updates of the state.
const CART_STATE_UPDATES = declareStateUpdates<CartState>({
addPizzaToCart: (name: string) => (state) => ({
...state,
orders: state.orders.filter((order) => order !== name),
});
orders: [...state.orders, name],
}),

// Declaring the state. `declareState()` returns a few factories for the store.
const CART_STATE = declareState<CartState>(() => ({ orders: [] }));
removePizzaFromCart: (name: string) => (state) => ({
...state,
orders: state.orders.filter((order) => order !== name),
}),
});

// Declaring the controller.
// It should provide methods for triggering the actions,
Expand All @@ -108,8 +109,14 @@ export type PizzaShopController = Controller<{
}>;

export function createPizzaShopController(): PizzaShopController {
// Creates the scope to track subscriptions
const scope = createScope();

// Creates the state store
const store = CART_STATE.createStore();
const store = withStoreUpdates(
scope.createStore(CART_STATE),
CART_STATE_UPDATES,
);

// Creates queries for the state data
const ordersQuery = store.query((state) => state.orders);
Expand All @@ -119,15 +126,10 @@ export function createPizzaShopController(): PizzaShopController {
const removePizza = createAction<string>();
const submitCart = createAction();

// Creates the scope for effects to track internal subscriptions
const scope = createScope();

// Handle simple actions
scope.handleAction(addPizza, (order) => store.update(addPizzaToCart(order)));
scope.handle(addPizza, (order) => store.updates.addPizzaToCart(order));

scope.handleAction(removePizza, (name) =>
store.update(removePizzaFromCart(name)),
);
scope.handle(removePizza, (name) => store.updates.removePizzaFromCart(name));

// Create a effect in a general way
const submitEffect = scope.createEffect<Array<string>>((orders) => {
Expand All @@ -145,9 +147,7 @@ export function createPizzaShopController(): PizzaShopController {
);

// Effect's results can be used as actions
scope.handleAction(submitEffect.done$, () =>
store.set(CART_STATE.initialState),
);
scope.handle(submitEffect.done$, () => store.set(CART_STATE));

return {
ordersQuery,
Expand Down
15 changes: 5 additions & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
import fastGlob from 'fast-glob';

export default {
roots: fastGlob.sync(['packages/*/src'], { onlyDirectories: true }),
collectCoverageFrom: ['packages/*/src/**/{!(index|testUtils),}.ts'],
roots: fastGlob.sync(['packages/*/src'], {
onlyDirectories: true,
ignore: ['packages/examples/*'],
}),
collectCoverageFrom: ['packages/*/src/**/{!(index|testUtils),}.{ts,tsx}'],
preset: 'ts-jest',
globals: {
'ts-jest': {
tsconfig: {
module: 'commonjs',
jsx: 'react',
},
},
},
};
Loading

0 comments on commit 7cffcd0

Please sign in to comment.