Last active
November 5, 2019 21:10
-
-
Save molekilla/102b81a0e139f8ce44d11cbfbad92aa6 to your computer and use it in GitHub Desktop.
tracking contract state
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
import { validate } from './Utils'; | |
import { IMethodOrEventCall } from '../types'; | |
import { SolidoContract } from '../core/SolidoContract'; | |
export type TrackStateBy = 'polling' | 'storeUpdates' | 'push'; | |
// TODO: Add to SolidoContract | |
// subjects - RxJS subjects for methods | |
export function _Read( | |
name: string, | |
contract: SolidoContract, | |
args: any[], | |
options: IMethodOrEventCall = {} | |
) { | |
return { | |
getStream: ()=> { | |
return contract.subjects[options.name]; // subject or observable | |
}, | |
dispatch: (track: TrackStateBy) => { | |
// Validate | |
if (options.validations) { | |
validate(options.validations, args); | |
} | |
const fn = contract.callMethod(options.name || name, args); | |
let $tracked = null; | |
if (track === 'polling') { | |
// Get Method | |
$tracked = interval(10) | |
.pipe( | |
from(fn), | |
switchMap(f => f) | |
); | |
// TODO: Add to subjects | |
} else if (track === 'push') { | |
// subscribed to write | |
contract.subjects[options.name].pipe( | |
from(fn), | |
switchMap(f => f); | |
) | |
} | |
// return Observable | |
return $tracked; | |
}, | |
call: () => { | |
// Validate | |
if (options.validations) { | |
validate(options.validations, args); | |
} | |
// Get Method | |
const call = contract.callMethod(options.name || name, args); | |
// Return response | |
return call; | |
} | |
}; | |
} | |
/** | |
* Annotates a Solido call | |
* @param options IMethodOrEventCall props | |
*/ | |
export function Read(options: IMethodOrEventCall = {}) { | |
return (target: any, propertyKey: string) => { | |
const read = async function (...args: any[]) { | |
return _Read(propertyKey, this, args, options); | |
}; | |
Object.defineProperty(target, propertyKey, { | |
value: read, | |
enumerable: false, | |
configurable: true | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment