<commit_before>'use strict';
/*
 *@decription 公共数据统一管理
 *@author xtx1130
*/

const rp = require('request-promise');
//const tough = require('tough-cookie');
const assert = require('assert');

class koax {
	constructor(){
		this.data = {};
		this.nameCache = void 0;
		this.dataCache = {};
	}
	//设置cookie 不做了，cookie独立出来一个类，也可以加到request中
	// setCookie(){
	// 	return this;
	// }
	//设置数据的key
	setName(name){
		assert(!this.data[name],'this name has been declared.');
		this.data[name]={};
		this.nameCache = name;
		return this;
	}
	//request请求接口，这里数据挂载到data视图上，async为以后多请求做准备
	//name 可不加，链式调用的时候name做缓存，取最近声明的name
	async request(options,name){
		let tplName = name||this.nameCache;
		assert(tplName,'no name has been declared.');
		try{
			let res = await rp(options);
			this.data[tplName] = res;
		}catch(e){
			throw e;
		}
		return this;
	}
	cached(name){
		let tplName = name||this.nameCache;
		return this;
	}
	//返回供koas调用的中间件，这里data挂在到ctx上
	middleware(){
		let _this = this;
		let dispatch = async (ctx,next) => {
			ctx.koax = ctx.koax||{};
			for(let i in _this.data){
				ctx.koax[i] = _this.data[i];
			}
			await next();
		}
		return dispatch;
	}
}
exports = module.exports = koax;<commit_msg>fix more than one instance bug and add cache Function<commit_after>'use strict';
/*
 *@decription 公共数据统一管理
 *@author xtx1130
 *@memo 每一个koax实例会相应的维护一套数据
*/

const rp = require('request-promise');
//const tough = require('tough-cookie');
const assert = require('assert');
const isEmptyObj = require('../../deps/isEmptyObj');

class koax {
	constructor(){
		this.data = {};
		this.nameCache = void 0;
		this.dataCache = {};
	}
	//设置cookie 不做了，cookie独立出来一个类，也可以加到request中
	// setCookie(){
	// 	return this;
	// }
	//设置数据的key
	setName(name){
		assert(!this.data[name],'this name has been declared.');
		this.data[name]={};
		this.nameCache = name;
		return this;
	}
	/*
	 *@description request请求接口，这里数据挂载到data视图上，
		async为以后多请求做准备
		name 可不加，链式调用的时候name做缓存，取最近声明的name
	*/
	async request(options,name){
		let tplName = name||this.nameCache;
		assert(tplName,'no name has been declared.');
		if (this.dataCache[tplName] && !isEmptyObj(this.data[tplName])) {
			return this;
		}
		try {
			let res = await rp(options);
			this.data[tplName] = res;
		} catch (e) {
			throw e;
		}
		return this;
	}
	cached(name){
		let tplName = name||this.nameCache;
		this.dataCache[tplName] = true;
		return this;
	}
	//返回供koas调用的中间件，这里data挂在到ctx上
	middleware(){
		let _this = this;
		let dispatch = async (ctx,next) => {
			ctx.koax = ctx.koax||{};
			let copy = Object.assign(ctx.koax,_this.data);
			await next();
		}
		return dispatch;
	}
}
exports = module.exports = koax;
{'fraction_non_alphanumeric': 0.15983452425723957, 'fraction_numerical': 0.00376081233546446, 'mean_word_length': 3.75, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6112683', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1152, 'n_words': 210}