Skip to content

Commit

Permalink
Merge pull request #14 from ice-lab/fix-indent
Browse files Browse the repository at this point in the history
chore: fix indent
  • Loading branch information
alvinhui authored Jul 1, 2019
2 parents e30da41 + 40fc820 commit 3c05293
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 176 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"react",
"react",
"import"
],
"parserOptions": {
Expand All @@ -19,5 +19,8 @@
"react": {
"version": "detect"
}
},
rules: {
"@typescript-eslint/indent": ["error", 2]
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/store",
"version": "0.1.3",
"version": "0.1.4",
"description": "基于 React Hooks 的数据流方案",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
40 changes: 20 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import Store from './store';

export default class Icestore {
private stores: {[namespace: string]: Store} = {};
private stores: {[namespace: string]: Store} = {};

public registerStore(namespace: string, bindings: object): Store {
if (this.stores[namespace]) {
throw new Error(`Namespace have been used: ${namespace}.`);
}

this.stores[namespace] = new Store(bindings);
return this.stores[namespace];
public registerStore(namespace: string, bindings: object): Store {
if (this.stores[namespace]) {
throw new Error(`Namespace have been used: ${namespace}.`);
}

private getModel(namespace: string): Store {
const store: Store = this.stores[namespace];
if (!store) {
throw new Error(`Not found namespace: ${namespace}.`);
}
return store;
}
this.stores[namespace] = new Store(bindings);
return this.stores[namespace];
}

public useStore(namespace: string): object {
return this.getModel(namespace).useStore();
private getModel(namespace: string): Store {
const store: Store = this.stores[namespace];
if (!store) {
throw new Error(`Not found namespace: ${namespace}.`);
}
return store;
}

public useStores(namespaces: string[]): object[] {
return namespaces.map(namespace => this.useStore(namespace));
}
public useStore(namespace: string): object {
return this.getModel(namespace).useStore();
}

public useStores(namespaces: string[]): object[] {
return namespaces.map(namespace => this.useStore(namespace));
}
}
98 changes: 49 additions & 49 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@ import * as isFunction from 'lodash.isfunction';
import { useState, useEffect } from 'react';

interface MethodFunc {
(): void;
(): void;
}

export default class Store {
private state: {[name: string]: any} = {};

private methods: {[name: string]: MethodFunc} = {};

private queue = [];

public constructor(bindings: object) {
Object.keys(bindings).forEach((key) => {
const value = bindings[key];

if (isFunction(value)) {
this.methods[key] = this.createMethod(value);
} else {
this.state[key] = value;
}
});
}

private getBindings() {
return { ...this.state, ...this.methods };
}

private createMethod(fun): MethodFunc {
return async (...args) => {
const newState = { ...this.state };
await fun.apply(newState, args);
this.setState(newState);
return this.getBindings();
};
}

private setState(newState: object): void {
this.state = newState;
this.queue.forEach(setState => setState(newState));
}

public useStore(): object {
const [, setState] = useState(this.state);
useEffect(() => {
this.queue.push(setState);
return () => {
const index = this.queue.indexOf(setState);
this.queue.splice(index, 1);
};
}, []);

return this.getBindings();
}
private state: {[name: string]: any} = {};

private methods: {[name: string]: MethodFunc} = {};

private queue = [];

public constructor(bindings: object) {
Object.keys(bindings).forEach((key) => {
const value = bindings[key];

if (isFunction(value)) {
this.methods[key] = this.createMethod(value);
} else {
this.state[key] = value;
}
});
}

private getBindings() {
return { ...this.state, ...this.methods };
}

private createMethod(fun): MethodFunc {
return async (...args) => {
const newState = { ...this.state };
await fun.apply(newState, args);
this.setState(newState);
return this.getBindings();
};
}

private setState(newState: object): void {
this.state = newState;
this.queue.forEach(setState => setState(newState));
}

public useStore(): object {
const [, setState] = useState(this.state);
useEffect(() => {
this.queue.push(setState);
return () => {
const index = this.queue.indexOf(setState);
this.queue.splice(index, 1);
};
}, []);

return this.getBindings();
}
}
Loading

0 comments on commit 3c05293

Please sign in to comment.