Skip to content

Commit aceff5a

Browse files
committed
Update: remove inject store & readme cleanup
1 parent fea8281 commit aceff5a

File tree

2 files changed

+69
-70
lines changed

2 files changed

+69
-70
lines changed

README.md

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
# onejs
2-
An unified Javascript framework for building a high performance web application.
2+
A Javascript [framework](https://en.wikipedia.org/wiki/Software_framework) for building a high-performance web application.
33

44
## Motivation
5-
Onejs provides an abstract layer over a javascript library. It enables developer/organization to write a vanilla javascript code and bind it with framework you choose to build your application with.
5+
Onejs provides an abstract layer over a javascript library. It enables developer/organization to write a vanilla javascript code and bind it with the framework you choose to build your application with.
66

77
##### It's hard to switch between frameworks if your performance budget does not meet with one
8-
Considering all top javascript library adds considerable amount of initial javascript chunk into your application bundle i.e. react size is ~30kb. For any larger application, switch from one framework to another is relatively complex task and requires a lot of effort and time. onejs try to solve this problem and reduce the time and effort require in switching frameworks.
8+
Considering all top javascript library adds a considerable amount of initial javascript chunk into your application bundle i.e. react size is ~30kb. For any larger application, switching from one framework to another is a relatively complex task and requires a lot of effort and time. onejs try to solve this problem and reduce the time and effort require in switching frameworks.
99

10-
##### Classical component go polluted over the time and becomes hard to maintain
11-
This has been a common problem that after certain time your class component gets complex and a lot of business login being written into a single javascript file. With function programming approach we are trying to solve some of these problems. Developer needs to create small functions and attach those to your component. This allows you to maximize the usage of vanilla javascript.
10+
##### Classical component go polluted over time and becomes hard to maintain
11+
This has been a common problem that after certain amount of time your class component gets complex and a lot of business logic being written into a single javascript file. With the functional programming approach, we are trying to solve some of these problems. A developer needs to create small functions and attach those to your component. This allows you to maximize the usage of vanilla javascript.
1212

1313
## Installation
1414
```
1515
npm i -S @js-factory/onejs
1616
```
1717

1818
## APIs
19-
Onejs offers following apis.
19+
Onejs offers the following APIs.
2020

2121
- Component
2222
- withStore
2323
- createStore
2424
- actionCreator
25-
- injectStore
2625

27-
### Component
28-
**Component** is a higher level component. It creates react or preact component and binds all given properties and methods to it.
26+
### Component
27+
**Component** is a higher-level component. It creates a react or preact component and binds all given properties and methods to it.
2928

30-
Please read [hoc](https://www.npmjs.com/package/@js-factory/hoc/v/0.2.2) documentation for further details.
29+
Please read [hoc](https://www.npmjs.com/package/@js-factory/hoc) documentation for further details.
3130

3231
A typical component declaration looks like this.
3332

@@ -41,47 +40,44 @@ import someOtherHandler from './util/someOtherHandler';
4140
import FooTmpl from './FooTmpl';
4241

4342
@Component({
44-
componentDidMount,
45-
someOtherHandler,
46-
onClickHandler,
47-
state: {
48-
x: 0
49-
},
50-
instanceProps: {
51-
y: 0
52-
},
53-
template: FooTmpl
43+
componentDidMount,
44+
someOtherHandler,
45+
onClickHandler,
46+
state: {
47+
x: 0
48+
},
49+
instanceProps: {
50+
y: 0
51+
},
52+
template: FooTmpl
5453
})
5554
export default class FooComponent { }
5655

5756
```
5857

59-
### withStore
60-
Every frontend application needs a data store. `withStore` provide a simple data store and wire a newly created `Component` with application store. Your typical store will be a plain javascript object.
58+
### withStore
59+
Every frontend application (read SPA) needs a persistent data store. The application should be able to maintain its state during back & forth page transitions. `withStore` connects the component with application's data store.
6160

62-
```js
61+
Configuring `withStore` is very simple. It has two options. 1) **watcher** , 2) action
62+
63+
#### watcher
64+
Data store of an application is a big JavaScript objects. It holds the application state. **watcher** represents the keys in applications store (read a big javascript object). When you define watcher in `withStore`, onejs connects your component with the application store and any changes to these properties will update (re-render) the component.
6365

66+
// App data store
67+
68+
```js
6469
const appDataStore = {
65-
key1: {
66-
// some data
67-
},
68-
key2: {
69-
// some data
70-
},
71-
key3: {
72-
// some data
73-
}
70+
todos: [
71+
// todo
72+
],
73+
counter: 0 // initial value
7474
};
75-
7675
```
7776

78-
`withStore` configuration is very simple and has two options.
79-
80-
#### watcher
81-
**watcher** is nothing but keys in application store. When you define watcher with `withStore`, onejs connects your component with application store and any changes to these properties will trigger a re-render.
82-
8377
#### action
84-
Action allows you to update store.
78+
The only way to connect to the store is an `action`. Actions allow you to modify the application state.
79+
80+
**Complete Example**
8581

8682
```javascript
8783

@@ -94,59 +90,58 @@ import onClickHandler from './handlers/onClickHandler';
9490
import TodoTmpl from './ToDoTmpl';
9591

9692
@withStore({
97-
watcher: ['home', 'counter', 'todos'],
98-
actions: {
99-
increment,
100-
fetchToDoList
101-
}
93+
watcher: ['counter', 'todos'],
94+
actions: {
95+
increment,
96+
fetchToDoList
97+
}
10298
})
10399
@Component({
104-
componentDidMount,
105-
onClickHandler,
106-
state: {
107-
x: 0,
108-
},
109-
instanceProps: {
110-
y: 0
111-
},
112-
template: TodoTmpl
100+
componentDidMount,
101+
onClickHandler,
102+
state: {
103+
x: 0,
104+
},
105+
instanceProps: {
106+
y: 0
107+
},
108+
template: TodoTmpl
113109
})
114110
export default class ToDoContainer { }
115111

116112
// increment.js
117113
import { actionCreator } from '@js-factory/onejs';
118114

119115
export default actionCreator('INCREMENT', {
120-
key: 'counter',
121-
format({ count }) { // `reducer` middleware setting
122-
return {
123-
count: count + 1
124-
};
125-
}
116+
key: 'counter',
117+
format({ count }) { // `reducer` middleware setting
118+
return {
119+
count: count + 1
120+
};
121+
}
126122
});
127123

128124
// fetchTodoList.js
129125

130126
import { actionCreator } from '@js-factory/onejs';
131127

132128
export default actionCreator('FETCH_TODO_LIST', {
133-
key: 'todos', // store property
134-
url: 'https://jsonplaceholder.typicode.com/todos'
129+
key: 'todos', // store property
130+
url: 'https://jsonplaceholder.typicode.com/todos'
135131
});
136132

137133
```
138134

139135
### createStore
140-
**createStore** initializes application store. It takes 2 arguments to build the store. 1) initial state of an application, and 2) middlewares to execute before updating the store properties.
136+
**createStore** initializes application store. It takes 2 arguments to build the store. 1) initial state of an application, and 2) middleware(s) to execute before updating the store properties.
141137

