Skip to content

Commit

Permalink
refactor: move sharedState to presenter from context
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 10, 2020
1 parent 33b287b commit bb4cd29
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 81 deletions.
11 changes: 6 additions & 5 deletions src/Context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import he from 'he'
import { set, get } from 'lodash'
import { Macroable } from 'macroable'
import { EdgeError } from 'edge-error'
import { Presenter } from '../Presenter'
import { ContextContract } from '../Contracts'

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ export class Context extends Macroable implements ContextContract {
public $filename = ''
public $lineNumber = 0

constructor (public presenter: any, public sharedState: any) {
constructor (public presenter: Presenter) {
super()
}

Expand All @@ -76,7 +77,7 @@ export class Context extends Macroable implements ContextContract {
private getCurrentState () {
return Object.assign(
{},
this.sharedState,
this.presenter.sharedState,
this.presenter.state,
...this.frames,
)
Expand Down Expand Up @@ -221,10 +222,10 @@ export class Context extends Macroable implements ContextContract {
}

/**
* Finally fallback to defined globals
* Finally fallback to shared globals
*/
value = this.sharedState[key]
return typeof (value) === 'function' ? value.bind(this.sharedState) : value
value = this.presenter.sharedState[key]
return typeof (value) === 'function' ? value.bind(this.presenter.sharedState) : value
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Presenter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
* file that was distributed with this source code.
*/

import { PresenterContract } from '../Contracts'

/**
* The Base presenter is passed to context for reading
* the state values.
*
* However, a custom presenter a do a lot by defining
* custom properties and methods.
*/
export class Presenter {
constructor (public state: any) {
export class Presenter implements PresenterContract {
constructor (public state: any, public sharedState: any) {
}
}
29 changes: 12 additions & 17 deletions src/Template/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @module edge
*/

/*
* edge
*
Expand All @@ -26,10 +22,10 @@ export class Template {
* The shared state is used to hold the globals and locals,
* since it is shared with components too.
*/
private _sharedState: any
private sharedState: any

constructor (private _compiler: CompilerContract, globals: any, locals: any) {
this._sharedState = merge({}, globals, locals)
constructor (private compiler: CompilerContract, globals: any, locals: any) {
this.sharedState = merge({}, globals, locals)
}

/**
Expand All @@ -43,7 +39,7 @@ export class Template {
* ```
*/
public renderInline (templatePath: string): Function {
return new Function('template', 'ctx', this._compiler.compile(templatePath, true).template)
return new Function('template', 'ctx', this.compiler.compile(templatePath, true).template)
}

/**
Expand All @@ -58,11 +54,11 @@ export class Template {
* ```
*/
public renderWithState (template: string, state: any, slots: any): string {
const { template: compiledTemplate, Presenter } = this._compiler.compile(template, false)
const presenter = new (Presenter || BasePresenter)(merge(state, { $slots: slots }))
const ctx = new Context(presenter, this._sharedState)
const { template: compiledTemplate, Presenter } = this.compiler.compile(template, false)
const presenter = new (Presenter || BasePresenter)(merge(state, { $slots: slots }), this.sharedState)
const ctx = new Context(presenter)

return new Function('template', 'ctx', `return ${compiledTemplate}`)(this, ctx)
return new Function('template', 'ctx', compiledTemplate)(this, ctx)
}

/**
Expand All @@ -73,10 +69,9 @@ export class Template {
* ```
*/
public render (template: string, state: any): string {
const { template: compiledTemplate, Presenter } = this._compiler.compile(template, false)
const presenter = new (Presenter || BasePresenter)(state)
const ctx = new Context(presenter, this._sharedState)

return new Function('template', 'ctx', `return ${compiledTemplate}`)(this, ctx)
const { template: compiledTemplate, Presenter } = this.compiler.compile(template, false)
const presenter = new (Presenter || BasePresenter)(state, this.sharedState)
const ctx = new Context(presenter)
return new Function('template', 'ctx', compiledTemplate)(this, ctx)
}
}
4 changes: 2 additions & 2 deletions test/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ test.group('Compiler | Compile', (group) => {
try {
new Function('template', 'ctx', compiler.compile('index.edge', false).template)(
{},
new Context({ state: {} }, {}),
new Context({ state: {}, sharedState: {} }),
)
} catch (error) {
assert.equal(error.message, 'getUserName is not a function')
Expand Down Expand Up @@ -570,7 +570,7 @@ test.group('Compiler | Compile', (group) => {

try {
const fn = new Function('template', 'ctx', compiler.compile('index.edge', false).template)
fn({}, new Context({ state: {} }, {}))
fn({}, new Context({ state: {}, sharedState: {} }))
} catch (error) {
assert.equal(error.message, 'getContent is not a function')
assert.equal(error.filename, join(fs.basePath, 'index.edge'))
Expand Down
Loading

0 comments on commit bb4cd29

Please sign in to comment.