Browse Source

make_model: wrap random state variables

master
Bing Sun 5 years ago
parent
commit
c9d13d1cfd
Signed by: sunb GPG Key ID: F7795F8C590626AB
1 changed files with 18 additions and 15 deletions
  1. +18
    -15
      src/util/make_model.js

+ 18
- 15
src/util/make_model.js View File

@@ -48,8 +48,11 @@ export default (options={}) => {
} }


// some random variables to make things work // some random variables to make things work
let _xhr = null
let _promise = null
let _state = {
xhr: null,
promise: null,
loading: false
}


// construct the model // construct the model
let _model = { let _model = {
@@ -74,7 +77,7 @@ export default (options={}) => {
_cache.data = [] _cache.data = []
_cache.count = null _cache.count = null
_cache.upstream_limit = null _cache.upstream_limit = null
_xhr = null
_state.xhr = null
//this.fully_loaded = false //this.fully_loaded = false
} }
}, },
@@ -91,8 +94,8 @@ export default (options={}) => {


// be lazy 2: if there is a promise, return it if ambient matches or // be lazy 2: if there is a promise, return it if ambient matches or
// cancel it // cancel it
if (_promise != null)
return _promise
if (_state.promise != null)
return _state.promise


// TODO // TODO
// ambient = select + order + limit/offset // ambient = select + order + limit/offset
@@ -100,13 +103,13 @@ export default (options={}) => {
// current promise // current promise


// now the hard work // now the hard work
_promise = _configs.api.request({
_state.promise = _configs.api.request({
method: 'GET', method: 'GET',
url: _configs.endpoint, url: _configs.endpoint,
headers: _cache.count == null ? { headers: _cache.count == null ? {
Prefer: 'count=exact' Prefer: 'count=exact'
} : {}, } : {},
config: xhr => _xhr = xhr,
config: xhr => _state.xhr = xhr,
queries: [ queries: [
// transform model state to postgest queries // transform model state to postgest queries
// selects // selects
@@ -135,7 +138,7 @@ export default (options={}) => {
] ]
}).then(response => { }).then(response => {
// gather begin/end/count // gather begin/end/count
let [_range, _count] = _xhr.getResponseHeader('content-range').split('/')
let [_range, _count] = _state.xhr.getResponseHeader('content-range').split('/')
let [_begin, _end] = _range.split('-').map(v => ~~v) let [_begin, _end] = _range.split('-').map(v => ~~v)


// update count if presented // update count if presented
@@ -162,14 +165,14 @@ export default (options={}) => {
console.warn('The response range is narrower than requested, probably due to an upstream hard limit.') console.warn('The response range is narrower than requested, probably due to an upstream hard limit.')


// clean model state // clean model state
_promise = null
_state.promise = null
this.reset() this.reset()


// return data // return data
return _cache.data.slice(_begin, _end + 1) return _cache.data.slice(_begin, _end + 1)
}) })


return _promise
return _state.promise
}, },


select_all() { select_all() {
@@ -179,10 +182,10 @@ export default (options={}) => {


// be lazy 2: if there is a promise, return it if ambient matches or // be lazy 2: if there is a promise, return it if ambient matches or
// cancel it // cancel it
if (_promise != null)
return _promise
if (_state.promise != null)
return _state.promise


_promise = _configs.api.request({
_state.promise = _configs.api.request({
method: 'POST', method: 'POST',
url: '/rpc/select_all', url: '/rpc/select_all',
body: { body: {
@@ -203,11 +206,11 @@ export default (options={}) => {
}) })


// clean state // clean state
_promise = null
_state.promise = null
this.reset() this.reset()
}) })


return _promise
return _state.promise
}, },


export(options={}) { export(options={}) {


Loading…
Cancel
Save