@kazu69/miniflux
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,2 +0,2 @@ | ||
import { IDispatcher, IBindDispatcherToStore, IStore } from './types/index'; | ||
import { IDispatcher, IBindDispatcherToStore, IStore } from '../types/index.d'; | ||
export declare function ActionCreator(dispatcher: IDispatcher): { | ||
@@ -3,0 +3,0 @@ createAction: (type: string) => (payload: any) => void; |
@@ -1,33 +0,23 @@ | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function ActionCreator(dispatcher) { | ||
var createAction = function (type) { | ||
if (!type) { | ||
throw new Error("Should provide action type property."); | ||
} | ||
else { | ||
return function (payload) { | ||
return dispatcher.dispatch({ type: type, payload: payload }); | ||
}; | ||
} | ||
}; | ||
var createSubscriber = function (store) { | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
return dispatcher.register(store); | ||
}; | ||
return { createAction: createAction, createSubscriber: createSubscriber }; | ||
} | ||
exports.ActionCreator = ActionCreator; | ||
}); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function ActionCreator(dispatcher) { | ||
var createAction = function (type) { | ||
if (!type) { | ||
throw new Error("Should provide action type property."); | ||
} | ||
else { | ||
return function (payload) { | ||
return dispatcher.dispatch({ type: type, payload: payload }); | ||
}; | ||
} | ||
}; | ||
var createSubscriber = function (store) { | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
return dispatcher.register(store); | ||
}; | ||
return { createAction: createAction, createSubscriber: createSubscriber }; | ||
} | ||
exports.ActionCreator = ActionCreator; | ||
//# sourceMappingURL=actioncreator.js.map |
@@ -1,2 +0,2 @@ | ||
import { IReducer, IStore } from './types/index'; | ||
import { IReducer, IStore } from '../types/index.d'; | ||
export declare function CreateStore<TState>(reducer: IReducer, initialState: TState): IStore; |
@@ -1,18 +0,8 @@ | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./store"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var store_1 = require("./store"); | ||
function CreateStore(reducer, initialState) { | ||
return new store_1.default(reducer, initialState); | ||
} | ||
exports.CreateStore = CreateStore; | ||
}); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var store_1 = require("./store"); | ||
function CreateStore(reducer, initialState) { | ||
return new store_1.default(reducer, initialState); | ||
} | ||
exports.CreateStore = CreateStore; | ||
//# sourceMappingURL=createstore.js.map |
@@ -1,2 +0,2 @@ | ||
import { IAction, IStore, IDispatcher, IBindDispatcherToStore } from './types/index'; | ||
import { IAction, IStore, IDispatcher, IBindDispatcherToStore } from '../types/index.d'; | ||
export default class Dispatcher implements IDispatcher { | ||
@@ -3,0 +3,0 @@ private _stores; |
@@ -1,94 +0,84 @@ | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Dispatcher = /** @class */ (function () { | ||
function Dispatcher() { | ||
this._stores = []; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Dispatcher = /** @class */ (function () { | ||
function Dispatcher() { | ||
this._stores = []; | ||
Dispatcher.prototype.register = function (store) { | ||
var _this = this; | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
Dispatcher.prototype.register = function (store) { | ||
var _this = this; | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
var handlers = []; | ||
var middlewears = []; | ||
var handler = function (data) { | ||
if (handlers.length <= 0) { | ||
return; | ||
} | ||
handlers.map(function (handler) { | ||
handler.call(_this, data); | ||
}); | ||
}; | ||
var middlewear = function (data, action) { | ||
if (middlewears.length <= 0) { | ||
return; | ||
} | ||
middlewears.map(function (enhancer) { | ||
enhancer.call(_this, data, action); | ||
}); | ||
}; | ||
var removeMiddlewear = function (middlewear) { | ||
var index = middlewears.indexOf(middlewear); | ||
return middlewears.splice(index, 1); | ||
}; | ||
var unsubscribe = function (callback) { | ||
var index = handlers.indexOf(callback); | ||
return handlers.splice(index, 1); | ||
}; | ||
var subscribe = function (callbacks, preload, enhancers) { | ||
if (enhancers === void 0) { enhancers = []; } | ||
callbacks.map(function (callbak) { | ||
handlers.push(callbak); | ||
if (preload !== false) { | ||
callbak.call(_this, store.state); | ||
} | ||
}); | ||
enhancers.map(function (enhancer) { | ||
middlewears.push(enhancer); | ||
}); | ||
return { handlers: handlers, middlewears: middlewears }; | ||
}; | ||
this._stores.push({ store: store, handler: handler, middlewear: middlewear }); | ||
return { subscribe: subscribe, unsubscribe: unsubscribe, removeMiddlewear: removeMiddlewear }; | ||
}; | ||
Dispatcher.prototype.unregister = function (store) { | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
if (this._stores.length === 0) { | ||
var handlers = []; | ||
var middlewears = []; | ||
var handler = function (data) { | ||
if (handlers.length <= 0) { | ||
return; | ||
} | ||
var index = this._stores.indexOf(store); | ||
this._stores.splice(index, 1); | ||
handlers.map(function (handler) { | ||
handler.call(_this, data); | ||
}); | ||
}; | ||
Dispatcher.prototype.dispatch = function (action) { | ||
if (!action) { | ||
throw new Error("Should provide action."); | ||
} | ||
if (this._stores.length === 0) { | ||
var middlewear = function (data, action) { | ||
if (middlewears.length <= 0) { | ||
return; | ||
} | ||
this._stores.map(function (item) { | ||
var store = item.store; | ||
var handler = item.handler; | ||
var middlewear = item.middlewear; | ||
store.reduce.call(store, action, handler, middlewear); | ||
middlewears.map(function (enhancer) { | ||
enhancer.call(_this, data, action); | ||
}); | ||
}; | ||
Dispatcher.prototype.hasStore = function () { | ||
return this._stores.length > 0; | ||
var removeMiddlewear = function (middlewear) { | ||
var index = middlewears.indexOf(middlewear); | ||
return middlewears.splice(index, 1); | ||
}; | ||
return Dispatcher; | ||
}()); | ||
exports.default = Dispatcher; | ||
}); | ||
var unsubscribe = function (callback) { | ||
var index = handlers.indexOf(callback); | ||
return handlers.splice(index, 1); | ||
}; | ||
var subscribe = function (callbacks, preload, enhancers) { | ||
if (enhancers === void 0) { enhancers = []; } | ||
callbacks.map(function (callbak) { | ||
handlers.push(callbak); | ||
if (preload !== false) { | ||
callbak.call(_this, store.state); | ||
} | ||
}); | ||
enhancers.map(function (enhancer) { | ||
middlewears.push(enhancer); | ||
}); | ||
return { handlers: handlers, middlewears: middlewears }; | ||
}; | ||
this._stores.push({ store: store, handler: handler, middlewear: middlewear }); | ||
return { subscribe: subscribe, unsubscribe: unsubscribe, removeMiddlewear: removeMiddlewear }; | ||
}; | ||
Dispatcher.prototype.unregister = function (store) { | ||
if (!store) { | ||
throw new Error("Should provide store."); | ||
} | ||
if (this._stores.length === 0) { | ||
return; | ||
} | ||
var index = this._stores.indexOf(store); | ||
this._stores.splice(index, 1); | ||
}; | ||
Dispatcher.prototype.dispatch = function (action) { | ||
if (!action) { | ||
throw new Error("Should provide action."); | ||
} | ||
if (this._stores.length === 0) { | ||
return; | ||
} | ||
this._stores.map(function (item) { | ||
var store = item.store; | ||
var handler = item.handler; | ||
var middlewear = item.middlewear; | ||
store.reduce.call(store, action, handler, middlewear); | ||
}); | ||
}; | ||
Dispatcher.prototype.hasStore = function () { | ||
return this._stores.length > 0; | ||
}; | ||
return Dispatcher; | ||
}()); | ||
exports.default = Dispatcher; | ||
//# sourceMappingURL=dispatcher.js.map |
@@ -5,2 +5,8 @@ import Store from './store'; | ||
import { CreateStore } from './createstore'; | ||
export { Store, Dispatcher, ActionCreator, CreateStore }; | ||
declare const miniflux: { | ||
Store: typeof Store; | ||
Dispatcher: typeof Dispatcher; | ||
ActionCreator: typeof ActionCreator; | ||
CreateStore: typeof CreateStore; | ||
}; | ||
export default miniflux; |
@@ -1,21 +0,14 @@ | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./store", "./dispatcher", "./actioncreator", "./createstore"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var store_1 = require("./store"); | ||
exports.Store = store_1.default; | ||
var dispatcher_1 = require("./dispatcher"); | ||
exports.Dispatcher = dispatcher_1.default; | ||
var actioncreator_1 = require("./actioncreator"); | ||
exports.ActionCreator = actioncreator_1.ActionCreator; | ||
var createstore_1 = require("./createstore"); | ||
exports.CreateStore = createstore_1.CreateStore; | ||
}); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var store_1 = require("./store"); | ||
var dispatcher_1 = require("./dispatcher"); | ||
var actioncreator_1 = require("./actioncreator"); | ||
var createstore_1 = require("./createstore"); | ||
var miniflux = { | ||
Store: store_1.default, | ||
Dispatcher: dispatcher_1.default, | ||
ActionCreator: actioncreator_1.ActionCreator, | ||
CreateStore: createstore_1.CreateStore, | ||
}; | ||
exports.default = miniflux; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { IAction, IHandler, IMiddlewear, IReducer, IStore } from './types/index'; | ||
import { IAction, IHandler, IMiddlewear, IReducer, IStore } from '../types/index.d'; | ||
export default class Store<TState> implements IStore { | ||
@@ -3,0 +3,0 @@ private _state; |
@@ -1,59 +0,49 @@ | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Store = /** @class */ (function () { | ||
function Store(reducer, initialState) { | ||
this.reducer = reducer; | ||
this.state = initialState; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Store = /** @class */ (function () { | ||
function Store(reducer, initialState) { | ||
this.reducer = reducer; | ||
this.state = initialState; | ||
Object.defineProperty(Store.prototype, "state", { | ||
get: function () { | ||
return this._state; | ||
}, | ||
set: function (state) { | ||
this._state = state; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Store.prototype, "reducer", { | ||
get: function () { | ||
return this._reducer; | ||
}, | ||
set: function (reducer) { | ||
if (!reducer) { | ||
throw Error("Reducer is nessesary."); | ||
} | ||
this._reducer = reducer; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Store.prototype.reduce = function (action, handler, middlewear) { | ||
if (!this._reducer) { | ||
throw Error("store should has reducer"); | ||
} | ||
Object.defineProperty(Store.prototype, "state", { | ||
get: function () { | ||
return this._state; | ||
}, | ||
set: function (state) { | ||
this._state = state; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Store.prototype, "reducer", { | ||
get: function () { | ||
return this._reducer; | ||
}, | ||
set: function (reducer) { | ||
if (!reducer) { | ||
throw Error("Reducer is nessesary."); | ||
} | ||
this._reducer = reducer; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Store.prototype.reduce = function (action, handler, middlewear) { | ||
if (!this._reducer) { | ||
throw Error("store should has reducer"); | ||
} | ||
var state = this.state; | ||
if (middlewear) { | ||
middlewear.call(null, state, action); | ||
} | ||
state = this.reducer.call(null, state, action); | ||
if (handler) { | ||
handler.call(null, state); | ||
} | ||
this.state = state; | ||
return state; | ||
}; | ||
return Store; | ||
}()); | ||
exports.default = Store; | ||
}); | ||
var state = this.state; | ||
if (middlewear) { | ||
middlewear.call(null, state, action); | ||
} | ||
state = this.reducer.call(null, state, action); | ||
if (handler) { | ||
handler.call(null, state); | ||
} | ||
this.state = state; | ||
return state; | ||
}; | ||
return Store; | ||
}()); | ||
exports.default = Store; | ||
//# sourceMappingURL=store.js.map |
{ | ||
"name": "@kazu69/miniflux", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Mini flux architecture", | ||
@@ -11,13 +11,14 @@ "main": "dist/index.js", | ||
"homepage": "https://github.com/kazu69/miniflux", | ||
"types": [ | ||
"node" | ||
], | ||
"types": "./types/index.d.ts", | ||
"scripts": { | ||
"lint": "tslint -c ./.tslint.json src/*.ts", | ||
"prebuild": "npm run clean", | ||
"lint": "tslint -c ./tslint.json src/*.ts", | ||
"build": "tsc -p .", | ||
"watch": "tsc -w -p .", | ||
"clean": "rm -rf dist/*", | ||
"test": "npm run build && nyc ava test/*.js", | ||
"bundlebuild": "npx rollup -c rollup.config.js", | ||
"prepublish": "npm run build", | ||
"postinstall": "npm run build", | ||
"test": "npm run build && nyc ava test/*.js" | ||
"prebuild": "npm run clean && npm run lint", | ||
"postbuild": "npm run bundlebuild" | ||
}, | ||
@@ -33,2 +34,4 @@ "keywords": [ | ||
"nyc": "^11.6.0", | ||
"rollup": "^0.57.0", | ||
"rollup-plugin-typescript2": "^0.12.0", | ||
"sinon": "^4.4.4", | ||
@@ -35,0 +38,0 @@ "tslint": "^5.9.1", |
## @kazu69/miniflux | ||
[![Build Status](https://travis-ci.org/kazu69/miniflux.svg?branch=master)](https://travis-ci.org/kazu69/miniflux) | ||
> Mini flux architecture | ||
### example | ||
### Using | ||
```js | ||
import flux from '@kazu69/miniflux' | ||
const Dispatcher = flux.Dispatcher | ||
const ActionCreator = flux.ActionCreator | ||
const CreateStore = flux.CreateStore | ||
```sh | ||
npm install @kazu69/miniflux | ||
``` | ||
const initState = 100 | ||
function Reducer(state, action) { | ||
switch (action.type) { | ||
case 'INCRIMENT': | ||
state += 1 | ||
break | ||
case 'DECREASE': | ||
state -= 1 | ||
break | ||
default: | ||
break | ||
} | ||
return state | ||
} | ||
### Example | ||
const dispatch = new Dispatcher() | ||
const store = CreateStore(Reducer, initState) | ||
const actionCreator = new ActionCreator(dispatch) | ||
const bindDispatchertoStore = actionCreator.createSubscriber(store) | ||
const actions = { | ||
increase: actionCreator.createAction('INCRIMENT'), | ||
decrease: actionCreator.createAction('DECREASE') | ||
} | ||
const preload = true | ||
const updateCount = state => { | ||
console.log(`updateCount`) | ||
console.log(state) | ||
} | ||
const logger = (state, action) => { | ||
console.log(`logger`) | ||
console.log(state) | ||
console.log(action) | ||
} | ||
See example. | ||
If you use a browser you can use `dist/bundle.js`. | ||
let middlewears = [] | ||
let handlers = [] | ||
handlers.push (updateCount) | ||
middlewears.push (logger) | ||
### License | ||
bindDispatchertoStore.subscribe(handlers, preload, middlewears) | ||
// => updateCount | ||
// 100 | ||
actions.increase.call(null) | ||
// logger | ||
// 100 | ||
// { type: 'INCRIMENT', payload: undefined } | ||
// updateCount | ||
// 101 | ||
actions.increase.call(null) // => 102 | ||
// logger | ||
// 101 | ||
// { type: 'INCRIMENT', payload: undefined } | ||
// updateCount | ||
// 102 | ||
``` | ||
MIT © kazu69 | ||
### test | ||
```sh | ||
npm test | ||
``` |
@@ -1,2 +0,2 @@ | ||
import {IDispatcher, IBindDispatcherToStore, IStore} from './types/index' | ||
import {IDispatcher, IBindDispatcherToStore, IStore} from '../types/index.d' | ||
@@ -3,0 +3,0 @@ export function ActionCreator(dispatcher: IDispatcher) { |
import Store from './store' | ||
import {IReducer, IStore} from './types/index' | ||
import {IReducer, IStore} from '../types/index.d' | ||
@@ -4,0 +4,0 @@ export function CreateStore<TState>(reducer: IReducer, initialState: TState): IStore { |
@@ -1,12 +0,12 @@ | ||
import {IAction, IHandler, IMiddlewear, IStore, IDispatcher, IBindDispatcherToStore} from './types/index' | ||
import {IAction, IHandler, IMiddlewear, IStore, IDispatcher, IBindDispatcherToStore} from '../types/index.d' | ||
export default class Dispatcher implements IDispatcher { | ||
private _stores: any[] = []; | ||
private _stores: any[] = [] | ||
register(store: IStore): IBindDispatcherToStore { | ||
public register(store: IStore): IBindDispatcherToStore { | ||
if (!store) { | ||
throw new Error(`Should provide store.`) | ||
} | ||
let handlers: IHandler[] = [] | ||
let middlewears: IMiddlewear[] = [] | ||
const handlers: IHandler[] = [] | ||
const middlewears: IMiddlewear[] = [] | ||
@@ -17,3 +17,3 @@ const handler = (data) => { | ||
} | ||
handlers.map(handler => { | ||
@@ -23,3 +23,3 @@ handler.call(this, data) | ||
} | ||
const middlewear = (data, action) => { | ||
@@ -33,3 +33,3 @@ if (middlewears.length <= 0) { | ||
} | ||
const removeMiddlewear = middlewear => { | ||
@@ -39,3 +39,3 @@ const index = middlewears.indexOf(middlewear) | ||
} | ||
const unsubscribe = callback => { | ||
@@ -45,3 +45,3 @@ const index = handlers.indexOf(callback) | ||
} | ||
const subscribe = (callbacks, preload, enhancers = []) => { | ||
@@ -58,3 +58,3 @@ callbacks.map(callbak => { | ||
}) | ||
return {handlers, middlewears} | ||
@@ -66,3 +66,3 @@ } | ||
unregister(store: IStore): void { | ||
public unregister(store: IStore): void { | ||
if (!store) { | ||
@@ -75,3 +75,3 @@ throw new Error(`Should provide store.`) | ||
} | ||
const index = this._stores.indexOf(store) | ||
@@ -81,3 +81,3 @@ this._stores.splice(index, 1) | ||
dispatch(action: IAction): void { | ||
public dispatch(action: IAction): void { | ||
if (!action) { | ||
@@ -99,5 +99,5 @@ throw new Error(`Should provide action.`) | ||
hasStore(): boolean { | ||
public hasStore(): boolean { | ||
return this._stores.length > 0 | ||
} | ||
} |
@@ -6,3 +6,3 @@ import Store from './store' | ||
export { | ||
const miniflux = { | ||
Store, | ||
@@ -13,1 +13,3 @@ Dispatcher, | ||
} | ||
export default miniflux |
@@ -1,2 +0,2 @@ | ||
import {IAction, IHandler, IMiddlewear, IReducer, IStore} from './types/index' | ||
import {IAction, IHandler, IMiddlewear, IReducer, IStore} from '../types/index.d' | ||
@@ -31,3 +31,3 @@ export default class Store<TState> implements IStore { | ||
reduce(action: IAction, handler?: IHandler, middlewear?: IMiddlewear): TState { | ||
public reduce(action: IAction, handler?: IHandler, middlewear?: IMiddlewear): TState { | ||
if (!this._reducer) { | ||
@@ -50,4 +50,4 @@ throw Error(`store should has reducer`) | ||
this.state = state | ||
return state | ||
return state | ||
} | ||
} | ||
} |
{ | ||
"compilerOptions": { | ||
"target": "ES5", | ||
"module": "umd", | ||
"declaration": true, | ||
"module": "CommonJS", | ||
"sourceMap": true, | ||
@@ -11,2 +10,3 @@ "noUnusedLocals": true, | ||
"alwaysStrict": true, | ||
"declaration": true, | ||
"strict": false, | ||
@@ -13,0 +13,0 @@ "outDir": "./dist" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82252
89
768
8
22