@overwolf/overwolf-api-ts
Version:
utilities and wrappers for common Overwolf API tasks
62 lines (61 loc) • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OWGamesEvents = void 0;
const timer_1 = require("./timer");
class OWGamesEvents {
constructor(delegate, requiredFeatures, featureRetries = 10) {
this.onInfoUpdates = (info) => {
this._delegate.onInfoUpdates(info.info);
};
this.onNewEvents = (e) => {
this._delegate.onNewEvents(e);
};
this._delegate = delegate;
this._requiredFeatures = requiredFeatures;
this._featureRetries = featureRetries;
}
async getInfo() {
return new Promise((resolve) => {
overwolf.games.events.getInfo(resolve);
});
}
async setRequiredFeatures() {
let tries = 1, result;
while (tries <= this._featureRetries) {
result = await new Promise(resolve => {
overwolf.games.events.setRequiredFeatures(this._requiredFeatures, resolve);
});
if (result.status === 'success') {
console.log('setRequiredFeatures(): success: ' + JSON.stringify(result, null, 2));
return (result.supportedFeatures.length > 0);
}
await timer_1.Timer.wait(3000);
tries++;
}
console.warn('setRequiredFeatures(): failure after ' + tries + ' tries' + JSON.stringify(result, null, 2));
return false;
}
registerEvents() {
this.unRegisterEvents();
overwolf.games.events.onInfoUpdates2.addListener(this.onInfoUpdates);
overwolf.games.events.onNewEvents.addListener(this.onNewEvents);
}
unRegisterEvents() {
overwolf.games.events.onInfoUpdates2.removeListener(this.onInfoUpdates);
overwolf.games.events.onNewEvents.removeListener(this.onNewEvents);
}
async start() {
console.log(`[ow-game-events] START`);
this.registerEvents();
await this.setRequiredFeatures();
const { res, status } = await this.getInfo();
if (res && status === 'success') {
this.onInfoUpdates({ info: res });
}
}
stop() {
console.log(`[ow-game-events] STOP`);
this.unRegisterEvents();
}
}
exports.OWGamesEvents = OWGamesEvents;