Skip to content

Commit

Permalink
Merge pull request #1 from young-do/0.2
Browse files Browse the repository at this point in the history
0.2
  • Loading branch information
young-do authored Aug 22, 2022
2 parents 09d4ce9 + 16c683d commit 37eb96b
Show file tree
Hide file tree
Showing 20 changed files with 743 additions and 2,467 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
coverage
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import { atom, on } from '@youngdo/rx-state';
export const count$ = atom<number>(0, 'count$', count$ => {
on(INCREASE).subscribe(() => count$.set(count$.value + 1));
on(DECREASE).subscribe(() => count$.set(count$.value - 1));

// // or use 'update'
// on(INCREASE).subscribe(() => count$.update(prev => prev + 1));
// on(DECREASE).subscribe(() => count$.update(prev => prev - 1));
});
```

Expand Down
46 changes: 19 additions & 27 deletions examples/todo-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/todo-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// @note: for debug
import { setLogLevel } from '@youngdo/rx-state';
setLogLevel('named');

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
Expand Down
58 changes: 25 additions & 33 deletions examples/todo-svelte/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/todo-svelte/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { dispatch, logSnapshot, setTraceTarget } from '@youngdo/rx-state';
import { dispatch, logSnapshot, setLogLevel } from '@youngdo/rx-state';
import { onMount } from 'svelte';
import { AddTodo, ChangeTodoListStatus, DeleteTodoList } from './store/action';
import { Todo, todoList$ } from './store/states';
Expand Down Expand Up @@ -41,7 +41,7 @@ const deleteAllCompleted = () => {
};
onMount(() => {
setTraceTarget('all')
setLogLevel('all')
logSnapshot()
const changeFilterByHash = () => {
Expand Down
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@youngdo/rx-state",
"version": "0.1.0-alpha.20",
"version": "0.2.0",
"description": "State management library by using Rxjs.",
"homepage": "https://github.com/young-do/rx-state#readme",
"bugs": {
Expand All @@ -22,22 +22,19 @@
"build": "tsc --project tsconfig.prod.json",
"clean": "rm -rf ./dist",
"prepare": "yarn clean && yarn build",
"test": "jest"
"test": "vitest",
"test:coverage": "vitest --coverage"
},
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"rxjs": "7.5.4"
"rxjs": "7.5.6"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/lodash.clonedeep": "^4.5.6",
"jest": "^27.4.7",
"nanoid": "^3.1.30",
"c8": "^7.11.3",
"prettier": "^2.5.1",
"ts-jest": "^27.1.2",
"typescript": "4.5.4"
"typescript": "4.5.4",
"vitest": "^0.18.0"
},
"peerDependencies": {
"rxjs": "7.5.4"
"rxjs": "7.5.6"
}
}
46 changes: 25 additions & 21 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Observable, Subject } from 'rxjs';
import { nanoid } from 'nanoid';
import { logForAction } from './logger';
import { createLabelerWithCount } from './utils/generate-label';
import { generateKey } from './utils/gerenate-key';
import { logForAction } from './utils/logger';

export type Action<T = void> = {
(payload: T): [string, string, T];
toString: () => string;
(payload: T): ActionTuple<T>;
getKey: () => string;
debugLabel: string;
};
export type ActionTuple<T> = [key: string, debugLabel: string, payload: T];

const actionManager = {
map: Object.create(null) as Record<string, Subject<unknown>>,
Expand All @@ -21,37 +23,39 @@ const actionManager = {
return (this.map[key] = subject);
},
};
const getDefaultLabel = createLabelerWithCount('unnamed_action');

export const createAction = <T = void>(debugLabel?: string) => {
const key = `#${nanoid()}`;
export function createAction<T = void>(debugLabel?: string): Action<T> {
const key = `#${generateKey()}`;
actionManager.set(key);

const action: Action<T> = (payload: T) => [key, action.debugLabel, payload];
action.toString = () => key;
const action = ((payload: T) => [key, action.debugLabel, payload]) as Action<T>;
action.getKey = () => key;
action.debugLabel = debugLabel || getDefaultLabel();

return action;
};
}

export function dispatch<T = void>(ActionTuple: ActionTuple<T>): void;
export function dispatch<T = void>(action: Action<T>, payload: T): void;
export function dispatch<T extends void>(action: Action<T>): void;
export function dispatch<T = void>(actionOrActionTuple: Action<T> | ActionTuple<T>, payload?: T) {
const isAction = typeof actionOrActionTuple === 'function';

export const dispatch = <T = void>(actionOrTuple: Action<T> | [string, string, T], payload?: T) => {
const isAction = typeof actionOrTuple === 'function';
const key = isAction ? actionOrActionTuple.getKey() : actionOrActionTuple[0];
const debugLabel = isAction ? actionOrActionTuple.debugLabel : actionOrActionTuple[1];
payload = isAction ? payload : actionOrActionTuple[2];

const key = isAction ? actionOrTuple.toString() : actionOrTuple[0];
const subject = actionManager.get(key);
const debugLabel = isAction ? actionOrTuple.debugLabel : actionOrTuple[1];
payload = isAction ? payload : actionOrTuple[2];

logForAction(debugLabel, payload);

subject.next(payload);
};
}

export const on = <T = void>(action: Action<T>) => {
const key = action.toString();
export function on<T = void>(action: Action<T>) {
const key = action.getKey();
const subject = actionManager.get(key);

return subject.asObservable() as Observable<T>;
};

let index = 0;
const getDefaultLabel = () => `#${index++}_unnamed_action`;
}
Loading

0 comments on commit 37eb96b

Please sign in to comment.