Skip to content

Commit

Permalink
fix(core) Add instanceof alternatives for Serializer and Factory objects
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Nov 12, 2019
1 parent 4d01ad0 commit 1ec5b38
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Factory {
+ Relationship newRelationship(String,String,String) throws TypeNotFoundException
+ Resource newTransaction(String,String,String,Object,String,boolean,boolean)
+ Resource newEvent(String,String,String,Object,String,boolean,boolean)
+ boolean hasInstance(object)
}
class AssetDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
Expand Down Expand Up @@ -216,4 +217,5 @@ class Serializer {
+ void setDefaultOptions(Object)
+ Object toJSON(Resource,Object,boolean,boolean,boolean,boolean,boolean) throws Error
+ Resource fromJSON(Object,Object,boolean,boolean)
+ boolean hasInstance(object)
}
3 changes: 3 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 0.82.5 {d4d49fabba1c6ce465c446e2dccad488} 2019-11-10
- Add instance of alternatives to Factory and Serializer

Version 0.82.1 {dee013e99a3c2d6acc4eddfb00aad2a2} 2019-10-22
- Make several constructors public
- Add model loader utility class
Expand Down
12 changes: 12 additions & 0 deletions packages/concerto-core/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Factory {
*/
constructor(modelManager) {
this.modelManager = modelManager;
this._isFactory = true;
}

/**
Expand Down Expand Up @@ -317,6 +318,17 @@ class Factory {
return generateParams;
}


/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
*
* @param {object} object - The object to test against
* @returns {boolean} - True, if the object is an instance of a Factory
*/
static [Symbol.hasInstance](object){
return typeof object !== 'undefined' && object !== null && Boolean(object._isFactory);
}
}

module.exports = Factory;
12 changes: 12 additions & 0 deletions packages/concerto-core/lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Serializer {
this.factory = factory;
this.modelManager = modelManager;
this.defaultOptions = Object.assign({}, baseDefaultOptions);
this._isSerializer = true;
}

/**
Expand Down Expand Up @@ -188,6 +189,17 @@ class Serializer {

return resource;
}

/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
*
* @param {object} object - The object to test against
* @returns {boolean} - True, if the object is an instance of a Serializer
*/
static [Symbol.hasInstance](object){
return typeof object !== 'undefined' && object !== null && Boolean(object._isSerializer);
}
}

module.exports = Serializer;

0 comments on commit 1ec5b38

Please sign in to comment.