1
0

Disable background jobs on mobile

This commit is contained in:
Zef Hemel 2023-01-25 18:29:47 +01:00
parent ca8b8d9a84
commit bd152dd297
12 changed files with 139 additions and 101 deletions

View File

@ -24,6 +24,7 @@ export class Space extends EventEmitter<SpaceEvents>
watchedPages = new Set<string>();
private initialPageListLoad = true;
private saving = false;
watchInterval?: number;
constructor(readonly spacePrimitives: SpacePrimitives) {
super();
@ -89,7 +90,10 @@ export class Space extends EventEmitter<SpaceEvents>
}
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<SpaceEvents>
this.updatePageList().catch(console.error);
}
unwatch() {
if (this.watchInterval) {
clearInterval(this.watchInterval);
}
}
async deletePage(name: string): Promise<void> {
await this.getPageMeta(name); // Check if page exists, if not throws Error
await this.spacePrimitives.deleteFile(`${name}.md`);

View File

@ -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
);
}
}

View File

@ -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"
}
}

View File

@ -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)
})
});

View File

@ -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";

View File

@ -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;

View File

@ -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"

View File

@ -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<CronHookT> {
tasks: Cron[] = [];
constructor(private system: System<CronHookT>) {
}
apply(system: System<CronHookT>): 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);
}
});
}),
);
}
}
}

View File

@ -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/

View File

@ -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/
cp dist_bundle/web/global.plug.json website_build/
cp web/images/logo.ico website_build/

20
scripts/build_website_old.sh Executable file
View File

@ -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/

View File

@ -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",