@towercg2/server
Version:
The server runtime for the TowerCG2 video graphics system.
60 lines (59 loc) • 2.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = __importDefault(require("lodash"));
const client_1 = require("@towercg2/client");
const ServerPlugin_1 = require("../../ServerPlugin");
const reducer_1 = require("./reducer");
const actions_1 = require("./actions");
class TickPlugin extends ServerPlugin_1.ServerPlugin {
constructor() {
super(...arguments);
this.DEFAULT_CONFIG = {};
this.tickIntervals = {};
}
buildDefaultConfig() {
return {
tickers: {
default: {
interval: 1000
}
}
};
}
buildDefaultState() {
return {};
}
buildReducer() {
return reducer_1.reducer;
}
async initialize(http) {
for (const [tickerName, tickerConfig] of lodash_1.default.toPairs(this.config.tickers)) {
const tickerInterval = tickerConfig.interval;
this.logger.debug({ tickerName, tickerInterval }, "Setting ticker.");
this.tickIntervals[tickerName] = setInterval(() => this._handleTick(tickerName), tickerInterval);
}
this.client.handleEvent("tick:elapsed", async (payload) => {
this.logger.debug("Ponging from own tick.");
});
}
async cleanup() {
for (const [tickerName, tickerConfig] of lodash_1.default.toPairs(this.config.tickers)) {
const interval = this.tickIntervals[tickerName];
clearInterval(interval);
}
}
_handleTick(tickerName) {
this.logger.debug({ tickerName }, "Tick.");
this.store.dispatch(actions_1.incrementTick(tickerName));
const event = {
type: client_1.TickEventNames.ELAPSED,
payload: { name: tickerName, value: this.store.getState().ticks[tickerName] }
};
this.broadcastEvent(event);
}
}
TickPlugin.pluginName = client_1.TickPluginName;
exports.TickPlugin = TickPlugin;