142138
```javascript
143139
// bootstrap.js
144140

145-
import { createStore, injectStore } from '@js-factory/onejs';
141+
import { createStore } from '@js-factory/onejs';
146142
import middleware from './middleware'
147143

148144
const store = createStore({} , /* optional */ middleware);
149-
injectStore(store);
150145

151146
export default store;
152147

@@ -159,10 +154,7 @@ export default store;
159154
import { actionCreator } from '@js-factory/onejs';
160155

161156
export default actionCreator('FETCH_TODO_LIST', {
162-
key: 'todos', // store property
163-
url: 'https://jsonplaceholder.typicode.com/todos'
157+
key: 'todos', // store property
158+
url: 'https://jsonplaceholder.typicode.com/todos'
164159
});
165160
```
166-
167-
### injectStore
168-
**injectStore** binds store with `Component`. It's an important step before your application is setup.

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import Component, { injectStore } from '@js-factory/hoc/packages/preact/component_v2';
2-
import { withStore, createStore, actionCreator } from '@js-factory/store/src';
2+
import { withStore, createStore as cs, actionCreator } from '@js-factory/store/src';
33

44
const withState = Component;
5+
6+
const createStore = (initialState, middleware) => {
7+
const store = cs(initialState, middleware);
8+
injectStore(store);
9+
return store;
10+
}
11+
512
export {
613
withState,
714
withStore,

0 commit comments

Comments
 (0)