Skip to content
Open
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
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ProxyCache } from "./proxy-cache"
import { addSelectorToTree, getRefDescedents, removeSelectorFromTree, SelectorTreeBranch } from "./selector-map"
import { isPromise } from "./utils/isPromise"

export type ObjectTree = object

Expand Down Expand Up @@ -299,9 +300,9 @@ export class MutationsManager {
return combinedPatches
}

mutate <T extends ObjectTree>(
mutate <T extends ObjectTree, K extends (mutable: T) => unknown>(
target: T,
callback: (mutable: T) => unknown
callback: K
) {
const isOuterMostTransactionForThisObject = !this.hasRoot(target)
if ( isOuterMostTransactionForThisObject ) {
Expand All @@ -313,7 +314,18 @@ export class MutationsManager {
return
}

callback(proxyWrapper)
const result = callback(proxyWrapper)

if ( isPromise(result) ) {
return result.then(() => {
// only return the patches on the top most level
if ( isOuterMostTransactionForThisObject ) {
return this.commit(target)
}

return []
})
}

// only return the patches on the top most level
if ( isOuterMostTransactionForThisObject ) {
Expand All @@ -326,9 +338,9 @@ export class MutationsManager {

const mutationsManager = new MutationsManager()

export const mutate = <T extends ObjectTree>(
export const mutate = <T extends ObjectTree, K extends (mutable: T) => unknown>(
stateTree: T,
callback: (mutable: T) => unknown
callback: K
) => {
return mutationsManager.mutate(stateTree, callback)
}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/isPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function isPromise(p: any): p is Promise<unknown> {
return (
p !== null &&
typeof p === 'object' &&
typeof p.then === 'function' &&
typeof p.catch === 'function'
)
}
8 changes: 4 additions & 4 deletions tests/nested-mutate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('nested mutate calls', () => {
it('generates correct patches on each nested mutate call for the same object', () => {
const state: Record<string, string> = {}

let innerPatches1: JSONPatchEnhanced[] = []
let innerPatches2: JSONPatchEnhanced[] = []
let innerPatches1: JSONPatchEnhanced[] | Promise<JSONPatchEnhanced[]> = []
let innerPatches2: JSONPatchEnhanced[] | Promise<JSONPatchEnhanced[]> = []

const outerPatch = mutate(state, (modifiable) => {
modifiable.changedTopLevelMutate = 'changed'
Expand All @@ -33,8 +33,8 @@ describe('nested mutate calls', () => {
const state1: Record<string, string> = {}
const state2: Record<string, string> = {}

let outerPatchState2: JSONPatchEnhanced[] = []
let innerPatchState1: JSONPatchEnhanced[] = []
let outerPatchState2: JSONPatchEnhanced[] | Promise<JSONPatchEnhanced[]> = []
let innerPatchState1: JSONPatchEnhanced[] | Promise<JSONPatchEnhanced[]> = []

const outerPatchState1 = mutate(state1, (modifiable) => {
modifiable.changedTopLevelMutate = 'changed'
Expand Down
8 changes: 4 additions & 4 deletions tests/tasklistapp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ describe('Task List project test suite', () => {
})


it('creates json patch for task add, update, remove', () => {
it('creates json patch for task add, update, remove', async () => {
const clientTaskList = new TaskListProject() // client
const serverTaskList = new TaskListProject() // server

// ui click -> bla bla bla -> action to add task
const clientPatchArray = mutate(clientTaskList, (mutateTaskList) => {
const clientPatchArray = await mutate(clientTaskList, (mutateTaskList) => {
mutateTaskList.createTask('test task')
})

Expand All @@ -243,7 +243,7 @@ describe('Task List project test suite', () => {


const NEW_TASK_TITLE = 'renamed task title'
const jsonPatchForModificationOfTask = mutate(clientTaskList, (mutateTaskList) => {
const jsonPatchForModificationOfTask = await mutate(clientTaskList, (mutateTaskList) => {
const targetTask = mutateTaskList.tasks[idOfCreatedTask]
targetTask!.title = NEW_TASK_TITLE
targetTask!.checked = true
Expand All @@ -254,7 +254,7 @@ describe('Task List project test suite', () => {
expect(serverTaskList.tasks[idOfCreatedTask]).toHaveProperty('checked', true)
expect(serverTaskList.tasks[idOfCreatedTask]).toHaveProperty('title', NEW_TASK_TITLE)

const jsonPatchForDelete = mutate(clientTaskList, (mutateTaskList) => {
const jsonPatchForDelete = await mutate(clientTaskList, (mutateTaskList) => {
mutateTaskList.removeTaskById(idOfCreatedTask)
})

Expand Down