-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: adding in serializable object literal
- Loading branch information
Nick Angelou
committed
Apr 29, 2020
1 parent
db9fd42
commit 2972f1c
Showing
20 changed files
with
289 additions
and
103 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
export const Serializable = () => ({ | ||
Exception, | ||
Vector, | ||
ComprModeType | ||
}) => () => { | ||
let _instance = null | ||
|
||
/** | ||
* @implements Serializable | ||
*/ | ||
|
||
/** | ||
* @interface Serializable | ||
*/ | ||
return { | ||
/** | ||
* Get the underlying WASM instance | ||
* | ||
* @private | ||
* @readonly | ||
* @name Serializable#instance | ||
* @type {instance} | ||
*/ | ||
get instance() { | ||
return _instance | ||
}, | ||
|
||
/** | ||
* Inject this object with a raw WASM instance. No type checking is performed. | ||
* | ||
* @private | ||
* @function | ||
* @name Serializable#unsafeInject | ||
* @param {instance} instance WASM instance | ||
*/ | ||
unsafeInject(instance) { | ||
if (_instance) { | ||
_instance.delete() | ||
_instance = null | ||
} | ||
_instance = instance | ||
}, | ||
|
||
/** | ||
* Delete the underlying WASM instance. | ||
* | ||
* Should be called before dereferencing this object to prevent the | ||
* WASM heap from growing indefinitely. | ||
* @function | ||
* @name Serializable#delete | ||
*/ | ||
delete() { | ||
if (_instance) { | ||
_instance.delete() | ||
_instance = null | ||
} | ||
}, | ||
|
||
/** | ||
* Save to a base64 string | ||
* | ||
* @function | ||
* @name Serializable#save | ||
* @param {ComprModeType} [compression={@link ComprModeType.deflate}] The compression mode to use | ||
* @returns {String} Base64 encoded string | ||
*/ | ||
save(compression = ComprModeType.deflate) { | ||
try { | ||
return _instance.saveToString(compression) | ||
} catch (e) { | ||
throw Exception.safe(e) | ||
} | ||
}, | ||
|
||
/** | ||
* Save as a binary Uint8Array | ||
* | ||
* @function | ||
* @name Serializable#saveArray | ||
* @param {ComprModeType} [compression={@link ComprModeType.deflate}] The compression mode to use | ||
* @returns {Uint8Array} A byte array containing the Serializable object in binary form | ||
*/ | ||
saveArray(compression = ComprModeType.deflate) { | ||
const tempVect = Vector(new Uint8Array(0)) | ||
const instance = _instance.saveToArray(compression) | ||
tempVect.unsafeInject(instance) | ||
const tempArr = tempVect.toArray() | ||
tempVect.delete() | ||
return tempArr | ||
} | ||
} | ||
} |
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
Oops, something went wrong.