diff --git a/src/conf/constants.js b/src/conf/constants.js index b58491c3..981c46c5 100644 --- a/src/conf/constants.js +++ b/src/conf/constants.js @@ -31,7 +31,8 @@ export let CONSTANTS = { SERIALIZATION_TYPES: { METHOD: 'postrobot_method', - ERROR: 'postrobot_error' + ERROR: 'postrobot_error', + PROMISE: 'postrobot_promise' }, SEND_STRATEGIES: { diff --git a/src/lib/methods.js b/src/lib/methods.js index b1bce1d5..7e724029 100644 --- a/src/lib/methods.js +++ b/src/lib/methods.js @@ -89,6 +89,18 @@ function serializeError(err : mixed) : SerializedError { }; } +type SerializePromise = { + __type__ : string, + __then__ : SerializedMethod +}; + +function serializePromise(destination : any, domain : string, promise : ZalgoPromise, name : string) : SerializePromise { + return { + __type__: CONSTANTS.SERIALIZATION_TYPES.PROMISE, + __then__: serializeMethod(destination, domain, (resolve, reject) => promise.then(resolve, reject), `${name}.then`) + }; +} + export function serializeMethods(destination : any, domain : string, obj : Object) : Object { return replaceObject({ obj }, (item, key) => { @@ -99,10 +111,15 @@ export function serializeMethods(destination : any, domain : string, obj : Objec if (item instanceof Error) { return serializeError(item); } + + if (ZalgoPromise.isPromise(item)) { + // $FlowFixMe + return serializePromise(destination, domain, item, key.toString()); + } }).obj; } -export function deserializeMethod(source : any, origin : string, obj : Object) : Object { +export function deserializeMethod(source : any, origin : string, obj : Object) : Function { function wrapper() : ZalgoPromise { let args = Array.prototype.slice.call(arguments); @@ -135,6 +152,10 @@ export function deserializeError(source : any, origin : string, obj : Object) : return new Error(obj.__message__); } +export function deserializePromise(source : any, origin : string, prom : Object) : ZalgoPromise { + return new ZalgoPromise((resolve, reject) => deserializeMethod(source, origin, prom.__then__)(resolve, reject)); +} + export function deserializeMethods(source : any, origin : string, obj : Object) : Object { return replaceObject({ obj }, (item, key) => { @@ -147,5 +168,9 @@ export function deserializeMethods(source : any, origin : string, obj : Object) return deserializeError(source, origin, item); } + if (typeof item === 'object' && item !== null && isSerialized(item, CONSTANTS.SERIALIZATION_TYPES.PROMISE)) { + return deserializePromise(source, origin, item); + } + }).obj; }