From 4958a49b6993317787caa5957dd9de4aff794569 Mon Sep 17 00:00:00 2001
From: Yad Smood <1415488+ysmood@users.noreply.github.com>
Date: Fri, 11 Oct 2024 17:32:28 +0800
Subject: [PATCH] refactor to utils module and middlewares
---
.eslintrc.cjs | 2 +-
README.md | 6 +-
cspell.json | 13 +++
examples/CounterPersistent.tsx | 8 +-
examples/MonolithStore/store/index.ts | 2 +-
examples/TodoApp/Actions.tsx | 4 +-
examples/TodoApp/Filter.tsx | 6 +-
examples/TodoApp/README.md | 19 ++++
examples/TodoApp/Title.tsx | 2 +-
examples/TodoApp/TodoItem.tsx | 48 ++++++---
examples/TodoApp/TodoList.tsx | 2 +-
examples/TodoApp/ToggleAll.tsx | 4 +-
examples/TodoApp/{todos => store}/actions.ts | 22 ++--
examples/TodoApp/store/filter/index.ts | 12 +++
examples/TodoApp/store/filter/types.ts | 5 +
examples/TodoApp/store/index.ts | 19 ++++
examples/TodoApp/store/logger.ts | 15 +++
examples/TodoApp/store/todos/index.ts | 80 ++++++++++++++
examples/TodoApp/store/todos/types.ts | 7 ++
examples/{ => TodoApp/store/todos}/utils.ts | 0
examples/TodoApp/todos/filter.ts | 13 ---
examples/TodoApp/todos/index.ts | 86 ---------------
examples/app.tsx | 48 +++++++++
examples/index.tsx | 36 +------
package.json | 5 +-
src/immer.test.tsx | 20 +++-
src/immer.ts | 27 ++---
src/index.test.tsx | 28 +----
src/index.ts | 71 ++++++------
src/persistent.test.tsx | 9 +-
src/persistent.ts | 107 +++++++++----------
src/utils.test.tsx | 51 +++++++++
src/utils.ts | 47 ++++++++
33 files changed, 507 insertions(+), 317 deletions(-)
create mode 100644 cspell.json
create mode 100644 examples/TodoApp/README.md
rename examples/TodoApp/{todos => store}/actions.ts (58%)
create mode 100644 examples/TodoApp/store/filter/index.ts
create mode 100644 examples/TodoApp/store/filter/types.ts
create mode 100644 examples/TodoApp/store/index.ts
create mode 100644 examples/TodoApp/store/logger.ts
create mode 100644 examples/TodoApp/store/todos/index.ts
create mode 100644 examples/TodoApp/store/todos/types.ts
rename examples/{ => TodoApp/store/todos}/utils.ts (100%)
delete mode 100644 examples/TodoApp/todos/filter.ts
delete mode 100644 examples/TodoApp/todos/index.ts
create mode 100644 examples/app.tsx
create mode 100644 src/utils.test.tsx
create mode 100644 src/utils.ts
diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index c5bc1ed..613752e 100644
--- a/.eslintrc.cjs
+++ b/.eslintrc.cjs
@@ -14,6 +14,6 @@ module.exports = {
'warn',
{ allowConstantExport: true },
],
- 'no-console': ['error', { allow: ['warn', 'error'] }],
+ 'no-console': ['error', { allow: ['info', 'warn', 'error'] }],
},
}
diff --git a/README.md b/README.md
index d9d356c..5f8a388 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
# Overview
An elegant state management solution for React.
+The philosophy of this project is to keep the core simple and scalable by exposing low-level accessibility and middleware composition, not by adding options.
+All the non-core functions are just examples of how you can compose functions to achieve common features.
## Features
@@ -8,7 +10,8 @@ An elegant state management solution for React.
- **Type safe**: The state is type safe and the return value is intuitive.
- **Global**: The state is global, you can access it anywhere.
- **Scalable**: Naturally scale large state into multiple modules and files without performance degradation.
-- **Tiny**: Less than [0.3KB](https://bundlephobia.com/package/create-global-state).
+- **Middlewares**: Simple and type-safe middleware composition interface.
+- **Tiny**: About [0.3KB](https://bundlephobia.com/package/create-global-state) Minified + Gzipped.
## Documentation
@@ -46,3 +49,4 @@ Its implementation is not type safe and the return value is not intuitive. It's
The typescript support is not good enough, the API is not intuitive. `create-global-state` is more like `useState` which aligns with the react API style. Check the [comparison](https://github.com/ysmood/create-global-state/issues/1). Zustand [Slices Pattern](https://zustand.docs.pmnd.rs/guides/slices-pattern) can cause naming conflict issues.
`create-global-state` can naturally scale states by modules and files.
+The setter of zustand must be called within a react component, while the setter of `create-global-state` can be called anywhere.
diff --git a/cspell.json b/cspell.json
new file mode 100644
index 0000000..b958163
--- /dev/null
+++ b/cspell.json
@@ -0,0 +1,13 @@
+{
+ "words": [
+ "gettify",
+ "immer",
+ "immerify",
+ "subscribify",
+ "todomvc",
+ "todos",
+ "tsbuildinfo",
+ "wouter",
+ "zustand"
+ ]
+}
diff --git a/examples/CounterPersistent.tsx b/examples/CounterPersistent.tsx
index c591094..946258a 100644
--- a/examples/CounterPersistent.tsx
+++ b/examples/CounterPersistent.tsx
@@ -1,4 +1,4 @@
-import create from "create-global-state/lib/persistent";
+import create, { saveHistory } from "create-global-state/lib/persistent";
import { useEffect } from "react";
import { useHashLocation } from "wouter/use-hash-location";
@@ -22,7 +22,11 @@ export default function CounterPersistent() {
}
function Button() {
- return ;
+ return (
+
+ );
}
function Display() {
diff --git a/examples/MonolithStore/store/index.ts b/examples/MonolithStore/store/index.ts
index 6568fb4..ffce86f 100644
--- a/examples/MonolithStore/store/index.ts
+++ b/examples/MonolithStore/store/index.ts
@@ -1,6 +1,6 @@
// Here we use Immer to update the store immutably, Immer is optional,
// you can use vanilla js or other libs to update the store.
-import create from "create-global-state/lib/immer";
+import { create } from "create-global-state/lib/immer";
// Define the monolith store for the entire web app.
// It contains a list of counters and a title string.
diff --git a/examples/TodoApp/Actions.tsx b/examples/TodoApp/Actions.tsx
index 4f3262e..478bbc7 100644
--- a/examples/TodoApp/Actions.tsx
+++ b/examples/TodoApp/Actions.tsx
@@ -1,6 +1,6 @@
import Filter from "./Filter";
-import { addTodo, clearCompleted } from "./todos/actions";
-import { useZeroDone } from "./todos";
+import { addTodo, clearCompleted } from "./store/actions";
+import { useZeroDone } from "./store/todos";
import ToggleAll from "./ToggleAll";
export default function Actions() {
diff --git a/examples/TodoApp/Filter.tsx b/examples/TodoApp/Filter.tsx
index a80ad3c..516a566 100644
--- a/examples/TodoApp/Filter.tsx
+++ b/examples/TodoApp/Filter.tsx
@@ -1,7 +1,11 @@
-import { filters, setFilter } from "./todos/filter";
+import { useEffect } from "react";
+import { setFilter } from "./store/filter";
+import { filters } from "./store/filter/types";
// The component to filter the todos.
export default function Filter() {
+ useEffect(() => {});
+
return (