Last active
August 26, 2016 06:38
-
-
Save jmakeig/9632a85a01aeb01dc9e926a98f366f69 to your computer and use it in GitHub Desktop.
“Flatten” class instances so they can be copied across xdmp:invoke boundaries in MarkLogic
This file contains hidden or 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
| 'use strict' | |
| const ctx = {modules: 0, root: '/modules/root/path'}; | |
| const obj = fn.head( | |
| //xdmp.invoke('main.sjs') | |
| xdmp.invoke('main.sjs', ctx) | |
| ); | |
| obj.say(); | |
| // ==> "Bob says: Hi!"" |
This file contains hidden or 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
| 'use strict'; | |
| /** | |
| * CAUTION: This is a total hack. | |
| * | |
| * For objects that cross an `xdmp.invoke()` boundary | |
| * only the enumerable properties of the instance are | |
| * copied, not all of the inherited ones from the | |
| * prototype chain. | |
| * | |
| * This function creates a new instance of a type and then | |
| * copies all of the enumerable properties from the | |
| * immediate prototype onto the newly created instance. | |
| * | |
| * @param {class|function} Type | |
| * @returns A new instance that behaves like a `Type` | |
| */ | |
| function newInstance(Type, ...args) { | |
| // <http://stackoverflow.com/a/3871769/563324> | |
| let instance = Object.create(Type.prototype); | |
| instance.constructor = Type; | |
| // `Type` cannot be declared as a class becuase | |
| // class constructor functions can only be | |
| // called with the `new` operator. | |
| const constructed = Type.apply(instance, args); | |
| if('object' === typeof constructed) { | |
| instance = constructed; | |
| } | |
| Object | |
| .getOwnPropertyNames(Type.prototype) | |
| .forEach(prop => instance[prop] = Type.prototype[prop]); | |
| return instance; | |
| } | |
| /** | |
| * ES5-style class | |
| */ | |
| function Speaker() { | |
| this.name = 'Bob'; | |
| } | |
| Speaker.prototype = { | |
| declare(message) { | |
| return `${this.name} says: ${message}`; | |
| } | |
| } | |
| /** | |
| * Singleton that has an instance of the | |
| * `Speaker` class. | |
| */ | |
| const obj = { | |
| msg: 'Hi!', | |
| speaker: newInstance(Speaker), | |
| say() { | |
| return this.speaker.declare(this.msg); | |
| } | |
| }; | |
| obj; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment