├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .prettierrc.yaml ├── README.md ├── babel.config.js ├── index.js ├── package.json ├── src ├── cpu.js ├── memory.js ├── network.js └── savedata.js ├── test ├── cpu.spec.js ├── memory.spec.js ├── network.spec.js └── savedata.spec.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = tabs 7 | indent_size = 2 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | 11 | [test/**/expected.css] 12 | insert_final_newline = false 13 | 14 | [{package.json,.travis.yml,.eslintrc.json}] 15 | indent_style = space 16 | indent_size = 2 17 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "rules": { 4 | "indent": ["error", "tab", { "SwitchCase": 1 }], 5 | "semi": [2, "always"], 6 | "keyword-spacing": [2, { "before": true, "after": true }], 7 | "space-before-blocks": [2, "always"], 8 | "no-mixed-spaces-and-tabs": [2, "smart-tabs"], 9 | "no-cond-assign": 0, 10 | "no-unused-vars": 2, 11 | "object-shorthand": [2, "always"], 12 | "no-const-assign": 2, 13 | "no-class-assign": 2, 14 | "no-this-before-super": 2, 15 | "no-var": 2, 16 | "no-unreachable": 2, 17 | "valid-typeof": 2, 18 | "quote-props": [2, "as-needed"], 19 | "one-var": [2, "never"], 20 | "prefer-arrow-callback": 2, 21 | "prefer-const": [2, { "destructuring": "all" }], 22 | "arrow-spacing": 2, 23 | "no-inner-declarations": 0 24 | }, 25 | 26 | "env": { 27 | "es6": true, 28 | "browser": true, 29 | "node": true, 30 | "jest": true 31 | }, 32 | "extends": [ 33 | "eslint:recommended", 34 | "plugin:import/errors", 35 | "plugin:import/warnings" 36 | ], 37 | "plugins": ["import"], 38 | "parserOptions": { 39 | "ecmaVersion": 9, 40 | "sourceType": "module", 41 | "ecmaFeatures": { 42 | "experimentalObjectRestSpread": true 43 | } 44 | }, 45 | "settings": { 46 | "import/resolver": { 47 | "node": { 48 | "extensions": [".js"] 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # macOS stuff 3 | .DS_Store 4 | .AppleDouble 5 | .LSOverride 6 | 7 | # duh 8 | /node_modules/ 9 | 10 | # logs 11 | logs 12 | *.log 13 | npm-debug.log* 14 | yarn-debug.log* 15 | yarn-error.log* 16 | 17 | # testy stuff 18 | coverage 19 | 20 | # editor 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | -------------------------------------------------------------------------------- /.prettierrc.yaml: -------------------------------------------------------------------------------- 1 | useTabs: true 2 | singleQuote: true 3 | trailingComma: 'es5' 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svelte-adaptive-sensors 2 | 3 | Sensors to help you deliver adaptive sensors to users depending on their network-type, memory, cpu, and saveData settings. A svelte version of [`react-adaptive-hooks`](https://github.com/GoogleChromeLabs/react-adaptive-hooks/) although there are very few differences between the two libraries currently. 4 | 5 | This library makes it easier to get information about a user's device, settings and network and alter your app's behaviour using these metrics. 6 | 7 | [Check it out liiiiiiive.](https://svelte.dev/repl/e00898da2de14e40a337a6ea5ac060f8?version=3.14.0) 8 | 9 | Currently 4 APIs are supported: 10 | 11 | - [Network via effective connection type](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType) 12 | - [Data Saver preferences](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/saveData) 13 | - [Device memory](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory) 14 | - [Logical CPU cores](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorConcurrentHardware/hardwareConcurrency) 15 | 16 | ## Install 17 | 18 | With `npm`: 19 | 20 | ```bash 21 | npm install --save-dev svelte-adaptive-sensors 22 | ``` 23 | 24 | Or `yarn`: 25 | 26 | ```bash 27 | yarn add --dev svelte-adaptive-sensors 28 | ``` 29 | 30 | ## Use 31 | 32 | Import them: 33 | 34 | ```js 35 | import { 36 | getCpuInfo, 37 | getMemoryInfo, 38 | getNetworkInfo, 39 | getSaveDataInfo, 40 | } from 'svelte-adaptive-sensors'; 41 | ``` 42 | 43 | And then use them. 44 | 45 | ## API 46 | 47 | All functions (or stores, in the case of `getNetworkInfo`) return an object with a `supported` property. This value is `false` if the API is not supported and `true` if it is. 48 | 49 | ### `getCpuInfo` 50 | 51 | A simple function that returns information about a user's logical processor cores using the `navigator.hardwareConcurrency` API. 52 | 53 | This value is static and will never change. User don't routinely swap out their CPU when using an app and if they do then I wnat to hear about it. 54 | 55 | ```ts 56 | getCpuInfo() = { 57 | supported: Boolean, 58 | processors:? Number 59 | }; 60 | ``` 61 | 62 | If `supported` is `false` then the `processors` property will not be present. 63 | 64 | ```svelte 65 | 70 | 71 | {#if supported && processors > 4} 72 |