Skip to content

Commit

Permalink
Merge pull request #4 from lengfangbing/feature/develop
Browse files Browse the repository at this point in the history
release v1.1.0
  • Loading branch information
lengfangbing authored Feb 12, 2022
2 parents e645823 + c58a548 commit fb83b57
Show file tree
Hide file tree
Showing 16 changed files with 2,907 additions and 197 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
# 生产环境打包产物
lib
# 开发环境打包产物
dev
# 类型文件
*.d.ts
37 changes: 37 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"standard"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
2
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
],
"@typescript-eslint/ban-types": "off"
}
}
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ CVS
npm-debug.log
src
.babelrc
.eslintignore
.editorconfig
.eslintrc.json
package-lock.json
rollup.*
.idea
node_modules
Expand Down
68 changes: 41 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,41 @@
```typescript
import createPipe from '@bell-crow/pipe-core';
const value = createPipe({
name: 'crow',
age: 3
name: 'crow',
age: 3
}, {
getValue(value) {
return { ...value }
},
getName(value) {
return value.name;
},
getAge(value) {
return value.age;
}
getName(value) {
return value.name;
},
getAge(value) {
return value.age;
},
getValue(value) {
return value;
}
});
value
.pipeStart()
.getAge(age => console.log(`age is ${age}`))
.getName(name => console.log(`name is ${name}`))
.getValue(value => console.log(`value is ${JSON.stringify(value)}`))
.getValue((_, update) => {
const value = { name: 'bell-crow', age: 1 };
update(value as typeof _);
console.log(`change value: ${JSON.stringify(value)}`);
})
.getValue(value => console.log(`changed value is ${JSON.stringify(value)}`))
.pipeEnd()
.then()
.catch();
.pipeStart()
.getAge(age => {
console.log(`first age is ${age}`);
return age + 1;
}))
.pipe<number>((age, update) => {
console.log(`changed age is ${age}}`);
update({ age: 99 });
})
.getName(name => {
console.log(`first name is ${name}`);
return { newName: name + Date.now() };
}))
.pipe<{ newName: string }>((name, update) => {
console.log(`changed name is ${name.newName}`);
update('pipe-crow');
})
.getValue(value => console.log(`changed value is ${value}`))
.pipeEnd()
.then()
.catch();
```

## Have any explain about the usage?
Expand Down Expand Up @@ -62,10 +70,15 @@ value
> ```
> The created pipe is one `pipeline`. We can do some process based on one piece of the pipe called `process function`.
> Every `process function` could provide two parameters. The first is the `real-time value`. The second is one function can `update the value`.
> The `process function` could provide a function called `pipe`. It would receive the return value of the `process function`. And the first `pipe`'s return value would be the parameter of the secord `pipe` function. The `pipe` also supports function to update value.
> ```typescript
> await value
> .pipeStart() // start the pipe
> .getValue(value => console.log(`value is ${JSON.stringify(value)}`))
> .getValue(value => {
> console.log(`value is ${JSON.stringify(value)}`);
> return value.name;
> })
> .pipe<string>(name => console.log(`value.name is ${name}`))
> .getValue((_, update) => {
> // update the value, the update is synchronous
> const value = { name: 'bell-crow', age: 1 };
Expand All @@ -79,5 +92,6 @@ value
## TODO
* [ ] one process can mix multiple piece of the pipe function
* [ ] append piece of the pipe function dynamic
* [x] one process can mix multiple piece of the pipe function
* [x] append piece of the pipe function dynamic
* [ ] type auto support for pipe function
215 changes: 215 additions & 0 deletions lib/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};

function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}

function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}

var CoreValueFactory = /** @class */ (function () {
function CoreValueFactory(value) {
this.coreValue = value;
this.funcArray = [];
}
CoreValueFactory.prototype.getTempValue = function () {
return this.tempValue;
};
CoreValueFactory.prototype.setTempValue = function (val) {
this.tempValue = val;
};
CoreValueFactory.prototype.getValue = function () {
return this.coreValue;
};
CoreValueFactory.prototype.setValue = function (value) {
this.coreValue = __assign(__assign({}, this.coreValue), value);
};
// push一个function
CoreValueFactory.prototype.appendFunc = function (func) {
this.funcArray.push(func);
};
CoreValueFactory.prototype.initFuncArray = function () {
this.funcArray = [];
};
// 消费整个functions列表
CoreValueFactory.prototype.execFuncArray = function () {
return __awaiter(this, void 0, void 0, function () {
var _i, _a, func;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_i = 0, _a = this.funcArray;
_b.label = 1;
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 4];
func = _a[_i];
return [4 /*yield*/, func.call(null)];
case 2:
_b.sent();
_b.label = 3;
case 3:
_i++;
return [3 /*break*/, 1];
case 4:
// 重置
this.initFuncArray();
this.setTempValue(undefined);
return [2 /*return*/];
}
});
});
};
// 返回一个value的实例
CoreValueFactory.createCoreValue = function (value) {
return new CoreValueFactory(value);
};
return CoreValueFactory;
}());
function entriesLoop(target, callback) {
for (var _i = 0, _a = Object.entries(target); _i < _a.length; _i++) {
var _b = _a[_i], name_1 = _b[0], func = _b[1];
callback(name_1, func);
}
}
function createCoreValue(value) {
return CoreValueFactory.createCoreValue(value);
}
function createPipeEnd(coreValue) {
var _this = this;
var pipeEnd = function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, coreValue.execFuncArray()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
return {
pipeEnd: pipeEnd
};
}
// 待补充config的每一项方法的返回值。可以for...of遍历config后去修改每一项方法的返回的pipe方法,参数为config修改后的返回值
function createPipe(coreValue, pipeFuncConfig) {
var _this = this;
// 创建pipe的逻辑代码, 直接补充pipeEnd方法
var res = {};
// 实现变种的config方法
entriesLoop(pipeFuncConfig, function (name, func) {
var returnValue = Object.assign(res, {
pipe: function (customFunction) {
coreValue.appendFunc(function () { return __awaiter(_this, void 0, void 0, function () {
var tempValue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, customFunction(coreValue.getTempValue(), coreValue.setValue.bind(coreValue))];
case 1:
tempValue = _a.sent();
coreValue.setTempValue(tempValue);
return [2 /*return*/];
}
});
}); });
return Object.assign(returnValue, createPipeEnd(coreValue));
}
});
res[name] = function (customFunction) {
// 定义config的实现方法
coreValue.appendFunc(function () { return __awaiter(_this, void 0, void 0, function () {
var returnValue, tempValue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, func.call(null, coreValue.getValue())];
case 1:
returnValue = _a.sent();
return [4 /*yield*/, customFunction(returnValue, coreValue.setValue.bind(coreValue))];
case 2:
tempValue = _a.sent();
coreValue.setTempValue(tempValue);
return [2 /*return*/];
}
});
}); });
return returnValue;
};
});
return __assign(__assign({}, res), createPipeEnd(coreValue));
}
// 创建pipeStart的方法
function createPipeStart(coreValue, pipeFuncConfig) {
var pipeStart = function () {
// 重置执行数组
coreValue.initFuncArray();
return createPipe(coreValue, pipeFuncConfig);
};
return {
pipeStart: pipeStart
};
}
function createPipeCore(value, pipeFuncConfig) {
var coreValue = createCoreValue(value);
// 把开始的value的pipeEnd剔除掉
return createPipeStart(coreValue, pipeFuncConfig);
}

exports["default"] = createPipeCore;
Loading

0 comments on commit fb83b57

Please sign in to comment.