Skip to content

Commit

Permalink
Merge pull request #143 from triwav/add_field_to_getValue
Browse files Browse the repository at this point in the history
Add `field` to getValue and fix usage of `field` on onFieldChangeOnce
  • Loading branch information
triwav authored Sep 5, 2024
2 parents db15b13 + b6f72c8 commit 84427e5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion client/src/NetworkProxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import * as assert from 'assert';
import { utils } from './utils';
import { ecp, odc, device, proxy } from '.';

describe('NetworkProxy', function () {
// Skipping as pretty slow to run and sometimes fails
describe.skip('NetworkProxy', function () {
before(async () => {
await device.deploy({
rootDir: '../testProject',
Expand Down
11 changes: 11 additions & 0 deletions client/src/OnDeviceComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,17 @@ describe('OnDeviceComponent', function () {
expect(timeTaken).to.be.a('number');
});

it('should succeed if provided a field', async () => {
const args = { base: 'global', keyPath: 'AuthManager', field: 'isLoggedIn' } as ODC.BaseKeyPath;
await setAndVerifyValue({ ...args, value: false });
const observePromise = odc.onFieldChangeOnce({ ...args });
await setAndVerifyValue({ ...args, value: true });
const { value, observerFired, timeTaken } = await observePromise;
expect(value).to.be.true;
expect(observerFired).to.be.true;
expect(timeTaken).to.be.a('number');
});

it('should wait for value to match if requested and should work with simple match property', async () => {
const args = { base: 'global', keyPath: 'stringValue' } as ODC.BaseKeyPath;
const expectedValue = utils.addRandomPostfix('secondValue');
Expand Down
6 changes: 4 additions & 2 deletions client/src/OnDeviceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,19 @@ export class OnDeviceComponent {
public async onFieldChangeOnce(args: ODC.OnFieldChangeOnceArgs, options: ODC.RequestOptions = {}) {
this.conditionallyAddDefaultBase(args);
this.conditionallyAddDefaultNodeReferenceKey(args);
args = this.breakOutFieldFromKeyPath(args);

const match = args.match;
if (match !== undefined) {
// Check if it's an object. Also have to check constructor as array is also an instanceof Object, make sure it has the keyPath key
if (((match instanceof Object) && (match.constructor.name === 'Object') && ('keyPath' in match))) {
this.conditionallyAddDefaultBase(match);
} else {
// If it's not we take base and keyPath from the base and keyPath args
// If it's not we take base and keyPath from the base, keyPath and field args
args.match = {
base: args.base,
keyPath: args.keyPath,
field: args.field,
value: (match as any)
};
}
Expand All @@ -226,7 +228,7 @@ export class OnDeviceComponent {

args.retryTimeout = retryTimeout;

const result = await this.sendRequest(ODC.RequestType.onFieldChangeOnce, this.breakOutFieldFromKeyPath(args), options);
const result = await this.sendRequest(ODC.RequestType.onFieldChangeOnce, args, options);
return result.json as {
/** If a match value was provided and already equaled the requested value the observer won't get fired. This lets you be able to check if that occurred or not */
observerFired: boolean;
Expand Down
9 changes: 6 additions & 3 deletions client/src/types/OnDeviceComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ export interface GetFocusedNodeArgs extends MaxChildDepth, NodeRefKey {
returnFocusedArrayGridChild?: boolean;
}

export interface GetValueArgs extends BaseKeyPath {}
export interface GetValueArgs extends BaseKeyPath {
/** Allows supplying a keypath to a node and the field fpr that node separately to allow for better integration with an elements library */
field?: string;
}

export interface GetValuesArgs {
/** Retrieve multiple values with a single request. A list of the individual getValue args */
Expand Down Expand Up @@ -368,7 +371,7 @@ export interface IsSubtypeArgs extends BaseKeyPath {
matchOnSelfSubtype?: boolean;
}

interface MatchObject extends BaseKeyPath {
interface MatchObject extends GetValueArgs {
/** 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 All @@ -380,7 +383,7 @@ export interface OnFieldChangeOnceArgs extends BaseKeyPath {
/** If the `keyPath` does not exist yet, this specifies how long to wait before erroring out in milliseconds */
retryTimeout?: number;

/** The field that we want to observe for changes on. If not supplied, the last part of `keyPath` will be */
/** The field that we want to observe for changes on. If not supplied, the last part of `keyPath` will be used */
field?: string;

/** If provided will only return when this matches (including if it already equals that value) */
Expand Down
17 changes: 16 additions & 1 deletion device/components/RTA_OnDeviceComponent.brs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ function processGetValueRequest(request as Object) as Object
end if

keyPath = RTA_getStringAtKeyPath(args, "keyPath")
if RTA_isNonEmptyString(args.field) then
if keyPath = "" then
keyPath = args.field
else
keyPath += "." + args.field
end if
end if


if RTA_isNonEmptyString(keyPath) then
value = RTA_getValueAtKeyPath(base, keyPath, "[[VALUE_NOT_FOUND]]")
Expand Down Expand Up @@ -325,7 +333,13 @@ end function
function processOnFieldChangeOnceRequest(request as Object) as Dynamic
args = request.args
requestId = request.id
result = processGetValueRequest(args)

' We have to exclude the field from the args so we can get the parent node
getValueArgs = {}
getValueArgs.append(args)
getValueArgs.field = invalid

result = processGetValueRequest(getValueArgs)
if RTA_isErrorObject(result) then
return result
end if
Expand All @@ -336,6 +350,7 @@ function processOnFieldChangeOnceRequest(request as Object) as Dynamic
parentRTA_isNode = RTA_isNode(node)
fieldExists = parentRTA_isNode AND node.doesExist(field)
timePassed = 0

if NOT parentRTA_isNode OR NOT fieldExists then
retryTimeout = args.retryTimeout
if retryTimeout > 0 then
Expand Down

0 comments on commit 84427e5

Please sign in to comment.