Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isSubtype #139

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions client/src/OnDeviceComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,46 @@ describe('OnDeviceComponent', function () {
});
});

describe('isSubtype', function () {
it('should return true if self subtype matches by default', async () => {
const isSubtype = await odc.isSubtype({
keyPath: '#pagesContainerGroup',
subtype: 'Group'
});
expect(isSubtype).to.be.true;
});

it('should return false if self subtype matches and matchOnSelfSubtype=false', async () => {
const isSubtype = await odc.isSubtype({
keyPath: '#pagesContainerGroup',
matchOnSelfSubtype: false,
subtype: 'Group'
});
expect(isSubtype).to.be.false;
});

it('should return false if subtype does not match', async () => {
const isSubtype = await odc.isSubtype({
keyPath: '#pagesContainerGroup',
subtype: 'RowList'
});
expect(isSubtype).to.be.false;
});

it('should throw an error if node does not exist', async () => {
try {
await odc.isSubtype({
keyPath: '#doesNotExist',
subtype: 'Group'
});
} catch (e) {
// failed as expected
return;
}
assert.fail('Should have thrown an exception');
});
});

describe('setValue', function () {
it('should be able to set a key on global', async () => {
await setAndVerifyValue({
Expand Down
8 changes: 7 additions & 1 deletion client/src/OnDeviceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class OnDeviceComponent {
return result.json as ODC.ReturnTimeTaken;
}

public async isShowingOnScreen(args: ODC.GetValueArgs, options: ODC.RequestOptions = {}) {
public async isShowingOnScreen(args: ODC.IsShowingOnScreenArgs, options: ODC.RequestOptions = {}) {
this.conditionallyAddDefaultBase(args);
this.conditionallyAddDefaultNodeReferenceKey(args);
const result = await this.sendRequest(ODC.RequestType.isShowingOnScreen, args, options);
Expand All @@ -614,6 +614,12 @@ export class OnDeviceComponent {
isFullyShowing: boolean;
};
}

public async isSubtype(args: ODC.IsSubtypeArgs, options: ODC.RequestOptions = {}) {
this.conditionallyAddDefaultBase(args);
const result = await this.sendRequest(ODC.RequestType.isSubtype, {...args, convertResponseToJsonCompatible: false}, options);
return result.json.isSubtype as boolean;
}
//#endregion

//#region requests run on task thread
Expand Down
15 changes: 14 additions & 1 deletion client/src/types/OnDeviceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum RequestType {
getVolumeList = 'getVolumeList',
hasFocus = 'hasFocus',
isInFocusChain = 'isInFocusChain',
isSubtype = 'isSubtype',
isShowingOnScreen = 'isShowingOnScreen',
onFieldChangeOnce = 'onFieldChangeOnce',
readFile = 'readFile',
Expand All @@ -42,7 +43,7 @@ export enum RequestType {
writeRegistry = 'writeRegistry',
}

export type RequestArgs = CallFuncArgs | CreateChildArgs | GetFocusedNodeArgs | GetValueArgs | GetValuesArgs | HasFocusArgs | IsInFocusChainArgs | OnFieldChangeOnceArgs | SetValueArgs | ReadRegistryArgs | WriteRegistryArgs | DeleteRegistrySectionsArgs | DeleteEntireRegistrySectionsArgs | StoreNodeReferencesArgs | GetNodesInfoArgs | FindNodesAtLocationArgs | CreateDirectoryArgs | DeleteEntireRegistrySectionsArgs | DeleteFileArgs | DeleteNodeReferencesArgs | DisableScreensaverArgs | FocusNodeArgs | GetAllCountArgs | GetDirectoryListingArgs | GetNodesWithPropertiesArgs | GetRootsCountArgs | GetServerHostArgs | GetVolumeListArgs | ReadFileArgs | RenameFileArgs | SetSettingsArgs | StartResponsivenessTestingArgs | StatPathArgs | WriteFileArgs | RemoveNodeArgs |RemoveNodeChildrenArgs | DisableScreensaverArgs;
export type RequestArgs = CallFuncArgs | CreateChildArgs | GetFocusedNodeArgs | GetValueArgs | GetValuesArgs | HasFocusArgs | IsInFocusChainArgs | OnFieldChangeOnceArgs | SetValueArgs | ReadRegistryArgs | WriteRegistryArgs | DeleteRegistrySectionsArgs | DeleteEntireRegistrySectionsArgs | StoreNodeReferencesArgs | GetNodesInfoArgs | FindNodesAtLocationArgs | CreateDirectoryArgs | DeleteEntireRegistrySectionsArgs | DeleteFileArgs | DeleteNodeReferencesArgs | DisableScreensaverArgs | FocusNodeArgs | GetAllCountArgs | GetDirectoryListingArgs | GetNodesWithPropertiesArgs | GetRootsCountArgs | GetServerHostArgs | GetVolumeListArgs | IsShowingOnScreenArgs | IsSubtypeArgs | ReadFileArgs | RenameFileArgs | SetSettingsArgs | StartResponsivenessTestingArgs | StatPathArgs | WriteFileArgs | RemoveNodeArgs |RemoveNodeChildrenArgs | DisableScreensaverArgs;

export enum BaseType {
global = 'global',
Expand Down Expand Up @@ -355,6 +356,18 @@ export interface FindNodesAtLocationArgs extends StoreNodeReferencesArgs {
includeBoundingRectInfo?: true;
}

export interface IsShowingOnScreenArgs extends BaseKeyPath {}

export interface IsSubtypeArgs extends BaseKeyPath {
/** The subtype we are checking against */
subtype: string;

/** isSubtype does not normally match the current node's subtype which is usually not the desired behavior.
* Because of this we also will match on the node's own subtype by default.
* Setting this to false will make it match on only a descendant subtype. */
matchOnSelfSubtype?: boolean;
}

interface MatchObject extends BaseKeyPath {
/** If the match value is passed in then the observer will be fired when the field value equals to the value in match */
value: ComparableValueTypes;
Expand Down
33 changes: 33 additions & 0 deletions device/components/RTA_OnDeviceComponent.brs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sub init()
"hasFocus": processHasFocusRequest
"isInFocusChain": processIsInFocusChainRequest
"isShowingOnScreen": processIsShowingOnScreenRequest
"isSubtype": processIsSubtypeRequest
"onFieldChangeOnce": processOnFieldChangeOnceRequest
"removeNode": processRemoveNodeRequest
"removeNodeChildren": processRemoveNodeChildrenRequest
Expand Down Expand Up @@ -577,6 +578,38 @@ function calculateNodeCount(nodes) as Object
return result
end function

function processIsSubtypeRequest(request) as Object
args = request.args

subtype = args.subtype
if NOT RTA_isString(subtype) then
return RTA_buildErrorResponseObject("Missing valid 'subtype' param")
end if

result = processGetValueRequest(args)
if RTA_isErrorObject(result) then
return result
end if

if result.found <> true then
return RTA_buildErrorResponseObject("No value found at key path '" + RTA_getStringAtKeyPath(args, "keyPath") + "'")
end if

node = result.value
if NOT RTA_isNode(node) then
return RTA_buildErrorResponseObject("Value at key path '" + RTA_getStringAtKeyPath(args, "keyPath") + "' was not a node")
end if

isSubtype = node.isSubtype(subtype)
if NOT isSubtype AND RTA_getBooleanAtKeyPath(args, "matchOnSelfSubtype", true) then
isSubtype = (node.subtype() = subtype)
end if

return {
"isSubtype": isSubtype
}
end function

function processStoreNodeReferencesRequest(request as Object) as Object
args = request.args
nodeRefKey = args.nodeRefKey
Expand Down
Loading