Last active
December 29, 2015 09:59
-
-
Save oxUnd/7654437 to your computer and use it in GitHub Desktop.
fis collect resource algorithm
you can enter to http://pythontutor.com/visualize.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
fis collect resource algorithm | |
''' | |
# | |
# static resoruce | |
# | |
collection = {} | |
# | |
# async js | |
# | |
async = { | |
'res': {}, | |
'pkg': {} | |
} | |
# | |
# loaded | |
# | |
loaded = {} | |
# | |
# async deleted | |
# | |
asyncDeleted = {} | |
resMap = { | |
"res": { | |
"async_0.js": { | |
'type': 'js', | |
'uri': '/static/widget/async_0.js', | |
'deps': ['d.js'] | |
}, | |
"a0.js": { | |
'type': 'js', | |
'uri': '/static/widget/a0.js', | |
}, | |
"a.js": { | |
'uri': '/static/widget/a.js', | |
'type': 'js', | |
'pkg': 'p0', | |
'deps': ['a0.js'] | |
}, | |
"b.js": { | |
'uri': '/static/widget/b.js', | |
'type': 'js', | |
'pkg': 'p0' | |
}, | |
"c.js": { | |
'uri': '/static/widget/c.js', | |
'type': 'js', | |
'extras': { | |
'async': ['async_0.js'] | |
} | |
}, | |
"d.js": { | |
'uri': '/static/widget/d.js', | |
'type': 'js' | |
}, | |
}, | |
"pkg": { | |
"p0": { | |
'uri': '/static/pkg/p0.js', | |
'has': ['a.js', 'b.js'], | |
'type': 'js' | |
} | |
} | |
} | |
def delAsync(resId): | |
if resId in asyncDeleted: | |
return True | |
else: | |
asyncDeleted[resId] = True | |
res = async['res'][resId] | |
# first deps | |
if 'deps' in res: | |
for depId in res['deps']: | |
delAsync(depId) | |
# second self | |
if 'pkg' in res: | |
if res['pkg'] in async['pkg']: | |
pkg = async['pkg'][res['pkg']] | |
del async['pkg'][res['pkg']] | |
collection[res['type']].append(pkg['uri']) | |
for hasId in pkg['has']: | |
loaded[hasId] = pkg['uri'] | |
delAsync(hasId) | |
else: | |
del async['res'][resId] | |
else: | |
collection[res['type']].append(res['uri']) | |
loaded[resId] = res['uri'] | |
del async['res'][resId] | |
# | |
# load deps | |
# | |
def loadDeps(res, isAsync): | |
if 'deps' in res: | |
for depId in res['deps']: | |
load(depId, isAsync) | |
if 'extras' in res: | |
if 'async' in res['extras']: | |
arrAsync = res['extras']['async'] | |
for asyncId in arrAsync: | |
# change isAsync status == True | |
load(asyncId, True) | |
return | |
def load(resId, isAsync): | |
if resId in loaded: | |
if resId in async['res']: | |
delAsync(resId) | |
# if loaded, return it | |
return loaded[resId] | |
arrRes = resMap['res'] | |
if resId in arrRes: | |
uri = '' | |
res = arrRes[resId] | |
arrHasPkg = {} | |
pkg = {} | |
if 'pkg' in res: | |
arrPkg = resMap['pkg'] | |
pkg = arrPkg[res['pkg']] | |
uri = pkg['uri'] | |
# first set loaded | |
for hasId in pkg['has']: | |
loaded[hasId] = uri | |
for hasId in pkg['has']: | |
# collect pkg | |
arrHasPkg[hasId] = arrRes[hasId] | |
loadDeps(arrRes[hasId], isAsync) | |
else: | |
uri = res['uri'] | |
loaded[resId] = uri | |
loadDeps(res, isAsync) | |
if isAsync and res['type'] == 'js': | |
if len(pkg) > 0: | |
async['pkg'][res['pkg']] = pkg; | |
async['res'].update(arrHasPkg) | |
else: | |
async['res'][resId] = res | |
else: | |
if res['type'] not in collection: | |
collection[res['type']] = [] | |
collection[res['type']].append(uri) | |
return uri | |
return False | |
load('a.js', False) | |
load('b.js', False) | |
load('c.js', False) | |
load('async_0.js', False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment