feat: Move API to exposed class

This commit is contained in:
Callum Loh 2023-03-26 14:40:06 +01:00
parent 218d3feed9
commit 4ab9202d48
3 changed files with 43 additions and 9 deletions

View file

@ -33,13 +33,13 @@ An API is exposed to add user functions via Javascript. The name is treated as a
An instance of the Obsidian app is passed to all user function as the first and only paramater.
To clear locks for jobs added via the API you can add a job with the corrosponding name and then pass the name to the `clearJobLock(name: string)`
To clear locks for jobs added via the API you can add a job with the corrosponding name and then pass the name to the `clearJobLock(name: string)` function also in the API.
Example of a user function
```javascript
const cron = app.plugins.plugins.cron;
const cron = app.plugins.plugins.cron.api;
cron.addCronJob('addCronJob', "* * * * 3", {"enableMobile": true}, function(app){console.log('Job has ran!')});

24
src/api.ts Normal file
View file

@ -0,0 +1,24 @@
import { CronJobFunc, CronJobSettings } from "./job"
import Cron from "./main"
export default class CronAPI {
static instance: CronAPI
public static get(plugin: Cron) {
return {
addCronJob(name: string, frequency: string, settings: CronJobSettings, job: CronJobFunc) {
return plugin.addCronJob(name, frequency, settings, job)
},
runJob(name: string) {
return plugin.runJob(name)
},
clearJobLock(name: string) {
return plugin.clearJobLock(name)
},
getJob(name: string) {
return plugin.getJob(name)
},
}
}
}

View file

@ -4,6 +4,7 @@ import { CronLock } from './lockManager';
import CronLockManager from './lockManager';
import CronSettingTab from './settings';
import SyncChecker from './syncChecker';
import CronAPI from './api';
export interface CronSettings {
cronInterval: number;
@ -36,6 +37,7 @@ export default class Cron extends Plugin {
syncChecker: SyncChecker
lockManager: CronLockManager
jobs: { [key: string]: Job }
api: CronAPI
async onload() {
console.log("Loading Obsidian CRON!");
@ -50,7 +52,7 @@ export default class Cron extends Plugin {
// load our cronjobs
this.loadCrons()
this.loadInterval()
this.api = CronAPI.get(this)
this.app.workspace.onLayoutReady(() => {
if(this.settings.runOnStartup) {
this.runCron()
@ -76,21 +78,29 @@ export default class Cron extends Plugin {
}
public addCronJob(name: string, frequency: string, settings: CronJobSettings, job: CronJobFunc) {
if(this.jobs[name]) throw new Error("CRON Job already exists")
const existingJob = this.getJob(name)
if(existingJob) throw new Error("CRON Job already exists")
this.jobs[name] = new Job(name, name, job, frequency, settings, this.app, this, this.syncChecker)
}
public async runJob(name: string) {
if(!this.jobs[name]) throw new Error("CRON Job doesn't exist")
await this.jobs[name].runJob()
const job = this.getJob(name)
if(!job) throw new Error("CRON Job doesn't exist")
await job.runJob()
}
public clearJobLock(name: string) {
if(!this.jobs[name]) throw new Error("CRON Job doesn't exist")
const job = this.getJob(name)
if(!job) throw new Error("CRON Job doesn't exist")
job.clearJobLock()
}
this.jobs[name].clearJobLock()
public getJob(name: string): Job | null {
for (const [, job] of Object.entries(this.jobs)) {
if(job.name == name) return job
}
return null
}
public onunload() {