@@ -1829,75 +1829,47 @@ function sandBox(script, name, verbose, debug, context) {
1829
1829
1830
1830
sandbox . verbose && sandbox . log ( 'createState(name=' + name + ', initValue=' + initValue + ', forceCreation=' + forceCreation + ', common=' + JSON . stringify ( common ) + ', native=' + JSON . stringify ( native ) + ')' , 'debug' ) ;
1831
1831
1832
- adapter . getObject ( name , function ( err , obj ) {
1832
+ let id = adapter . namespace + '.' + name ;
1833
+ if ( name . match ( / ^ j a v a s c r i p t \. \d + \. / ) || name . startsWith ( '0_userdata.0.' ) ) {
1834
+ id = name ;
1835
+ }
1836
+ adapter . getForeignObject ( id , function ( err , obj ) {
1833
1837
if ( err || ! obj || forceCreation ) {
1834
1838
// todo: store object in objects to have this object directly after callback
1835
1839
// create new one
1836
- if ( name . match ( / ^ j a v a s c r i p t \. \d + \. / ) || name . startsWith ( '0_userdata.0.' ) ) {
1837
- adapter . setForeignObject ( name , {
1838
- common : common ,
1839
- native : native ,
1840
- type : 'state'
1841
- } , function ( err ) {
1842
- err && adapter . log . warn ( 'Cannot set object "' + name + '": ' + err ) ;
1843
-
1844
- if ( initValue !== undefined ) {
1845
- if ( isObject ( initValue ) && initValue . ack !== undefined ) {
1846
- adapter . setForeignState ( name , initValue , callback ) ;
1847
- } else {
1848
- adapter . setForeignState ( name , initValue , true , callback ) ;
1849
- }
1850
- } else if ( ! forceCreation ) {
1851
- adapter . setForeignState ( name , null , true , callback ) ;
1840
+ adapter . setForeignObject ( id , {
1841
+ common : common ,
1842
+ native : native ,
1843
+ type : 'state'
1844
+ } , function ( err ) {
1845
+ err && adapter . log . warn ( 'Cannot set object "' + id + '": ' + err ) ;
1846
+
1847
+ if ( initValue !== undefined ) {
1848
+ if ( isObject ( initValue ) && initValue . ack !== undefined ) {
1849
+ adapter . setForeignState ( id , initValue , callback ) ;
1852
1850
} else {
1853
- if ( typeof callback === 'function' ) {
1854
- try {
1855
- callback . call ( sandbox , name ) ;
1856
- } catch ( e ) {
1857
- errorInCallback ( e ) ; //adapter.log.error('Error in callback: ' + e)
1858
- }
1859
- }
1851
+ adapter . setForeignState ( id , initValue , true , callback ) ;
1860
1852
}
1861
- } ) ;
1862
- } else {
1863
- adapter . setObject ( name , {
1864
- common : common ,
1865
- native : native ,
1866
- type : 'state'
1867
- } , function ( err ) {
1868
- err && adapter . log . warn ( 'Cannot set object "' + name + '": ' + err ) ;
1869
-
1870
- if ( initValue !== undefined ) {
1871
- if ( isObject ( initValue ) && initValue . ack !== undefined ) {
1872
- adapter . setState ( name , initValue , callback ) ;
1873
- } else {
1874
- adapter . setState ( name , initValue , true , callback ) ;
1875
- }
1876
- } else if ( ! forceCreation ) {
1877
- adapter . setState ( name , null , true , callback ) ;
1878
- } else {
1879
- if ( typeof callback === 'function' ) {
1880
- try {
1881
- callback . call ( sandbox , name ) ;
1882
- } catch ( e ) {
1883
- errorInCallback ( e ) ; //adapter.log.error('Error in callback: ' + e)
1884
- }
1853
+ } else if ( ! forceCreation ) {
1854
+ adapter . setForeignState ( id , null , true , callback ) ;
1855
+ } else {
1856
+ if ( typeof callback === 'function' ) {
1857
+ try {
1858
+ callback . call ( sandbox , id ) ;
1859
+ } catch ( e ) {
1860
+ errorInCallback ( e ) ; //adapter.log.error('Error in callback: ' + e)
1885
1861
}
1886
1862
}
1887
- } ) ;
1888
- }
1889
- } else {
1890
- if ( ! adapter . config . subscribe && ! states [ name ] && states [ adapter . namespace + '.' + name ] === undefined ) {
1891
- if ( ! name . startsWith ( adapter . namespace ) ) {
1892
- states [ adapter . namespace + '.' + name ] = { val : null , ack : true } ;
1893
- } else {
1894
- states [ name ] = { val : null , ack : true } ;
1895
1863
}
1864
+ } ) ;
1865
+ } else {
1866
+ if ( ! adapter . config . subscribe && ! states [ id ] && states [ adapter . namespace + '.' + id ] === undefined ) {
1867
+ states [ id ] = { val : null , ack : true } ;
1896
1868
}
1897
1869
// state yet exists
1898
1870
if ( typeof callback === 'function' ) {
1899
1871
try {
1900
- callback . call ( sandbox , name ) ;
1872
+ callback . call ( sandbox , id ) ;
1901
1873
} catch ( e ) {
1902
1874
errorInCallback ( e ) ; //adapter.log.error('Error in callback: ' + e)
1903
1875
}
0 commit comments