Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 9fcd281

Browse files
Revise and generalize hand-me-down options
1 parent 675c651 commit 9fcd281

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

packages/library/src/core.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ export const status = Object.freeze({
1414
done: 3,
1515
})
1616

17+
// Default options ----------------------------------------
18+
// Attributes to pass on to nested items (as names)
19+
export const handMeDowns = [
20+
'debug',
21+
'datastore',
22+
'el',
23+
]
24+
1725
// Generic building block for experiment
1826
export class Component extends EventHandler {
1927
constructor(options={}) {
@@ -58,6 +66,12 @@ export class Component extends EventHandler {
5866
// There is no timeout by default
5967
timeout: null,
6068

69+
// Setup hand-me-downs
70+
// (array is copied on purpose)
71+
handMeDowns: [
72+
...handMeDowns,
73+
],
74+
6175
...options,
6276

6377
// Setup media handling
@@ -150,6 +164,21 @@ export class Component extends EventHandler {
150164
return
151165
}
152166

167+
// Collect options 'handed down' from higher-level components
168+
if (this.parent) {
169+
const foo = this.parents.reduce(
170+
// Accumulate handed down options from parents
171+
(acc, cur) => {
172+
cur.options.handMeDowns.forEach(o => acc.add(o))
173+
return acc
174+
},
175+
new Set(),
176+
).forEach(
177+
// 'inherit' the option from the parent component
178+
o => (this.options[o] = this.options[o] || this.parent.options[o])
179+
)
180+
}
181+
153182
// Direct output to the HTML element with the attribute
154183
// data-labjs-section="main", unless a different element
155184
// has been provided explicitly
@@ -410,14 +439,6 @@ Component.metadata = {
410439
},
411440
}
412441

413-
// Default options ----------------------------------------
414-
// Attributes to pass on to nested items (as names)
415-
export const handMeDowns = [
416-
'debug',
417-
'datastore',
418-
'el',
419-
]
420-
421442
// Simple components --------------------------------------
422443
// A Dummy component does nothing but end
423444
// immediately as soon as it is called

packages/library/src/flow.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Flow control components for lab.js
22
import { shuffle, mean, isFunction } from 'lodash'
33
import { entries } from 'core-js/fn/array'
4-
import { Component, status, handMeDowns } from './core'
4+
import { Component, status } from './core'
55

66
// Helper function to handle nested components
77
export const prepareNested = function(nested, parent) {
@@ -19,13 +19,6 @@ export const prepareNested = function(nested, parent) {
1919
}
2020
})
2121

22-
// Pass on specified attributes
23-
nested.forEach((c) => {
24-
parent.options.handMeDowns.forEach((k) => {
25-
c.options[k] = c.options[k] || parent.options[k]
26-
})
27-
})
28-
2922
// Trigger prepare on all nested components
3023
return Promise.all(
3124
nested.map(c => c.prepare(false)), // indicate automated call
@@ -42,10 +35,6 @@ export class Sequence extends Component {
4235
content: [],
4336
// Shuffle items, if so desired
4437
shuffle: false,
45-
// Use default hand-me-downs
46-
// unless directed otherwise
47-
// (note that the hand-me-downs are copied)
48-
handMeDowns: [...handMeDowns],
4938
...options,
5039
})
5140

@@ -186,7 +175,6 @@ export class Parallel extends Component {
186175
// that are run in parallel.
187176
content: [],
188177
mode: 'race',
189-
handMeDowns: [...handMeDowns],
190178
...options,
191179
})
192180
}

packages/library/src/html.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { extend } from 'lodash'
33
import 'whatwg-fetch'
44

5-
import { Component, status, handMeDowns } from './core'
5+
import { Component, status } from './core'
66
import { prepareNested } from './flow'
77

88
// html.Screens display HTML when run
@@ -180,7 +180,6 @@ export class Frame extends Component {
180180
content: null,
181181
context: '',
182182
contextSelector: '',
183-
handMeDowns: [...handMeDowns],
184183
...options,
185184
})
186185
}

0 commit comments

Comments
 (0)