Skip to content

Commit b51dc55

Browse files
committed
feat(core): reads now receive a context as a third parameter
Read functions receive a `context` as a third parameter. The context has the following properties: - subsystems (The map of running subsystems) - subsystemSequence (The same, but with the order of the subsystems as specified on kernel startup) - config (The configuration object provided to the kernel)
1 parent af3e128 commit b51dc55

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

packages/core/src/read/__tests__/read.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,64 @@ describe('Read Subsytem', () => {
8787
})
8888
})
8989

90+
describe('Read function', () => {
91+
const app = Subsystem.create(() => ({
92+
name: 'app',
93+
}))
94+
95+
const readFn = jest.fn()
96+
readFn.mockReturnValue(
97+
Promise.resolve(
98+
http.asResponse(
99+
{ kind: 'scene', title: 'Scene Title X' },
100+
'https://netcetera.com/x.json'
101+
)
102+
)
103+
)
104+
afterEach(() => readFn.mockClear())
105+
106+
app.read.register(/test.json$/, readFn)
107+
108+
const kernel = Kernel.create(
109+
[enrich, transform, effect, update, read, app],
110+
{
111+
kind: 'app',
112+
[propNames.children]: ['content'],
113+
114+
content: {
115+
kind: ['__read', 'scene'],
116+
uri: 'https://netcetera.com/test.json',
117+
},
118+
},
119+
{ foo: 1 }
120+
)
121+
122+
test('read fn parameters', async () => {
123+
let content = kernel.query(['content'])
124+
125+
kernel.dispatch(
126+
action.atCursor(
127+
content,
128+
readActions.read(content.get('uri'), { revalidate: true })
129+
)
130+
)
131+
132+
await sleep(60)
133+
134+
expect(readFn).toHaveBeenCalledWith(
135+
'https://netcetera.com/test.json',
136+
{
137+
revalidate: true,
138+
},
139+
expect.any(Object)
140+
)
141+
142+
const context = readFn.mock.calls[0][2]
143+
expect(context).toHaveProperty('config', { foo: 1 })
144+
expect(context).toHaveProperty('subsystems', expect.any(Object))
145+
expect(context).toHaveProperty('subsystemSequence', expect.any(Array))
146+
})
147+
})
90148
describe('Refreshing', () => {
91149
const app = Subsystem.create(() => ({
92150
name: 'app',
@@ -126,6 +184,7 @@ describe('Refreshing', () => {
126184
content: {
127185
kind: ['__read', 'scene'],
128186
uri: 'https://netcetera.com/test.json',
187+
revalidate: true,
129188
},
130189
})
131190

packages/core/src/read/impl.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ async function performRead(context, readParams) {
8787
enrichment,
8888
transformation,
8989
} = context.subsystems.read.context
90+
9091
const kernel = context
9192

9293
const { uri, opts } = readParams
9394
const reader = registry.get(uri) || registry.get(fallback)
9495

9596
if (reader != null) {
96-
const readResponse = await reader(uri, opts)
97+
const readResponse = await reader(
98+
uri,
99+
opts,
100+
R.pick(['config', 'subsystems', 'subsystemSequence'], context)
101+
)
97102

98103
if (!isResponse(readResponse)) {
99104
throw new Error(

0 commit comments

Comments
 (0)