react-adaptive-hooks
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -19,6 +19,10 @@ /* | ||
import { useHardwareConcurrency } from './'; | ||
afterEach(function() { | ||
// Reload hook for every test | ||
jest.resetModules(); | ||
}); | ||
describe('useHardwareConcurrency', () => { | ||
test(`should return window.navigator.hardwareConcurrency`, () => { | ||
const { useHardwareConcurrency } = require('./'); | ||
const { result } = renderHook(() => useHardwareConcurrency()); | ||
@@ -34,2 +38,3 @@ expect(result.current.numberOfLogicalProcessors).toBe(window.navigator.hardwareConcurrency); | ||
}); | ||
const { useHardwareConcurrency } = require('./'); | ||
const { result } = renderHook(() => useHardwareConcurrency()); | ||
@@ -46,2 +51,3 @@ | ||
}); | ||
const { useHardwareConcurrency } = require('./'); | ||
const { result } = renderHook(() => useHardwareConcurrency()); | ||
@@ -48,0 +54,0 @@ |
@@ -17,17 +17,12 @@ /* | ||
import { useState } from 'react'; | ||
let initialHardwareConcurrency; | ||
if (typeof navigator !== 'undefined' && 'hardwareConcurrency' in navigator) { | ||
initialHardwareConcurrency = { numberOfLogicalProcessors: navigator.hardwareConcurrency }; | ||
} else { | ||
initialHardwareConcurrency = { unsupported: true }; | ||
} | ||
const useHardwareConcurrency = () => { | ||
let initialHardwareConcurrency; | ||
if ('hardwareConcurrency' in navigator) { | ||
initialHardwareConcurrency = {numberOfLogicalProcessors: navigator.hardwareConcurrency}; | ||
} else { | ||
initialHardwareConcurrency = {unsupported: true}; | ||
} | ||
const [hardwareConcurrency] = useState(initialHardwareConcurrency); | ||
return { ...hardwareConcurrency }; | ||
return { ...initialHardwareConcurrency }; | ||
}; | ||
export { useHardwareConcurrency }; |
@@ -17,31 +17,25 @@ /* | ||
import { useState } from 'react'; | ||
let unsupported; | ||
if (typeof navigator !== 'undefined' && 'deviceMemory' in navigator) { | ||
unsupported = false; | ||
} else { | ||
unsupported = true; | ||
} | ||
let initialMemoryStatus; | ||
if (!unsupported) { | ||
const performanceMemory = 'memory' in performance ? performance.memory : null; | ||
initialMemoryStatus = { | ||
deviceMemory: navigator.deviceMemory, | ||
totalJSHeapSize: performanceMemory ? performanceMemory.totalJSHeapSize : null, | ||
usedJSHeapSize: performanceMemory ? performanceMemory.usedJSHeapSize : null, | ||
jsHeapSizeLimit: performanceMemory ? performanceMemory.jsHeapSizeLimit : null | ||
}; | ||
} else { | ||
initialMemoryStatus = { unsupported }; | ||
} | ||
const useMemoryStatus = () => { | ||
if ('deviceMemory' in navigator) { | ||
unsupported = false; | ||
} else { | ||
unsupported = true; | ||
} | ||
let initialMemoryStatus; | ||
if (!unsupported) { | ||
const performanceMemory = ('memory' in performance) ? performance.memory : null; | ||
initialMemoryStatus = { | ||
deviceMemory: navigator.deviceMemory, | ||
totalJSHeapSize: performanceMemory ? performanceMemory.totalJSHeapSize : null, | ||
usedJSHeapSize: performanceMemory ? performanceMemory.usedJSHeapSize : null, | ||
jsHeapSizeLimit: performanceMemory ? performanceMemory.jsHeapSizeLimit : null | ||
}; | ||
} else { | ||
initialMemoryStatus = {unsupported}; | ||
} | ||
const [memoryStatus] = useState(initialMemoryStatus); | ||
return { ...memoryStatus }; | ||
return { ...initialMemoryStatus }; | ||
}; | ||
export { useMemoryStatus }; |
@@ -19,3 +19,6 @@ /* | ||
import { useMemoryStatus } from './'; | ||
afterEach(function() { | ||
// Reload hook for every test | ||
jest.resetModules(); | ||
}); | ||
@@ -31,2 +34,3 @@ const getMemoryStatus = currentResult => ({ | ||
test(`should return "true" for unsupported case`, () => { | ||
const { useMemoryStatus } = require('./'); | ||
const { result } = renderHook(() => useMemoryStatus()); | ||
@@ -53,2 +57,3 @@ | ||
const { useMemoryStatus } = require('./'); | ||
const { result } = renderHook(() => useMemoryStatus()); | ||
@@ -55,0 +60,0 @@ |
{ | ||
"name": "react-adaptive-hooks", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Give users a great experience best suited to their device and network constraints", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -143,10 +143,10 @@ # React Adaptive Loading Hooks · ![](https://img.shields.io/github/license/GoogleChromeLabs/react-adaptive-hooks.svg) [![Build Status](https://travis-ci.org/GoogleChromeLabs/react-adaptive-hooks.svg?branch=master)](https://travis-ci.org/GoogleChromeLabs/react-adaptive-hooks) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-adaptive-hooks) | ||
function MyComponent() { | ||
const { effectiveConnectionType } = useNetworkStatus(); | ||
return ( | ||
<div> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
{ effectiveConnectionType === '4g' ? <Full /> : <Light /> } | ||
</Suspense> | ||
</div> | ||
); | ||
const { effectiveConnectionType } = useNetworkStatus(); | ||
return ( | ||
<div> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
{ effectiveConnectionType === '4g' ? <Full /> : <Light /> } | ||
</Suspense> | ||
</div> | ||
); | ||
} | ||
@@ -188,16 +188,13 @@ | ||
const Component = React.lazy(() => { | ||
return new Promise(resolve => { | ||
navigator.connection ? resolve(navigator.connection.effectiveType) : resolve(null) | ||
}).then((effectiveType) => { | ||
switch (effectiveType) { | ||
case "3g": | ||
return import(/* webpackChunkName: "light" */ "./light.js"); | ||
break; | ||
case "4g": | ||
return import(/* webpackChunkName: "full" */ "./full.js"); | ||
break; | ||
default: | ||
return import(/* webpackChunkName: "full" */ "./full.js") | ||
} | ||
}); | ||
const effectiveType = navigator.connection ? navigator.connection.effectiveType : null | ||
switch (effectiveType) { | ||
case "3g": | ||
return import(/* webpackChunkName: "light" */ "./light.js"); | ||
break; | ||
case "4g": | ||
return import(/* webpackChunkName: "full" */ "./full.js"); | ||
break; | ||
default: | ||
return import(/* webpackChunkName: "full" */ "./full.js") | ||
} | ||
}); | ||
@@ -204,0 +201,0 @@ |
@@ -17,16 +17,12 @@ /* | ||
import { useState } from 'react'; | ||
let unsupported; | ||
if ('connection' in navigator && 'saveData' in navigator.connection) { | ||
unsupported = false; | ||
} else { | ||
unsupported = true; | ||
} | ||
const saveData = unsupported ? null : navigator.connection.saveData === true; | ||
const useSaveData = () => { | ||
if ('connection' in navigator && 'saveData' in navigator.connection) { | ||
unsupported = false; | ||
} else { | ||
unsupported = true; | ||
} | ||
const initialSaveData = unsupported ? null : navigator.connection.saveData === true; | ||
const [saveData] = useState(initialSaveData); | ||
return { unsupported, saveData }; | ||
@@ -33,0 +29,0 @@ }; |
@@ -1,2 +0,1 @@ | ||
/* | ||
@@ -19,6 +18,11 @@ * Copyright 2019 Google LLC | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useSaveData } from './'; | ||
afterEach(function() { | ||
// Reload hook for every test | ||
jest.resetModules(); | ||
}); | ||
describe('useSaveData', () => { | ||
test(`should return "true" for unsupported case`, () => { | ||
const { useSaveData } = require('./'); | ||
const { result } = renderHook(() => useSaveData()); | ||
@@ -32,2 +36,3 @@ expect(result.current.unsupported).toBe(true); | ||
}; | ||
const { useSaveData } = require('./'); | ||
const { result } = renderHook(() => useSaveData()); | ||
@@ -42,2 +47,3 @@ | ||
}; | ||
const { useSaveData } = require('./'); | ||
const { result } = renderHook(() => useSaveData()); | ||
@@ -44,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38348
343
272