Skip to content

Commit

Permalink
Merge pull request #109 from yezyilomo/improve_store.subscribe
Browse files Browse the repository at this point in the history
Improve store.subscribe
  • Loading branch information
yezyilomo authored Feb 4, 2022
2 parents 157c8de + c24be7c commit d5a5854
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ store.setState("count", 0); // Create "count" global state and add it to the st

function ClicksCounter(props){
// Use "count" global state
const [count, setCount, updateCount] = store.useState("count");
const [count, setCount] = store.useState("count");

const incrementCount = (e) => {
setCount(count+1)
Expand Down Expand Up @@ -246,11 +246,10 @@ Other allowed configurations are `selector` & `patcher`. These are used for spec
const selector = (user) => user.name; // Subscribe to user.name only
const patcher = (user, name) => {user.name = name}; // Update user.name

const [name, setName, updateName] = store.useState("user", {selector: selector, patcher: patcher});
const [name, setName] = store.useState("user", {selector: selector, patcher: patcher});

let handleNameChange = (e) => {
setName(e.target.value);
// updateName(name => e.target.value); You can do this if you like to use `updatName`
}

return (
Expand Down Expand Up @@ -324,7 +323,7 @@ function UserInfo(props){
const selector = (user) => user.name;
const patcher = (user, name) => {user.name = name};

const [name, dispatch] = store.useReducer(myReducer, "user", {selector: selector, patcher: patcher});
const [name, dispatch] = store.useReducer(myReducer, "user", {selector, patcher});

// Other stuff
}
Expand Down Expand Up @@ -352,7 +351,7 @@ If you want to listen to changes in a store you can subscribe to it by using `st

```js
// Subscribe to store changes
const unsubscribe = store.subscribe(function({key: String, value: Any}){
const unsubscribe = store.subscribe(function(key: String, value: Any){
// key is the key for a global state that has changed
// value is the new value of a global state
})
Expand All @@ -366,7 +365,7 @@ If you want to subscribe to a single global state you can use
```js
// Subscribe to store changes
const unsubscribe = store.getState(key).subscribe(function(value){
// value is the new value of a global state
// value is the new value of a global state
})

// You can unsubscribe by calling the result
Expand Down Expand Up @@ -554,7 +553,8 @@ store.useState(
store.useReducer(
reducer: Function,
key: String,
config?: {default: Any, persist: Boolean, ...otherConfigs})
config?: {default: Any, persist: Boolean, ...otherConfigs}
)
```

By default the value of `persist` in all cases is false(which means it doesn't save global states to a permanent storage), so if you want to activate it, you have to set it to be true.
Expand Down Expand Up @@ -710,7 +710,8 @@ count.getValue() // This will give 0

count.updateValue(val => val+1) // This will increment the value of count

const unsubscribe = count.subscribe(val => console.log(val)) // This will print whenever count change
// This will print whenever count change
const unsubscribe = count.subscribe(val => console.log(val))

unsubscribe() // This will unsubscribe the observer above
```
Expand Down Expand Up @@ -766,11 +767,10 @@ function UserName(props){
const selector = (user) => user.name; // Subscribe to user.name only
const patcher = (user, name) => {user.name = name}; // Update user.name

const [name, setName, updateName] = useGlobalState(user, {selector: selector, patcher: patcher});
const [name, setName] = useGlobalState(user, {selector: selector, patcher: patcher});

const handleNameChange = (e) => {
setName(e.target.value);
// updateName(name => e.target.value); You can do this if you like to use `updatName`
}

return (
Expand Down Expand Up @@ -846,7 +846,7 @@ function UserInfo(props){
const selector = (user) => user.name;
const patcher = (user, name) => {user.name = name};

const [name, dispatch] = useGlobalStateReducer(myReducer, user, {selector: selector, patcher: patcher});
const [name, dispatch] = useGlobalStateReducer(myReducer, user, {selector, patcher});

// Other stuffs
}
Expand Down
13 changes: 6 additions & 7 deletions src/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { useGlobalStateReducer } from './useGlobalStateReducer';


type Reducer = (state: any, action: any) => any
type Event = { key: string, value: any }
type Observer = (event: Event) => void
type Observer = (key: string, value: any) => void
type Config<T> = {
default?: T,
selector?: (state: any) => any,
Expand Down Expand Up @@ -80,9 +79,9 @@ class Store {
return unsubscribe
}

onStoreUpdate(event: Event): void {
onStoreUpdate(key: string, value: any): void {
this.subscriptions.forEach(subscription => {
subscription(event);
subscription(key, value);
});
}

Expand Down Expand Up @@ -127,7 +126,7 @@ class Store {
const onGlobalStateChange = (newValue: any) => {
// Note key, persist & timerId variables depends on the scope

this.onStoreUpdate({ key: key, value: newValue });
this.onStoreUpdate(key, newValue);

if (shouldPersist) {
this.persistentStorage.saveState(key, newValue, false);
Expand Down Expand Up @@ -188,7 +187,7 @@ class Store {
// Notify subscribers to a store that a global state has been removed
if (this.value.has(key)) {
const newGlobalState = this.getState(key);
this.onStoreUpdate({ key: key, value: newGlobalState.getValue() });
this.onStoreUpdate(key, newGlobalState.getValue());
}
// Rerender all components using this global state
oldState.refresh();
Expand Down Expand Up @@ -225,7 +224,7 @@ class Store {
// Notify subscribers to a store that a global state has been removed
if (this.value.has(key)) {
const newGlobalState = this.getState(key);
this.onStoreUpdate({ key: key, value: newGlobalState.getValue() });
this.onStoreUpdate(key, newGlobalState.getValue());
}

// Rerender all components depending on this global state
Expand Down
4 changes: 2 additions & 2 deletions tests/subscription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ test('should update testVal1 & testVal2 through subscribers', () => {
const { result } = renderHook(() => store.useState("count"))

act(() => {
store.subscribe((event) => {
store.subscribe((key, value) => {
testVal1 = 1
})
store.getState("count").subscribe((val) => {
store.getState("count").subscribe((value) => {
testVal2 = 2
})
result.current[2](count => 1)
Expand Down

0 comments on commit d5a5854

Please sign in to comment.