Skip to content

Commit

Permalink
WIP-split-current-code-into-a-core-and-implementation-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wingleung committed Jan 17, 2021
1 parent 1b5b5cf commit 74626bf
Show file tree
Hide file tree
Showing 30 changed files with 7,133 additions and 2,027 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
/coverage

# production
/build
/dist
dist/

# misc
.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packages": [
"packages/*"
],
"version": "0.0.0"
}
8,734 changes: 6,723 additions & 2,011 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@
"author": "Google Chrome",
"license": "Apache-2.0",
"private": false,
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"source": "index.js",
"scripts": {
"build": "microbundle",
"dev": "microbundle watch",
"build": "lerna run build",
"prepare": "npm run build",
"test": "jest",
"test:coverage": "jest --coverage"
},
"peerDependencies": {
"react": "^16.8.0"
"test": "lerna run test"
},
"devDependencies": {
"@babel/preset-env": "^7.5.5",
"@testing-library/react-hooks": "^1.1.0",
"babel-polyfill": "^6.26.0",
"jest": "^24.8.0",
"lerna": "^3.22.1",
"microbundle": "0.11.0",
"react": "16.9.0",
"react-test-renderer": "16.9.0"
Expand Down
1 change: 1 addition & 0 deletions packages/core-adaptive-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `core-adaptive-hooks`
21 changes: 21 additions & 0 deletions packages/core-adaptive-hooks/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module.exports = {
presets: [
"@babel/preset-env"
]
};
5 changes: 3 additions & 2 deletions index.js → ...adaptive-hooks/lib/core-adaptive-hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { useNetworkStatus } from './network';
export { useCoreNetworkStatus } from './network';
export { useCoreMediaCapabilitiesDecodingInfo } from './media-capabilities';

export { useSaveData } from './save-data';
export { useMemoryStatus } from './memory';
export { useHardwareConcurrency } from './hardware-concurrency';
export { useMediaCapabilitiesDecodingInfo } from './media-capabilities';
File renamed without changes.
39 changes: 39 additions & 0 deletions packages/core-adaptive-hooks/lib/media-capabilities/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const isMediaCapabilitiesSupported = () => typeof navigator !== 'undefined' && 'mediaCapabilities' in navigator;

const useCoreMediaCapabilitiesDecodingInfo = async mediaDecodingConfig => {
const supported = isMediaCapabilitiesSupported();

if (!supported) {
return {};
}

let decodingInfo;

try {
decodingInfo = await navigator
.mediaCapabilities
.decodingInfo(mediaDecodingConfig);
} catch (e) {
console.error(e);
}

return decodingInfo
};

export { isMediaCapabilitiesSupported, useCoreMediaCapabilitiesDecodingInfo };
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'babel-polyfill';

const mediaDecodingConfig = {
type: 'file',
audio: {
contentType: 'audio/mp3',
channels: 2,
bitrate: 132700,
samplerate: 5200
}
};

const mediaCapabilitiesMapper = {
'audio/mp3': {
powerEfficient: true,
smooth: true,
supported: true
}
};

describe('useMediaCapabilitiesDecodingInfo', () => {
test('should return {} on unsupported platforms', async () => {
const { useCoreMediaCapabilitiesDecodingInfo } = require('./');

const result = await useCoreMediaCapabilitiesDecodingInfo();

expect(result).toStrictEqual({});
});

test('should return {} on unsupported platforms with config given', async () => {
const { useCoreMediaCapabilitiesDecodingInfo } = require('./');
const result = await useCoreMediaCapabilitiesDecodingInfo(mediaDecodingConfig);

expect(result).toStrictEqual({});
});

test('should return mediaCapabilitiesInfo for given media configuration', async () => {
const originalError = console.error;
console.error = jest.fn();

const mockDecodingInfo = jest.fn().mockImplementation(() => Promise.resolve({
...mediaCapabilitiesMapper[mediaDecodingConfig.audio.contentType]
}));

global.navigator.mediaCapabilities = {
decodingInfo: mockDecodingInfo
};

const { useCoreMediaCapabilitiesDecodingInfo } = require('./');

try {
const result = await useCoreMediaCapabilitiesDecodingInfo(mediaDecodingConfig);

expect(result.powerEfficient).toEqual(true);
expect(result.smooth).toEqual(true);
expect(result.supported).toEqual(true);
} finally {
console.error = originalError;
}
});
});
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions packages/core-adaptive-hooks/lib/network/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const isNetworkStatusSupported = () => typeof navigator !== 'undefined' && 'connection' in navigator && 'effectiveType' in navigator.connection;

const useCoreNetworkStatus = () => {
const navigatorConnection = navigator.connection;

return isNetworkStatusSupported() ?
{
effectiveConnectionType: navigatorConnection.effectiveType
} :
{}
};

export { isNetworkStatusSupported, useCoreNetworkStatus };
39 changes: 39 additions & 0 deletions packages/core-adaptive-hooks/lib/network/network.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { useCoreNetworkStatus } from './';

describe('useCoreNetworkStatus', () => {
test(`should return "{}" for unsupported case`, () => {
const result = useCoreNetworkStatus();

expect(result).toStrictEqual({});
});

test('should return 4g of effectiveConnectionType', () => {
global.navigator.connection = {
effectiveType: '4g'
};

const result = useCoreNetworkStatus();

const expectedConnection = {
effectiveConnectionType: '4g'
};

expect(result).toStrictEqual(expectedConnection);
});
});
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions packages/core-adaptive-hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "core-adaptive-hooks",
"version": "0.0.1",
"description": "core adaptive hooks",
"keywords": [
"adaptive-hooks"
],
"author": "Google Chrome",
"homepage": "https://github.com/GoogleChromeLabs/react-adaptive-hooks#readme",
"license": "Apache-2.0",
"extends": "../../babel.config.js",
"main": "dist/index.js",
"module": "dist/index.module.js",
"unpkg": "dist/index.umd.js",
"source": "lib/core-adaptive-hooks.js",
"directories": {
"lib": "lib"
},
"files": [
"lib"
],
"devDependencies": {
"@babel/preset-env": "^7.5.5",
"babel-polyfill": "^6.26.0",
"jest": "^24.8.0",
"lerna": "^3.22.1",
"microbundle": "0.11.0"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/GoogleChromeLabs/react-adaptive-hooks.git"
},
"scripts": {
"build": "microbundle",
"dev": "microbundle watch",
"prepare": "npm run build",
"test": "jest",
"test:coverage": "jest --coverage"
},
"bugs": {
"url": "https://github.com/GoogleChromeLabs/react-adaptive-hooks/issues"
}
}
1 change: 1 addition & 0 deletions packages/react-adaptive-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `react-adaptive-hooks`
21 changes: 21 additions & 0 deletions packages/react-adaptive-hooks/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module.exports = {
presets: [
"@babel/preset-env"
]
};
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { renderHook, act } from '@testing-library/react-hooks';

import { useNetworkStatus } from './';
import { useNetworkStatus } from './index';

describe('useNetworkStatus', () => {
const map = {};
Expand Down
6 changes: 6 additions & 0 deletions packages/react-adaptive-hooks/lib/react-adaptive-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { useSaveData } from '../../core-adaptive-hooks/lib/save-data';
export { useMemoryStatus } from '../../core-adaptive-hooks/lib/memory';
export { useHardwareConcurrency } from '../../core-adaptive-hooks/lib/hardware-concurrency';

export { useNetworkStatus } from './network';
export { useMediaCapabilitiesDecodingInfo } from './media-capabilities';
Loading

0 comments on commit 74626bf

Please sign in to comment.