forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
219 lines (199 loc) · 6.66 KB
/
index.ts
File metadata and controls
219 lines (199 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/GametestDB/
/**
* @license MIT
* @author JaylyMC
* @project https://github.com/JaylyDev/GametestDB/
*/
/**
* The `timer` module exposes a global API for scheduling functions to
* be called at some future period of time.
*
* The timer functions within this script implement a similar API as the timers API
* provided by Web Browsers and Node.js but use a different internal implementation that is
* built for QuickJS, specifically for Minecraft Bedrock Edition script APIs (experimental).
*/
/**
* Internal timer class that can be used to reference
* the set Timeout or Interval object.
*/
class Timer {
_idleTimeout: number;
_idleStart: number;
_onTimeout: (...args: any) => void;
_repeat: boolean;
_destroyed: boolean;
constructor(idleTimeout: number, idleStart: number, onTimeout: (...args: any) => void, repeat: boolean, destroyed: boolean, ...args: any) {
this._idleTimeout = idleTimeout;
this._idleStart = idleStart;
this._onTimeout = onTimeout;
this._repeat = repeat;
this._destroyed = destroyed;
(async (): Promise<void> => {
if (repeat === true) {
while (true) {
if (await this._destroyed === true) return;
const executionTime = idleStart + idleTimeout;
if (new Date().getTime() >= executionTime) {
this._onTimeout(...args);
this._idleStart = idleStart = new Date().getTime();
};
};
} else {
const executionTime = idleStart + idleTimeout;
while (new Date().getTime() < executionTime) {
if (await this._destroyed === true) return;
};
this._onTimeout(...args);
};
})();
};
};
class Timeout extends Timer {
_idleTimeout: number;
_idleStart: number;
_onTimeout: (...args: any) => void;
_repeat: boolean;
_destroyed: boolean;
constructor(idleTimeout: number, idleStart: number, onTimeout: (...args: any) => void, repeat: boolean, destroyed: boolean, ...args: any) {
super(idleTimeout, idleStart, onTimeout, repeat, destroyed, ...args);
this._idleTimeout = idleTimeout;
this._idleStart = idleStart;
this._onTimeout = onTimeout;
this._repeat = repeat;
this._destroyed = destroyed;
};
};
/**
* Internal timer class that can be used to reference
* the set Immediate object.
*/
class Immediate {
_onImmediate: (...args: any) => void;
_argv: any[];
_destroyed: boolean = false;
constructor(onImmediate: (...args: any) => void, ...args: any) {
this._onImmediate = onImmediate;
this._argv = args;
(async (): Promise<void> => {
if (await this._destroyed === true) return;
await this._onImmediate(...args);
})();
};
};
function Validation (parameter: any, instance: any) {
if (parameter instanceof instance) return;
throw TypeError("Native type conversion failed");
};
/**
* @param callback a function to execute as its first argument
* @param ms the millisecond delay defined as a number as the second argument.
* @param args Additional arguments may also be included and these will be passed on to the function.
*
* @example
* ```js
* function myFunc(arg) {
* console.log(`arg was => ${arg}`);
* }
*
* setTimeout(myFunc, 1500, 'funky');
* ```
*/
// @ts-ignore
function setTimeout (callback: (...args: any) => void, ms?: number, ...args: any): Timeout {
Validation(callback, Function);
const idleTime: number = typeof ms === "number" ? ms : 1;
const startTime: number = new Date().getTime();
// @ts-ignore
return new Timeout(idleTime, startTime, callback, false, false, ...args);
};
/**
* By passing said object into the respective clear function,
* execution of that object will be halted completely.
* @param timeoutId Timeout object returned by `setTimeout()`
*/
// @ts-ignore
function clearTimeout (timeoutId: Timeout | undefined): void {
if (!(timeoutId instanceof Timeout)) return;
timeoutId._destroyed = true;
timeoutId._onTimeout = () => {};
};
/**
* @param callback Takes a function argument that will run an infinite number of times
* @param ms A given millisecond delay as the second argument
* @param args Just like `setTimeout()`, additional arguments can be added beyond the delay,
* and these will be passed on to the function call.
* @example
* ```js
* function intervalFunc() {
* console.log('Cant stop me now!');
* }
*
* setInterval(intervalFunc, 1500);
* ```
*/
// @ts-ignore
function setInterval (callback: (...args: any) => void, ms?: number, ...args: any): Timer {
Validation(callback, Function);
const idleTime: number = typeof ms === "number" ? ms : 1;
const startTime: number = new Date().getTime();
// @ts-ignore
return new Timer(idleTime, startTime, callback, true, false, ...args);
};
/**
* By passing said object into the respective clear function,
* execution of that object will be halted completely.
* @param intervalId Interval object returned by `setInterval()`
*/
// @ts-ignore
function clearInterval (intervalId: Timer | undefined): void {
if (!(intervalId instanceof Timer)) return;
intervalId._destroyed = true;
intervalId._onTimeout = () => {};
};
/**
* @param callback The function to execute
* @param args Any subsequent arguments will be passed to the function when it is executed.
*
* @example
* ```js
* console.log('before immediate');
*
* setImmediate((arg) => {
* console.log(`executing immediate: ${arg}`);
* }, 'so immediate');
*
* console.log('after immediate');
* ```
*/
// @ts-ignore
function setImmediate (callback: (args: void) => void, ...args: any): Immediate {
Validation(callback, Function);
// @ts-ignore
return new Immediate(callback, ...args);
};
/**
* By passing said object into the respective clear function,
* execution of that object will be halted completely.
* @param immediateId Immediate object returned by `setImmediate()`
*/
// @ts-ignore
function clearImmediate (immediateId: Immediate | undefined): void {
if (!(immediateId instanceof Immediate)) return;
immediateId._destroyed = true;
immediateId._onImmediate = () => {};
};
/**
* Timers function are exported so developers
* can choose which set of functions they want to
* withdraw from the `timers` module.
*/
export {
setTimeout,
clearTimeout,
setInterval,
clearInterval,
setImmediate,
clearImmediate
};