@npaw/forge-web 9.7.0 → 9.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/dist/index.cjs CHANGED
@@ -146,7 +146,9 @@ var CAPTURE_PLAN_BITS = {
146
146
  // kernel never sets them and they must never be reassigned so existing
147
147
  // bits never renumber.
148
148
  SECTIONS: 1 << 13,
149
- ATTRIBUTION: 1 << 14
149
+ ATTRIBUTION: 1 << 14,
150
+ /** The operator enabled a motion switch, so location may be observed. */
151
+ LOCATION: 1 << 15
150
152
  };
151
153
  function isCapturePlanEnabled(bits, bit) {
152
154
  return (bits & bit) !== 0;
@@ -3983,6 +3985,166 @@ function boundaryDiagnostic(error) {
3983
3985
  };
3984
3986
  }
3985
3987
 
3988
+ // src/connection-type.ts
3989
+ function connectionTypeFromNetworkType(type) {
3990
+ switch (type) {
3991
+ case "wifi":
3992
+ return "wifi";
3993
+ case "cellular":
3994
+ return "cellular";
3995
+ case "ethernet":
3996
+ return "ethernet";
3997
+ case "bluetooth":
3998
+ case "wimax":
3999
+ case "other":
4000
+ return "other";
4001
+ case "none":
4002
+ case "unknown":
4003
+ return "unknown";
4004
+ default:
4005
+ return "unknown";
4006
+ }
4007
+ }
4008
+ var ForgeConnectionTypeObserver = class {
4009
+ constructor(publish, connection = readNavigatorConnection()) {
4010
+ this.publish = publish;
4011
+ this.connection = connection;
4012
+ }
4013
+ /**
4014
+ * Publishes the current value and starts listening. Starting twice is a
4015
+ * no-op, so a host that re-initialises does not stack listeners.
4016
+ */
4017
+ start() {
4018
+ var _a;
4019
+ const connection = this.connection;
4020
+ if (!connection || this.listener) {
4021
+ return;
4022
+ }
4023
+ this.report(connectionTypeFromNetworkType(connection.type));
4024
+ const listener = () => {
4025
+ this.report(connectionTypeFromNetworkType(connection.type));
4026
+ };
4027
+ try {
4028
+ (_a = connection.addEventListener) == null ? void 0 : _a.call(connection, "change", listener);
4029
+ this.listener = listener;
4030
+ } catch (e) {
4031
+ }
4032
+ }
4033
+ /**
4034
+ * Stops listening and clears the published value, so a torn-down instance
4035
+ * cannot keep a stale class attached to later events.
4036
+ */
4037
+ stop() {
4038
+ var _a, _b;
4039
+ const listener = this.listener;
4040
+ this.listener = void 0;
4041
+ this.lastPublished = void 0;
4042
+ if (!listener) {
4043
+ return;
4044
+ }
4045
+ try {
4046
+ (_b = (_a = this.connection) == null ? void 0 : _a.removeEventListener) == null ? void 0 : _b.call(_a, "change", listener);
4047
+ } catch (e) {
4048
+ }
4049
+ this.publish(void 0);
4050
+ }
4051
+ /** Publishes only genuine changes; `change` also fires on speed estimates. */
4052
+ report(observed) {
4053
+ if (this.lastPublished === observed) {
4054
+ return;
4055
+ }
4056
+ this.lastPublished = observed;
4057
+ this.publish(observed);
4058
+ }
4059
+ };
4060
+ function readNavigatorConnection() {
4061
+ const navigatorRef = globalThis.navigator;
4062
+ return navigatorRef == null ? void 0 : navigatorRef.connection;
4063
+ }
4064
+
4065
+ // src/location.ts
4066
+ var ForgeLocationObserver = class {
4067
+ constructor(publish, environment = readGeolocationEnvironment()) {
4068
+ this.starting = false;
4069
+ /** Set by stop(), so a start() awaiting its permission query gives up. */
4070
+ this.stopped = false;
4071
+ this.publish = publish;
4072
+ this.environment = environment;
4073
+ }
4074
+ /**
4075
+ * Starts observing if - and only if - the permission is already `granted`.
4076
+ * Starting twice is a no-op.
4077
+ */
4078
+ async start() {
4079
+ const environment = this.environment;
4080
+ if (!environment || this.watchId !== void 0 || this.starting) {
4081
+ return;
4082
+ }
4083
+ this.starting = true;
4084
+ this.stopped = false;
4085
+ try {
4086
+ const state = await environment.queryPermission();
4087
+ if (state !== "granted") {
4088
+ return;
4089
+ }
4090
+ if (this.watchId !== void 0 || this.stopped) {
4091
+ return;
4092
+ }
4093
+ this.watchId = environment.watchPosition(
4094
+ (position) => this.publish(fixFromPosition(position)),
4095
+ () => {
4096
+ }
4097
+ );
4098
+ } catch (e) {
4099
+ } finally {
4100
+ this.starting = false;
4101
+ }
4102
+ }
4103
+ /** Stops observing. Safe to call when never started. */
4104
+ stop() {
4105
+ this.stopped = true;
4106
+ const watchId = this.watchId;
4107
+ this.watchId = void 0;
4108
+ if (watchId === void 0 || !this.environment) {
4109
+ return;
4110
+ }
4111
+ try {
4112
+ this.environment.clearWatch(watchId);
4113
+ } catch (e) {
4114
+ }
4115
+ }
4116
+ };
4117
+ function fixFromPosition(position) {
4118
+ return {
4119
+ latitude: position.coords.latitude,
4120
+ longitude: position.coords.longitude,
4121
+ accuracyMetres: position.coords.accuracy,
4122
+ timestampMs: position.timestamp
4123
+ };
4124
+ }
4125
+ function readGeolocationEnvironment() {
4126
+ const navigatorRef = globalThis.navigator;
4127
+ const geolocation = navigatorRef == null ? void 0 : navigatorRef.geolocation;
4128
+ const permissions = navigatorRef == null ? void 0 : navigatorRef.permissions;
4129
+ if (!geolocation || !(permissions == null ? void 0 : permissions.query)) {
4130
+ return void 0;
4131
+ }
4132
+ return {
4133
+ queryPermission: async () => {
4134
+ const status = await permissions.query({ name: "geolocation" });
4135
+ return status == null ? void 0 : status.state;
4136
+ },
4137
+ watchPosition: (onFix, onError) => geolocation.watchPosition(onFix, onError, {
4138
+ // Coarse and cheap: this answers "moving or not", which needs neither
4139
+ // navigation-grade accuracy nor its battery cost.
4140
+ enableHighAccuracy: false,
4141
+ maximumAge: 3e4,
4142
+ timeout: 6e4
4143
+ }),
4144
+ clearWatch: (id) => geolocation.clearWatch(id)
4145
+ };
4146
+ }
4147
+
3986
4148
  // src/video.ts
