Skip to content

Commit

Permalink
Add iterable handlers (ending mutators)
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Feb 13, 2024
1 parent 1bc9d70 commit 66a4e21
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
26 changes: 20 additions & 6 deletions lib/mutation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ function prepareContext (context, unwrapOptions) {
commitMode: mutentOptions.commitMode !== undefined
? parseCommitMode(mutentOptions.commitMode)
: context.commitMode,
handlers: context.handlers.concat(
normalizeMutators(mutentOptions.handlers)
),
hooks: mergeHooks(context.hooks, normalizeHooks(mutentOptions.hooks)),
intent,
multiple: isMultiple(intent, argument),
Expand All @@ -121,12 +124,23 @@ function prepareContext (context, unwrapOptions) {
}
}

async function * iterateMethod (context, mutators) {
const { adapter, argument, intent, multiple, options } = context

const iterable = context.mutators.concat(mutators).reduce(
(accumulator, mutator) => mutator(accumulator, context),
iterateEntities(iterateContext(context), context)
async function * iterateMethod (ctx, mutators) {
const { adapter, argument, intent, multiple, options } = ctx

const chain = [
// Plugins' mutators
...ctx.mutators,
// Local chain mutators
...mutators,
// Internal end/safe mutator
mutatorClose,
// Plugins' handlers (end mutators)
...ctx.handlers
]

const iterable = chain.reduce(
(acc, mutator) => mutator(acc, ctx),
iterateEntities(iterateContext(ctx), ctx)
)

let count = 0
Expand Down
1 change: 1 addition & 0 deletions lib/mutation.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function buildMutation (intent, argument, context = {}) {
adapter: {},
argument,
commitMode: 'AUTO',
handlers: [],
intent,
mutators: [],
writeMode: 'AUTO',
Expand Down
16 changes: 15 additions & 1 deletion lib/store.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class Store {
const {
adapter,
commitMode,
handlers,
hooks,
mutators,
plugins,
Expand All @@ -27,13 +28,21 @@ export class Store {
// Save (valid) defaults
this.adapter = adapter
this.commitMode = 'AUTO'
this.handlers = []
this.hooks = normalizeHooks()
this.mutators = []
this.writeMode = 'AUTO'
this.writeSize = 16

// Parse and save submitted options
this.register({ commitMode, hooks, mutators, writeMode, writeSize })
this.register({
commitMode,
handlers,
hooks,
mutators,
writeMode,
writeSize
})

// Register plugins
if (Array.isArray(plugins)) {
Expand All @@ -50,6 +59,7 @@ export class Store {
register (plugin) {
const {
commitMode,
handlers,
hooks,
mutators,
writeMode,
Expand All @@ -59,6 +69,9 @@ export class Store {
if (commitMode !== undefined) {
this.commitMode = parseCommitMode(commitMode)
}
if (handlers !== undefined) {
this.handlers = this.handlers.concat(normalizeMutators(handlers))
}
if (hooks !== undefined) {
this.hooks = mergeHooks(this.hooks, normalizeHooks(hooks))
}
Expand Down Expand Up @@ -92,6 +105,7 @@ export class Store {
adapter: this.adapter,
argument,
commitMode: this.commitMode,
handlers: this.handlers,
hooks: this.hooks,
intent,
mutators: this.mutators,
Expand Down
21 changes: 21 additions & 0 deletions mutent.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,24 @@ test('bulkEntities over bulk', async t => {
'SECOND_UPDATE'
])
})

test('store handlers', async t => {
t.plan(3)

const store = new Store({
adapter: {},
handlers: [
async function * (iterable, ctx) {
t.like(ctx, { multiple: false })
try {
yield * iterable
} catch (err) {
t.like(err, { code: 'EMUT_PARTIAL_ADAPTER' })
}
}
]
})

const result = await store.create('Oh no').unwrap()
t.is(result, null)
})

0 comments on commit 66a4e21

Please sign in to comment.