Skip to content

Commit

Permalink
增加 测试、编译指令
Browse files Browse the repository at this point in the history
  • Loading branch information
walker27 committed Nov 25, 2016
1 parent 0d6b118 commit 2283423
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 43 deletions.
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动程序",
"program": "${workspaceRoot}\\index.js",
"cwd": "${workspaceRoot}",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "附加到进程",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
Binary file modified README.md
Binary file not shown.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/index.js');
module.exports = require('./dist/ObservableAjax.js');
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
"version": "1.0.0",
"description": "this is a ajax with observer design pattern",
"main": "index.js",
"npm-script-info":{
"test":"用npm run --silent test 隐藏来自npm的报错"
},
"scripts": {
"test": "none"
"test": "mocha ./test/test.js",
"build_global": "tsc && mkdirp ./dist/global && node ./tools/make-umd-bundle.js && node ./tools/make-closure-core.js",
"build_cjs": "tsc -m commonjs --outDir ./dist/cjs/",
"clean_dist": "shx rm -rf ./dist/*"
},
"repository": {
"type": "git",
Expand All @@ -21,7 +27,11 @@
},
"homepage": "https://github.com/walker27/observableAjax#readme",
"devDependencies": {
"chai": "^3.5.0",
"google-closure-compiler-js": "^20161024.0.0",
"rollup": "^0.36.3"
}
"mocha": "^3.1.2",
"rollup": "^0.36.3",
"shx": "^0.2.1"
},
"dependencies": {}
}
50 changes: 28 additions & 22 deletions src/ObservableAjax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Subject from './subject';
import getPropByRouter from './getPropByRouter';
const
_id = (a: any) => a,
isFunction = (maybeFunc: any) => typeof(maybeFunc) === 'function',
isFunction = (maybeFunc: any) => typeof (maybeFunc) === 'function',
isNotFunction = (maybeFunc: any) => !isFunction(maybeFunc),
noop = () => {},
noop = () => { },
alwaysTrue: booleanFunc = () => true;

/**
Expand All @@ -19,36 +19,42 @@ function _triggerEvent(ctx: ObservableAjax) {
/**
* 请求类型:
*/
enum EajaxType {
enum EajaxType {
/**
* 网络请求
*/
Net = 519,
/**
* 从其他堆栈中获取
*/
Trans
*/
Trans
};
/**
* 事件类型
*/
enum FlowType {
/**
* 请求开始之前
*/
updateStart = 519,
/**
* 请求结束并在全部通知完成之后
*/
updateEnd,
/**
* 请求出错时,在updateEnd之前
*/
updateFailed,
/**
* 请求开始之前
*/
updateStart = 519,
/**
* 请求结束并在全部通知完成之后
*/
updateEnd,
/**
* 请求出错时,在updateEnd之前
*/
updateFailed,
}
class ObservableAjax {
private ajaxFunc: Function;
public params:any = {};
/**
* 请求参数
*/
public params: any = {};
/**
* 请求参数对象的被观察对象
*/
private paramSubject = new Subject();
public ajaxResponse: any = {};
private paramVerifier: booleanFunc = alwaysTrue;
Expand All @@ -69,7 +75,7 @@ class ObservableAjax {
* 设置参数校验器
*/
setParamVerifier(verifier: booleanFunc) {
if(isNotFunction(verifier))
if (isNotFunction(verifier))
return;
this.paramVerifier = verifier;
return this;
Expand Down Expand Up @@ -174,11 +180,11 @@ class ObservableAjax {
transData();
}

function successCb(resObj: any, params ?: any) {
function successCb(resObj: any, params?: any) {
self.ajaxResponse = resObj;
let transformedData = self.responseTransformer(resObj, params);
if (transformedData) {
self.resSubject.eachObserver(function(observer, idx) {
self.resSubject.eachObserver(function (observer, idx) {
observer(getPropByRouter(transformedData, observer['__router']), self.params);
});
}
Expand All @@ -194,7 +200,7 @@ class ObservableAjax {
function transData() {
const timeout = opt.delay;
if (timeout === 0 || timeout) {
setTimeout(function() { successCb(opt.resData, self.params); }, timeout);
setTimeout(function () { successCb(opt.resData, self.params); }, timeout);
return;
}
successCb(opt.resData, self.params);
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let
should = require('chai').should(),
{default: ObservableAjax} = require('../dist/cjs/observableAjax.js');

describe('type check', function(){
describe('observableAjax.ajaxType', function(){
it(' should exist Net and Trans', function(){
// ObservableAjax.ajaxType;
should.exist(ObservableAjax.ajaxType);
should.exist(ObservableAjax.ajaxType.Net);
should.exist(ObservableAjax.ajaxType.Trans);
});
});

describe('observableAjax.eventType', function(){
it(' should exist updateStart and updateEnd and maybeNone and updateFailed', function(){
// ObservableAjax.ajaxType;
should.exist(ObservableAjax.eventType);
should.exist(ObservableAjax.eventType.updateStart);
should.exist(ObservableAjax.eventType.updateEnd);
should.exist(ObservableAjax.eventType.updateFailed);
});
});
})
6 changes: 3 additions & 3 deletions tools/make-closure-core.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let
compiler = require('google-closure-compiler-js').compile,
fs = require('fs'),
sourceFile = fs.readFileSync('./dist/ObservableAjax.js', 'utf8'),
sourceFile = fs.readFileSync('./dist/global/ObservableAjax.js', 'utf8'),
compilerFlags = {
jsCode: [{src: sourceFile}],
languageIn: 'ES5',
createSourceMap: true
},
output = compiler(compilerFlags);

fs.writeFileSync('./dist/ObservableAjax.min.js', output.compiledCode, 'utf-8');
fs.writeFileSync('./dist/ObservableAjax.min.js.map', output.srouceMap, 'utf-8');
fs.writeFileSync('./dist/global/ObservableAjax.min.js', output.compiledCode, 'utf-8');
fs.writeFileSync('./dist/global/ObservableAjax.min.js.map', output.srouceMap, 'utf-8');
4 changes: 2 additions & 2 deletions tools/make-umd-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ let
path = require('path');

rollup.rollup({
entry: './dist/temp/ObservableAjax.js',
entry: './dist/es6/ObservableAjax.js',
}).then(function(bundle){
let result = bundle.generate({
format: 'umd',
moduleName: 'ObservableAjax',
sourceMap: true,
})
fs.writeFileSync('./dist/ObservableAjax.js', result.code);
fs.writeFileSync('./dist/global/ObservableAjax.js', result.code);
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"removeComments": true,
"preserveConstEnums": true,
// "suppressImplicitAnyIndexErrors":true,
"outDir": "./dist/temp/",
"outDir": "./dist/es6/",
"sourceMap": true
},
"include": [
Expand Down
11 changes: 0 additions & 11 deletions typings/ObservableAjax.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/// <reference path="./subject.d.ts" />

// interface IEnumFlowType {
// updateStart: 'updateStart';
// updateEnd: 'updateEnd';
// updateFailed: 'updateFailed';
// }

// interface IflowEvent{
// updateStart: ISubject;
// updateEnd: ISubject;
// updateFailed: ISubject;
// [propName: string]: any;
// }
interface booleanFunc{
(...args: any[]): boolean;
}
Expand Down

0 comments on commit 2283423

Please sign in to comment.