Unverified Commit 3fe9b4c8 authored by Max Proske's avatar Max Proske Committed by GitHub

Remove `with-cerebral` example (#42980)

parent 365b164b
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# Declarative State & Side-effect management with [CerebralJS](https://cerebraljs.com/)
Use [CerebralJS](https://cerebraljs.com/) to manage an apps state and side effects in a declarative manner:
Declarative CerebralJS:
```js
;[
setLoading(true),
getUser,
{
success: setUser,
error: setError,
},
setLoading(false),
]
```
vs imperative JS:
```js
function getUser() {
this.isLoading = true
ajax
.get('/user')
.then((user) => {
this.data = user
this.isLoading = false
})
.catch((error) => {
this.error = error
this.isLoading = false
})
}
```
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-cerebral)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-cerebral&project-name=with-cerebral&repository-name=with-cerebral)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-cerebral with-cerebral-app
```
```bash
yarn create next-app --example with-cerebral with-cerebral-app
```
```bash
pnpm create next-app --example with-cerebral with-cerebral-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
const pad = (n) => (n < 10 ? `0${n}` : n)
const format = (t) =>
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`
export default function Clock(props) {
return (
<div className={props.light ? 'light' : ''}>
{format(new Date(props.lastUpdate))}
<style jsx>{`
div {
padding: 15px;
color: #82fa58;
display: inline-block;
font: 50px menlo, monaco, monospace;
background-color: #000;
}
.light {
background-color: #999;
}
`}</style>
</div>
)
}
import React from 'react'
import Link from 'next/link'
import { connect } from '@cerebral/react'
import { state, signal } from 'cerebral/tags'
import Clock from './Clock'
export default connect(
{
lastUpdate: state`clock.lastUpdate`,
light: state`clock.light`,
mounted: signal`clock.mounted`,
unMounted: signal`clock.unMounted`,
},
class Page extends React.Component {
componentDidMount() {
this.props.mounted()
}
componentWillUnmount() {
this.props.unMounted()
}
render() {
return (
<div>
<h1>{this.props.title}</h1>
<Clock lastUpdate={this.props.lastUpdate} light={this.props.light} />
<nav>
<Link href={this.props.linkTo}>Navigate</Link>
</nav>
</div>
)
}
}
)
export function startTimer({ clock }) {
clock.start('clock.secondTicked')
}
export function stopTimer({ clock }) {
clock.stop()
}
import { mounted, unMounted, secondTicked } from './signals'
import provider from './provider'
export default {
state: {
lastUpdate: 0,
light: false,
},
signals: {
mounted,
unMounted,
secondTicked,
},
provider,
}
// milliseconds per second
const SECOND = 1000
let timer = null
export default function provider(context) {
context.clock = {
start(signalPath) {
const signal = context.controller.getSignal(signalPath)
function tick() {
const now = Date.now()
signal({ now })
timer = setTimeout(tick, SECOND - (now % SECOND))
}
tick()
},
stop() {
clearTimeout(timer)
},
}
return context
}
import { set } from 'cerebral/operators'
import { state, props } from 'cerebral/tags'
import { startTimer, stopTimer } from './actions'
export const mounted = [startTimer, set(state`clock.light`, true)]
export const unMounted = stopTimer
export const secondTicked = set(state`clock.lastUpdate`, props`now`)
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@cerebral/react": "^2.1.0",
"cerebral": "^3.1.0",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
import { Controller } from 'cerebral'
import Devtools from 'cerebral/devtools'
import { Container } from '@cerebral/react'
import Page from '../components/Page'
import clock from '../modules/clock'
const Home = (props) => {
const { stateChanges } = props
const controller = Controller({
devtools:
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
? null
: Devtools({ host: 'localhost:8787' }),
modules: { clock },
stateChanges: stateChanges,
})
return (
<Container controller={controller}>
<Page title="Index Page" linkTo="/other" />
</Container>
)
}
export default Home
import { Controller } from 'cerebral'
import Devtools from 'cerebral/devtools'
import { Container } from '@cerebral/react'
import Page from '../components/Page'
import clock from '../modules/clock'
const Other = (props) => {
const { stateChanges } = props
const controller = Controller({
devtools:
process.env.NODE_ENV === 'production' || typeof window === 'undefined'
? null
: Devtools({ host: 'localhost:8787' }),
modules: { clock },
stateChanges: stateChanges,
})
return (
<Container controller={controller}>
<Page title="Index Page" linkTo="/other" />
</Container>
)
}
export default Other
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment