New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

swop-store

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swop-store - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

build/responsive_attr.d.ts

2

build/index.d.ts
export * from './swop';
export * from './queue';
export * from './tool';
export * from './static_store';
export * from './responsive_attr';

@@ -1,4 +0,7 @@

export * from './swop';
export * from './queue';
export * from './tool';
export * from './static_store';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
tslib_1.__exportStar(require("./swop"), exports);
tslib_1.__exportStar(require("./queue"), exports);
tslib_1.__exportStar(require("./tool"), exports);
tslib_1.__exportStar(require("./responsive_attr"), exports);

@@ -6,2 +6,4 @@ export declare type endTypes = (...args: any[]) => void;

export interface QueueTypes {
fx: UnitFun<any>[];
end: endTypes;
register<T>(fun: RejisterFun<T>): Queue;

@@ -12,6 +14,6 @@ emit(...args: any[]): Queue;

export declare class Queue implements QueueTypes {
private fx;
fx: UnitFun<any>[];
end: endTypes;
private lock;
private is_init_emit;
end: endTypes;
constructor();

@@ -18,0 +20,0 @@ register<A>(fun: RejisterFun<A>): Queue;

@@ -1,12 +0,18 @@

export class Queue {
constructor() {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Queue = (function () {
function Queue() {
this.fx = [];
this.lock = false;
this.is_init_emit = true;
this.end = () => { };
this.end = function () { };
}
register(fun) {
const { fx, is_init_emit } = this;
const queue_fun = (next, ...args) => {
fun(next, ...args);
Queue.prototype.register = function (fun) {
var _a = this, fx = _a.fx, is_init_emit = _a.is_init_emit;
var queue_fun = function (next) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
fun.apply(void 0, [next].concat(args));
};

@@ -20,5 +26,10 @@ fx.push(queue_fun);

return this;
}
emit(...args) {
const { fx, lock } = this;
};
Queue.prototype.emit = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _a = this, fx = _a.fx, lock = _a.lock;
if (lock) {

@@ -28,20 +39,27 @@ return this;

if (!fx.length) {
this.end(...args);
this.end.apply(this, args);
this.is_init_emit = true;
return this;
}
const current_fun = fx.shift();
var current_fun = fx.shift();
if (current_fun) {
this.lock = true;
current_fun((...params) => {
this.lock = false;
this.emit(...params);
}, ...args);
current_fun.apply(void 0, [function () {
var params = [];
for (var _i = 0; _i < arguments.length; _i++) {
params[_i] = arguments[_i];
}
_this.lock = false;
_this.emit.apply(_this, params);
}].concat(args));
}
return this;
}
remove(start, end = 1) {
};
Queue.prototype.remove = function (start, end) {
if (end === void 0) { end = 1; }
this.fx.splice(start, end);
return this;
}
}
};
return Queue;
}());
exports.Queue = Queue;

@@ -1,2 +0,2 @@

import { DataContainer } from './static_store';
import { DataContainer } from './responsive_attr';
import { QueueTypes, UnitFun } from './queue';

@@ -26,3 +26,3 @@ export declare type Store<I> = {

export interface sendData {
origin_data: null | any;
origin_data: any;
id: FunUnit['id'];

@@ -52,4 +52,4 @@ }

use(match: I | D | '*', fun: Middleware<I, D>): Swop<I, R>;
call(name: I, data?: null): Promise<any>;
response(data: sendData): Promise<any>;
call(name: I, data?: any): Promise<any>;
response(data: sendData | string): Promise<any>;
get_queue(name: I): SingleStroeQueue['queue'];

@@ -56,0 +56,0 @@ get_funs(name: I): SingleStroeQueue['funs'];

@@ -1,24 +0,30 @@

import { warn } from './tool';
import { DataContainer } from './static_store';
import { Queue } from './queue';
const DELIMITER = '_:_';
const ID_REG = new RegExp(`,?"id":"[^}]+${DELIMITER}.+_:_swopid",?`, 'g');
export class Swop extends DataContainer {
constructor({ json_stringify = false, json_parse = false, } = {}) {
super();
this.store = {};
this.middleware = [];
this.json_stringify = json_stringify;
this.json_parse = json_parse;
this.send = () => {
warn('You must override the 【send】 method', true);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var tool_1 = require("./tool");
var responsive_attr_1 = require("./responsive_attr");
var queue_1 = require("./queue");
var DELIMITER = '_:_';
var ID_REG = new RegExp(",?\"id\":\"[^}]+" + DELIMITER + ".+_:_swopid\",?", 'g');
var Swop = (function (_super) {
tslib_1.__extends(Swop, _super);
function Swop(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.json_stringify, json_stringify = _c === void 0 ? false : _c, _d = _b.json_parse, json_parse = _d === void 0 ? false : _d;
var _this = _super.call(this) || this;
_this.store = {};
_this.middleware = [];
_this.json_stringify = json_stringify;
_this.json_parse = json_parse;
_this.send = function () {
tool_1.warn('You must override the 【send】 method', true);
};
this.init();
_this.init();
return _this;
}
call_middleware(match, params) {
let i = 0;
const middleware = this.middleware;
const length = middleware.length;
Swop.prototype.call_middleware = function (match, params) {
var i = 0;
var middleware = this.middleware;
var length = middleware.length;
for (; i < length; i++) {
const mid = middleware[i];
var mid = middleware[i];
if (mid.match === match || mid.match === 'all') {

@@ -28,14 +34,19 @@ mid.fun(params);

}
}
create_callback(name, resolve) {
return (next, nextSwopFun, data, ...params) => {
const response_data = [
this.get_json_origin_data(data),
};
Swop.prototype.create_callback = function (name, resolve) {
var _this = this;
return function (next, nextSwopFun, data) {
var params = [];
for (var _i = 3; _i < arguments.length; _i++) {
params[_i - 3] = arguments[_i];
}
var response_data = [
_this.get_json_origin_data(data),
{
next,
params,
nextSwopFun,
next: next,
params: params,
nextSwopFun: nextSwopFun,
}
];
this.call_middleware(name, {
_this.call_middleware(name, {
value: response_data,

@@ -46,9 +57,9 @@ match: name

};
}
search(name, id) {
};
Swop.prototype.search = function (name, id) {
if (typeof id !== 'string' || (id && !id.includes(name))) {
warn(`【${id}】is invalid id`);
tool_1.warn("\u3010" + id + "\u3011is invalid id");
}
const list = this.store[name].funs || [];
for (let i = 0, funUnit; funUnit = list[i]; i++) {
var list = this.store[name].funs || [];
for (var i = 0, funUnit = void 0; funUnit = list[i]; i++) {
if (id === funUnit.id) {

@@ -58,15 +69,15 @@ return list.splice(i, 1)[0].fun_body;

}
}
get_name_by_id(id) {
};
Swop.prototype.get_name_by_id = function (id) {
return id.split(DELIMITER)[0];
}
get_id(data) {
};
Swop.prototype.get_id = function (data) {
if (typeof data === 'string') {
if (!data.includes('origin_data' || !data.includes('swopid'))) {
warn('The response data must contain 【origin_data】 and 【id】');
tool_1.warn('The response data must contain 【origin_data】 and 【id】');
}
const ID_GROUP_REG = new RegExp(`(,?"id":")([^}]+${DELIMITER}.+_:_swopid)"(,?)`, 'g');
const match = ID_GROUP_REG.exec(data);
var ID_GROUP_REG = new RegExp("(,?\"id\":\")([^}]+" + DELIMITER + ".+_:_swopid)\"(,?)", 'g');
var match = ID_GROUP_REG.exec(data);
if (!match || (match && !match[2])) {
warn(`Invalid id`);
tool_1.warn("Invalid id");
}

@@ -76,9 +87,9 @@ return match[2];

return data.id;
}
get_json_origin_data(data) {
};
Swop.prototype.get_json_origin_data = function (data) {
if (typeof data === 'string') {
data = data.replace(ID_REG, '');
const match = /({?"origin_data":)(.+)}(,.*)!?/g.exec(data);
var match = /({?"origin_data":)(.+)}(,.*)!?/g.exec(data);
if (!match) {
return data.replace(/({?"origin_data":)(.+)}/g, (k1, k2, k3) => k3);
return data.replace(/({?"origin_data":)(.+)}/g, function (k1, k2, k3) { return k3; });
}

@@ -88,25 +99,26 @@ return match[2];

return data.origin_data;
}
send_request(name, data, reject) {
const stringify_data = this.json_stringify
};
Swop.prototype.send_request = function (name, data, reject) {
var stringify_data = this.json_stringify
? this.convert_json(data, 'stringify', reject)
: data;
this.send(name, stringify_data, data);
}
use(match, fun) {
};
Swop.prototype.use = function (match, fun) {
match === '*' && (match = 'all');
const push_data = { match: match, fun };
var push_data = { match: match, fun: fun };
this.middleware.push(push_data);
return this;
}
call(name, data = null) {
return new Promise((resolve, reject) => {
const { store, random_str } = this;
const fun = this.create_callback(name, resolve);
const current_unit = store[name];
const funUnit = {
};
Swop.prototype.call = function (name, data) {
var _this = this;
return new Promise(function (resolve, reject) {
var _a = _this, store = _a.store, random_str = _a.random_str;
var fun = _this.create_callback(name, resolve);
var current_unit = store[name];
var funUnit = {
fun_body: fun,
id: name + DELIMITER + random_str(),
};
const send_data = {
var send_data = {
origin_data: data,

@@ -116,5 +128,5 @@ id: funUnit.id,

if (!current_unit) {
this.store[name] = {
_this.store[name] = {
funs: [funUnit],
queue: new Queue(),
queue: new queue_1.Queue(),
};

@@ -125,11 +137,12 @@ }

}
this.send_request(name, send_data, reject);
_this.send_request(name, send_data, reject);
});
}
response(data) {
return new Promise((resolve, reject) => {
const { store, convert_json, json_parse } = this;
};
Swop.prototype.response = function (data) {
var _this = this;
return new Promise(function (resolve, reject) {
var _a = _this, store = _a.store, convert_json = _a.convert_json, json_parse = _a.json_parse;
if (typeof data !== 'string' &&
(typeof data !== 'object' || data === null)) {
warn(`response data must be JSON string or javascript object`);
tool_1.warn("response data must be JSON string or javascript object");
}

@@ -139,17 +152,21 @@ if (json_parse) {

}
const id = this.get_id(data);
const name = this.get_name_by_id(id);
const { funs, queue } = store[name];
var id = _this.get_id(data);
var name = _this.get_name_by_id(id);
var _b = store[name], funs = _b.funs, queue = _b.queue;
if (store[name]) {
queue.register((next, ...args) => {
const compatible = {
fun_body() {
warn('next Swop function is 【undefined】', true);
queue.register(function (next) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var compatible = {
fun_body: function () {
tool_1.warn('next Swop function is 【undefined】', true);
return false;
},
};
let current_call_fun = this.search(name, id);
let next_Swop_fun = (funs[0] || compatible).fun_body;
var current_call_fun = _this.search(name, id);
var next_Swop_fun = (funs[0] || compatible).fun_body;
if (current_call_fun) {
current_call_fun(next, next_Swop_fun, data, ...args);
current_call_fun.apply(void 0, [next, next_Swop_fun, data].concat(args));
resolve(true);

@@ -160,14 +177,17 @@ }

});
}
get_queue(name) {
const compatible = this.store[name] || [{ queue: false }];
};
Swop.prototype.get_queue = function (name) {
var compatible = this.store[name] || [{ queue: false }];
return compatible.queue;
}
get_funs(name) {
const compatible = this.store[name] || [{ funs: false }];
};
Swop.prototype.get_funs = function (name) {
var compatible = this.store[name] || [{ funs: false }];
return compatible.funs;
}
}
export function CreateSwop(opions) {
};
return Swop;
}(responsive_attr_1.DataContainer));
exports.Swop = Swop;
function CreateSwop(opions) {
return new Swop(opions);
}
exports.CreateSwop = CreateSwop;

@@ -0,9 +1,11 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function sync_promise() {
let resolve;
let reject;
const _promise = new Promise((_resolve, _reject) => {
var resolve;
var reject;
var _promise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
return { promise: _promise, resolve, reject };
return { promise: _promise, resolve: resolve, reject: reject };
}

@@ -19,7 +21,8 @@ function convert_json(data, identifier, reject) {

}
function random_str(range = 16) {
const chart_str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKMLNOPQRSTUVWSYZ_!~@#$%^&*()+=-><,.?/';
const randomNum = () => parseInt(String(Math.random() * chart_str.length));
let str = '';
for (let i = 0; i < range; i++) {
function random_str(range) {
if (range === void 0) { range = 16; }
var chart_str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKMLNOPQRSTUVWSYZ_!~@#$%^&*()+=-><,.?/';
var randomNum = function () { return parseInt(String(Math.random() * chart_str.length)); };
var str = '';
for (var i = 0; i < range; i++) {
str += chart_str[randomNum()];

@@ -29,15 +32,20 @@ }

}
export class Tool {
random_str(range) {
var Tool = (function () {
function Tool() {
}
Tool.prototype.random_str = function (range) {
return random_str();
}
sync_promise() {
};
Tool.prototype.sync_promise = function () {
return sync_promise();
}
convert_json(data, identifier, reject) {
};
Tool.prototype.convert_json = function (data, identifier, reject) {
return convert_json(data, identifier, reject);
}
}
export function warn(error_text, is_warn = false) {
const message = `${error_text} --- from Swop.js.`;
};
return Tool;
}());
exports.Tool = Tool;
function warn(error_text, is_warn) {
if (is_warn === void 0) { is_warn = false; }
var message = error_text + " --- from Swop.js.";
if (!is_warn) {

@@ -48,1 +56,2 @@ throw Error(message);

}
exports.warn = warn;
{
"name": "swop-store",
"version": "1.0.3",
"version": "1.0.4",
"description": "Swop is a small library for data interaction with clients.",
"main": "./build/index.js",
"scripts": {
"clean": "rm -rf ./build",
"start": "webpack-dev-server",
"build": "npm run clean && tsc --project ./release.tsconfig.json",
"build:dev": "npm run clean && tsc --pretty --noEmitOnError",
"watch": "tsc --watch --pretty --noEmitOnError"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"ts-loader": "^3.5.0",
"tslib": "^1.9.0",
"typescript": "2.5.3",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
},
"scripts": {},
"repository": {

@@ -29,4 +11,10 @@ "type": "git",

},
"keywords": [
"swop",
"data",
"store",
"interface"
],
"author": "Supertaotao",
"license": "ISC"
}

@@ -11,28 +11,28 @@ # Welcome to the Swop 🎉🎉🎉

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
swop 使用[`aop`][aop_wiki]的理念进行设计开发,通过对于数据的阶段式操作来做一些事情,参考一下`express`和`redux`的`middleware`,swop 能让开发者更好的处理数据,更好的异步和错误处理。
swop 使用 [`aop`][aop_wiki] 的理念进行设计开发,通过对于数据的拦截来做一些事情,swop 内置了中间件,参考一下`express`和`redux`的`middleware`,但不同的是 swop 的`middleware`没有next函数,swop 能让开发者更好的处理数据,更好的异步和错误处理。
## API
### options
- **json_stringify**(default false):
与客户端交互时是否让 swop 对传输的数据进行json stringify。
- **json_stringify**:
与客户端交互时是否让 swop 对传输的数据进行json stringify,默认值为 `false`。
- **json_parse**(default false):
客户端响应时是否让 swop 对传输的数据进行json parse。
- **json_parse**:
客户端响应时是否让 swop 对传输的数据进行json parse,默认值为 `false`。
### [swop 类 api][swop_api]
- [call][call]
- [response][response]
- [create][create]
- [use][use]
- [get_all_data][get_all_data]
- [get_queue][get_queue]
- [get_funs][get_funs]
- [clear_polling][clear_polling]
### swop 类 api
- #### [`call(name, [data])`][call]
- #### [`response(data)`][response]
- #### [`create(attr_name, [default_value], [read_only])`][create]
- #### [`use(name, fun)`][use]
- #### [`get_all_data()`][get_all_data]
- #### [`get_queue(name)`][get_queue]
- #### [`get_funs(name)`][get_funs]
- #### [`clear_polling([name])`][clear_polling]
### [states api][states_api]
- [get][get]
- [set][set]
- [subscribe][subscribe]
- [unsubscribe][unsubscribe]
- [polling][polling]
### 绑定属性 api
- #### [`get()`][get]
- #### [`set(value)`][set]
- #### [`subscribe(fun)`][subscribe]
- #### [`unsubscribe()`][unsubscribe]
- #### [`polling([name], [data], [fun])`][polling]

@@ -188,3 +188,3 @@ ### 实例化 swop

```javascript
const all_stata = S.get_all_data();
const all_data = S.get_all_data();
```

@@ -227,3 +227,3 @@

## states api
## 绑定属性 api
当通过[`create`][create] api 创建一个静态属性后,每个静态属性都会生成对应的方法,需要注意的是当生成的绑定数据是`readOnly`时,当前绑定属性只有`get`方法。

@@ -238,3 +238,3 @@

### set
set 方法会给当前绑定数据重新复制,返回值是当前绑定属性
set 方法会给当前绑定数据重新赋值,返回值是当前绑定属性
```javascript

@@ -380,3 +380,2 @@ S.dataOne.set(1);

[swop_api]:#swop-%E7%B1%BB-api-1
[call]:#call

@@ -391,3 +390,2 @@ [response]:#response

[states_api]:#states-api-1
[get]:#get

@@ -394,0 +392,0 @@ [set]:#set

@@ -6,3 +6,3 @@ const webpack = require('webpack');

module.exports = {
entry: __dirname + "/index.ts",
entry: __dirname + "/test/d_test.ts",
output: {

@@ -9,0 +9,0 @@ path: __dirname,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc