From bd152dd297f9dd9208da97322bd571a083d8ee44 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 25 Jan 2023 18:29:47 +0100 Subject: [PATCH] Disable background jobs on mobile --- common/spaces/space.ts | 12 ++- common/spaces/sync.ts | 4 +- import_map.json | 3 +- mobile/boot.ts | 29 +++++-- mobile/deps.ts | 2 + mobile/ios/App/App.xcodeproj/project.pbxproj | 4 +- netlify.toml | 5 +- plugos/hooks/cron.ts | 79 +++++++++++--------- scripts/build_demo.sh | 43 ----------- scripts/build_website.sh | 37 +++++++-- scripts/build_website_old.sh | 20 +++++ scripts/generate_fs_list.ts | 2 +- 12 files changed, 139 insertions(+), 101 deletions(-) delete mode 100755 scripts/build_demo.sh create mode 100755 scripts/build_website_old.sh diff --git a/common/spaces/space.ts b/common/spaces/space.ts index bfdc675..b922f21 100644 --- a/common/spaces/space.ts +++ b/common/spaces/space.ts @@ -24,6 +24,7 @@ export class Space extends EventEmitter watchedPages = new Set(); private initialPageListLoad = true; private saving = false; + watchInterval?: number; constructor(readonly spacePrimitives: SpacePrimitives) { super(); @@ -89,7 +90,10 @@ export class Space extends EventEmitter } watch() { - setInterval(() => { + if (this.watchInterval) { + clearInterval(this.watchInterval); + } + this.watchInterval = setInterval(() => { safeRun(async () => { if (this.saving) { return; @@ -109,6 +113,12 @@ export class Space extends EventEmitter this.updatePageList().catch(console.error); } + unwatch() { + if (this.watchInterval) { + clearInterval(this.watchInterval); + } + } + async deletePage(name: string): Promise { await this.getPageMeta(name); // Check if page exists, if not throws Error await this.spacePrimitives.deleteFile(`${name}.md`); diff --git a/common/spaces/sync.ts b/common/spaces/sync.ts index b626c44..d52c456 100644 --- a/common/spaces/sync.ts +++ b/common/spaces/sync.ts @@ -357,7 +357,9 @@ export class SpaceSync { } syncCandidates(files: FileMeta[]): FileMeta[] { - return files.filter((f) => !f.name.startsWith("_plug/")); + return files.filter((f) => + !f.name.startsWith("_plug/") && f.lastModified > 0 + ); } } diff --git a/import_map.json b/import_map.json index 9b63e4f..6ba4a2e 100644 --- a/import_map.json +++ b/import_map.json @@ -20,6 +20,7 @@ "yaml": "https://deno.land/std/encoding/yaml.ts", "@capacitor/core": "https://esm.sh/@capacitor/core@4.6.2", - "@capacitor/filesystem": "https://esm.sh/@capacitor/filesystem@4.1.4?external=@capacitor/core" + "@capacitor/filesystem": "https://esm.sh/@capacitor/filesystem@4.1.4?external=@capacitor/core", + "@capacitor/app": "https://esm.sh/@capacitor/app@4.1.1?external=@capacitor/core" } } diff --git a/mobile/boot.ts b/mobile/boot.ts index cc43c8f..34cb8b3 100644 --- a/mobile/boot.ts +++ b/mobile/boot.ts @@ -6,7 +6,7 @@ import { PageNamespaceHook } from "../common/hooks/page_namespace.ts"; import { SilverBulletHooks } from "../common/manifest.ts"; import { System } from "../plugos/system.ts"; import { BuiltinSettings } from "../web/types.ts"; -import { CapacitorHttp, Directory } from "./deps.ts"; +import { Directory, CapacitorApp } from "./deps.ts"; import { CapacitorSpacePrimitives } from "./spaces/capacitor_space_primitives.ts"; import { AssetBundlePlugSpacePrimitives } from "../common/spaces/asset_bundle_space_primitives.ts"; @@ -48,7 +48,9 @@ safeRun(async () => { const db = new CapacitorDb("data.db"); await db.init(); - system.addHook(new CronHook()); + const cronHook = new CronHook(system); + + system.addHook(cronHook); // for store await ensureStoreTable(db, "store"); @@ -76,11 +78,11 @@ safeRun(async () => { indexSyscalls, ); - const serverSpace = new Space(spacePrimitives); - serverSpace.watch(); + const space = new Space(spacePrimitives); + space.watch(); const settings = await ensureAndLoadSettings( - serverSpace, + space, false, ) as BuiltinSettings; @@ -98,7 +100,7 @@ safeRun(async () => { console.log("Booting..."); const editor = new Editor( - serverSpace, + space, system, eventHook, document.getElementById("sb-root")!, @@ -107,4 +109,19 @@ safeRun(async () => { ); await editor.init(); + + CapacitorApp.addListener("pause", () => { + console.log("PAUSING APP-------") + space.unwatch(); + cronHook.stop(); + }); + CapacitorApp.addListener("resume", () => { + console.log("RESUMING APP-------") + space.watch(); + cronHook.reloadCrons(); + }); + + CapacitorApp.addListener("appRestoredResult", (result) => { + console.log("Restored state", result) + }) }); diff --git a/mobile/deps.ts b/mobile/deps.ts index f0cdebb..aba31e4 100644 --- a/mobile/deps.ts +++ b/mobile/deps.ts @@ -1,3 +1,5 @@ export { Capacitor, CapacitorHttp } from "@capacitor/core"; +export { App as CapacitorApp } from '@capacitor/app'; + export { Directory, Encoding, Filesystem } from "@capacitor/filesystem"; export type { WriteFileResult } from "@capacitor/filesystem"; diff --git a/mobile/ios/App/App.xcodeproj/project.pbxproj b/mobile/ios/App/App.xcodeproj/project.pbxproj index dbb8fb2..7f9b099 100644 --- a/mobile/ios/App/App.xcodeproj/project.pbxproj +++ b/mobile/ios/App/App.xcodeproj/project.pbxproj @@ -350,7 +350,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 6; + CURRENT_PROJECT_VERSION = 7; DEVELOPMENT_TEAM = Z92J6WM6X8; INFOPLIST_FILE = App/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = SilverBullet; @@ -376,7 +376,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 6; + CURRENT_PROJECT_VERSION = 7; DEVELOPMENT_TEAM = Z92J6WM6X8; INFOPLIST_FILE = App/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = SilverBullet; diff --git a/netlify.toml b/netlify.toml index 62aec2c..b2c901c 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,6 +1,3 @@ [build] publish = "website_build" -command = "./scripts/build_demo.sh" - -[context.demo] - command = "./scripts/build_demo.sh" +command = "./scripts/build_website.sh" \ No newline at end of file diff --git a/plugos/hooks/cron.ts b/plugos/hooks/cron.ts index 1507f8c..37b5d54 100644 --- a/plugos/hooks/cron.ts +++ b/plugos/hooks/cron.ts @@ -2,57 +2,66 @@ import { Hook, Manifest } from "../types.ts"; import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js"; import { safeRun } from "../util.ts"; import { System } from "../system.ts"; +import { timingSafeEqual } from "https://deno.land/std@0.152.0/crypto/timing_safe_equal"; export type CronHookT = { cron?: string | string[]; }; export class CronHook implements Hook { + tasks: Cron[] = []; + constructor(private system: System) { + } + apply(system: System): void { - let tasks: Cron[] = []; + this.system = system; system.on({ plugLoaded: () => { - reloadCrons(); + this.reloadCrons(); }, - plugUnloaded() { - reloadCrons(); + plugUnloaded: () => { + this.reloadCrons(); }, }); - reloadCrons(); + this.reloadCrons(); + } - function reloadCrons() { - tasks.forEach((task) => task.stop()); - tasks = []; - for (const plug of system.loadedPlugs.values()) { - if (!plug.manifest) { + stop() { + this.tasks.forEach((task) => task.stop()); + this.tasks = []; + } + + reloadCrons() { + this.stop(); + for (const plug of this.system.loadedPlugs.values()) { + if (!plug.manifest) { + continue; + } + for ( + const [name, functionDef] of Object.entries( + plug.manifest.functions, + ) + ) { + if (!functionDef.cron) { continue; } - for ( - const [name, functionDef] of Object.entries( - plug.manifest.functions, - ) - ) { - if (!functionDef.cron) { - continue; - } - const crons = Array.isArray(functionDef.cron) - ? functionDef.cron - : [functionDef.cron]; - for (const cronDef of crons) { - tasks.push( - new Cron(cronDef, () => { - // console.log("Now acting on cron", cronDef); - safeRun(async () => { - try { - await plug.invoke(name, [cronDef]); - } catch (e: any) { - console.error("Execution of cron function failed", e); - } - }); - }), - ); - } + const crons = Array.isArray(functionDef.cron) + ? functionDef.cron + : [functionDef.cron]; + for (const cronDef of crons) { + this.tasks.push( + new Cron(cronDef, () => { + // console.log("Now acting on cron", cronDef); + safeRun(async () => { + try { + await plug.invoke(name, [cronDef]); + } catch (e: any) { + console.error("Execution of cron function failed", e); + } + }); + }), + ); } } } diff --git a/scripts/build_demo.sh b/scripts/build_demo.sh deleted file mode 100755 index 3193247..0000000 --- a/scripts/build_demo.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -echo "Install Deno" -curl -fsSL https://deno.land/install.sh | sh -export PATH=~/.deno/bin:$PATH - -echo "Generating version number..." -echo "export const version = '$(git rev-parse HEAD)';" > version.ts - -echo "Building silver bullet" -deno task build -echo "Cleaning website build dir" -rm -rf website_build -mkdir -p website_build/fs/_plug -echo "Copying silverbullet runtime files" -cp -r dist_bundle/web/* website_build/ - -echo "And all plugs" -cp -r dist_bundle/_plug/* website_build/fs/_plug/ -echo "And additional ones" -curl https://raw.githubusercontent.com/silverbulletmd/silverbullet-mermaid/main/mermaid.plug.json > website_build/fs/_plug/mermaid.plug.json -echo "But remove some plugs" -rm -rf website_build/fs/_plug/{directive,plugmd,publish,share}.plug.json -#echo "Copying netlify config files" -#cp website/{_redirects,_headers} website_build/ - -echo "Copying website content into fs/" -cp -r website/* website_build/fs/ -rm website_build/fs/{_redirects,_headers} - -echo "Copy website files another time into the root" -cp -r website/* website_build/ - -echo "Generating file listing" -deno run -A scripts/generate_fs_list.ts > website_build/index.json - -echo > website_build/empty.md - -echo "Bundling..." -deno task bundle -cp dist/silverbullet.js website_build/ -cp dist_bundle/web/global.plug.json website_build/ -cp web/images/logo.ico website_build/ \ No newline at end of file diff --git a/scripts/build_website.sh b/scripts/build_website.sh index ed3278b..3193247 100755 --- a/scripts/build_website.sh +++ b/scripts/build_website.sh @@ -1,20 +1,43 @@ #!/bin/bash - -echo "Now building SilverBullet bundle" +echo "Install Deno" curl -fsSL https://deno.land/install.sh | sh export PATH=~/.deno/bin:$PATH echo "Generating version number..." echo "export const version = '$(git rev-parse HEAD)';" > version.ts -echo "Building..." -deno task build -deno task install +echo "Building silver bullet" +deno task build +echo "Cleaning website build dir" rm -rf website_build -silverbullet publish --index -o website_build website +mkdir -p website_build/fs/_plug +echo "Copying silverbullet runtime files" +cp -r dist_bundle/web/* website_build/ + +echo "And all plugs" +cp -r dist_bundle/_plug/* website_build/fs/_plug/ +echo "And additional ones" +curl https://raw.githubusercontent.com/silverbulletmd/silverbullet-mermaid/main/mermaid.plug.json > website_build/fs/_plug/mermaid.plug.json +echo "But remove some plugs" +rm -rf website_build/fs/_plug/{directive,plugmd,publish,share}.plug.json +#echo "Copying netlify config files" +#cp website/{_redirects,_headers} website_build/ + +echo "Copying website content into fs/" +cp -r website/* website_build/fs/ +rm website_build/fs/{_redirects,_headers} + +echo "Copy website files another time into the root" +cp -r website/* website_build/ + +echo "Generating file listing" +deno run -A scripts/generate_fs_list.ts > website_build/index.json + +echo > website_build/empty.md echo "Bundling..." deno task bundle cp dist/silverbullet.js website_build/ -cp dist_bundle/web/global.plug.json website_build/ \ No newline at end of file +cp dist_bundle/web/global.plug.json website_build/ +cp web/images/logo.ico website_build/ \ No newline at end of file diff --git a/scripts/build_website_old.sh b/scripts/build_website_old.sh new file mode 100755 index 0000000..ed3278b --- /dev/null +++ b/scripts/build_website_old.sh @@ -0,0 +1,20 @@ +#!/bin/bash + + +echo "Now building SilverBullet bundle" +curl -fsSL https://deno.land/install.sh | sh +export PATH=~/.deno/bin:$PATH + +echo "Generating version number..." +echo "export const version = '$(git rev-parse HEAD)';" > version.ts +echo "Building..." +deno task build +deno task install + +rm -rf website_build +silverbullet publish --index -o website_build website + +echo "Bundling..." +deno task bundle +cp dist/silverbullet.js website_build/ +cp dist_bundle/web/global.plug.json website_build/ \ No newline at end of file diff --git a/scripts/generate_fs_list.ts b/scripts/generate_fs_list.ts index 0adbb12..f0574b1 100644 --- a/scripts/generate_fs_list.ts +++ b/scripts/generate_fs_list.ts @@ -18,7 +18,7 @@ for await ( const s = await Deno.stat(fullPath); allFiles.push({ name: fullPath.substring(rootDir.length + 1), - lastModified: 0, + lastModified: Date.now(), contentType: mime.getType(fullPath) || "application/octet-stream", size: s.size, perm: "rw",