Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix broken npm run watch:docs #2841

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 462,7 @@ Pending streams can be created by calling `stream()` with no parameters.
var pending = stream()
```

If a stream is dependent on more than one stream, any of its parent streams is in a pending state, the dependent streams is also in a pending state, and does not update its value.
If a stream is dependent on more than one stream and any of its parent streams is in a pending state, the dependent stream is also in a pending state, and does not update its value.

```javascript
var a = stream(5)
Expand Down Expand Up @@ -558,13 558,13 @@ console.log(serialized) // logs 123

Unlike libraries like Knockout, Mithril.js streams do not trigger re-rendering of templates. Redrawing happens in response to event handlers defined in Mithril.js component views, route changes, or after [`m.request`](request.md) calls resolve.

If redrawing is desired in response to other asynchronous events (e.g. `setTimeout`/`setInterval`, websocket subscription, 3rd party library event handler, etc), you should manually call [`m.redraw()`](redraw.md)
If redrawing is desired in response to other asynchronous events (e.g. `setTimeout`/`setInterval`, websocket subscription, 3rd party library event handler, etc.), you should manually call [`m.redraw()`](redraw.md).

---

### What is Fantasy Land

[Fantasy Land](https://github.com/fantasyland/fantasy-land) specifies interoperability of common algebraic structures. In plain english, that means that libraries that conform to Fantasy Land specs can be used to write generic functional style code that works regardless of how these libraries implement the constructs.
[Fantasy Land](https://github.com/fantasyland/fantasy-land) specifies interoperability of common algebraic structures. In plain English, that means that libraries that conform to Fantasy Land specs can be used to write generic functional style code that works regardless of how these libraries implement the constructs.

For example, say we want to create a generic function called `plusOne`. The naive implementation would look like this:

Expand All @@ -574,7 574,7 @@ function plusOne(a) {
}
```

The problem with this implementation is that it can only be used with a number. However it's possible that whatever logic produces a value for `a` could also produce an error state (wrapped in a Maybe or an Either from a library like [Sanctuary](https://github.com/sanctuary-js/sanctuary) or [Ramda-Fantasy](https://github.com/ramda/ramda-fantasy)), or it could be a Mithril.js stream, or a [flyd](https://github.com/paldepind/flyd) stream, etc. Ideally, we wouldn't want to write a similar version of the same function for every possible type that `a` could have and we wouldn't want to be writing wrapping/unwrapping/error handling code repeatedly.
The problem with this implementation is that it can only be used with a number. However it's possible that whatever logic produces a value for `a` could also produce an error state (wrapped in a Maybe or an Either from a library like [Sanctuary](https://github.com/sanctuary-js/sanctuary) or [Ramda-Fantasy](https://github.com/ramda/ramda-fantasy)), or it could be a Mithril.js stream, a [Flyd](https://github.com/paldepind/flyd) stream, etc. Ideally, we wouldn't want to write a similar version of the same function for every possible type that `a` could have and we wouldn't want to be writing wrapping/unwrapping/error handling code repeatedly.

This is where Fantasy Land can help. Let's rewrite that function in terms of a Fantasy Land algebra:

Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion render/hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 38,7 @@ function execSelector(state, vnode) {
vnode.tag = state.tag
vnode.attrs = {}

if (!isEmpty(state.attrs) && !isEmpty(attrs)) {
if (!isEmpty(state.attrs)) {
var newAttrs = {}

for (var key in attrs) {
Expand Down
9 changes: 9 additions & 0 deletions render/tests/test-hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 580,15 @@ o.spec("hyperscript", function() {
o(nodeB.attrs.className).equals("b")
o(nodeB.attrs.a).equals("b")
})
o("handles shared empty attrs (#2821)", function() {
var attrs = {}

var nodeA = m(".a", attrs)
var nodeB = m(".b", attrs)

o(nodeA.attrs.className).equals("a")
o(nodeB.attrs.className).equals("b")
})
o("doesnt modify passed attributes object", function() {
var attrs = {a: "b"}
m(".a", attrs)
Expand Down
72 changes: 58 additions & 14 deletions scripts/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 83,20 @@ async function makeGenerator() {
})
}

async function loadLayoutFile() {
const layout = await fs.readFile(r("docs/layout.html"), "utf-8")
const docsSelect = await archiveDocsSelect()
return layout.replaceAll("[archive-docs]", docsSelect)
}

async function loadNavigationFiles() {
const [guides, methods] = await Promise.all([
fs.readFile(r("docs/nav-guides.md"), "utf-8"),
fs.readFile(r("docs/nav-methods.md"), "utf-8"),
])
return [guides, methods]
}

async function getArchiveDirs() {
const dirs = await fs.readdir(r("dist/archive"))
const ver = "v" version;
Expand Down Expand Up @@ -125,6 139,18 @@ class Generator {
this._layout = opts.layout
}

setLayout(layout) {
this._layout = layout
}

setGuides(guides) {
this._guides = guides
}

setMethods(methods) {
this._methods = methods
}

async compilePage(file, markdown) {
file = path.basename(file)
const link = new RegExp(
Expand Down Expand Up @@ -266,27 292,45 @@ if (require.main === module) {
require("./_command")({
exec: generate,
async watch() {
let timeout, genPromise
function updateGenerator() {
if (timeout == null) return
clearTimeout(timeout)
genPromise = new Promise((resolve) => {
timeout = setTimeout(function() {
timeout = null
resolve(makeGenerator().then((g) => g.generate()))
}, 100)
})
const generator = await makeGenerator()

function isLayoutFile(relative) {
return relative === "layout.html"
}

function isNavigationFile(relative) {
return ["nav-guides.md", "nav-methods.md"].includes(relative)
}

async function updateGenerator() {
await generator.generate()
}

async function updateFile(file) {
if ((/^layout\.html$|^archive$|^nav-/).test(file)) {
updateGenerator()
const relative = path.relative(r("docs"), file)
if (isLayoutFile(relative)) {
generator.setLayout(await loadLayoutFile())
await generator.generate()
} else if (isNavigationFile(relative)) {
const [guides, methods] = await loadNavigationFiles()
generator.setGuides(guides)
generator.setMethods(methods)
await generator.generate()
} else {
await generator.generateSingle(file)
}
(await genPromise).generateSingle(file)
}

async function removeFile(file) {
(await genPromise).eachTarget(file, (dest) => fs.unlink(dest))
const relative = path.relative(r("docs"), file)
if (isLayoutFile(relative)) {
throw `Error: the layout file "${relative}" is required!`
} else if (isNavigationFile(relative)) {
throw `Error: the navigation file "${relative}" is required!`
}
const relativeDist = relative.replace(/\.md$/, ".html")
await generator.eachTarget(relativeDist, (dest) => fs.unlink(dest))
console.log("Removed: " relative)
}

require("chokidar").watch(r("docs"), {
Expand Down