-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): Add AssemblyScript SDK #10
Conversation
@@ -0,0 +1,16 @@ | |||
import { JSON } from '../../../node_modules/assemblyscript-json/assembly/index'; | |||
|
|||
export function jsonArrToUint8Array(array: JSON.Arr): Uint8Array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a good candidate for some unit tests?
|
||
const response = String.UTF8.decode(responseBuffer); | ||
|
||
return PromiseStatus.fromStr(response, new HttpResponse, new HttpResponse); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still bothers me 😛
Quick doodle I came up with:
type FromBuffer<T> = (buffer: Uint8Array) => T;
export class PromiseStatus<F, R> {
private constructor(
public fulfilled: F | null,
public rejected: R | null
) {}
isFulfilled(): this is true {
return this.fulfilled !== null;
}
isRejected() {
return this.rejected !== null;
}
static fromStr<F, R>(promiseStatus: string, fulfilled: FromBuffer<F>, rejected: FromBuffer<R>): PromiseStatus<F, R> {
const value = JSON.parse(promiseStatus);
let fulfilledResult: F | null = null;
let rejectedResult: R | null = null;
const rawFulfilled = value.getArr('Fulfilled');
if (rawFulfilled) {
fulfilledResult = fulfilled(rawFulfilled);
}
const rawRejected = value.getArr('Rejected');
if (rawRejected) {
rejectedResult = rejected(rawRejected);
}
return new PromiseStatus(fulfilledResult, rejectedResult);
}
}
class HTTPResponseSuccess {
public readonly success = true;
constructor(public buffer: Uint8Array) {};
}
class HTTPResponseFailure {
public readonly failure = true;
constructor(public buffer: Uint8Array) {};
}
function newSuccess(buffer: Uint8Array) {
return new HTTPResponseSuccess(buffer);
}
function newFailure(buffer: Uint8Array) {
return new HTTPResponseFailure(buffer);
}
const promiseStatus = PromiseStatus.fromStr('a', newSuccess, newFailure);
if (promiseStatus.isFulfilled()) {
promiseStatus.fulfilled?.success;
} else {
promiseStatus.rejected?.failure;
}
The main thing I still dislike is that the isFulfilled
check still doesn't let TS infer that the property is not null
.
Not saying this is the way to go but hopefully this triggers some new ideas on how to improve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also don't mind the implementation in fromStr
, I made a few edits so my editor would shut up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest we can just ditch the isFulfilled and isRejected, i'm also not really using them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The remaining open comments are more (really) nice to haves.
4c7c887
to
08af076
Compare
08af076
to
5cb9a01
Compare
Motivation
Adding this allows developers to develop data requests using AssemblyScript
Explanation of Changes
Testing
npm test
Related PRs and Issues
closes #2