@@ -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
0 commit comments