Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

motion-sensors-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

motion-sensors-polyfill - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

src/geolocation-sensor.js

321

motion-sensors.js
// @ts-check
const slot = window["__sensor__"] = Symbol("__sensor__");
import { __sensor__, Sensor, defineReadonlyProperties } from "./sensor.js";
let orientation = {};
const slot = __sensor__;
let orientation;
// @ts-ignore
if (screen.orientation) {
// @ts-ignore
orientation = screen.orientation;

@@ -11,2 +15,3 @@ } else if (screen.msOrientation) {

} else {
orientation = {};
Object.defineProperty(orientation, "angle", {

@@ -17,129 +22,5 @@ get: () => { return (window.orientation || 0) }

function defineProperties(target, descriptions) {
for (const property in descriptions) {
Object.defineProperty(target, property, {
configurable: true,
value: descriptions[property]
});
}
}
class EventTarget {
constructor() {
const eventTarget = document.createDocumentFragment();
this.addEventListener = (...args) => {
return eventTarget.addEventListener(...args);
}
this.removeEventListener = (...args) => {
return eventTarget.removeEventListener(...args);
}
this.dispatchEvent = (event) => {
defineProperties(event, { currentTarget: this, target: this });
const methodName = `on${event.type}`;
if (typeof this[methodName] == "function") {
this[methodName](event);
}
const retValue = eventTarget.dispatchEvent(event);
defineProperties(event, { currentTarget: null, target: null });
return retValue;
}
}
}
function defineReadonlyProperties(target, slot, descriptions) {
const propertyBag = target[slot] || (target[slot] = new WeakMap);
for (const property in descriptions) {
propertyBag[property] = descriptions[property];
Object.defineProperty(target, property, {
get: () => propertyBag[property]
});
}
}
function defineOnEventListener(target, name) {
Object.defineProperty(target, `on${name}`, {
enumerable: true,
configurable: false,
writable: true,
value: null
});
}
const SensorState = {
ERROR: 0,
IDLE: 1,
ACTIVATING: 2,
ACTIVE: 3,
}
export class Sensor extends EventTarget {
constructor(options) {
super();
this[slot] = new WeakMap;
defineOnEventListener(this, "reading");
defineOnEventListener(this, "activate");
defineOnEventListener(this, "error");
defineReadonlyProperties(this, slot, {
activated: false,
hasReading: false,
timestamp: null
})
this[slot].setState = (value) => {
switch(value) {
case SensorState.ERROR: {
let error = new SensorErrorEvent("error", {
error: new DOMException("Could not connect to a sensor")
});
this.dispatchEvent(error);
this.stop(); // Moves to IDLE state.
break;
}
case SensorState.IDLE: {
this[slot].activated = false;
this[slot].hasReading = false;
this[slot].timestamp = null;
break;
}
case SensorState.ACTIVATING: {
break;
}
case SensorState.ACTIVE: {
let activate = new Event("activate");
this[slot].activated = true;
this.dispatchEvent(activate);
break;
}
}
};
this[slot].frequency = null;
if (window && window.parent != window.top) {
throw new DOMException("Only instantiable in a top-level browsing context", "SecurityError");
}
if (options && typeof(options.frequency) == "number") {
if (options.frequency > 60) {
this.frequency = options.frequency;
}
}
}
start() { }
stop() { }
}
const DeviceOrientationMixin = (superclass, ...eventNames) => class extends superclass {
constructor(...args) {
// @ts-ignore
super(args);

@@ -153,14 +34,10 @@

}
}
start() {
super.start();
this[slot].setState(SensorState.ACTIVATING);
window.addEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
this[slot].activateCallback = () => {
window.addEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
stop() {
super.stop();
this[slot].setState(SensorState.IDLE);
window.removeEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
this[slot].deactivateCallback = () => {
window.removeEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
}

@@ -251,26 +128,30 @@ };

function worldToScreen(quaternion) {
return !quaternion ? null :
rotateQuaternionByAxisAngle(
quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
);
}
class SensorErrorEvent extends Event {
constructor(type, errorEventInitDict) {
super(type, errorEventInitDict);
// @ts-ignore
export const RelativeOrientationSensor = window.RelativeOrientationSensor ||
class RelativeOrientationSensor extends DeviceOrientationMixin(Sensor, "deviceorientation") {
constructor(options = {}) {
super(options);
if (!errorEventInitDict || !errorEventInitDict.error instanceof DOMException) {
throw TypeError(
"Failed to construct 'SensorErrorEvent':" +
"2nd argument much contain 'error' property"
);
switch (options.coordinateSystem || 'world') {
case 'screen':
Object.defineProperty(this, "quaternion", {
get: () => worldToScreen(this[slot].quaternion)
});
break;
case 'world':
default:
Object.defineProperty(this, "quaternion", {
get: () => this[slot].quaternion
});
}
Object.defineProperty(this, "error", {
configurable: false,
writable: false,
value: errorEventInitDict.error
});
}
};
export const RelativeOrientationSensor = window.RelativeOrientationSensor ||
class RelativeOrientationSensor extends DeviceOrientationMixin(Sensor, "deviceorientation") {
constructor(options) {
super(options);
this[slot].handleEvent = event => {

@@ -284,3 +165,3 @@ // If there is no sensor we will get values equal to null.

// the choice.
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -290,3 +171,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -306,20 +187,7 @@

Object.defineProperty(this, "quaternion", {
get: () => {
// Screen adjusted quaternion.
return !this[slot].quaternion ? null :
rotateQuaternionByAxisAngle(
this[slot].quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
)
}
});
this[slot].deactivateCallback = () => {
this[slot].quaternion = null;
}
}
stop() {
super.stop();
this[slot].quaternion = null;
}
populateMatrix(mat) {

@@ -330,8 +198,22 @@ toMat4FromQuat(mat, this.quaternion);

// @ts-ignore
export const AbsoluteOrientationSensor = window.AbsoluteOrientationSensor ||
class AbsoluteOrientationSensor extends DeviceOrientationMixin(
Sensor, "deviceorientationabsolute", "deviceorientation") {
constructor(options) {
constructor(options = {}) {
super(options);
switch (options.coordinateSystem || 'world') {
case 'screen':
Object.defineProperty(this, "quaternion", {
get: () => worldToScreen(this[slot].quaternion)
});
break;
case 'world':
default:
Object.defineProperty(this, "quaternion", {
get: () => this[slot].quaternion
});
}
this[slot].handleEvent = event => {

@@ -347,3 +229,3 @@ // If absolute is set, or webkitCompassHeading exists,

// the alpha, beta and gamma attributes set to null.
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -353,3 +235,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -371,20 +253,7 @@

Object.defineProperty(this, "quaternion", {
get: () => {
// Screen adjusted quaternion.
return !this[slot].quaternion ? null :
rotateQuaternionByAxisAngle(
this[slot].quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
)
}
});
this[slot].deactivateCallback = () => {
this[slot].quaternion = null;
}
}
stop() {
super.stop();
this[slot].quaternion = null;
}
populateMatrix(mat) {

@@ -395,2 +264,3 @@ toMat4FromQuat(mat, this.quaternion);

// @ts-ignore
export const Gyroscope = window.Gyroscope ||

@@ -403,3 +273,3 @@ class Gyroscope extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.rotationRate.alpha === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -409,3 +279,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -428,12 +298,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const Accelerometer = window.Accelerometer ||

@@ -446,3 +316,3 @@ class Accelerometer extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.accelerationIncludingGravity.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -452,3 +322,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -471,12 +341,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const LinearAccelerationSensor = window.LinearAccelerationSensor ||

@@ -489,3 +359,3 @@ class LinearAccelerationSensor extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.acceleration.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -495,3 +365,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -514,12 +384,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const GravitySensor = window.GravitySensor ||

@@ -532,3 +402,3 @@ class GravitySensor extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.acceleration.x === null || event.accelerationIncludingGravity.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -538,3 +408,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -557,10 +427,9 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
{
"name": "motion-sensors-polyfill",
"version": "0.2.0",
"version": "0.3.0",
"description": "A polyfill for the motion sensors based on the W3C Generic Sensor API",

@@ -15,3 +15,3 @@ "main": "motion-sensors.js",

"scripts": {
"build": "cp src/motion-sensors.js .",
"build": "cp src/*.js .",
"test": "npm run build",

@@ -18,0 +18,0 @@ "checksize": "uglifyjs motion-sensors.js -mc --toplevel | gzip -9 | wc -c"

// @ts-check
const slot = window["__sensor__"] = Symbol("__sensor__");
import { __sensor__, Sensor, defineReadonlyProperties } from "./sensor.js";
let orientation = {};
const slot = __sensor__;
let orientation;
// @ts-ignore
if (screen.orientation) {
// @ts-ignore
orientation = screen.orientation;

@@ -11,2 +15,3 @@ } else if (screen.msOrientation) {

} else {
orientation = {};
Object.defineProperty(orientation, "angle", {

@@ -17,129 +22,5 @@ get: () => { return (window.orientation || 0) }

function defineProperties(target, descriptions) {
for (const property in descriptions) {
Object.defineProperty(target, property, {
configurable: true,
value: descriptions[property]
});
}
}
class EventTarget {
constructor() {
const eventTarget = document.createDocumentFragment();
this.addEventListener = (...args) => {
return eventTarget.addEventListener(...args);
}
this.removeEventListener = (...args) => {
return eventTarget.removeEventListener(...args);
}
this.dispatchEvent = (event) => {
defineProperties(event, { currentTarget: this, target: this });
const methodName = `on${event.type}`;
if (typeof this[methodName] == "function") {
this[methodName](event);
}
const retValue = eventTarget.dispatchEvent(event);
defineProperties(event, { currentTarget: null, target: null });
return retValue;
}
}
}
function defineReadonlyProperties(target, slot, descriptions) {
const propertyBag = target[slot] || (target[slot] = new WeakMap);
for (const property in descriptions) {
propertyBag[property] = descriptions[property];
Object.defineProperty(target, property, {
get: () => propertyBag[property]
});
}
}
function defineOnEventListener(target, name) {
Object.defineProperty(target, `on${name}`, {
enumerable: true,
configurable: false,
writable: true,
value: null
});
}
const SensorState = {
ERROR: 0,
IDLE: 1,
ACTIVATING: 2,
ACTIVE: 3,
}
export class Sensor extends EventTarget {
constructor(options) {
super();
this[slot] = new WeakMap;
defineOnEventListener(this, "reading");
defineOnEventListener(this, "activate");
defineOnEventListener(this, "error");
defineReadonlyProperties(this, slot, {
activated: false,
hasReading: false,
timestamp: null
})
this[slot].setState = (value) => {
switch(value) {
case SensorState.ERROR: {
let error = new SensorErrorEvent("error", {
error: new DOMException("Could not connect to a sensor")
});
this.dispatchEvent(error);
this.stop(); // Moves to IDLE state.
break;
}
case SensorState.IDLE: {
this[slot].activated = false;
this[slot].hasReading = false;
this[slot].timestamp = null;
break;
}
case SensorState.ACTIVATING: {
break;
}
case SensorState.ACTIVE: {
let activate = new Event("activate");
this[slot].activated = true;
this.dispatchEvent(activate);
break;
}
}
};
this[slot].frequency = null;
if (window && window.parent != window.top) {
throw new DOMException("Only instantiable in a top-level browsing context", "SecurityError");
}
if (options && typeof(options.frequency) == "number") {
if (options.frequency > 60) {
this.frequency = options.frequency;
}
}
}
start() { }
stop() { }
}
const DeviceOrientationMixin = (superclass, ...eventNames) => class extends superclass {
constructor(...args) {
// @ts-ignore
super(args);

@@ -153,14 +34,10 @@

}
}
start() {
super.start();
this[slot].setState(SensorState.ACTIVATING);
window.addEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
this[slot].activateCallback = () => {
window.addEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
stop() {
super.stop();
this[slot].setState(SensorState.IDLE);
window.removeEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
this[slot].deactivateCallback = () => {
window.removeEventListener(this[slot].eventName, this[slot].handleEvent, { capture: true });
}
}

@@ -251,26 +128,30 @@ };

function worldToScreen(quaternion) {
return !quaternion ? null :
rotateQuaternionByAxisAngle(
quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
);
}
class SensorErrorEvent extends Event {
constructor(type, errorEventInitDict) {
super(type, errorEventInitDict);
// @ts-ignore
export const RelativeOrientationSensor = window.RelativeOrientationSensor ||
class RelativeOrientationSensor extends DeviceOrientationMixin(Sensor, "deviceorientation") {
constructor(options = {}) {
super(options);
if (!errorEventInitDict || !errorEventInitDict.error instanceof DOMException) {
throw TypeError(
"Failed to construct 'SensorErrorEvent':" +
"2nd argument much contain 'error' property"
);
switch (options.coordinateSystem || 'world') {
case 'screen':
Object.defineProperty(this, "quaternion", {
get: () => worldToScreen(this[slot].quaternion)
});
break;
case 'world':
default:
Object.defineProperty(this, "quaternion", {
get: () => this[slot].quaternion
});
}
Object.defineProperty(this, "error", {
configurable: false,
writable: false,
value: errorEventInitDict.error
});
}
};
export const RelativeOrientationSensor = window.RelativeOrientationSensor ||
class RelativeOrientationSensor extends DeviceOrientationMixin(Sensor, "deviceorientation") {
constructor(options) {
super(options);
this[slot].handleEvent = event => {

@@ -284,3 +165,3 @@ // If there is no sensor we will get values equal to null.

// the choice.
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -290,3 +171,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -306,20 +187,7 @@

Object.defineProperty(this, "quaternion", {
get: () => {
// Screen adjusted quaternion.
return !this[slot].quaternion ? null :
rotateQuaternionByAxisAngle(
this[slot].quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
)
}
});
this[slot].deactivateCallback = () => {
this[slot].quaternion = null;
}
}
stop() {
super.stop();
this[slot].quaternion = null;
}
populateMatrix(mat) {

@@ -330,8 +198,22 @@ toMat4FromQuat(mat, this.quaternion);

// @ts-ignore
export const AbsoluteOrientationSensor = window.AbsoluteOrientationSensor ||
class AbsoluteOrientationSensor extends DeviceOrientationMixin(
Sensor, "deviceorientationabsolute", "deviceorientation") {
constructor(options) {
constructor(options = {}) {
super(options);
switch (options.coordinateSystem || 'world') {
case 'screen':
Object.defineProperty(this, "quaternion", {
get: () => worldToScreen(this[slot].quaternion)
});
break;
case 'world':
default:
Object.defineProperty(this, "quaternion", {
get: () => this[slot].quaternion
});
}
this[slot].handleEvent = event => {

@@ -347,3 +229,3 @@ // If absolute is set, or webkitCompassHeading exists,

// the alpha, beta and gamma attributes set to null.
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -353,3 +235,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -371,20 +253,7 @@

Object.defineProperty(this, "quaternion", {
get: () => {
// Screen adjusted quaternion.
return !this[slot].quaternion ? null :
rotateQuaternionByAxisAngle(
this[slot].quaternion,
[0, 0, 1],
- orientation.angle * Math.PI / 180
)
}
});
this[slot].deactivateCallback = () => {
this[slot].quaternion = null;
}
}
stop() {
super.stop();
this[slot].quaternion = null;
}
populateMatrix(mat) {

@@ -395,2 +264,3 @@ toMat4FromQuat(mat, this.quaternion);

// @ts-ignore
export const Gyroscope = window.Gyroscope ||

@@ -403,3 +273,3 @@ class Gyroscope extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.rotationRate.alpha === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -409,3 +279,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -428,12 +298,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const Accelerometer = window.Accelerometer ||

@@ -446,3 +316,3 @@ class Accelerometer extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.accelerationIncludingGravity.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -452,3 +322,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -471,12 +341,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const LinearAccelerationSensor = window.LinearAccelerationSensor ||

@@ -489,3 +359,3 @@ class LinearAccelerationSensor extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.acceleration.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -495,3 +365,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -514,12 +384,12 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
// @ts-ignore
export const GravitySensor = window.GravitySensor ||

@@ -532,3 +402,3 @@ class GravitySensor extends DeviceOrientationMixin(Sensor, "devicemotion") {

if (event.acceleration.x === null || event.accelerationIncludingGravity.x === null) {
this[slot].setState(SensorState.ERROR);
this[slot].notifyError("Could not connect to a sensor", "NotReadableError");
return;

@@ -538,3 +408,3 @@ }

if (!this[slot].activated) {
this[slot].setState(SensorState.ACTIVE);
this[slot].notifyActivatedState();
}

@@ -557,10 +427,9 @@

});
}
stop() {
super.stop();
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
this[slot].deactivateCallback = () => {
this[slot].x = null;
this[slot].y = null;
this[slot].z = null;
}
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc