Skip to content

Commit 0cef07c

Browse files
committed
Refine to 0.1.0
1 parent 21c9180 commit 0cef07c

File tree

6 files changed

+109
-23
lines changed

6 files changed

+109
-23
lines changed

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
src
1+
ignore_gitignore

README.md

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,100 @@
1-
# Flumt
1+
# Flumpt
22

33
Conceptual Implementation of EventEmitter based Flux.
44

5-
// TODO Documentation
5+
## Concepts
66

7+
Flux is (...What you think) but all you need is just an `EventEmitter`.
8+
9+
Interface is inpired by `Om`.
710

811
## Example
912

1013
### Hello World
1114

1215
```js
1316
import * as React from "react";
14-
import {Flux} from "flumt";
17+
import {Flux, Component} from "flumpt";
1518
import {render} from "react-dom";
1619

17-
class App extends Flux<{}> {
18-
render(prors) {
19-
return <divHelloWorld</div>;
20+
class MyComponent extends Component {
21+
componentDidMount() {
22+
this.dispatch("increment");
23+
}
24+
render() {
25+
return (
26+
<div>
27+
{this.props.counter}
28+
<button onClick={() => this.dispatch("increment")}>increment</button>
29+
</div>
30+
);
31+
}
32+
}
33+
34+
class App extends Flux {
35+
subscribe() { // `subscribe` is called once in constructor
36+
this.on("increment", () => {
37+
this.update(({count}) => {
38+
return {count: count + 1}; // return next state
39+
});
40+
});
41+
}
42+
render(state) {
43+
return <MyComponent {}...state}/>;
2044
}
2145
}
2246

2347
// Setup renderer
24-
const app = new App(el => {
25-
render(el, document.querySelector("#root"));
48+
const app = new App({
49+
renderer: el => {
50+
render(el, document.querySelector("#root"));
51+
},
52+
initialState: {count: 0}
2653
});
27-
app.update(_initialState => ({}))
54+
55+
app.on(":start-updating", () => {
56+
// overlay ui lock
57+
});
58+
app.on(":end-updating", () => {
59+
// hide ui lock
60+
});
61+
62+
app.update(_initialState => ({count: 1})) // it fires rendering
63+
```
64+
65+
- `Flux` is `EventEmitter`
66+
- `Component` is just `ReactComponent` with `dispatch` method.
67+
- You can also use `flumpt.mixin` with React.createClass
68+
69+
## With TypeScript
70+
71+
Need `node react reace-dom es6-promise` type definitions.
72+
73+
- `npm install -g dtsm`
74+
- `dtsm install node react reace-dom`
75+
76+
77+
```js
78+
import {Flux} from "flumpt";
79+
80+
interface State {
81+
count: number;
82+
}
83+
84+
class App extends Flux<State> {
85+
subscribe() {
86+
this.on("increment", () => {
87+
this.update((s: State) => {
88+
return {count: count + 1};
89+
});
90+
});
91+
}
92+
// ... render or others
93+
}
2894
```
95+
96+
See detail in `index.d.ts`
97+
98+
## LICENSE
99+
100+
MIT

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Component<Props, State> extends React.Component<Props, State> {
99
}
1010

1111
export class Flux<State> {
12-
constructor(render: Function);
12+
constructor(initalizers: {renderer: Function; initialState: State});
1313
state: State;
1414
on: (eventName: string, fn: Function) => void;
1515
update(updater: (s?: State) => State | Promise<State>): Promise<any>;

package.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "flumt",
3-
"version": "0.0.10",
2+
"name": "flumpt",
3+
"version": "0.1.0",
44
"description": "Conceptual Implementation of EventEmitter based Flux.",
5-
"main": "lib/flumt.js",
5+
"main": "lib/flumpt.js",
66
"scripts": {
77
"prepublish": "babel src --out-dir lib",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
10-
"author": "",
11-
"license": "ISC",
10+
"author": "mizchi",
11+
"license": "MIT",
1212
"peerDependencies": {
1313
"react": ">=0.14"
1414
},
@@ -24,10 +24,17 @@
2424
},
2525
"repository": {
2626
"type": "git",
27-
"url": "git+https://github.com/mizchi/flumt.git"
27+
"url": "git+https://github.com/mizchi/flumpt.git"
2828
},
2929
"bugs": {
30-
"url": "https://github.com/mizchi/flumt/issues"
30+
"url": "https://github.com/mizchi/flupmt/issues"
3131
},
32-
"homepage": "https://github.com/mizchi/flumt#readme"
32+
"homepage": "https://github.com/mizchi/flumpt#readme",
33+
"dependencies": {
34+
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.13",
35+
"babel-preset-es2015": "^6.3.13",
36+
"babel-preset-stage-2": "^6.3.13",
37+
"babel-preset-react": "^6.3.13",
38+
"react": "^0.14.3"
39+
}
3340
}

src/flumt.js renamed to src/flumpt.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ export function createRenderer({emitter, render}) {
4141
}
4242

4343
export class Flux extends EventEmitter {
44-
constructor(renderer) {
44+
constructor({renderer, initialState}) {
4545
super();
46-
this.state = {};
46+
this.state = initialState ? {};
4747
this._renderer = createRenderer({emitter: this, render: renderer});
48+
this._renderedElement = null;
4849
this.subscribe();
4950
this.updating = false;
5051
// this._updatingQueues = []; // TODO
@@ -62,17 +63,23 @@ export class Flux extends EventEmitter {
6263
this.emit(":start-updating");
6364
return promiseOrState.then(nextState => {
6465
this.state = nextState;
65-
this._renderer(this.render(this.state));
66+
this._renderedElement = this._renderer(this.render(this.state));
6667
this.updating = false;
6768
this.emit(":end-updating");
6869
});
6970
} else {
7071
this.state = promiseOrState;
71-
this._renderer(this.render(this.state));
72+
this._renderedElement = this._renderer(this.render(this.state));
7273
this.updating = false;
7374
}
7475
}
7576

77+
_setState(...args) {
78+
if (this._renderedElement) {
79+
this._renderedElement.setState(...args)
80+
}
81+
}
82+
7683
render(props) {
7784
// override me
7885
throw "Override Me"
File renamed without changes.

0 commit comments

Comments
 (0)