
import page from "$utils/tool/page";
export default class {

    public socket;

    constructor(socket) {
        this.socket = socket;
    }

    // 请求的数据data
    data = {};

    // 标识表
    dataOrder = [];

    // 响应信息
    responseData = undefined;

    // 是否到达结束范围
    end = false;

    // 唯一id
    key = 'id'

    // 请求
    request(data: LibRequestContext){

        // 查看分页
        let resultData = page.page(this.dataOrder,data.requestData.page.page,data.requestData.page.pageSize);

        if (this.responseData === undefined
            ||
            resultData.length < data.requestData.page.pageSize && !this.end
        ) {
            return undefined;
        } else {
            resultData = JSON.parse(JSON.stringify(page.getData(this.data,resultData)));
            this.responseData.data.data = resultData;
            return this.responseData;
        }

    }

    // 添加数据
    addItem(item,type='push'){
        return page.addItem(this.data,this.dataOrder,item,item[this.key],type);
    }

    // 移除数据
    removeItem(id){
        return page.removeItem(this.data,this.dataOrder,id);
    }

    // 成功
    success(data: LibRequestContext){

        let useTargetData = data.responseData.data.data;

        let count = useTargetData.length;

        // 添加数据
        for (let i=0,count=useTargetData.length;i<count;i++) {
            this.addItem(useTargetData[i]);
        }

        if (!this.end && count< data.requestData.page.pageSize) {
            this.end = true;
        }

    }

}