Last active
November 11, 2018 13:13
-
-
Save guillemcordoba/a392ac08e8d7bb3f6d6dd51c407710f9 to your computer and use it in GitHub Desktop.
Holochain websocket's wrapper as an Angular service (original code in https://github.com/holochain/hc-web-client)
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
import { Injectable } from '@angular/core'; | |
import { Client } from 'rpc-websockets'; | |
export const connect = url => | |
new Promise((fulfill, reject) => { | |
const ws = new Client(url); | |
ws.on('open', () => { | |
const call = (dnaHash, zome, capability, func) => params => { | |
return ws.call(`${dnaHash}/${zome}/${capability}/${func}`, params); | |
}; | |
const close = ws.close; | |
fulfill({ call, close, ws }); | |
}); | |
}); | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class HolochainService { | |
protected _call; | |
constructor() {} | |
/** | |
* Necessary initialize function | |
* @param url | |
* @param dna | |
* @param capabilities | |
*/ | |
public init(url: string, dna: string, zome: string, capabilities: string) { | |
connect(url) | |
.then( | |
({ call }) => | |
(this._call = function_name => | |
call(dna, zome, capabilities, function_name)) | |
) | |
.catch(console.error); | |
} | |
/** | |
* Generic call function to any method of the DNA | |
* @param function_name | |
* @param data | |
*/ | |
public call(function_name: string, data: any): Promise<any> { | |
return this._call(function_name)(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment