index.uts 3.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { RequestTask, SyncOptions } from "./interface.uts";
import { log } from "./utils.uts";
Y
yurj26 已提交
3
export type AsyncOptions = {
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9
    type : string;
    success : (res : string) => void;
    fail : (res : string) => void;
    complete : (res : string) => void;
};

Y
yurj26 已提交
10
export type {
Q
qiang 已提交
11
    SyncOptions
Y
yurj26 已提交
12 13
} from "./interface.uts";

fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
type SyntaxResult = {
    name : string
};

type SyncResult = {
    msg : string
}

/**
 * 导出一个属性
 */
export const MAX = 100;

/**
 * 导出一个同步方法
 * @returns
 */
export function testSync(msg : string) : SyncResult {
    console.log("log test");
    log("log test1");

    const res : SyncResult = {
        msg: `hello ${msg}`
    }
    return res
    // return {
    //   msg: `hello ${msg}`,
    // };
}
/**
 * 导出一个同步方法(触发了数组越界异常)
 */
export function testSyncError() {
    const arr : string[] = [];
    console.log(arr[1]);
}
/**
 * 导出一个带callback的同步方法
 * @param opts
 */
export function testSyncWithCallback(opts : AsyncOptions) : SyntaxResult {
    if (opts.type == "success") {
        opts.success("success");
    } else {
        opts.fail("fail");
    }
    opts.complete("complete");

    const res : SyntaxResult = {
        name: "testSyncWithCallback"
    }
    return res;
    // return { name: "testSyncWithCallback" };
}
Q
qiang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80



async function testAwaitPromise(res : SyntaxResult) : Promise<SyntaxResult> {
    // #ifdef APP-ANDROID
    return await new Promise(function (resolve : (res : SyntaxResult) => void) {
        resolve(res)
    })
    // #endif
    // #ifndef APP-ANDROID
    return res
    // #endif
}
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * 导出一个异步方法
 * @returns
 */
export async function testAsync(opts : AsyncOptions) : Promise<SyntaxResult> {
    if (opts.type == "success") {
        opts.success("success");
    } else {
        opts.fail("fail");
    }
    opts.complete("complete");

    const res : SyntaxResult = {
        name: "testAsync"
    }
Q
qiang 已提交
96
    return await testAwaitPromise(res);
fxy060608's avatar
fxy060608 已提交
97 98 99
    // return { name: "testAsync" };
}

Y
yurj26 已提交
100
export type TestOptions = {
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    name : string;
    callback : (res : string) => void;
};

export class Test {
    id : number;
    name : string;
    static type : string = "Test";
    constructor(id : number, options : TestOptions) {
        this.id = id;
        this.name = options.name;
        options.callback("Test.constructor");
    }
    static testClassStaticSyncWithCallback(opts : AsyncOptions) : SyntaxResult {
        return testSyncWithCallback(opts);
    }
    static async testClassStaticAsync(opts : AsyncOptions) : Promise<SyntaxResult> {
        const res = await testAsync(opts);
        return res;
    }
    testClassSyncWithCallback(opts : AsyncOptions) : SyntaxResult {
        return testSyncWithCallback(opts);
    }
    async testClassAsync(opts : AsyncOptions) : Promise<SyntaxResult> {
        const res = await testAsync(opts);
        return res;
    }
}

class RequestTaskImpl implements RequestTask {
    url : string
    constructor(url : string) {
        this.url = url
    }
    abort() : RequestTask {
        return this
    }
    onCallback(callback : (res : string) => void) {
        callback("onCallback")
    }
    sync(options : SyncOptions) : string {
        options.success?.("success")
        options.complete?.("success")
        return "sync"
    }
}

148
export function request(url : string) : RequestTask | null {
fxy060608's avatar
fxy060608 已提交
149 150
    return new RequestTaskImpl(url)
}