Skip to content

Commit

Permalink
multi ref paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim de Beer committed Jul 16, 2017
1 parent 940fdb6 commit 1411227
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 88 deletions.
118 changes: 68 additions & 50 deletions src/transpile/element/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,86 @@ const plainText = (status, node) => {
}
}

const createSubs = (subs, prop) => {
for (let i = 0, len = prop.val.length; i < len; i++) {
const key = prop.val[i]
if (!subs[key]) {
subs[key] = {}
}
subs = subs[key]
}
return subs
}

const parseSingleStruct = (status, node, prop) => {
const listeners = getListeners(status, 'new')
const subs = createSubs(status.subs, prop)
if (!subs.val || subs.val !== true) subs.val = string('shallow')
const updateListeners = getListeners(merge(
status, { subs, path: prop.val }
), 'update')
const id = ++status.id
let newValue, updateValue
if (prop.expression.type === 'inline') {
const replacementKey = prop.expression.replacementKey
newValue = prop.expression.val.replace(
new RegExp(replacementKey, 'g'),
prop.val.length === 1
? `s.get(${prop.val.map(string).join(',')}, '').compute()`
: `s.get([${prop.val.map(string).join(',')}], '').compute()`
)
updateValue = prop.expression.val.replace(new RegExp(replacementKey, 'g'), 's.compute()')
}
const parentId = node.parent.openingElement.id // bit lame to put id in opening element....
const line = status.ui.createText(status, id, parentId, newValue, listeners)
if (line) listeners.push(line)
const line2 = status.ui.updateText(status, id, parentId, updateValue, prop.val, updateListeners)
if (line2) updateListeners.push(line2)
}

const parseExpressionContainer = (status, node) => {
if (node.type === 'JSXExpressionContainer') {
const prop = createPropFromExpression(status, node.expression)
// props types: struct, raw, reference
// also need to take status path into account for subscriptions
if (prop.type === 'struct') {
const listeners = getListeners(status, 'new')
// status from current subs path
// most simple

// RESUSE -- also not good enough
// need to check relative subs paths also need to add those when making a prop
let subs = status.subs
for (let i = 0, len = prop.val.length; i < len; i++) {
const key = prop.val[i]
if (!subs[key]) {
subs[key] = {}
}
subs = subs[key]
}
if (!subs.val || subs.val !== true) {
subs.val = string('shallow')
}

console.log(prop.val)
const updateListeners = getListeners(merge(
status,
{
subs,
path: prop.val // not enough of course
}
), 'update')
// REUSE
// default value === '' since we use it as text here...
// remove needs to be handled seperately -- then it will become an empty string as well
const id = ++status.id
let newValue, updateValue
if (prop.expression.type === 'inline') {
// reuse of computes etc
const replacementKey = prop.expression.replacementKey
newValue = prop.expression.val.replace(
new RegExp(replacementKey, 'g'),
prop.val.length === 1
? `s.get(${prop.val.map(string).join(',')}, '').compute()`
: `s.get([${prop.val.map(string).join(',')}], '').compute()`
)
// not enough shit....
console.log('------>expression', prop.expression.val, replacementKey)
updateValue = prop.expression.val.replace(new RegExp(replacementKey, 'g'), 's.compute()')
}
const parentId = node.parent.openingElement.id // bit lame to put id in opening element....
const line = status.ui.createText(status, id, parentId, newValue, listeners)
if (line) listeners.push(line)
// get path and levels
const line2 = status.ui.updateText(status, id, parentId, updateValue, prop.val, updateListeners)
if (line2) updateListeners.push(line2)
parseSingleStruct(status, node, prop)
} else if (prop.type === 'raw') {

} else if (prop.type === 'child') {

} else if (prop.type === 'reference') {
// nested refs as well -- also you dont care about prev props here anymore
// needs to be recursive this whole replacement thing
// do that in a bit
// dont need recursion if you handle all that in expression parsing
const id = ++status.id
const parentId = node.parent.openingElement.id // bit lame to put id in opening element....

if (prop.expression.type === 'inline') {
const listeners = getListeners(status, 'new')
let i = prop.expression.replacementKey.length
var newValue = prop.expression.val
while (i--) {
// so in general well allways need an array and an object for refs (to map keys)
// also type struct needs to get subs path included
const replacementKey = prop.expression.replacementKey[i]
const target = prop.val[replacementKey]
if (target.type === 'struct') {
newValue = newValue.replace(
new RegExp(replacementKey, 'g'),
target.val.length === 1
? `s.get(${target.val.map(string).join(',')}, '').compute()`
: `s.get([${target.val.map(string).join(',')}], '').compute()`
)

// ok lets get this update shit going
}
}
const line = status.ui.createText(status, id, parentId, newValue, listeners)
if (line) listeners.push(line)
// now update
}
}
}
}
Expand Down
34 changes: 31 additions & 3 deletions src/transpile/element/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { walker, isEqual, extractPath, getObject, showcode } = require('../util')
var cnt = 0

