Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99431195
method-chooser.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jan 24, 12:31
Size
1 KB
Mime Type
text/x-java
Expires
Sun, Jan 26, 12:31 (2 d)
Engine
blob
Format
Raw Data
Handle
23794707
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
method-chooser.js
View Options
import
NativeMethod
from
'./methods/native.js'
;
import
IndexeDbMethod
from
'./methods/indexed-db.js'
;
import
LocalstorageMethod
from
'./methods/localstorage.js'
;
import
SimulateMethod
from
'./methods/simulate.js'
;
import
{
isNode
}
from
'./util'
;
// order is important
const
METHODS
=
[
NativeMethod
,
// fastest
IndexeDbMethod
,
LocalstorageMethod
];
/**
* The NodeMethod is loaded lazy
* so it will not get bundled in browser-builds
*/
if
(
isNode
)
{
/**
* we use the non-transpiled code for nodejs
* because it runs faster
*/
const
NodeMethod
=
require
(
'../../src/methods/'
+
// use this hack so that browserify and others
// do not import the node-method by default
// when bundling.
'node.js'
);
/**
* this will be false for webpackbuilds
* which will shim the node-method with an empty object {}
*/
if
(
typeof
NodeMethod
.
canBeUsed
===
'function'
)
{
METHODS
.
push
(
NodeMethod
);
}
}
export
function
chooseMethod
(
options
)
{
let
chooseMethods
=
[].
concat
(
options
.
methods
,
METHODS
).
filter
(
Boolean
);
// directly chosen
if
(
options
.
type
)
{
if
(
options
.
type
===
'simulate'
)
{
// only use simulate-method if directly chosen
return
SimulateMethod
;
}
const
ret
=
chooseMethods
.
find
(
m
=>
m
.
type
===
options
.
type
);
if
(
!
ret
)
throw
new
Error
(
'method-type '
+
options
.
type
+
' not found'
);
else
return
ret
;
}
/**
* if no webworker support is needed,
* remove idb from the list so that localstorage is been chosen
*/
if
(
!
options
.
webWorkerSupport
&&
!
isNode
)
{
chooseMethods
=
chooseMethods
.
filter
(
m
=>
m
.
type
!==
'idb'
);
}
const
useMethod
=
chooseMethods
.
find
(
method
=>
method
.
canBeUsed
());
if
(
!
useMethod
)
throw
new
Error
(
'No useable methode found:'
+
JSON
.
stringify
(
METHODS
.
map
(
m
=>
m
.
type
)));
else
return
useMethod
;
}
Event Timeline
Log In to Comment