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

add ignore parameter to serialize #325

Merged
merged 1 commit into from Apr 13, 2020
Merged
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
5 changes: 4 additions & 1 deletion packages/effector/fork.d.ts
Expand Up @@ -18,7 +18,10 @@ export function hydrate(domain: Domain, config: {values: ValueMap}): void
/**
serialize state on server
*/
export function serialize(scope: Scope): {[sid: string]: any}
export function serialize(
scope: Scope,
{ignore = []}: {ignore?: Array<Store<any>>} = {},
): {[sid: string]: any}

/** bind event to scope from .watch call */
export function scopeBind<T>(unit: Unit<T>): (payload: T) => void
Expand Down
63 changes: 63 additions & 0 deletions src/effector/__tests__/fork.test.js
@@ -0,0 +1,63 @@
//@noflow

import {createDomain} from 'effector'
import {fork, serialize} from 'effector/fork'

it('serialize stores to object of sid as keys', () => {
const app = createDomain()
const $a = app.createStore('value', {sid: 'a'})
const $b = app.createStore([], {sid: 'b'})
const $c = app.createStore(null, {sid: 'c'})
const $d = app.createStore(false, {sid: 'd'})

const scope = fork(app)
const values = serialize(scope)

expect(values).toMatchInlineSnapshot(`
Object {
"a": "value",
"b": Array [],
"c": null,
"d": false,
}
`)
})

it('serialize stores with ignore parameter', () => {
const app = createDomain()
const $a = app.createStore('value', {sid: 'a'})
const $b = app.createStore([], {sid: 'b'})
const $c = app.createStore(null, {sid: 'c'})
const $d = app.createStore(false, {sid: 'd'})

const scope = fork(app)
const values = serialize(scope, {ignore: [$b, $d]})

expect(values).toMatchInlineSnapshot(`
Object {
"a": "value",
"c": null,
}
`)
})

it('serialize stores in nested domain', () => {
const app = createDomain()
const first = app.createDomain()
const second = app.createDomain()
const third = second.createDomain()
const $a = first.createStore('value', {sid: 'a'})
const $b = second.createStore([], {sid: 'b'})
const $c = third.createStore(null, {sid: 'c'})
const $d = app.createStore(false, {sid: 'd'})

const scope = fork(app)
const values = serialize(scope, {ignore: [$d, $a]})

expect(values).toMatchInlineSnapshot(`
Object {
"b": Array [],
"c": null,
}
`)
})
8 changes: 7 additions & 1 deletion src/effector/fork.js
Expand Up @@ -45,13 +45,19 @@ export function hydrate(domain, {values}) {
/**
serialize state on server
*/
export function serialize({clones}) {
export function serialize(
{clones},
{ignore = []}: {ignore?: Array<Store<any>>} = {},
) {
const result = {}
for (const {meta, scope, reg} of clones) {
if (meta.unit !== 'store') continue
if (!meta.sid) continue
result[meta.sid] = reg[scope.state.id].current
}
for (const {sid} of ignore) {
if (sid) delete result[sid]
}
return result
}

Expand Down
65 changes: 65 additions & 0 deletions src/types/__tests__/effector/fork.test.js
@@ -0,0 +1,65 @@
// @flow
/* eslint-disable no-unused-vars */
import {createDomain, Store} from 'effector'
import {fork, serialize} from 'effector/fork'

const typecheck = '{global}'

describe('serialize cases (should pass)', () => {
test('serialize(Scope): {[sid: string]: any}', () => {
const app = createDomain()
const $a = app.createStore('demo')

const scope = fork(app)
const values: {[sid: string]: string} = serialize(scope)

expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors

--flow--
no errors
"
`)
})

test('serialize(Scope, {ignore:[Store<any>]}): {[sid: string]: any}', () => {
const app = createDomain()
const $a = app.createStore('demo')
const $b = app.createStore(5)

const scope = fork(app)
const values: {[sid: string]: number} = serialize(scope, {ignore: [$b]})

expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors

--flow--
no errors
"
`)
})
})

describe('serialize cases (should fail)', () => {
test('serialize(Scope, {ignore:[Event<any>]}): {[sid: string]: any}', () => {
const app = createDomain()
const event = app.createEvent()

const scope = fork(app)
const values: {[sid: string]: any} = serialize(scope, {ignore: [event]})

expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors

--flow--
no errors
"
`)
})
})