Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Sep 29, 2024
1 parent 614b3a4 commit 40d1d52
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,23 @@ Generate construction by specified template.

## `client.createElementaryEventSubscriptions(params: ScEventSubscriptionParams[]): ScEventSubscription[]`

Subscribe to event. Event callback, passed to `ScEventSubscriptionParams` constructor, has 4 parameters: subscribedAddr, foundConnector addr, foundNode addr and created eventId. The last one may be used to destroy event after having some specific result.
Subscribe to event. Event callback, passed to `ScEventSubscriptionParams` constructor, has 4 parameters: `subscribedAddr`, `connectorAddr` (sc-address of sc-connector generated or erased from or to sc-element with sc-address `subscribedAddr`), `otherElementAddr` (it equals to source sc-element of sc-connector with sc-address `connectorAddr`, if target sc-element of this sc-connector is sc-element with sc-address `subscribedAddr`, otherwise, it equals to target sc-element of this sc-connector) and created eventId. The last one may be used to destroy event after having some specific result.

```ts
import { ScAddr } from "ts-sc-client";
import { client } from "../path-to-client";

const callback = (subscribedAddr: ScAddr, foundConnector: ScAddr, foundNode: ScAddr, createdEventId: number) => {
const callback = (subscribedAddr: ScAddr, connectorAddr: ScAddr, otherElementAddr: ScAddr, createdEventId: number) => {
// some logic here
}

const evtParams = new ScEventSubscriptionParams(
const eventSubscriptionParams = new ScEventSubscriptionParams(
fakeAddr2,
ScEventType.RemoveIngoingEdge,
callback
);

const res = await client.createElementaryEventSubscriptions([evtParams]);
const res = await client.createElementaryEventSubscriptions([eventSubscriptionParams]);
```