3987
4149
  var PlayerAdapter = class {
3988
4150
  attach(video) {
@@ -4579,6 +4741,7 @@ var _NPAWForge = class _NPAWForge {
4579
4741
  this.videos = /* @__PURE__ */ new Map();
4580
4742
  }
4581
4743
  static async create(options) {
4744
+ var _a, _b;
4582
4745
  const { bridge } = await ForgeRuntimeBridge.create(options);
4583
4746
  const forge = new _NPAWForge(bridge);
4584
4747
  if (options.startSessionOnInitialize) {
@@ -4586,9 +4749,23 @@ var _NPAWForge = class _NPAWForge {
4586
4749
  }
4587
4750
  forge.captureHandle = installCapture(forge, options.capture);
4588
4751
  forge.capturePlanUnsubscribe = bridge.onCapturePlan((plan) => {
4589
- var _a, _b;
4590
- (_b = (_a = forge.captureHandle) == null ? void 0 : _a.applyPlan) == null ? void 0 : _b.call(_a, plan);
4752
+ var _a2, _b2;
4753
+ (_b2 = (_a2 = forge.captureHandle) == null ? void 0 : _a2.applyPlan) == null ? void 0 : _b2.call(_a2, plan);
4591
4754
  });
4755
+ if ((_a = options.autoLocation) != null ? _a : true) {
4756
+ forge.locationObserver = new ForgeLocationObserver((fix) => {
4757
+ void forge.recordLocationFix(fix);
4758
+ });
4759
+ bridge.onCapturePlan((plan) => {
4760
+ forge.applyLocationPlan(plan);
4761
+ });
4762
+ }
4763
+ if ((_b = options.autoConnectionType) != null ? _b : true) {
4764
+ forge.connectionTypeObserver = new ForgeConnectionTypeObserver((connectionType) => {
4765
+ void forge.setAutoConnectionType(connectionType);
4766
+ });
4767
+ forge.connectionTypeObserver.start();
4768
+ }
4592
4769
  return forge;
4593
4770
  }
4594
4771
  onEvent(listener) {
@@ -4754,6 +4931,40 @@ var _NPAWForge = class _NPAWForge {
4754
4931
  setLogLevel(level) {
4755
4932
  return this.runtime.process({ type: "setLogLevel", level });
4756
4933
  }
4934
+ /**
4935
+ * Reports the browser's current connection class to the kernel. Called by the
4936
+ * automatic observer; a `connectionType` set through video metadata keeps
4937
+ * precedence over it.
4938
+ */
4939
+ setAutoConnectionType(connectionType) {
4940
+ return this.runtime.process({ type: "setAutoConnectionType", connectionType });
4941
+ }
4942
+ /** Forwards a location fix to the kernel, which owns every threshold. */
4943
+ recordLocationFix(fix) {
4944
+ return this.runtime.process({
4945
+ type: "recordLocationFix",
4946
+ latitude: fix.latitude,
4947
+ longitude: fix.longitude,
4948
+ accuracyM: fix.accuracyMetres,
4949
+ timestampMs: fix.timestampMs
4950
+ });
4951
+ }
4952
+ /**
4953
+ * Starts or stops location observation to match the capture plan. Nothing is
4954
+ * observed - not even the permission state - until an operator enables one
4955
+ * of the two motion switches.
4956
+ */
4957
+ applyLocationPlan(plan) {
4958
+ const observer = this.locationObserver;
4959
+ if (!observer) {
4960
+ return;
4961
+ }
4962
+ if ((plan.bits & CAPTURE_PLAN_BITS.LOCATION) !== 0) {
4963
+ void observer.start();
4964
+ } else {
4965
+ observer.stop();
4966
+ }
4967
+ }
4757
4968
  applicationForeground(foreground) {
4758
4969
  return this.runtime.process({
4759
4970
  type: "applicationForeground",
@@ -4807,7 +5018,7 @@ var _NPAWForge = class _NPAWForge {
4807
5018
  return (_b = video == null ? void 0 : video.removeAdAdapter()) != null ? _b : false;
4808
5019
  }
4809
5020
  async destroy() {
4810
- var _a, _b;
5021
+ var _a, _b, _c, _d;
4811
5022
  for (const video of this.videos.values()) {
4812
5023
  video.detach();
4813
5024
  }
@@ -4817,6 +5028,10 @@ var _NPAWForge = class _NPAWForge {
4817
5028
  this.capturePlanUnsubscribe = void 0;
4818
5029
  (_b = this.captureHandle) == null ? void 0 : _b.dispose();
4819
5030
  this.captureHandle = void 0;
5031
+ (_c = this.connectionTypeObserver) == null ? void 0 : _c.stop();
5032
+ this.connectionTypeObserver = void 0;
5033
+ (_d = this.locationObserver) == null ? void 0 : _d.stop();
5034
+ this.locationObserver = void 0;
4820
5035
  return this.runtime.destroy();
4821
5036
  }
4822
5037
  whenIdle() {