Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow terminal defer #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/server-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions examples/server-postgres-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/channels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
47 changes: 47 additions & 0 deletions packages/core/src/operations/terminal-defer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import cuillere, { Cuillere, defer, terminal } from '..'

describe('terminal defer', () => {
let cllr: Cuillere

beforeEach(() => {
cllr = cuillere()
})

it('should allow to defer in parent frame', async () => {
const actions: string[] = []

function* resource(id: number) {
actions.push(`create resource ${id}`)

yield terminal(defer(function* cleanupResource() {
actions.push(`cleanup resource ${id}`)
}))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to return something ? For a resource it could be very usefull

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'll change the test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, actually no...


function* test1() {
yield resource(1)

actions.push('use resource 1')
}

function* test2() {
yield resource(2)

actions.push('use resource 2')

throw new Error()
}

await expect(cllr.call(test1)).resolves.toBeUndefined()
await expect(cllr.call(test2)).rejects.toBeInstanceOf(Error)

expect(actions).toEqual([
'create resource 1',
'use resource 1',
'cleanup resource 1',
'create resource 2',
'use resource 2',
'cleanup resource 2',
])
})
})
3 changes: 1 addition & 2 deletions packages/core/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HandlerDescriptor, Validator } from './plugins'
import {
Operation, OperationObject, Wrapper, Execute, CallOperation, NextOperation,
execute, isOperationObject, isOperation, isWrapper, isFork, isDefer, isRecover, isTerminal, coreNamespace,
execute, isOperationObject, isOperation, isWrapper, isFork, isRecover, isTerminal, coreNamespace,
} from './operations'
import { error, unrecognizedOperation, CancellationError, captured } from './errors'
import { isGenerator, Generator } from './generator'
Expand Down Expand Up @@ -270,7 +270,6 @@ export class Stack {
coreValidators: Record<string, (operation: OperationObject) => void> = {
[`${coreNamespace}/terminal`]({ operation }: Wrapper) {
if (isFork(operation)) throw new TypeError('terminal forks are forbidden')
if (isDefer(operation)) throw new TypeError('terminal defers are forbidden')
if (isRecover(operation)) throw new TypeError('terminal recovers are forbidden')
if (isTerminal(operation)) throw new TypeError('terminals cannot be nested')
},
Expand Down
16 changes: 5 additions & 11 deletions packages/core/src/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cuillere, { Cuillere, defer, fork, recover, terminal } from '.'
import cuillere, { Cuillere, fork, Plugin, recover, terminal } from '.'
import { OperationObject } from './operations'

describe('validation', () => {
Expand Down Expand Up @@ -53,14 +53,6 @@ describe('validation', () => {
await expect(cllr.call(test)).rejects.toStrictEqual(new TypeError('terminal forks are forbidden'))
})

it('should not accept defer operation', async () => {
function* test() {
yield terminal(defer(dummy()))
}

await expect(cllr.call(test)).rejects.toStrictEqual(new TypeError('terminal defers are forbidden'))
})

it('should not accept recover operation', async () => {
function* test() {
yield terminal(recover())
Expand All @@ -85,7 +77,7 @@ describe('validation', () => {
answer: 42
}

await cuillere({
const plugin: Plugin = {
namespace: '@cuillere/test',
handlers: {
* test({ answer }: TestOperation) {
Expand All @@ -97,7 +89,9 @@ describe('validation', () => {
if (answer !== 42) throw TypeError('answer should be 42')
},
},
}).call(function* test() {
}

await cuillere(plugin).call(function* test() {
yield { kind: '@cuillere/test/test', answer: 42 }
try {
yield { kind: '@cuillere/test/test', answer: 666 }
Expand Down
4 changes: 2 additions & 2 deletions packages/mariadb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/server-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"apollo-server-core": "~2.25.2",
"apollo-server-plugin-base": "^0.13.0",
"eslint": "^7.32.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/server-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.0.2",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"apollo-server-plugin-base": "^0.13.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
Expand Down