Skip to content

Commit

Permalink
Adjust base class, apply for sender
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee committed Nov 23, 2022
1 parent 2b00cb5 commit 5575993
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
10 changes: 5 additions & 5 deletions wrappers/node/src/api/out-of-band-sender.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ffi from 'node-napi-rs';
import { VCXInternalError } from '../errors';
import { rustAPI } from '../rustlib';
import { VCXBase } from './vcx-base';
import { VCXBase1 } from './vcx-base-1';
import { ISerializedData } from './common';

export interface IOOBSerializedData {
Expand Down Expand Up @@ -35,7 +35,7 @@ export enum HandshakeProtocol {
DidExchangeV1 = "DidExchangeV1",
}

export class OutOfBandSender extends VCXBase<IOOBSerializedData> {
export class OutOfBandSender extends VCXBase1<IOOBSerializedData> {
public static async create(config: IOOBCreateData): Promise<OutOfBandSender> {
const oob = new OutOfBandSender(config.source_id);
try {
Expand Down Expand Up @@ -101,7 +101,7 @@ export class OutOfBandSender extends VCXBase<IOOBSerializedData> {
}
}

protected _serializeFn = rustAPI().vcx_out_of_band_sender_serialize;
protected _deserializeFn = rustAPI().vcx_out_of_band_sender_deserialize;
protected _releaseFn = rustAPI().vcx_out_of_band_sender_release;
protected _serializeFn = ffi.toStringSender;
protected _deserializeFn = ffi.fromStringSender;
protected _releaseFn = ffi.releaseSender;
}
95 changes: 95 additions & 0 deletions wrappers/node/src/api/vcx-base-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as ffi from 'ffi-napi';
import { VCXInternalError } from '../errors';
import { createFFICallbackPromise, ICbRef } from '../utils/ffi-helpers';
import { ISerializedData } from './common';

export type IVCXBaseCreateFn = (cb: ICbRef) => number;

export abstract class VCXBase1<SerializedData> {
private _handleRef!: number;

protected static async _deserialize<T extends VCXBase1<unknown>, P = unknown>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
VCXClass: new (sourceId: string, args?: any) => T,
objData: ISerializedData<{ source_id: string }>,
constructorParams?: P,
): Promise<T> {
try {
const obj = new VCXClass(objData.source_id || objData.data.source_id, constructorParams);
await obj._initFromData(objData);
return obj;
} catch (err: any) {
throw new VCXInternalError(err);
}
}

protected abstract _serializeFn: (handle: number) => string;
protected abstract _deserializeFn: (data: string) => number;
protected _sourceId: string;

constructor(sourceId: string) {
this._sourceId = sourceId;
}

/**
*
* Data returned can be used to recreate an entity by passing it to the deserialize function.
*
* Same json object structure that is passed to the deserialize function.
*
* Example:
*
* ```
* data = await object.serialize()
* ```
*/
public async serialize(): Promise<ISerializedData<SerializedData>> {
try {
return JSON.parse(this._serializeFn(this.handle));
} catch (err: any) {
throw new VCXInternalError(err);
}
}

/** The source Id assigned by the user for this object */
get sourceId(): string {
return this._sourceId;
}

protected async _create(createFn: IVCXBaseCreateFn): Promise<void> {
const handleRes = await createFFICallbackPromise<number>(
(resolve, reject, cb) => {
const rc = createFn(cb);
if (rc) {
reject(rc);
}
},
(resolve, reject) =>
ffi.Callback(
'void',
['uint32', 'uint32', 'uint32'],
(xHandle: number, err: number, handle: number) => {
if (err) {
reject(err);
return;
}
resolve(handle);
},
),
);
this._setHandle(handleRes);
}

private async _initFromData(objData: ISerializedData<{ source_id: string }>): Promise<void> {
const objHandle = this._deserializeFn(JSON.stringify(objData))
this._setHandle(objHandle);
}

protected _setHandle(handle: number): void {
this._handleRef = handle;
}

get handle(): number {
return this._handleRef;
}
}

0 comments on commit 5575993

Please sign in to comment.