Skip to content

Commit 1fc0344

Browse files
committed
Revert "rename to react-one-state"
This reverts commit be0fd88.
1 parent 60ad972 commit 1fc0344

15 files changed

+187
-133
lines changed

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cSpell.words": [
3+
"globx",
4+
"mystore",
5+
"todos"
6+
]
7+
}

.vscode/terminals.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"autorun": false,
3+
"terminals": [
4+
{
5+
"name": "compile packagfe",
6+
"description": "This is a description",
7+
"command": "tsc --watch index.ts"
8+
},
9+
{
10+
"name": "local website",
11+
"description": "This is a description",
12+
"command": "cd website; npm start -- --port 4000"
13+
},
14+
{
15+
"name": "deploy website",
16+
"description": "This is a description",
17+
"command": "cd website; echo npm run build"
18+
}
19+
]
20+
}

README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# react-one-state
1+
# GlobX
22

33
A global state manager that let you read and change the the whole state from all your components.
44

5-
A new solution for creating simple and powerful stores for React apps. The react-one-state let you change and read any part of the store from every component or traditional JavaScript code.
5+
I built a new solution for creating simple and powerful stores for React apps. GlobX let you change and read any part of the store from every component or traditional JavaScript code.
66

7-
Let's take the Redux todo example. (https://github.com/reduxjs/redux/tree/master/examples/todos) Wen implemented it in react-one-state (https://github.com/Aminadav/react-one-state-todo) it's saving about 90% of the code.
7+
I took the Redux todo example. (https://github.com/reduxjs/redux/tree/master/examples/todos) cloned it and reimplemented it in GlobX (https://github.com/Aminadav/globx-todo). I saved about 90% of the code. (I'm recommended you to inspect the code, I'm sure you will ❤️ it)
88

9-
## Why react-one-state?
9+
## Why Glob'x?
1010

1111
- Very easy to refactor.
1212
- Great for agility. Let you easily refactor.
@@ -27,13 +27,13 @@ Let's take the Redux todo example. (https://github.com/reduxjs/redux/tree/master
2727
- I Like JSON. And the component should use the store's property like any other JSON object. And without using or creating selectors.
2828
- I wouldn't say I like immutable objects. I prefer to push, pull, and change arrays and objects without cloning.
2929
- I don't like that sometimes I'm getting previous store values because of cloning, and I'm not particularly eager to fix it using useRef and similar fixes. I prefer to have one state ojbect.
30-
- In Redux all IDE smart features like "Find all references" or "rename symbol" doesn't work for keys in the store (unless your spend more time to create type definitaions). In react-one-state it's working out of the box, so my code always have the naming I wish and I don't afraid to refactor.
30+
- In Redux all IDE smart features like "Find all references" or "rename symbol" doesn't work for keys in the store (unless your spend more time to create type definitaions). In GlobX it's working out of the box, so my code always have the naming I wish and I don't afraid to refactor.
3131

32-
## How to use react-one-state?
32+
## How to use GlobX?
3333

34-
Create a new file (`store.js`) and call the react-one-state `newStore` function with your initial store. And export the result.
34+
Create a new file (`store.js`) and call the GlobX `newStore` function with your initial store. And export the result.
3535

36-
```js
36+
```
3737
const store = NewStore({
3838
todos:[],
3939
filter:'ALL',
@@ -45,9 +45,9 @@ export default store
4545

4646
*Suppport all smart IDE's features such as autocomplete for keys in your store, rename symbols and find all references*
4747

48-
react-one-state will create one store for your whole app. You can read any part of the store easily by importing the store.js file. This is a simple JSON file.
48+
GlobX will create one store for your whole app. You can read any part of the store easily by importing the store.js file. This is a simple JSON file.
4949

50-
```js
50+
```
5151
import store from './store'
5252
5353
console.log(store.filter)
@@ -66,9 +66,9 @@ import store from './store'
6666
</div>
6767
```
6868

69-
To know which components should re-render you have to use the react-one-state hook useRerenderIfChange and tell it which part of the store this component is dependent on.
69+
To know which components should re-render you have to use the GlobX hook useRerenderIfChange and tell it which part of the store this component is dependent on.
7070

71-
```js
71+
```
7272
import store from './store'
7373
function MyComponent(){
7474
store.useRerenderIfChange(()=>[store.counter]) // Each time counter change re-render this component
@@ -78,30 +78,30 @@ function MyComponent(){
7878
}
7979
```
8080

81-
That it! You know everything about react-one-state.
81+
That it! You know everything about Globx.
8282

8383
- `createStore` - called once.
8484
- `updateStore` - called every time you want to rerender your UI.
8585
- `useRerenderIfChange` - called on every component that depended on specific part of the store.
8686

8787
## Demo App:
88-
Please see the full working demo app: https://github.com/Aminadav/react-one-state/tree/master/examples/todo
88+
Please see the full working demo app: https://github.com/Aminadav/globx/tree/master/examples/todo
8989

90-
See the website: https://react-one-state.js.org
90+
See the website: https://globx.js.org
9191

9292
Link to the first Reddit post about it: https://www.reddit.com/r/reactjs/comments/m27hqm/globux_a_global_state_manager_that_let_you_read/
9393

9494
## Installation
9595

96-
npm i react-one-state
96+
npm i globx
9797

9898
Demo:
9999

100100
```js
101101
// First you should creating the store.
102102
// Usually you will create one store for each app and share this variable.
103103
// Usually you will put this in separate file.
104-
import {newStore} from "react-one-state"
104+
import {newStore} from "globx"
105105

106106
const store = newStore({
107107
key1:0,

examples/todo/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-one-state todos example
1+
# Globx todos example
22

33
This is just the example app. Try to compare to Redux and see how much less code you have to write. Meaning less bugs, and more features in less time.
44

@@ -7,9 +7,9 @@ The Reddit post about it:
77
https://www.reddit.com/r/reactjs/comments/m27hqm/globux_a_global_state_manager_that_let_you_read/
88

99

10-
Please star and watch the react-one-state repo to be notified when I publish documentation.
10+
Please star and watch the Globx repo to be notified when I publish documentation.
1111

12-
https://github.com/Aminadav/react-one-state
12+
https://github.com/Aminadav/globx
1313

1414

1515
Both repos are open for contributions. (Very welcome)

examples/todo/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/todo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"react-scripts": "^3.4.1"
77
},
88
"dependencies": {
9-
"react-one-state": "^1.0.7",
9+
"globx": "^1.0.7",
1010
"prop-types": "^15.7.2",
1111
"react": "^16.13.1",
1212
"react-dom": "^16.13.1"

index.js

+34-64
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,57 @@
1-
"use strict"
2-
exports.__esModule = true
3-
exports.newStore = void 0
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.newStore = void 0;
44
//@ts-check
5-
var React = require("react")
6-
function cloneDeep(obj) {
7-
return JSON.parse(JSON.stringify(obj))
8-
}
9-
function isEqual(a,b) {
10-
if(typeof a!=typeof b) {
11-
return false
12-
}
13-
if(typeof a=='object' && typeof b=='object') {
14-
if (Object.keys(a).length!=Object.keys(b).length) return false
15-
}
16-
return JSON.stringify(a)==JSON.stringify(b)
17-
}
5+
var react_1 = require("react");
6+
var isEqual = require("lodash.isequal");
7+
var cloneDeep = require("lodash.clonedeep");
188
function newStore(state) {
199
//@ts-ignore
20-
var Store = state
21-
var callbacks = {}
22-
var callbacksNextAvailableKey = 0
23-
24-
var onBeforeRenders=[]
25-
Store.onBeforeRender=(callback)=>onBeforeRenders.push(callback)
26-
var beforeLastRender
27-
function updateStoreNow() {
28-
29-
scheduled = false
30-
if(onBeforeRenders.length>0) {
31-
if(beforeLastRender) { // skip first time
32-
onBeforeRenders.forEach(x=>x(beforeLastRender))
33-
}
34-
beforeLastRender = cloneDeep(Store)
35-
}
10+
var Store = state;
11+
var callbacks = {};
12+
var callbacksNextAvailableKey = 0;
13+
function updateStoreNow() {
14+
scheduled = false;
3615
for (var i in callbacks) {
37-
callbacks[i]()
16+
callbacks[i]();
3817
}
3918
}
40-
41-
42-
var scheduled = false
19+
var scheduled = false;
4320
function Schedule() {
4421
if (scheduled)
45-
return
46-
scheduled = true
47-
setTimeout(updateStoreNow, 0)
22+
return;
23+
scheduled = true;
24+
setTimeout(updateStoreNow, 0);
4825
}
4926
/**
5027
* @example useRerenderIfChange((store) => [store.scale, store.othervalue])
5128
* @param callback {(store:Store)=>void}
5229
*/
53-
Store.useRerenderIfChange = function (callback,name) {
54-
var _a = React.useState(0), _ = _a[0], setMeToRerender = _a[1]
55-
var callbacksKeyRef = React.useRef(callbacksNextAvailableKey++)
30+
Store.useRerenderIfChange = function (callback) {
31+
var _a = react_1.useState(0), _ = _a[0], setMeToRerender = _a[1];
32+
var callbacksKeyRef = react_1.useRef(callbacksNextAvailableKey++);
5633
/** The array from the user. Cloned so new changes will not be there */
57-
var cloned = React.useCallback(()=>cloneDeep(callback(Store)))
58-
var lastValueRef = React.useRef(cloned)
59-
React.useEffect(function () {
34+
var lastValueRef = react_1.useRef(cloneDeep(callback(Store)));
35+
react_1.useEffect(function () {
6036
// When object mount, add a function to the callbacks, to change if the value changes
6137
// it will trigged by the "updateStore below"
6238
callbacks[callbacksKeyRef.current] = function () {
63-
var currentValue = callback(Store) // The new array of values calculated from the store
39+
var currentValue = callback(Store); // The new array of values calculated from the store
6440
if (!isEqual(currentValue, lastValueRef.current)) {
65-
lastValueRef.current = cloneDeep(currentValue) // Update the old array.
66-
if(React.startTransition) { // support React 18 {
67-
React.startTransition(()=>{
68-
setMeToRerender(function (prev) { return prev + 1 }) // Rerender the objects
69-
})
70-
} else {
71-
setMeToRerender(function (prev) { return prev + 1 }) // Rerender the objects
72-
}
41+
lastValueRef.current = cloneDeep(currentValue); // Update the old array.
42+
setMeToRerender(function (prev) { return prev + 1; }); // Rerender the objects
7343
}
74-
}
44+
};
7545
return function () {
7646
// When object unmount we don't need this callback any more.
77-
delete callbacks[callbacksKeyRef.current]
78-
}
79-
}, [])
80-
return lastValueRef.current
81-
}
47+
delete callbacks[callbacksKeyRef.current];
48+
};
49+
}, []);
50+
return lastValueRef.current;
51+
};
8252
Store.updateStore = function () {
83-
Schedule()
84-
}
85-
return Store
53+
Schedule();
54+
};
55+
return Store;
8656
}
87-
exports.newStore = newStore
57+
exports.newStore = newStore;

index.ts

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
1-
//@ts-nocheck
1+
//@ts-check
2+
import { useState, useEffect, useRef } from "react"
3+
import isEqual = require("lodash.isequal")
4+
import cloneDeep = require("lodash.clonedeep")
5+
26
interface a{
37
updateStore:()=>void
48
useRerenderIfChange:(callback)=>void
5-
onBeforeRender:(callback:(prevStore)=>void)=>void
69
}
7-
810
export function newStore<T>(state:T):(a & T) {
9-
}
11+
//@ts-ignore
12+
var Store:(a & T)=state
13+
var callbacks = {}
14+
var callbacksNextAvailableKey = 0
15+
16+
function updateStoreNow() {
17+
scheduled=false
18+
for (var i in callbacks) {
19+
callbacks[i]()
20+
}
21+
}
22+
var scheduled=false
23+
function Schedule(){
24+
if(scheduled) return
25+
scheduled=true
26+
setTimeout(updateStoreNow,0)
27+
}
28+
/**
29+
* @example useRerenderIfChange((store) => [store.scale, store.othervalue])
30+
* @param callback {(store:Store)=>void}
31+
*/
32+
Store.useRerenderIfChange=function(callback) {
33+
34+
var [_, setMeToRerender] = useState(0)
35+
var callbacksKeyRef = useRef(callbacksNextAvailableKey++)
36+
37+
/** The array from the user. Cloned so new changes will not be there */
38+
var lastValueRef = useRef(cloneDeep(callback(Store)))
39+
useEffect(() => {
40+
// When object mount, add a function to the callbacks, to change if the value changes
41+
// it will trigged by the "updateStore below"
42+
callbacks[callbacksKeyRef.current] = () => {
43+
var currentValue = callback(Store) // The new array of values calculated from the store
44+
if (!isEqual(currentValue, lastValueRef.current)) {
45+
lastValueRef.current = cloneDeep(currentValue) // Update the old array.
46+
setMeToRerender((prev) => prev + 1) // Rerender the objects
47+
}
48+
}
49+
return () => {
50+
// When object unmount we don't need this callback any more.
51+
delete callbacks[callbacksKeyRef.current]
52+
}
53+
}, [])
54+
return lastValueRef.current
55+
}
56+
57+
Store.updateStore=function() {
58+
Schedule()
59+
}
60+
return Store
61+
}
62+
63+

package.json

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
{
2-
"name": "react-one-state",
3-
"version": "1.0.13",
4-
"descript/urlion": "",
2+
"name": "globx",
3+
"version": "1.0.10",
4+
"description": "",
55
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
69
"repository": {
710
"type": "git",
8-
"url": "git+https://github.com/Aminadav/react-one-state.git"
11+
"url": "git+https://github.com/Aminadav/globx.git"
912
},
1013
"keywords": [
1114
"react"
1215
],
1316
"author": "Aminadav Glickshtein",
1417
"license": "ISC",
1518
"bugs": {
16-
"url": "https://github.com/Aminadav/react-one-state/issues"
19+
"url": "https://github.com/Aminadav/globux/issues"
1720
},
18-
"homepage": "https://github.com/Aminadav/react-one-state#readme",
21+
"homepage": "https://github.com/Aminadav/globx#readme",
1922
"dependencies": {
2023
"lodash.clonedeep": "",
2124
"lodash.isequal": ""

0 commit comments

Comments
 (0)