1'use strict';
2
3class HW_TIMER {
4    constructor(options) {
5        if (!options || !options.id) {
6            throw new Error('options is invalid');
7        }
8
9        this.options = {
10            id: options.id
11        };
12
13        this.success = options.success || function(){};
14        this.fail = options.fail || function(){};
15        this._open();
16    }
17
18    _open() {
19        this.timerInstance = __native.TIMER.open(this.options.id);
20        if (!this.timerInstance) {
21            this.fail();
22            return;
23        }
24        this.success();
25    }
26
27    setTimeout(cb, timeout) {
28        if (!this.timerInstance) {
29            throw new Error("timer not init");
30        }
31        var ret = __native.TIMER.setTimeout(this.timerInstance, cb, timeout);
32        if (ret != 0) {
33            throw new Error("set time out failed");
34        }
35    }
36
37    clearTimeout() {
38        if (!this.timerInstance) {
39            throw new Error("timer not init");
40        }
41        var ret = __native.TIMER.clearTimeout(this.timerInstance);
42        if (ret != 0) {
43            throw new Error("clear time out failed");
44        }
45    }
46
47    setInterval(cb, timeout) {
48        if (!this.timerInstance) {
49            throw new Error("timer not init");
50        }
51        var ret = __native.TIMER.setInterval(this.timerInstance, cb, timeout);
52        if (ret != 0) {
53            throw new Error("set interval failed");
54        }
55    }
56
57    clearInterval() {
58        if (!this.timerInstance) {
59            throw new Error("timer not init");
60        }
61        var ret = __native.TIMER.clearInterval(this.timerInstance);
62        if (ret != 0) {
63            throw new Error("clear interval failed");
64        }
65    };
66
67    close() {
68        if (!this.timerInstance) {
69            throw new Error("timer not init");
70        }
71        __native.TIMER.close(this.timerInstance);
72    };
73}
74
75function open(options) {
76    return new HW_TIMER(options);
77}
78
79module.exports = {
80    open,
81}
82