Skip to content

Commit db55dea

Browse files
authored
Fix event notifications for instance changes (#78)
* Update PropertyValueChangedEventData.m * Improve event notification and subsref handling in Schema Pass the object instance to PropertyValueChangedEventData for more accurate event source tracking. Enhance isSubsForPublicPropertyValue to correctly handle subsref indexing into object arrays and improve public property detection logic.
1 parent e155dc2 commit db55dea

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

code/internal/+openminds/+abstract/Schema.m

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
obj = builtin('subsasgn', obj, subs, value);
340340

341341
% Assign new value and trigger event
342-
evtData = PropertyValueChangedEventData(value, oldValue, true); % true for linked prop
342+
evtData = PropertyValueChangedEventData(value, oldValue, true, obj); % true for linked prop
343343
obj.notify('PropertyWithLinkedInstanceChanged', evtData)
344344

345345
elseif numel(subs) > 1 && strcmp(subs(2).type, '.')
@@ -360,7 +360,7 @@
360360

361361
% Assign new value and trigger event
362362
linkedObj.subsasgn(subs(2:end), value);
363-
evtData = PropertyValueChangedEventData(value, oldValue, true); % true for linked prop
363+
evtData = PropertyValueChangedEventData(value, oldValue, true, obj); % true for linked prop
364364
obj.notify('PropertyWithLinkedInstanceChanged', evtData)
365365

366366
elseif numel(subs) > 1 && strcmp(subs(2).type, '()')
@@ -436,10 +436,19 @@
436436

437437
obj = builtin('subsasgn', obj, subs, value);
438438

439-
if numel(obj) >= 1
439+
if ~isempty(obj)
440440
if obj.isSubsForPublicPropertyValue(subs)
441-
evtData = PropertyValueChangedEventData(value, oldValue, false); % false for property which is not embedded or linked
442-
obj.notify('InstanceChanged', evtData)
441+
if isscalar(obj)
442+
evtSource = obj;
443+
else
444+
if strcmp(subs(1).type, '()')
445+
evtSource = builtin('subsref', obj, subs(1));
446+
else
447+
error('Unexpected error. Please report')
448+
end
449+
end
450+
evtData = PropertyValueChangedEventData(value, oldValue, false, evtSource); % false for property which is not embedded or linked
451+
evtSource.notify('InstanceChanged', evtData)
443452
% fprintf('Set "primitive" property type of %s\n', class(obj))
444453
end
445454
end
@@ -701,10 +710,24 @@
701710
% Return true if subs represent dot-indexing on a public property
702711

703712
tf = false;
713+
714+
tempSubs = subs;
715+
716+
% If we are indexing into a subset of the objects, lets strip
717+
% of the first subs element.
718+
if strcmp( tempSubs(1).type, '()' )
719+
if isscalar(tempSubs) % i.e instance() or instance(2)
720+
return
721+
else
722+
tempSubs = tempSubs(2:end);
723+
end
724+
end
704725

705-
if strcmp( subs(1).type, '.' )
726+
% If subs represent dot-indexing, check if it is for a public
727+
% property
728+
if strcmp( tempSubs(1).type, '.' )
706729
propNames = properties(obj);
707-
tf = any( strcmp(subs(1).subs, propNames) );
730+
tf = any( strcmp(tempSubs.subs, propNames) );
708731
end
709732
end
710733

code/internal/+openminds/+internal/+event/PropertyValueChangedEventData.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
NewValue
66
OldValue
77
IsLinkedProperty = false
8+
IsPropertyOf
89
end
910

1011
methods
11-
function obj = PropertyValueChangedEventData(newValue, oldValue, isLinkedProperty)
12+
function obj = PropertyValueChangedEventData(newValue, oldValue, isLinkedProperty, isPropertyOf)
1213
if nargin < 3 || isempty(isLinkedProperty)
1314
isLinkedProperty = false;
1415
end
1516
obj.NewValue = newValue;
1617
obj.OldValue = oldValue;
1718
obj.IsLinkedProperty = isLinkedProperty;
19+
obj.IsPropertyOf = isPropertyOf;
1820
end
1921
end
2022
end

0 commit comments

Comments
 (0)