From 47cd6a182d7f8266d4109f528d5245377c9f2ad6 Mon Sep 17 00:00:00 2001 From: Bing Sun Date: Mon, 27 Apr 2020 23:13:49 +0800 Subject: [PATCH] make_model: re-enable select_all --- src/util/make_model.js | 60 ++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/util/make_model.js b/src/util/make_model.js index 98a8232..487eb6b 100644 --- a/src/util/make_model.js +++ b/src/util/make_model.js @@ -63,7 +63,7 @@ export default (options={}) => { reset() { let ambient_queries = [ ...(_configs.selects ? _configs.selects : []), - ...(_configs.wheres ? _configs.wheres.map(query => ({label: query.label, op: query.op, value: typeof query.value == 'function' ? query.value() : query.value})) : []), + ...(_configs.wheres ? _configs.wheres.map(where => ({label: where.label, op: where.op, value: typeof where.value == 'function' ? where.value() : where.value})) : []), ...(_configs.order ? _configs.order : []) ] @@ -81,8 +81,6 @@ export default (options={}) => { // main methods select(offset=0, limit=Infinity) { - _cache.data = _cache.data || [] - // normalize limit if (limit == Infinity) limit = _cache.upstream_limit || _cache.count || Infinity @@ -172,44 +170,48 @@ export default (options={}) => { }) return _promise - } - } - - _model.reset() - return _model -} - -export const default1 = (args) => { - // private model configs - return { - // model & model state - fully_loaded: false, + }, - // load full model if privileged - load_full() { - this.reset() + select_all() { + // be lazy 1: if the data is presented, return the value immediately + if (this.data().length == _cache.count && !this.data().includes(undefined)) + return Promise.resolve(this.data()) - if (this.fully_loaded) - return Promise.resolve(this.list) + // be lazy 2: if there is a promise, return it if ambient matches or + // cancel it + if (_promise != null) + return _promise - return api.request({ + _promise = _configs.api.request({ method: 'POST', - url: '/rpc/query_all', + url: '/rpc/select_all', body: { endpoint: _configs.endpoint, - selects: Array.from(_configs.selects, ([label, config]) => ({label: label, alias: config.alias})), - queries: _configs.queries ? _configs.queries.map(query => ({label: query.label, op: query.op, value: typeof query.value == 'function' ? query.value() : query.value})) : [], + selects: _configs.selects || [], + wheres: _configs.wheres ? _configs.wheres.map(where => ({label: where.label, op: where.op, value: typeof where.value == 'function' ? where.value() : where.value})) : [], order: _configs.order } }).then(response => { - this.list = response.map(data => { - _value_filters.forEach(([label, config]) => data[config.alias || label] = config.value_filter(data[config.alias || label])) + _cache.count = response.length + _cache.data = response.map(data => { + // process values + _configs.selects && _configs.selects + .filter(select => select.processor) + .forEach(select => data[select.alias || select.label] = select.processor(data[select.alias || select.label])) + return data }) - this.fully_loaded = true - return this.list + // clean state + _promise = null + this.reset() }) - } + + return _promise } } + + // initialize model + _model.reset() + return _model +}