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

View File

@ -357,7 +357,9 @@ export class SpaceSync {
} }
syncCandidates(files: FileMeta[]): FileMeta[] { 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", "yaml": "https://deno.land/std/encoding/yaml.ts",
"@capacitor/core": "https://esm.sh/@capacitor/core@4.6.2", "@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 { SilverBulletHooks } from "../common/manifest.ts";
import { System } from "../plugos/system.ts"; import { System } from "../plugos/system.ts";
import { BuiltinSettings } from "../web/types.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 { CapacitorSpacePrimitives } from "./spaces/capacitor_space_primitives.ts";
import { AssetBundlePlugSpacePrimitives } from "../common/spaces/asset_bundle_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"); const db = new CapacitorDb("data.db");
await db.init(); await db.init();
system.addHook(new CronHook()); const cronHook = new CronHook(system);
system.addHook(cronHook);
// for store // for store
await ensureStoreTable(db, "store"); await ensureStoreTable(db, "store");
@ -76,11 +78,11 @@ safeRun(async () => {
indexSyscalls, indexSyscalls,
); );
const serverSpace = new Space(spacePrimitives); const space = new Space(spacePrimitives);
serverSpace.watch(); space.watch();
const settings = await ensureAndLoadSettings( const settings = await ensureAndLoadSettings(
serverSpace, space,
false, false,
) as BuiltinSettings; ) as BuiltinSettings;
@ -98,7 +100,7 @@ safeRun(async () => {
console.log("Booting..."); console.log("Booting...");
const editor = new Editor( const editor = new Editor(
serverSpace, space,
system, system,
eventHook, eventHook,
document.getElementById("sb-root")!, document.getElementById("sb-root")!,
@ -107,4 +109,19 @@ safeRun(async () => {
); );
await editor.init(); 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 { Capacitor, CapacitorHttp } from "@capacitor/core";
export { App as CapacitorApp } from '@capacitor/app';
export { Directory, Encoding, Filesystem } from "@capacitor/filesystem"; export { Directory, Encoding, Filesystem } from "@capacitor/filesystem";
export type { WriteFileResult } from "@capacitor/filesystem"; export type { WriteFileResult } from "@capacitor/filesystem";

View File

@ -350,7 +350,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 6; CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = Z92J6WM6X8; DEVELOPMENT_TEAM = Z92J6WM6X8;
INFOPLIST_FILE = App/Info.plist; INFOPLIST_FILE = App/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = SilverBullet; INFOPLIST_KEY_CFBundleDisplayName = SilverBullet;
@ -376,7 +376,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 6; CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = Z92J6WM6X8; DEVELOPMENT_TEAM = Z92J6WM6X8;
INFOPLIST_FILE = App/Info.plist; INFOPLIST_FILE = App/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = SilverBullet; INFOPLIST_KEY_CFBundleDisplayName = SilverBullet;

View File

@ -1,6 +1,3 @@
[build] [build]
publish = "website_build" publish = "website_build"
command = "./scripts/build_demo.sh" command = "./scripts/build_website.sh"
[context.demo]
command = "./scripts/build_demo.sh"

View File

@ -2,29 +2,39 @@ import { Hook, Manifest } from "../types.ts";
import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js"; import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js";
import { safeRun } from "../util.ts"; import { safeRun } from "../util.ts";
import { System } from "../system.ts"; import { System } from "../system.ts";
import { timingSafeEqual } from "https://deno.land/std@0.152.0/crypto/timing_safe_equal";
export type CronHookT = { export type CronHookT = {
cron?: string | string[]; cron?: string | string[];
}; };
export class CronHook implements Hook<CronHookT> { export class CronHook implements Hook<CronHookT> {
tasks: Cron[] = [];
constructor(private system: System<CronHookT>) {
}
apply(system: System<CronHookT>): void { apply(system: System<CronHookT>): void {
let tasks: Cron[] = []; this.system = system;
system.on({ system.on({
plugLoaded: () => { plugLoaded: () => {
reloadCrons(); this.reloadCrons();
}, },
plugUnloaded() { plugUnloaded: () => {
reloadCrons(); this.reloadCrons();
}, },
}); });
reloadCrons(); this.reloadCrons();
}
function reloadCrons() { stop() {
tasks.forEach((task) => task.stop()); this.tasks.forEach((task) => task.stop());
tasks = []; this.tasks = [];
for (const plug of system.loadedPlugs.values()) { }
reloadCrons() {
this.stop();
for (const plug of this.system.loadedPlugs.values()) {
if (!plug.manifest) { if (!plug.manifest) {
continue; continue;
} }
@ -40,7 +50,7 @@ export class CronHook implements Hook<CronHookT> {
? functionDef.cron ? functionDef.cron
: [functionDef.cron]; : [functionDef.cron];
for (const cronDef of crons) { for (const cronDef of crons) {
tasks.push( this.tasks.push(
new Cron(cronDef, () => { new Cron(cronDef, () => {
// console.log("Now acting on cron", cronDef); // console.log("Now acting on cron", cronDef);
safeRun(async () => { safeRun(async () => {
@ -56,7 +66,6 @@ export class CronHook implements Hook<CronHookT> {
} }
} }
} }
}
validateManifest(manifest: Manifest<CronHookT>): string[] { validateManifest(manifest: Manifest<CronHookT>): string[] {
const errors: string[] = []; const errors: string[] = [];

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 #!/bin/bash
echo "Install Deno"
echo "Now building SilverBullet bundle"
curl -fsSL https://deno.land/install.sh | sh curl -fsSL https://deno.land/install.sh | sh
export PATH=~/.deno/bin:$PATH export PATH=~/.deno/bin:$PATH
echo "Generating version number..." echo "Generating version number..."
echo "export const version = '$(git rev-parse HEAD)';" > version.ts 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 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..." echo "Bundling..."
deno task bundle deno task bundle
cp dist/silverbullet.js website_build/ 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); const s = await Deno.stat(fullPath);
allFiles.push({ allFiles.push({
name: fullPath.substring(rootDir.length + 1), name: fullPath.substring(rootDir.length + 1),
lastModified: 0, lastModified: Date.now(),
contentType: mime.getType(fullPath) || "application/octet-stream", contentType: mime.getType(fullPath) || "application/octet-stream",
size: s.size, size: s.size,
perm: "rw", perm: "rw",