Skip to content

Commit

Permalink
Export lifecycle (#88)
Browse files Browse the repository at this point in the history
* cleanups

* index.d.ts updated

* bump version
  • Loading branch information
geoffhendrey authored Oct 24, 2024
1 parent 6bd3fea commit 4aabbb0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stated-js",
"version": "0.1.41",
"version": "0.1.42",
"license": "Apache-2.0",
"description": "JSONata embedded in JSON",
"main": "./dist/src/index.js",
Expand Down
16 changes: 8 additions & 8 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type PlanStep = {
forkId:string,
didUpdate:boolean
}
export type Mutation = {jsonPtr:JsonPointerString, op:Op, value:any};
export type Mutation = {jsonPtr:JsonPointerString, op:Op, data:any};

//A Transaction is a set of changes applied atomically.
export type Transaction ={
Expand Down Expand Up @@ -1223,16 +1223,16 @@ export default class TemplateProcessor {
*/
private async applyTransaction(transaction: Transaction) {
const ptrs: JsonPointerString[] = [];
for (const { jsonPtr, value, op } of transaction.mutations) {
for (const { jsonPtr, data, op } of transaction.mutations) {
ptrs.push(jsonPtr);
if (op === 'set') {
jp.set(this.output, jsonPtr, value);
jp.set(this.output, jsonPtr, data);
} else if (op === 'delete') {
jp.remove(this.output, jsonPtr);
} else {
throw new Error(`Transaction cannot include Op type ${op}`);
}
await this.callDataChangeCallbacks(value, jsonPtr, op === 'delete', op);
await this.callDataChangeCallbacks(data, jsonPtr, op === 'delete', op);
}
await this.callDataChangeCallbacks(this.output, ptrs);
}
Expand All @@ -1254,16 +1254,16 @@ export default class TemplateProcessor {
* @public
*/
public setTransactionCallback(cb: (transaction: Transaction) => Promise<void>) {
const dataChangeCallback = async (value: any, jsonPtrs: JsonPointerString | JsonPointerString[], removed?: boolean, op?: Op) => {
const dataChangeCallback = async (root: any, jsonPtrs: JsonPointerString | JsonPointerString[], removed?: boolean, op?: Op) => {
if (!Array.isArray(jsonPtrs)) { // This is the case where the update is for the root document
throw new Error(`DataChangeHandler for transaction bundling was illegally registered on ${jsonPtrs} (it can only be registered on '/'`);
}
const mutations: Mutation[] = jsonPtrs.map(jsonPtr => {
const value = jp.has(this.output, jsonPtr) ? jp.get(this.output, jsonPtr) : undefined;
const data = jp.has(this.output, jsonPtr) ? jp.get(this.output, jsonPtr) : undefined;
return {
value,
data,
jsonPtr,
op: value === undefined ? "delete" : "set",
op: data === undefined ? "delete" : "set",
};
});
const transaction: Transaction = {
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './TemplateProcessor.ts';
export * as MetaInfoProducer from './MetaInfoProducer.ts';
export {default as JsonPointer} from './JsonPointer.ts'
export {stringifyTemplateJSON} from './utils/stringify.ts'
export * from './Lifecycle.js'

6 changes: 3 additions & 3 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3231,9 +3231,9 @@ test("test transaction", async () => {
const transaction = {
op: "transaction",
mutations:[
{op: "set", jsonPtr: "/b", value: 42},
{op: "delete", jsonPtr: "/c", value: undefined},
{op: "set", jsonPtr: "/d", value: 42}
{op: "set", jsonPtr: "/b", data: 42},
{op: "delete", jsonPtr: "/c", data: undefined},
{op: "set", jsonPtr: "/d", data: 42}
]
}
const tp = new TemplateProcessor(o);
Expand Down

0 comments on commit 4aabbb0

Please sign in to comment.