const createPropFromExpression = (status, node) => {
var val = {}
const props = status.props
const args = status.args
const val = {}
const expression = status.code.slice(node.start, node.end).split('')
const start = node.start
// need to have path as well
Expand All @@ -21,6 +21,7 @@ const createPropFromExpression = (status, node) => {
const path = extractPath(status, child)
if (!props) {
if (!val.type) {
val.fromSubscription = status.path
val.type = 'struct'
val.val = path
const replacementKey = `__${++cnt}__`
Expand Down Expand Up @@ -69,8 +70,34 @@ const createPropFromExpression = (status, node) => {
if (val.type === 'struct' && isEqual(path, val.val)) {
const replacement = val.expression.replacementKey
val.expression.replacement.push([ getObject(status, child), replacement ])
} else {
console.log('THIS IS A MULTI SUBSCRIPTION NEED TO MAKE REF PROP TYPE')
} else if (val.type === 'struct') {
console.log('THIS IS A MULTI SUBSCRIPTION NEED TO MAKE REF PROP TYPE', path)
const prev = val
const expression = prev.expression
delete prev.expression
val = {
type: 'reference',
val: {
[expression.replacementKey]: prev
},
expression
}
// we dont need to parse recursively in the user
// all work will be done here
expression.replacementKey = [ expression.replacementKey ]
const nested = {}
nested.type = 'struct'
nested.val = path
nested.fromSubscription = status.path
const replacementKey = `__${++cnt}__`
const replacement = replacementKey
expression.replacementKey.push(replacementKey)
expression.replacement.push([ getObject(status, child), replacement ])
val.val[replacementKey] = nested
} else if (val.type === 'reference') {
// more then one
} else if (val.type === 'raw') {
// raw
}
}
} else {
Expand All @@ -92,6 +119,7 @@ const createPropFromExpression = (status, node) => {
return { type: 'error' }
}

// this replacement thing can become a util
if (val.expression.type === 'inline') {
let code = ''
let replacement = 0
Expand Down
10 changes: 7 additions & 3 deletions test/props/object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const App = (props) => {
return <div>
<h1>{props.nested}</h1>
<h2>{props}</h2>
<div>{props.bla + '!' + props.bla}</div>
<div>{props.bla + '!' + props.blurf}</div>
</div>
}

module.exports = App

/*
<h1>{props.nested}</h1>
<h2>{props}</h2>
<div>{props.bla + '!' + props.bla}</div>
*/
35 changes: 3 additions & 32 deletions test/props/result/object.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@

module.exports =
{
val: 'shallow',
val: 'switch',
_:
{
new: (s, type, subs, tree) => {
if (!tree._) tree._ = {}
var _1 = tree._[1] = document.createElement('div')
var _2 = tree._[2] = document.createElement('h1')
var _2 = tree._[2] = document.createElement('div')
_1.appendChild(_2)
_2.appendChild((tree._[3] = document.createTextNode(s.get('nested', '').compute())))
var _4 = tree._[4] = document.createElement('h2')
_1.appendChild(_4)
_4.appendChild((tree._[5] = document.createTextNode(s.get([], '').compute())))
var _6 = tree._[6] = document.createElement('div')
_1.appendChild(_6)
_6.appendChild((tree._[7] = document.createTextNode(s.get('bla', '').compute() + '!' + s.get('bla', '').compute())))
},
update: (s, type, subs, tree) => {
s._[5].nodeValue = s.compute()
}
},
nested:
{
val: 'shallow',
_:
{
update: (s, type, subs, tree) => {
s._p._[3].nodeValue = s.compute()
}
}
},
bla:
{
val: 'shallow',
_:
{
update: (s, type, subs, tree) => {
s._p._[7].nodeValue = s.compute() + '!' + s.compute()
}
_2.appendChild((tree._[3] = document.createTextNode(s.get('bla', '').compute() + '!' + s.get('blurf', '').compute())))
}
}
}

0 comments on commit 1411227

Please sign in to comment.