Skip to content

Commit

Permalink
revert(component): revert 8152392 (add memoizeWith()) (#63)
Browse files Browse the repository at this point in the history
affects: @action-land/component
  • Loading branch information
smehta91 authored and tusharmath committed Sep 25, 2019
1 parent 002a94b commit df229d0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 166 deletions.
86 changes: 11 additions & 75 deletions modules/component/src/componentNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ export type iComponentNext<P, S> = ComponentNext<mergeProps<P, S>>
* Removes duplicate A | A insertions.
*/
export type U<A, B> = A | B extends A & B ? A : A | B

/**
* Comparator function used for memoization
*/
type Comparator = <S, P>(s1: S, p1: P, s2?: S, p2?: P) => boolean

const defaultComparator: Comparator = () => true

const arg2 = <A, B>(a: A, b: B) => b

/**
Expand All @@ -51,8 +43,7 @@ export class ComponentNext<P1 extends ComponentProps> {
readonly _command: (a: Action<unknown>, b: unknown) => unknown,
readonly _view: (e: unknown, s: unknown, p: unknown) => unknown,
private readonly _children: {[k: string]: ComponentNext<any>},
private readonly _iActions: LinkedList<string | number>,
private readonly _comparator: Comparator
private readonly _iActions: LinkedList<string | number>
) {}

/**
Expand Down Expand Up @@ -81,8 +72,7 @@ export class ComponentNext<P1 extends ComponentProps> {
Nil,
() => undefined,
{},
LinkedList.empty,
defaultComparator
LinkedList.empty
)
}

Expand Down Expand Up @@ -147,8 +137,7 @@ export class ComponentNext<P1 extends ComponentProps> {
this._command,
this._view,
this._children,
this._iActions.prepend(type),
this._comparator
this._iActions.prepend(type)
)
}

Expand Down Expand Up @@ -197,8 +186,7 @@ export class ComponentNext<P1 extends ComponentProps> {
},
this._view,
this._children,
this._iActions.prepend(type),
this._comparator
this._iActions.prepend(type)
)
}

Expand Down Expand Up @@ -311,8 +299,7 @@ export class ComponentNext<P1 extends ComponentProps> {
},
this._view,
spec,
Object.keys(spec).reduce((a, b) => a.prepend(b), this._iActions),
this._comparator
Object.keys(spec).reduce((a, b) => a.prepend(b), this._iActions)
)
}
/**
Expand Down Expand Up @@ -373,25 +360,11 @@ export class ComponentNext<P1 extends ComponentProps> {
p: P
) => V
): iComponentNext<P1, {oView: V; iProps: P}> {
let cachedProps: iProps<P1>
let cachedState: oState<P1>
let cachedView: V

return new ComponentNext(
this._init,
this._update,
this._command,
function(this: ComponentNext<P1>, e: any, s: any, p: any) {
if (
cachedView !== undefined &&
this._comparator !== defaultComparator &&
this._comparator(s, p, cachedState, cachedProps)
) {
return cachedView
}
cachedProps = p
cachedState = s

(e: any, s: any, p) => {
const children: any = {}
for (let i in this._children) {
if (this._children.hasOwnProperty(i)) {
Expand All @@ -404,19 +377,17 @@ export class ComponentNext<P1 extends ComponentProps> {
...actions,
[key]: (ev: any) => e.of(key).emit(ev)
}))

return (cachedView = cb(
return cb(
{
actions: actions as any,
state: s,
children
},
p as any
))
)
},
this._children,
this._iActions,
this._comparator
this._iActions
)
}

Expand Down Expand Up @@ -446,41 +417,7 @@ export class ComponentNext<P1 extends ComponentProps> {
this._command,
this._view,
this._children,
this._iActions,
defaultComparator
)
}

/**
* Memoize component view based on comparator passed
* @param fn Comparator function which decides whether to return cached view or calculate new view
*/
memoizeWith(
fn: (
s1: oState<P1> | iState<P1>,
p1: iProps<P1>,
s2?: oState<P1> | iState<P1>,
p2?: iProps<P1>
) => boolean
): ComponentNext<P1> {
return new ComponentNext(
this._init,
this._update,
this._command,
this._view,
this._children,
this._iActions,
(...t) => {
const args = t as [
oState<P1> | iState<P1>,
iProps<P1>,
oState<P1> | iState<P1>,
iProps<P1>
]
return this._comparator === defaultComparator
? fn(...args)
: this._comparator(...args) && fn(...args)
}
this._iActions
)
}
/**
Expand All @@ -505,8 +442,7 @@ export class ComponentNext<P1 extends ComponentProps> {
component.command,
component.view as any,
{},
LinkedList.empty,
defaultComparator
LinkedList.empty
)
}
/**
Expand Down
71 changes: 0 additions & 71 deletions test/component/componentNext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,75 +328,4 @@ describe('ComponentNext', () => {
assert.deepEqual(actual, expected)
})
})

describe('memoizeWith', () => {
it('should not memoize component by default', () => {
const component = ComponentNext.lift({count: 1}).render(_ =>
Math.random()
)

const a = component._view({}, component._init(), null)
const b = component._view({}, component._init(), null)

assert.notEqual(a, b)
})
it('should memoize the view', () => {
const component = ComponentNext.lift(0)
.render(_ => Math.random())
.memoizeWith(() => true)
const a = component._view({}, component._init(), null)
const b = component._view({}, component._init(), null)

assert.strictEqual(a, b)
})

it('should compute view on first call', () => {
const component = ComponentNext.lift(0)
.render(_ => [_.state])
.memoizeWith(() => true)
const actual = component._view({}, component._init(), null)
const expected = [0]

assert.deepStrictEqual(actual, expected)
})

it('should pass pervious state/props to fn', () => {
const args = new Array<any>()
const component = ComponentNext.lift(0)
.render(_ => [_.state])
.memoizeWith((...t) => {
args.push(t)
return false
})

component._view({}, 100, 'A')
component._view({}, 200, 'B')

assert.deepStrictEqual(args, [[200, 'B', 100, 'A']])
})

it('should intersect multiple memoizeWith', () => {
const component = ComponentNext.lift(0)
.render(_ => [_.state])
.memoizeWith((s1, p1, s2, p2) => s1 === s2)
.memoizeWith((s1, p1, s2, p2) => p1 === p2)

const a = component._view({}, 100, 'A')
const b = component._view({}, 100, 'A')

assert.strictEqual(a, b)
})

it('should combine multiple memoizeWith', () => {
const component = ComponentNext.lift(0)
.render(_ => Math.random())
.memoizeWith(() => true)
.memoizeWith(() => false)

const a = component._view({}, 100, 'A')
const b = component._view({}, 100, 'A')

assert.notStrictEqual(a, b)
})
})
})
20 changes: 0 additions & 20 deletions typings/component.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,3 @@ $(

// $ExpectType { a: string; }
$(ComponentNext.lift({a: ''}).render(_ => _.state)).oView

// ComponentNext.memoizeWith should provide current state and props to the callback
ComponentNext.lift({color: 'red'})
.render((_, p: string) => p)

.memoizeWith((s1, p1, s2, p2) => {
// $ExpectType { color: string; }
s1

// $ExpectType string
p1

// $ExpectType { color: string; } | undefined
s2

// $ExpectType string | undefined
p2

return true
})

0 comments on commit df229d0

Please sign in to comment.