Skip to content

Commit bd94312

Browse files
author
bates
committed
Function unique name, callback unmount.
- Function unique name - Callback function unmount
1 parent 2907eeb commit bd94312

6 files changed

Lines changed: 60 additions & 22 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ With this component, you can create data that is stored throughout the session.
2222

2323
When the page is closed, the stored data is deleted. You can think of this data as a **global state**.
2424

25-
You can handle data changes from the ```Session.onSet((data) => {})``` event and set the ```state``` of the components.
25+
You can handle data changes from the ```Session.onSet(function UniqueName(data) { })``` event and set the ```state``` of the components.
2626

2727
##### Features
2828
- Set and get data from all components.
@@ -48,9 +48,11 @@ class Counter extends Component {
4848
}
4949
}
5050
componentDidMount() {
51-
Session.onSet((data) => {
51+
const counter = (data) => {
5252
this.setState({ counter: data["counter"] });
53-
});
53+
};
54+
55+
Session.onSet(counter);
5456
}
5557
render() {
5658
return (
@@ -116,7 +118,7 @@ Descriptions and configuration settings for component properties.
116118
- ```key``` ```{string}``` session item key.
117119
- ```value``` ```{Object|string}``` session item value, if you are using browser storage, it can only take ```{string}```.
118120

119-
- **```Session.gey(key)``` Get session item.**
121+
- **```Session.get(key)``` Get session item.**
120122
- ```key``` ```{string}``` session item key.
121123

122124
- **```Session.remove(key)``` Remove session item by key.**
@@ -126,9 +128,12 @@ Descriptions and configuration settings for component properties.
126128

127129
- **```Session.clear()``` Clear all items.**
128130

131+
- **```Session.unmount(callbackName)``` Unmount callback function.**
132+
- ```callbackName``` ```{string}``` callback unique name.
133+
129134
##### Events
130135

131-
- **```Session.onSet((data) => { /* do things */})``` Triggered when session data changes.**
136+
- **```Session.onSet(function UniqueName(data) { /* do things */ })``` Triggered when session data changes.**
132137

133138
- ```data``` new data set.
134139

index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,25 @@ function () {
150150
}, {
151151
key: "onSet",
152152
value: function onSet(callback) {
153-
callback(this.items());
154-
this.Callback.push(callback);
153+
var filter = this.Callback.filter(function (f) {
154+
return f.name === callback.name;
155+
});
156+
157+
if (filter.length === 0) {
158+
callback(this.items());
159+
this.Callback.push(callback);
160+
}
161+
}
162+
/**
163+
* @param {string} callbackName Callback function key.
164+
*/
165+
166+
}, {
167+
key: "unmount",
168+
value: function unmount(callbackName) {
169+
this.Callback = this.Callback.filter(function (f) {
170+
return f.name !== callbackName;
171+
});
155172
}
156173
}]);
157174

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "react-session-api",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Store the data throughout the session and move the data between components. You can easily move state between components.",
55
"main": "index.js",
66
"scripts": {
7+
"start": "npm run storybook",
78
"test": "react-scripts test",
89
"build": "./node_modules/.bin/babel src --out-file index.js",
910
"minify": "./node_modules/.bin/babel --minified src --out-file index.min.js ",

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,17 @@ class SessionStorage {
120120
* @param {func} callback Triggered when session items set.
121121
*/
122122
onSet(callback) {
123-
callback(this.items())
124-
this.Callback.push(callback);
123+
const filter = this.Callback.filter(f => f.name === callback.name);
124+
if (filter.length === 0) {
125+
callback(this.items())
126+
this.Callback.push(callback);
127+
}
128+
}
129+
/**
130+
* @param {string} callbackName Callback function key.
131+
*/
132+
unmount(callbackName) {
133+
this.Callback = this.Callback.filter(f => f.name !== callbackName);
125134
}
126135
}
127136

stories/components/Counter.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ class Counter extends Component {
99
}
1010
}
1111
componentDidMount() {
12-
Session.onSet((data) => {
12+
const counter = (data) => {
1313
this.setState({ counter: data["counter"] });
14-
});
14+
};
15+
16+
Session.onSet(counter);
17+
}
18+
componentWillUnmount() {
19+
Session.unmount("counter");
1520
}
1621
render() {
1722
return (

stories/components/DecreaseButton.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import React from 'react';
33
import Session from '../../src'
44

55
const DecreaseButton = () => {
6-
const onDecrease = () => {
7-
let counter = Session.get("counter") - 1;
8-
Session.set("counter", counter);
9-
}
10-
return (
11-
<button
12-
className="btn btn-sm btn-danger"
13-
onClick={(e) => onDecrease()}>
14-
Decrease Number
6+
const onDecrease = () => {
7+
let counter = Session.get("counter") - 1;
8+
Session.set("counter", counter);
9+
}
10+
11+
return (
12+
<button
13+
className="btn btn-sm btn-danger"
14+
onClick={(e) => onDecrease()}>
15+
Decrease Number
1516
</button>
16-
)
17+
)
1718
}
1819

1920
export default DecreaseButton;

0 commit comments

Comments
 (0)