Skip to content

Commit

Permalink
♻ Use anonymous imports to register functions on chain (#171)
Browse files Browse the repository at this point in the history
* 🔥 Remove ChainWrapper namespaces registering

* ♻️ Use anonymous imports to register chain functions
  • Loading branch information
nlepage committed Dec 15, 2017
1 parent 84ea1dd commit f383dd7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
46 changes: 43 additions & 3 deletions misc/generate-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const generateFlow = async () => {
const rootDir = path.resolve(packageDir, '../..')
const generatedDir = path.resolve(packageDir, 'generated')
const flowDir = path.resolve(generatedDir, 'flow')
const seqDir = path.resolve(generatedDir, 'seq')
await remove(generatedDir)
await ensureDir(flowDir)
await ensureDir(seqDir)

const items = await jsdoc.explain({
configure: path.resolve(rootDir, 'jsdoc.json'),
Expand All @@ -28,6 +30,7 @@ const generateFlow = async () => {
.value()

const namespaces = _.keys(itemsByNamespace)

await Promise.all(namespaces.map(async namespace => {
const nsDir = path.resolve(flowDir, namespace)
await ensureDir(nsDir)
Expand Down Expand Up @@ -61,13 +64,50 @@ export { curried as ${name} }
`${namespaces.map(namespace => {
const nsItems = itemsByNamespace[namespace].filter(({ name }) => !exportedNames.has(name))
nsItems.forEach(({ name }) => exportedNames.add(name))
/* eslint-disable */
/* eslint-disable comma-spacing,indent */
return `export {
${nsItems.map(({ name }) => ` ${name},`).join('\n')}
} from './${namespace}'`
}).join('\n\n')}
`,
/* eslint-enable */
`, /* eslint-enable */
)

await Promise.all(namespaces.map(async namespace => {
const nsDir = path.resolve(seqDir, namespace)
await ensureDir(nsDir)

const nsItems = itemsByNamespace[namespace]

await (async () => {
for (const { name } of nsItems) {
await writeFile(
path.resolve(nsDir, `${name}.js`),
/* eslint-disable indent */
`import { ChainWrapper } from 'seq/ChainWrapper'
import { ${name} } from 'core/${name}'
ChainWrapper.prototype.${name} = function(path, ...args) {
return this._call(${name}, path, args)
}
`, /* eslint-enable */
)
}
})()

await writeFile(
path.resolve(nsDir, 'index.js'),
/* eslint-disable indent */
`${nsItems.map(({ name }) => `import './${name}'`).join('\n')}
`, /* eslint-enable */
)
}))

await writeFile(
path.resolve(seqDir, 'all.js'),
/* eslint-disable indent */
`${namespaces.map(namespace => `import './${namespace}'`).join('\n')}
`, /* eslint-enable */
)
} catch (e) {
console.error(e) // eslint-disable-line no-console
Expand Down
40 changes: 8 additions & 32 deletions packages/immutadot/src/seq/ChainWrapper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import * as array from 'array'
import * as core from 'core'
import * as lang from 'lang'
import * as object from 'object'
import * as string from 'string'

import { unsafeToPath } from 'path/toPath'

/**
Expand Down Expand Up @@ -77,6 +71,14 @@ class ChainWrapper {
return this._commited
}

/**
* Function to be called by {@link seq.peek|peek} with the resolved unwrapped value.
* @memberof seq
* @callback peekCallback
* @param {Object} unwrapped The resolved unwrapped object
* @since 0.3.0
*/

/**
* Executes the chain sequence and calls <code>callback</code> with the unwrapped object.
* @param {seq.peekCallback} callback Function to be called with the resolved unwrapped value.
Expand Down Expand Up @@ -108,30 +110,4 @@ class ChainWrapper {
}
}

/**
* Function to be called by {@link seq.peek|peek} with the resolved unwrapped value.
* @memberof seq
* @callback peekCallback
* @param {Object} unwrapped The resolved unwrapped object
* @since 0.3.0
*/

// Add namespaces functions to the ChainWrapper prototype
const { convert, ...filteredCore } = core
const namespaces = [
array,
filteredCore,
lang,
object,
string,
]
namespaces.forEach(namespace => {
for (const fnName in namespace) {
const fn = namespace[fnName]
ChainWrapper.prototype[fnName] = function(path, ...args) {
return this._call(fn, path, args)
}
}
})

export { ChainWrapper }
1 change: 1 addition & 0 deletions packages/immutadot/src/seq/chain.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env jest */
import 'seq/core'
import { chain } from 'seq'
import { immutaTest } from 'test.utils'

Expand Down
4 changes: 4 additions & 0 deletions packages/immutadot/src/util/protect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'seq/core'

import { chain } from 'seq/chain'

import { isObject } from 'util/lang'

import {
prop,
} from 'path/consts'
Expand Down

0 comments on commit f383dd7

Please sign in to comment.