Last active
February 24, 2024 17:32
-
-
Save BoonHianLim/5cd4b2732d2489a0573108abae301a62 to your computer and use it in GitHub Desktop.
Reminder Function For Browser (Polling-based implementation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const LocalReminder = { | |
functionMap: new Map(), | |
forceStopAllReminder: false, | |
suppressAllReminderWarning: false, | |
getAllReminders: () => { | |
const functionList = [] | |
for (var value of LocalReminder.functionMap.values()) { | |
functionList.push(value) | |
} | |
return functionList | |
}, | |
getReminder: (functionName, supressWarning = false ) => { | |
const reminderFunc = LocalReminder.functionMap.get(functionName) | |
if (!reminderFunc) { | |
if (!supressWarning) { | |
console.log("Function not found.") | |
} | |
return null | |
} | |
return reminderFunc | |
}, | |
createReminder: (functionName, warningMsg, loopRequirement, triggerRequirement, earlyExitRequirement = () => {return false}, second = 5, overwrite = false) => { | |
const reminderFunc = LocalReminder.getReminder(functionName, true ); | |
if (reminderFunc && !overwrite) { | |
console.log("there is existing reminder function with this name. if you want to overwrite, set overwrite to true") | |
return null | |
} | |
const secondInMs = 1000 | |
const createdFunction = async () => { | |
LocalReminder.forceStopAllReminder = false | |
createdFunction.forceStopAllReminder = false | |
createdFunction.suppressReminderWarning = false | |
var hasAlert = false | |
while (createdFunction.loopRequirement() && !LocalReminder.forceStopAllReminder && !createdFunction.forceStopAllReminder) { | |
if (!LocalReminder.suppressAllReminderWarning && !createdFunction.suppressReminderWarning) { | |
console.log(`${createdFunction.reminderName()} is running`) | |
} | |
const delay = ms => new Promise(res => setTimeout(res, ms)); | |
if (createdFunction.earlyExitRequirement()) { | |
break | |
} | |
if (createdFunction.triggerRequirement()) { | |
alert(createdFunction.warningMsg); | |
hasAlert = true | |
break | |
} | |
await delay(createdFunction.second * secondInMs) | |
} | |
console.log("The reminder function,", createdFunction.reminderName(), "has ended"); | |
if (LocalReminder.forceStopAllReminder || createdFunction.forceStopAllReminder) { | |
console.log(`${createdFunction.reminderName()} reminder exit due to forced stop`) | |
return | |
} | |
if (!hasAlert) { | |
alert(`${createdFunction.reminderName()} reminder terminate without being triggered`); | |
} | |
} | |
createdFunction.reminderName = () => { return functionName } | |
createdFunction.warningMsg = warningMsg | |
createdFunction.loopRequirement = loopRequirement | |
createdFunction.triggerRequirement = triggerRequirement | |
createdFunction.earlyExitRequirement = earlyExitRequirement | |
createdFunction.second = second | |
LocalReminder.functionMap.set(functionName, createdFunction) | |
return createdFunction | |
}, | |
startReminder: (functionName) => { | |
const reminderFunc = LocalReminder.getReminder(functionName, true ) | |
if (!reminderFunc) { | |
console.log("Function not found, ") | |
return | |
} | |
reminderFunc() | |
console.log(`reminder ${reminderFunc.reminderName()} has started`) | |
}, | |
cancelReminder: (functionName) => { | |
const reminderFunc = LocalReminder.getReminder(functionName, true ) | |
if (!reminderFunc) { | |
console.log("Function not found, ") | |
return | |
} | |
reminderFunc.forceStopAllReminder = true | |
console.log(`set the reminderFunc ${reminderFunc.reminderName()} forceStop to true`) | |
}, | |
cancelAllReminders: () => { | |
const failToReturnFuncList = [] | |
const reminders = LocalReminder.getAllReminders(); | |
for (var reminder of reminders) { | |
reminder.forceStopAllReminder = true | |
} | |
console.log(`set all reminders forceStop to true`) | |
}, | |
deleteReminder: (functionName) => { | |
const reminderFunc = LocalReminder.getReminder(functionName, true) | |
if (!reminderFunc) { | |
console.log("Function not found, ") | |
return null | |
} | |
LocalReminder.cancelReminder(functionName) | |
LocalReminder.functionMap.delete(functionName) | |
const deletedFunc = LocalReminder.getReminder(functionName, true) | |
if (deletedFunc) { | |
console.log(`fail to remove ${deletedFunc.reminderName()}, returning for reference`) | |
return deletedFunc | |
} | |
return null | |
}, | |
deleteAllReminders: () => { | |
const failToReturnFuncList = [] | |
const reminders = LocalReminder.getAllReminders(); | |
for (var reminder of reminders) { | |
reminder.forceStopAllReminder = true | |
const failToReturn = LocalReminder.deleteReminder(reminder.reminderName()) | |
if (failToReturn) { | |
console.log(`some function fail to return, such as ${failToReturn.reminderName()}`) | |
failToReturnFuncList.push(failToReturn) | |
} | |
} | |
return failToReturnFuncList.length === 0 ? null : failToReturnFuncList | |
}, | |
getAllRemindersName: () => { | |
const functionNameList = [] | |
for (var key of LocalReminder.functionMap.keys()) { | |
functionNameList.push(key) | |
} | |
return functionNameList | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy this to console and enjoy!