-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for encoding TypedArrays as primitive objects for serialization #2911
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
636e644
Added support for encoding TypedArrays as primitive objects (represen…
jonmmease 5030d3a
Addressed minor review comments
jonmmease dfd44d5
JSLint
jonmmease ec21714
Move TypedArray convertion logic to a new Plotly.import method,
jonmmease 8de7b5a
Add TypedArray types to .eslintrc
jonmmease 5107c97
Rename Plotly.import to Plotly.decode
jonmmease 5cdb828
Add fallback for browsers that don't support TypedArrays
jonmmease 1ec957a
Convert encoded mock into test variable.
jonmmease a7c3814
Merge branch 'master' into typedArrayEncoding
jonmmease 2b9bda9
Remove circular dependency
jonmmease 669e8be
Added new top-level Plotly.encode function.
jonmmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
var d3 = require('d3'); | ||
var isNumeric = require('fast-isnumeric'); | ||
var hasHover = require('has-hover'); | ||
var b64 = require('base64-arraybuffer'); | ||
|
||
var Lib = require('../lib'); | ||
var Events = require('../lib/events'); | ||
|
@@ -2156,6 +2157,82 @@ exports.update = function update(gd, traceUpdate, layoutUpdate, _traces) { | |
}); | ||
}; | ||
|
||
/** | ||
* Convert a primitive TypedArray representation object into a TypedArray | ||
* @param {object} v: Object with `dtype` and `data` properties that | ||
* represens a TypedArray. | ||
* | ||
* @returns {TypedArray} | ||
*/ | ||
var dtypeStringToTypedarrayType = { | ||
int8: Int8Array, | ||
int16: Int16Array, | ||
int32: Int32Array, | ||
uint8: Uint8Array, | ||
uint8_clamped: Uint8ClampedArray, | ||
uint16: Uint16Array, | ||
uint32: Uint32Array, | ||
float32: Float32Array, | ||
float64: Float64Array | ||
}; | ||
|
||
function primitiveTypedArrayReprToTypedArray(v) { | ||
// v has dtype and data properties | ||
|
||
// Get TypedArray constructor type | ||
var TypeArrayType = dtypeStringToTypedarrayType[v.dtype]; | ||
|
||
// Process data | ||
var coercedV; | ||
var value = v.value; | ||
if(value instanceof ArrayBuffer) { | ||
// value is an ArrayBuffer | ||
coercedV = new TypeArrayType(value); | ||
} else if(value.constructor === DataView) { | ||
// value has a buffer property, where the buffer is an ArrayBuffer | ||
coercedV = new TypeArrayType(value.buffer); | ||
} else if(Array.isArray(value)) { | ||
// value is a primitive array | ||
coercedV = new TypeArrayType(value); | ||
} else if(typeof value === 'string' || | ||
value instanceof String) { | ||
// value is a base64 encoded string | ||
var buffer = b64.decode(value); | ||
coercedV = new TypeArrayType(buffer); | ||
} | ||
return coercedV; | ||
} | ||
|
||
function performImport(v) { | ||
if(Lib.isPrimitiveTypedArrayRepr(v)) { | ||
return primitiveTypedArrayReprToTypedArray(v); | ||
} else if(Lib.isTypedArray(v)) { | ||
return v; | ||
} else if(Array.isArray(v)) { | ||
return v.map(performImport); | ||
} else if(Lib.isPlainObject(v)) { | ||
var result = {}; | ||
for(var k in v) { | ||
if(v.hasOwnProperty(k)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we may list the keys using Object.getOwnPropertyNames and loop through them. |
||
result[k] = performImport(v[k]); | ||
} | ||
} | ||
return result; | ||
} else { | ||
return v; | ||
} | ||
} | ||
|
||
/** | ||
* Plotly.import: | ||
* Import an object or array into... TODO | ||
* @param v | ||
* @returns {object} | ||
*/ | ||
exports.import = function(v) { | ||
return performImport(v); | ||
}; | ||
|
||
/** | ||
* Plotly.react: | ||
* A plot/update method that takes the full plot state (same API as plot/newPlot) | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to append this list:
plotly.js/.eslintrc
Lines 12 to 21 in a8c6217
to make
npm run lint
pass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we'll need to add fallbacks so that browsers w/o typed array support don't break.
We have a "test" for that: