Skip to content

Commit

Permalink
Merge branch 'devel-2'
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Feb 10, 2022
2 parents 5c0d15b + b245719 commit f2ffc53
Show file tree
Hide file tree
Showing 37 changed files with 1,618 additions and 1,258 deletions.
6 changes: 4 additions & 2 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Object {
"render": [Function],
"themeType": [Function],
},
"CodeSplit": [Function],
"Dropdown": Object {
"$$typeof": Symbol(react.forward_ref),
"render": [Function],
Expand Down Expand Up @@ -113,6 +112,7 @@ Object {
"themeType": [Function],
},
"ScalableRect": [Function],
"Semaphore": [Function],
"ThemeProvider": [Function],
"Throbber": Object {
"$$typeof": Symbol(react.forward_ref),
Expand Down Expand Up @@ -149,6 +149,7 @@ Object {
},
"newBarrier": [Function],
"server": null,
"splitComponent": [Function],
"themed": [Function],
"time": [Function],
"useAsyncCollection": [Function],
Expand Down Expand Up @@ -266,7 +267,6 @@ Object {
"render": [Function],
"themeType": [Function],
},
"CodeSplit": [Function],
"Dropdown": Object {
"$$typeof": Symbol(react.forward_ref),
"render": [Function],
Expand Down Expand Up @@ -466,6 +466,7 @@ Object {
"themeType": [Function],
},
"ScalableRect": [Function],
"Semaphore": [Function],
"ThemeProvider": [Function],
"Throbber": Object {
"$$typeof": Symbol(react.forward_ref),
Expand Down Expand Up @@ -507,6 +508,7 @@ Object {
},
"newBarrier": [Function],
"server": [Function],
"splitComponent": [Function],
"themed": [Function],
"time": [Function],
"useAsyncCollection": [Function],
Expand Down
18 changes: 12 additions & 6 deletions __tests__/config/babel/__snapshots__/node-ssr.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
Expand All @@ -33,7 +35,6 @@ Object {
"generateScopedName": [Function],
},
],
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down Expand Up @@ -96,13 +97,14 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
],
"@babel/transform-runtime",
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down Expand Up @@ -149,7 +151,9 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
Expand All @@ -167,7 +171,6 @@ Object {
"generateScopedName": [Function],
},
],
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down Expand Up @@ -230,7 +233,9 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
Expand All @@ -248,7 +253,6 @@ Object {
"generateScopedName": [Function],
},
],
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down Expand Up @@ -311,13 +315,14 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
],
"@babel/transform-runtime",
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down Expand Up @@ -364,7 +369,9 @@ Object {
"./src",
],
"transformFunctions": Array [
"requireWeak",
"resolveWeak",
"webpack.requireWeak",
"webpack.resolveWeak",
],
},
Expand All @@ -382,7 +389,6 @@ Object {
"generateScopedName": [Function],
},
],
"@babel/syntax-dynamic-import",
Array [
"@dr.pogodin/transform-assets",
Object {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route, Routes } from 'react-router-dom';
import CodeSplit from 'components/CodeSplit';
import splitComponent from 'utils/splitComponent';

function Component() {
return <div>Hello World!</div>;
Expand All @@ -13,18 +13,14 @@ function InnerRouter() {
);
}

// NOTE: This is not how <CodeSplit> should be used to actually split the code
// during Webpack build, but this simplistic code is enough to test its behavior
// alongside React Router use.
function Split() {
return (
<CodeSplit
chunkName="split"
getComponentAsync={() => InnerRouter}
getComponentServer={() => InnerRouter}
/>
);
}
// Note: this is not exactly how splitComponent() should be used to actually
// split the code during Webpack build, but this simplistic code is enough
// to test its behavior alongside React Router use.
const Split = splitComponent({
chunkName: 'split',
getClientSide: () => InnerRouter,
serverSide: InnerRouter,
});

export function RoutingScene1() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* This is a sample component, doing a code split.
*/

import { useGlobalState } from '@dr.pogodin/react-global-state';

import splitComponent from 'utils/splitComponent';
import { requireWeak } from 'utils/webpack';

function Placeholder() {
return <div>PLACEHOLDER!</div>;
}

const SampleComponent1 = splitComponent({
chunkName: 'sample-component-1',
getClientSide: () => import(
/* webpackChunkName: 'sample-component-1' */ './SampleComponent'
),
});

const SampleComponent2 = splitComponent({
chunkName: 'sample-component-2',
getClientSide: () => import(
/* webpackChunkName: 'sample-component-2' */ './SampleComponent'
),
placeholder: Placeholder,
});

const SampleComponent3 = splitComponent({
chunkName: 'sample-component-3',
getClientSide: () => import(
/* webpackChunkName: 'sample-component-3' */ './SampleComponent'
),
placeholder: Placeholder,
serverSide: requireWeak('./SampleComponent', __dirname),
});

export default function SampleCodeSplit() {
const [testKey] = useGlobalState('test.key', 'testValue');
return (
<>
<h1>{testKey}</h1>
<SampleComponent1 />
<SampleComponent2 />
<SampleComponent3 />
</>
);
}
6 changes: 2 additions & 4 deletions config/babel/node-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
* matching (S)CSS transformations done by
* `@dr.pogodin/babel-plugin-react-css-modules` plugin on (S)CSS code.
*
* - Sets up [@babel/plugin-syntax-dynamic-import](https://www.npmjs.com/package/@babel/plugin-syntax-dynamic-import)
* to allow for dynamic `import()` statements.
*
* - Sets up [babel-plugin-tranform-assets](https://www.npmjs.com/package/@dr.pogodin/babel-plugin-transform-assets)
* to convert GIF, JPEG, JPG, and PNG imports into emitted asset paths, like
* `/images/[FILE_HASH].[FILE_EXTENSION]`. The `baseAssetsOutputPath` preset
Expand Down Expand Up @@ -74,7 +71,6 @@ function newBase(babel, options = {}) {

const baseAssetsOutputPath = options.baseAssetsOutputPath || '';
config.plugins.push(
'@babel/syntax-dynamic-import',
['@dr.pogodin/transform-assets', {
extensions: ['gif', 'jpeg', 'jpg', 'png'],
name: `${baseAssetsOutputPath}/images/[hash:20].[ext]`,
Expand All @@ -85,7 +81,9 @@ function newBase(babel, options = {}) {
(x) => x[0] === 'module-resolver',
)[1];
moduleResolverPluginOps.transformFunctions = [
'requireWeak',
'resolveWeak',
'webpack.requireWeak',
'webpack.resolveWeak',
];

Expand Down
62 changes: 62 additions & 0 deletions docs/docs/api/classes/Semaphore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Semaphore
```jsx
import { Semaphore } from '@dr.pogodin/react-utils';
```
The [Semaphore] class implements a synchronization primitive which allows
an async code to wait until the semaphore is set to the "ready" state, if
it is not in that state already. A semaphore can be switched between "ready"
and "non-ready" states as needed.

## Methods
- [constructor()] - Creates a new [Semaphore] instance.
- [setReady()] - Sets semaphore to the specified state.
- [waitReady()] - Creates a [Promise] which resolves as soon as the semaphore
is in "ready" state.

### constructor()
```jsx
const semaphore = new Semaphore(ready = false);
```
Creates a new [Semaphore] instance.

**Arguments**
- `ready` - **boolean** - Optional. The initial semaphore state. Defaults **false**.

### setReady()
```jsx
semaphore.setReady(ready);
```
Sets semaphore to the specified state.

**Arguments**
- `ready` - **boolean** - Target state.

### waitReady()
```jsx
semaphore.waitReady(): Promise;
```
Creates a [Promise] which resolves as soon as the semaphore is in the "ready"
state.

Note that unlike [Barrier] instances, a semaphore does not resolve all
pending [waitReady()] promises immediately when switched to "ready" state.
Setting a semaphore to "ready" will trigger an attempt to _synchronously_
resolve each pending [waitReady()] promise for that semaphore, for each of
these promises the actual state of the semaphore at the moment of resolution
attempt will be tested, and if it has switched back to "non-ready", the promise
will remain pending until the next resolution chance. Thus, a semaphore can
be used as [mutex], if each asynchronous code waiting for a semaphore sets it
back to "non-ready" state as soon as it resolved.

## Properties
- `semaphore.ready` - **boolean** - Read-only. Current semaphore state.

<!-- Re-usable links below -->

[Barrier]: /docs/api/classes/Barrier
[constructor()]: #constructor
[mutex]: https://en.wikipedia.org/wiki/Mutual_exclusion
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[Semaphore]: /docs/api/classes/Semaphore
[setReady()]: #setready
[waitReady()]: #waitready

0 comments on commit f2ffc53

Please sign in to comment.