Skip to content

Commit

Permalink
added: initial pure implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Dec 21, 2019
1 parent c16314b commit a21b6eb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
15 changes: 15 additions & 0 deletions riot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export interface RiotCoreComponent<P = object, S = object> {
$$(selector: string): [HTMLElement]
}

// Riot Pure Component interface that should be used together with riot.pure
export interface RiotPureComponent<C = object> {
mount(
element: HTMLElement,
context?: C,
): any
update(
context?: C,
): any
unmount(keepRootElement: boolean): any
}

// This object interface is created anytime a riot file will be compiled into javascript
export interface RiotComponentShell<P = object, S = object> {
readonly css?: string
Expand Down Expand Up @@ -72,4 +84,7 @@ export function component<P , S>(shell: RiotComponentShell<P, S>):(
initialProps?: P,
meta?: { slots: any[]; attributes: any[]; }
) => RiotComponent<P, S>
export function pure<
C = object,
F = ({ slots?: any[]; attributes?: any[]; props?: C; }) => RiotPureComponent<C>>(func: F): F
export const version: string
18 changes: 15 additions & 3 deletions src/core/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ATTRIBUTES_KEY_SYMBOL,
COMPONENTS_IMPLEMENTATION_MAP,
DOM_COMPONENT_INSTANCE_PROPERTY,
IS_PURE_SYMBOL,
PARENT_KEY_SYMBOL,
PLUGINS_SET,
TEMPLATE_KEY_SYMBOL
Expand Down Expand Up @@ -39,6 +40,12 @@ const COMPONENT_CORE_HELPERS = Object.freeze({
$$(selector){ return $(selector, this.root) }
})

const PURE_COMPONENT_API = Object.freeze({
update: noop,
mount: noop,
unmount: noop
})

const COMPONENT_LIFECYCLE_METHODS = Object.freeze({
shouldUpdate: noop,
onBeforeMount: noop,
Expand All @@ -50,9 +57,7 @@ const COMPONENT_LIFECYCLE_METHODS = Object.freeze({
})

const MOCKED_TEMPLATE_INTERFACE = {
update: noop,
mount: noop,
unmount: noop,
...PURE_COMPONENT_API,
clone: noop,
createDOM: noop
}
Expand Down Expand Up @@ -89,6 +94,13 @@ export function createComponent({css, template, exports, name}) {
) : MOCKED_TEMPLATE_INTERFACE

return ({slots, attributes, props}) => {
// pure components rendering will be managed by the end user
if (exports && exports[IS_PURE_SYMBOL])
return defineDefaults(
exports({ slots, attributes, props }),
PURE_COMPONENT_API
)

const componentAPI = callOrAssign(exports) || {}

const component = defineComponent({
Expand Down
1 change: 1 addition & 0 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const
PLUGINS_SET = new Set(),
IS_DIRECTIVE = 'is',
VALUE_ATTRIBUTE = 'value',
IS_PURE_SYMBOL = Symbol.for('pure'),
PARENT_KEY_SYMBOL = Symbol('parent'),
ATTRIBUTES_KEY_SYMBOL = Symbol('attributes'),
TEMPLATE_KEY_SYMBOL = Symbol('template')
11 changes: 11 additions & 0 deletions src/riot.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ export function component(implementation) {
)(implementation)
}

/**
* Lift a riot component Interface into a pure riot object
* @param {Function} func - RiotPureComponent factory function
* @returns {Function} the lifted original function received as argument
*/
export function pure(func) {
if (!isFunction(func)) panic('riot.pure accepts only arguments of type "function"')
func[globals.IS_PURE_SYMBOL] = true
return func
}

/** @type {string} current riot version */
export const version = 'WIP'

Expand Down
1 change: 1 addition & 0 deletions test/specs/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Riot compiler api', () => {
'install',
'uninstall',
'component',
'pure',
'version',
'__',
// compiler API
Expand Down
1 change: 1 addition & 0 deletions test/specs/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Riot core api', () => {
'install',
'uninstall',
'component',
'pure',
'version',
'__'
])
Expand Down

0 comments on commit a21b6eb

Please sign in to comment.