Skip to content

Commit fa582e4

Browse files
authored
Merge pull request #786 from vcync/next
fix: module hot reload (#755)
2 parents 2abcd2e + b67cd91 commit fa582e4

6 files changed

Lines changed: 95 additions & 46 deletions

File tree

src/application/utils/get-prop-default.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ export default async function getPropDefault(
1010
const { random, type } = prop;
1111
let defaultValue = prop.default;
1212

13-
if (store.state.dataTypes[type] && store.state.dataTypes[type].create) {
14-
const propData = useExistingData ? module.props[propName] : prop.default;
15-
16-
return await store.state.dataTypes[type].create(
17-
propData,
18-
module.meta.isGallery
19-
);
13+
if (store.state.dataTypes[type]) {
14+
if (!defaultValue && store.state.dataTypes[type].inputs) {
15+
defaultValue = store.state.dataTypes[type].inputs();
16+
}
17+
18+
const propData = useExistingData
19+
? module.props[propName] ?? defaultValue
20+
: defaultValue;
21+
22+
if (store.state.dataTypes[type].create) {
23+
return await store.state.dataTypes[type].create(
24+
propData,
25+
module.meta.isGallery
26+
);
27+
}
2028
}
2129

2230
if (useExistingData && typeof module.props?.[propName] !== "undefined") {

src/application/worker/index.worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,12 @@ async function start() {
342342
}
343343

344344
store.commit("groups/SWAP", {});
345-
store.commit("modules/SWAP", {});
346345
store.commit("inputs/SWAP", {});
346+
store.commit("modules/SWAP", {});
347347

348348
store.commit("groups/CLEAR_SWAP", {});
349-
store.commit("modules/CLEAR_SWAP", {});
350349
store.commit("inputs/CLEAR_SWAP", {});
350+
store.commit("modules/CLEAR_SWAP", {});
351351

352352
return;
353353
}

src/application/worker/store/modules/media.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const actions = {
4545
await store.dispatch("modules/registerModule", { module, hot: true });
4646
} catch (e) {
4747
console.error(`Could not load module`, item.name);
48-
console.log(module);
4948
console.error(e);
5049
return;
5150
}

src/application/worker/store/modules/modules.js

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,25 @@ async function initialiseModuleProperties(
4444
props,
4545
module,
4646
isGallery = false,
47-
useExistingData = false
47+
useExistingData = false,
48+
existingData = {},
49+
writeToSwap = false
4850
) {
4951
const propKeys = Object.keys(props);
52+
const propsWithoutId = [];
53+
54+
if (useExistingData) {
55+
for (let i = 0, len = propKeys.length; i < len; i += 1) {
56+
const prop = propKeys[i];
57+
const propDidExist = !!existingData.$props[prop];
58+
59+
if (propDidExist) {
60+
module.$props[prop].id = existingData.$props[prop].id;
61+
} else {
62+
propsWithoutId.push(prop);
63+
}
64+
}
65+
}
5066

5167
for (let i = 0, len = propKeys.length; i < len; i++) {
5268
const propKey = propKeys[i];
@@ -60,17 +76,22 @@ async function initialiseModuleProperties(
6076
useExistingData
6177
);
6278

63-
if (!isGallery && !useExistingData) {
79+
if (
80+
(!isGallery && !useExistingData) ||
81+
(propsWithoutId.length && propsWithoutId.indexOf(propKey) > -1)
82+
) {
6483
const inputBind = await store.dispatch("inputs/addInput", {
6584
type: "action",
6685
location: "modules/updateProp",
67-
data: { moduleId: module.$id, prop: propKey }
86+
data: { moduleId: module.$id, prop: propKey },
87+
writeToSwap
6888
});
6989

7090
if (
7191
prop.type in store.state.dataTypes &&
7292
store.state.dataTypes[prop.type].inputs
7393
) {
94+
console.log(propKey);
7495
const dataTypeInputs = store.state.dataTypes[prop.type].inputs();
7596
const dataTypeInputsKeys = Object.keys(dataTypeInputs);
7697

@@ -84,7 +105,8 @@ async function initialiseModuleProperties(
84105
prop: propKey,
85106
path: `[${key}]`
86107
},
87-
id: `${inputBind.id}-${key}`
108+
id: `${inputBind.id}-${key}`,
109+
writeToSwap
88110
});
89111
}
90112
}
@@ -139,32 +161,47 @@ const actions = {
139161
commit("ADD_REGISTERED_MODULE", { module: moduleDefinition });
140162

141163
if (hot) {
142-
const activeModuleValues = Object.values(state.active);
164+
const activeModuleValues = Object.values(state.active).filter(
165+
activeModule => activeModule.meta.name === name
166+
);
143167

144-
for (let i = 0; i < activeModuleValues.length; i += 1) {
145-
const activeModule = activeModuleValues[i];
168+
for (let i = 0, len = activeModuleValues.length; i < len; i += 1) {
169+
const existingActiveModule = activeModuleValues[i];
170+
const activeModule = { ...existingActiveModule };
146171

147-
if (activeModule.meta.name === name) {
148-
const { canvas } = rootState.outputs.main || {
149-
canvas: { width: 0, height: 0 }
150-
};
172+
const { canvas } = rootState.outputs.main || {
173+
canvas: { width: 0, height: 0 }
174+
};
151175

152-
if ("init" in moduleDefinition) {
153-
const { data } = activeModule;
154-
const returnedData = moduleDefinition.init({
155-
canvas,
156-
data: { ...data },
157-
props: activeModule.props
158-
});
176+
const { props } = moduleDefinition;
177+
178+
activeModule.$props = JSON.parse(JSON.stringify(props));
179+
180+
const initialisedModule = await initialiseModuleProperties(
181+
props,
182+
{ ...activeModule },
183+
false,
184+
true,
185+
existingActiveModule
186+
);
187+
188+
commit("ADD_ACTIVE_MODULE", { module: initialisedModule });
189+
190+
if ("init" in moduleDefinition) {
191+
const { data } = activeModule;
192+
const returnedData = moduleDefinition.init({
193+
canvas,
194+
data: { ...data },
195+
props: activeModule.props
196+
});
159197

160-
if (returnedData) {
161-
commit("UPDATE_ACTIVE_MODULE", {
162-
id: activeModule.$id,
163-
key: "data",
164-
value: returnedData,
165-
writeToSwap: false
166-
});
167-
}
198+
if (returnedData) {
199+
commit("UPDATE_ACTIVE_MODULE", {
200+
id: activeModule.$id,
201+
key: "data",
202+
value: returnedData,
203+
writeToSwap: false
204+
});
168205
}
169206
}
170207
}
@@ -224,19 +261,14 @@ const actions = {
224261
}
225262
}
226263

264+
module.$props = JSON.parse(JSON.stringify(props));
265+
227266
if (!existingModule) {
228267
module.$id = uuidv4();
229268
module.$moduleName = moduleName;
230-
module.$props = JSON.parse(JSON.stringify(props));
231-
232269
module.props = {};
233270

234-
await initialiseModuleProperties(
235-
props,
236-
module,
237-
moduleMeta.isGallery,
238-
existingModule
239-
);
271+
await initialiseModuleProperties(props, module, moduleMeta.isGallery);
240272

241273
const dataKeys = Object.keys(data);
242274
module.data = {};
@@ -311,7 +343,9 @@ const actions = {
311343
props,
312344
module,
313345
moduleMeta.isGallery,
314-
true
346+
true,
347+
existingModule,
348+
writeToSwap
315349
);
316350
}
317351

src/components/Controls/RangeControl.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ export default {
370370
requestAnimationFrame(this.draw);
371371
},
372372
373+
min() {
374+
requestAnimationFrame(this.draw);
375+
},
376+
377+
max() {
378+
requestAnimationFrame(this.draw);
379+
},
380+
373381
inputValue(value) {
374382
this.position = -value * this.spacingCalc;
375383
}

src/components/ModuleControl.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default {
4646
},
4747
4848
title() {
49-
return this.activeProp.label || this.prop;
49+
return this.activeProp?.label || this.prop;
5050
},
5151
5252
value() {

0 commit comments

Comments
 (0)