Skip to content

Instantly share code, notes, and snippets.

@greatbody
Created June 22, 2022 08:56
Show Gist options
  • Save greatbody/1922b633023569bcb44150dd54efcb46 to your computer and use it in GitHub Desktop.
Save greatbody/1922b633023569bcb44150dd54efcb46 to your computer and use it in GitHub Desktop.
class PageManager {
constructor() {
this.conditionalWorks = [];
this.activeConditionWork = null;
setInterval(() => {
this.conditionalWorks.forEach(cw => {
cw.conditionWork.report();
});
}, 1000);
}
stage(name) {
var conditionalWork = new ConditionalWork(name);
this.conditionalWorks.push({
name: name,
conditionWork: conditionalWork,
});
return conditionalWork;
}
off(name) {
var check = this.conditionalWorks.find(cw => cw.name === name);
if (check) {
check.conditionWork.off();
}
}
}
class ConditionalWork {
constructor(name) {
this.__name = name;
this.__conditionFn = null;
this.__actionFn = null;
this.__running = false;
this.__timeDuration = 0;
this.__intervalMethod = 'interval';
this.__handlerId = null;
}
__checkBeforeRun() {
if (!this.__conditionFn) {
throw new Error("Condition function must be defined");
}
if (!this.__timeDuration) {
throw new Error(`${this.__intervalMethod} must be defined`);
}
}
__complete() {
this.__running = false;
this.__handlerId = null;
}
asTimeout(timeout) {
if (this.__intervalMethod === 'interval') {
// initial set for timeout
if (isNaN(timeout)) {
throw new Error("Timeout must be a number");
} else {
if (timeout < 1) {
throw new Error("timeout must be greater than 0");
} else {
timeout = Math.floor(timeout);
}
}
} else {
throw new Error("Interval already set");
}
this.__timeDuration = timeout;
this.__intervalMethod = 'timeout';
return this;
}
on(conditionFn) {
if (typeof conditionFn !== "function") {
throw new Error("Condition function must be a function");
}
if (typeof this.__conditionFn === "function") {
throw new Error("Condition function already defined");
}
this.__conditionFn = conditionFn;
return this;
}
act(actionFn) {
// on must be called before act
if (typeof this.__conditionFn !== "function") {
throw new Error("Condition function must be defined first");
}
if (typeof actionFn !== "function") {
throw new Error("Action function must be a function");
}
this.__actionFn = actionFn;
return this;
}
every(interval) {
if (!isNaN(this.__timeDuration) && this.__timeDuration !== 0) {
throw new Error("Interval already set");
}
if (isNaN(interval)) {
throw new Error("Interval must be a number");
} else {
if (interval < 1) {
throw new Error("Interval must be greater than 0");
} else {
interval = Math.floor(interval);
}
}
this.__timeDuration = interval;
return this;
}
__hanlder = (cancelFn) => {
let shouldContinue = false;
if (this.__conditionFn()) {
shouldContinue = this.__actionFn();
} else {
shouldContinue = true;
}
if (shouldContinue === false) {
cancelFn(this.__handlerId);
this.__complete();
}
}
__timeoutHanlder = () => {
this.__hanlder(clearTimeout);
setTimeout(() => {
this.__timeoutHanlder();
}, this.__timeDuration);
}
__intervalHandler = () => {
if (!this.__handlerId) {
this.__handlerId = setInterval(this.__intervalHandler, this.__timeDuration);
} else {
this.__hanlder(clearInterval);
}
}
run = () => {
// check basic configurations: interval, conditionFn, isRunning
this.__checkBeforeRun();
if (this.__running) {
console.log("Work is already running");
return;
}
switch (this.__intervalMethod) {
case 'interval':
this.__intervalHandler();
this.__running = true;
break;
case 'timeout':
this.__timeoutHanlder();
this.__running = true;
default:
throw new Error("Interval method not defined");
}
}
report() {
if (this.__running) {
console.log(`${this.__name} is running`);
} else {
console.log(`${this.__name} is not running`);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment