-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutilities.js
196 lines (150 loc) · 4.06 KB
/
utilities.js
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2021 fvtt-lib-wrapper Rui Pinheiro
import test from 'tape';
import fs from 'fs';
import {PACKAGE_ID} from '../src/consts.js';
import {Log} from '../src/shared/log.js';
// Tell Node to give us longer callstacks
Error.stackTraceLimit = Infinity;
// Emulate hooks
class Hooks {
static callAll(hook) {
Log.debug$?.(`Hooks.callAll('${hook}') triggered`);
return true;
}
static call(hook) {
Log.debug$?.(`Hooks.call('${hook}') triggered`);
return true;
}
static once(key, cb) {
Log.debug$?.(`Hooks.once('${key}') triggered`);
cb();
}
};
globalThis.Hooks = Hooks;
// Empty FormApplication
class FormApplication {
}
globalThis.FormApplication = FormApplication;
// Game
class GameSettings {
constructor() {
this.SETTINGS = new Map();
}
register(module, name, data) {
if(module !== PACKAGE_ID)
throw `game.settings.register module must be '${PACKAGE_ID}', got '${module}'`;
this.set(module, name, data?.default);
}
set(module, setting, value) {
this.SETTINGS.set(`${module}.${setting}`, value);
}
get(module, setting) {
return this.SETTINGS.get(`${module}.${setting}`);
}
registerMenu() {
}
}
class Game {
constructor() {
this.reset();
}
reset() {
this.settings = new GameSettings();
this.user = { isGM: true, userId: 12345 };
this.userId = 12345;
this.data = {
userId: 12345,
users: [
{
_id: 12345,
role: 4
}
],
system: {
id: 'example-system'
},
world: {
id: 'example-world'
}
}
this.ready = true;
this.clear_modules();
}
clear_modules() {
this.modules = new Map();
this.add_module('lib-wrapper');
}
add_module(nm) {
const mdl = { id: nm, active: true, data: { title: nm } };
if(nm === PACKAGE_ID)
mdl.data = JSON.parse(fs.readFileSync('module.json', 'utf8'));
else
mdl.data = { title: nm };
this.modules.set(nm, mdl);
}
}
globalThis.game = new Game();
globalThis.game.clear_modules();
// UI Notifications
class UiNotifications {
error(msg) {
if(this !== globalThis.ui.notifications)
throw "ui.notifications.error 'this' is not 'globalThis.ui.notifications";
Log.error$?.(`(UI) ${msg}`);
}
warn(msg) {
if(this !== globalThis.ui.notifications)
throw "ui.notifications.warn 'this' is not 'globalThis.ui.notifications";
Log.warn$?.(`(UI) ${msg}`);
}
}
globalThis.ui = { notifications: new UiNotifications() };
// Dialog
class Dialog {
render() {}
}
globalThis.Dialog = Dialog;
// Wrap helpers to bypass libWrapper public API
export const wrap_front = function(obj, fn_name, fn, {is_setter=false, is_notify=false, chain=true, perf_mode='AUTO'}={}) {
const wrapper = libWrapper._UT_create_wrapper_from_object(obj, fn_name);
wrapper.get_fn_data(is_setter, is_notify, true).splice(0, 0, {
fn: fn,
priority: undefined,
active: true,
chain: chain,
perf_mode: libWrapper._UT_get_force_fast_mode() ? 'FAST' : perf_mode
});
};
export const unwrap_all_from_obj = function(obj, fn_name, is_setter=false) {
const wrapper = libWrapper._UT_create_wrapper_from_object(obj, fn_name);
wrapper.clear();
}
// Async helpers
export const test_combinations = async function(title, fn, {sync_async=true, fast_mode=true}={}) {
for(let is_async of sync_async ? [false, true] : [false]) {
for(let is_fast_mode of fast_mode ? [false, true] : [false]) {
test(`${title}${is_async ? ' (async)' : ''}${is_fast_mode ? ' (fast)' : ''}`, async function(t) {
t.test_async = is_async;
t.fast_mode = is_fast_mode;
libWrapper._UT_force_fast_mode(is_fast_mode);
return fn(t);
});
}
}
}
export const ASYNC_TIMEOUT = 10;
export const async_retval = function(in_value, timeout=ASYNC_TIMEOUT) {
return new Promise(resolve => setTimeout(() => { resolve(in_value) }, timeout));
}
export const retval = function(t, in_value, timeout=ASYNC_TIMEOUT) {
return t.test_async ? async_retval(in_value, timeout) : in_value;
}
export const is_promise = function(obj) {
return (typeof obj?.then === 'function')
}
export const sync_async_then = function(obj, fn) {
if(is_promise(obj))
return obj.then(fn);
return fn(obj);
}