In my clockface index.js I am registering this event
Answered! Go to the Best Answer.
@mkostersitzI think I see your problem.
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
I've never seen EventListeners used for `document`. Normally for HTML elements that you pull in like you did here
let appVersionLabel = document.getElementById("app-version");
From your original post, you said you want it to run everytime the clock loads (i.e. returning from another app)? If I'm not mistaken, you could do:
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
loadCurrentState("app activate");
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
I believe this should give you what you are wanting.
First thing that comes to mind is this: Is the event listener on the highest level possible (same level as your imports)?
For example:
import clock from "clock";
import document from "document";
.
.
.
// Functions and whatnot
.
.
.
// Register for activation event
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
Best Answerit is right after the imports in my code.
Best Answer@mkostersitz Alright, would you mind sharing from the beginning of your imports to the end of the event listener in question then?
I have a suspicion as to what the problem is, but seeing the code up til that part would confirm
Best Answer
Sorry for the delay here it is
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
appbit.onunload = saveCurrentState("app unload")This causes saveCurrentState to be executed immediately, and whatever it returns is then assigned to onunload. Presumably saveCurrentState returns nothing (ie, undefined), so that's what gets assigned to onunload. This means that no handler (listener) is being set.
You probably want something like
appbit.onunload = saveCurrentStatewhich will assign the function to onunload, rather than what the function returns.
f() causes f to be executed; f without () is a reference or link to the function which can be executed subsequently.
@mkostersitzI think I see your problem.
document.addEventListener("activate", (evt) => {
loadCurrentState("app activate");
});
I've never seen EventListeners used for `document`. Normally for HTML elements that you pull in like you did here
let appVersionLabel = document.getElementById("app-version");
From your original post, you said you want it to run everytime the clock loads (i.e. returning from another app)? If I'm not mistaken, you could do:
import * as document from "document";
import clock from "clock";
import * as userActivity from "user-activity";
import * as powerUtil from "./battery.js";
import * as heartMonitor from "./hrm.js";
import { me as appbit } from "appbit";
import * as weather from "./weather.js";
import * as fs from "fs";
import { launchApp as launchApp} from "system";
import * as logger from "../common/logger.js";
import * as cache from "./cacheManager.js";
import * as global from "../common/globals.js";
import * as timeDate from "./timeDate.js";
import { units as units } from "user-settings";
import * as messaging from "messaging";
import * as util from "../common/utils";
import { memory as memory } from "system";
import { vibration as vibration} from "haptics";
// Register for the unload event
appbit.onunload = saveCurrentState("app unload");
// Register for activation event
loadCurrentState("app activate");
// version
let appVersionLabel = document.getElementById("app-version");
if(global.settings.isDebug) {appVersionLabel.text = global.settings.appVersion;}
I believe this should give you what you are wanting.