## `client.destroyElementaryEventSubscriptions(eventIds: number[]): boolean`
Expand All @@ -370,7 +370,7 @@ Destroy an event. Input arguments are event ids returned by createElementaryEven
await client.destroyElementaryEventSubscriptions(eventIds);
```

## `client.searchKeynodes(...keynodesInShakeCase: string[]): { keynodesInCamelCase: ScAddr }`
## `client.searchKeynodes(...keynodesInSnakeCase: string[]): { keynodesInCamelCase: ScAddr }`

This method is a wrapper on resolveKeynodes. It returns object with specified keynodes strings as described bellow. Also it caches requested keynodes.

Expand Down
6 changes: 3 additions & 3 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Breaking changes
- This version is compatible with version of the sc-machine 0.10.0. All API methods were redesigned. Misleading ones were removed, new ones were added. See table below, to learn more about changes.
- This version is compatible with version of the sc-machine 0.10.0. All API methods were redesigned. Incorrect ones were removed, new ones were added. See table below, to learn more about changes.

| Deprecated method | Substitution method |
|----------------------------------------|-------------------------------------------|
Expand All @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
| createNode | generateNode |
| createLink | generateLink |
| createEdge | generateConnector |
| createElementsBySCs | generateElements_by_scs |
| createElementsBySCs | generateElementsBySCs |
| deleteElements | eraseElements |
| getLinksByContents | searchLinksByContents |
| getLinksByContentSubstrings | searchLinksByContentSubstrings |
Expand All @@ -38,7 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
| RemoveElement | BeforeEraseElement |
| ChangeContent | BeforeChangeLinkContent |

New sc-event types: `AfterGenerateConnector`, `AfterGenerateEdge`, `BeforeEraseConnector`, `BeforeEraseEdge` were added.
- New sc-event types: `AfterGenerateConnector`, `AfterGenerateEdge`, `BeforeEraseConnector`, `BeforeEraseEdge` were added.

### Added
- sc-event types: `AfterGenerateConnector`, `AfterGenerateOutgoingArc`, `AfterGenerateIncomingArc`, `AfterGenerateEdge`, `BeforeEraseConnector`, `BeforeEraseOutgoingArc`, `BeforeEraseIncomingArc`, `BeforeEraseEdge`, `BeforeEraseElement`, `BeforeChangeLinkContent`
Expand Down
2 changes: 1 addition & 1 deletion src/ScClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class ScClient {
* @deprecated ScClient `getLinksByContents` method is deprecated. Use `searchLinksByContents` instead.
*/
public async getLinksByContents(contents: string[]) {
console.warn("Warning: ScClient `getLinksByContents` method is deprecated. Use `getLinksByContents` instead.");
console.warn("Warning: ScClient `getLinksByContents` method is deprecated. Use `searchLinksByContents` instead.");
return this.searchLinksByContents(contents);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ScConstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ScConstruction {
alias?: string
) {
if (!type.isEdge()) {
invalidValue("You should pass edge type there");
invalidValue("You should pass connector type there");
}
const cmd = new ScConstructionCommand(type, {
src: source,
Expand Down
34 changes: 21 additions & 13 deletions src/ScHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ export class ScHelper {
return keynodes[snakeToCamelCase(String(addrOrSystemId))].value;
}

public getAnswer(actionNode: ScAddr) {
public getResult(actionNode: ScAddr) {
return new Promise<ScAddr>((resolve) => {
(async () => {
const { nrelAnswer } = await this._client.searchKeynodes("nrel_answer");
const { nrelResult } = await this._client.searchKeynodes("nrel_result");

const onActionFinished = async (
_subscibedAddr: ScAddr,
_subscribedAddr: ScAddr,
arc: ScAddr,
anotherAddr: ScAddr,
eventId: number
) => {
const template = new ScTemplate();
template.triple(nrelAnswer, ScType.EdgeAccessVarPosPerm, arc);
const isNrelAnswer = (await this._client.searchByTemplate(template))
template.triple(nrelResult, ScType.EdgeAccessVarPosPerm, arc);
const isNrelResult = (await this._client.searchByTemplate(template))
.length;
if (!isNrelAnswer) return;
this._client.eventsDestroy(eventId);
if (!isNrelResult) return;
this._client.destroyElementaryEventSubscriptions(eventId);
resolve(anotherAddr);
};

Expand All @@ -122,28 +122,36 @@ export class ScHelper {

const [eventId] = await this._client.createElementaryEventSubscriptions(eventParams);

const answerAlias = "_answer";
const resultAlias = "_result";

const template = new ScTemplate();
template.quintuple(
actionNode,
ScType.EdgeDCommonVar,
[ScType.NodeVar, answerAlias],
[ScType.NodeVar, resultAlias],
ScType.EdgeAccessVarPosPerm,
nrelAnswer
nrelResult
);
const searchRes = await this._client.searchByTemplate(template);

const answer = searchRes[0]?.get(answerAlias);
const result = searchRes[0]?.get(resultAlias);

if (!answer) return;
if (!result) return;

this._client.destroyElementaryEventSubscriptions(eventId.id);
resolve(answer);
resolve(result);
})();
});
}

/*!
* @deprecated ScHelper `getAnswer` method is deprecated. Use `getResult` instead.
*/
public getAnswer(actionNode: ScAddr) {
console.warn("Warning: ScHelper `getAnswer` method is deprecated. Use `getResult` instead.");
return this.getResult(actionNode);
}

public async generateLink(item: string) {
const constructionLink = new ScConstruction();
constructionLink.generateLink(
Expand Down
10 changes: 5 additions & 5 deletions src/ScSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class ScSet {
private _onInitialize: CallbackInitialize | null = null;
private _filterType: ScType | null | undefined = null; // just elements with this type will be processed by set

private _evtAddElement: ScEventSubscription | undefined;
private _evtRemoveElement: ScEventSubscription | undefined;
private _evtGenerateElement: ScEventSubscription | undefined;
private _evtEraseElement: ScEventSubscription | undefined;

constructor(
scClient: ScClient,
Expand All @@ -57,7 +57,7 @@ export class ScSet {
// subscribe to events
if (!this._addr) return;

const events = await this._scClient?.eventsCreate([
const eventSubscriptions = await this._scClient?.createElementaryEventSubscriptions([
new ScEventSubscriptionParams(
this._addr,
ScEventType.AfterGenerateOutgoingArc,
Expand All @@ -70,8 +70,8 @@ export class ScSet {
),
]);

this._evtAddElement = events?.[0];
this._evtRemoveElement = events?.[1];
this._evtGenerateElement = eventSubscriptions?.[0];
this._evtEraseElement = eventSubscriptions?.[1];

await this.iterateExistingElements();

Expand Down

0 comments on commit 40d1d52

Please sign in to comment.