Comparing version 0.0.26 to 0.0.27
@@ -0,1 +1,6 @@ | ||
declare global { | ||
interface ChildNode { | ||
__forgo?: NodeAttachedState; | ||
} | ||
} | ||
export declare type ForgoRef<T> = { | ||
@@ -32,3 +37,3 @@ value?: T; | ||
}; | ||
export declare type ForgoNode = string | number | boolean | object | BigInt | ForgoElement<string | ForgoComponentCtor<any>, any>; | ||
export declare type ForgoNode = string | number | boolean | object | null | BigInt | undefined | ForgoElement<string | ForgoComponentCtor<any>, any>; | ||
export declare type NodeAttachedComponentState<TProps> = { | ||
@@ -35,0 +40,0 @@ key?: string | number; |
@@ -17,3 +17,3 @@ "use strict"; | ||
}; | ||
const isString = (val) => typeof val === 'string'; | ||
const isString = (val) => typeof val === "string"; | ||
function setCustomEnv(value) { | ||
@@ -194,2 +194,3 @@ env = value; | ||
const component = ctor(forgoElement.props); | ||
assertIsComponent(component); | ||
boundary = component.error ? component : boundary; | ||
@@ -221,2 +222,3 @@ // Create new component state | ||
const component = ctor(forgoElement.props); | ||
assertIsComponent(component); | ||
boundary = component.error ? component : boundary; | ||
@@ -258,7 +260,5 @@ // We'll have to create a new component state | ||
// Children will not be an array if single item | ||
const forgoChildren = (forgoChildrenObj !== undefined | ||
? Array.isArray(forgoChildrenObj) | ||
? forgoChildrenObj | ||
: [forgoChildrenObj] | ||
: []); | ||
const forgoChildren = Array.isArray(forgoChildrenObj) | ||
? forgoChildrenObj | ||
: [forgoChildrenObj]; | ||
let forgoChildIndex = 0; | ||
@@ -270,16 +270,18 @@ if (forgoChildren) { | ||
if (!isForgoElement(forgoChild)) { | ||
// If the first node is a text node, we could pass that along. | ||
// No need to replace here, callee does that. | ||
if (childNodes[forgoChildIndex] && | ||
childNodes[forgoChildIndex].nodeType === TEXT_NODE_TYPE) { | ||
internalRender(stringOfPrimitiveNode(forgoChild), childNodes[forgoChildIndex], [], fullRerender, boundary); | ||
} | ||
// But otherwise, don't pass a replacement node. Just insert instead. | ||
else { | ||
const { node } = internalRender(stringOfPrimitiveNode(forgoChild), undefined, [], fullRerender, boundary); | ||
if (childNodes.length > forgoChildIndex) { | ||
parentElement.insertBefore(node, childNodes[forgoChildIndex]); | ||
if (forgoChild !== undefined && forgoChild !== null) { | ||
// If the first node is a text node, we could pass that along. | ||
// No need to replace here, callee does that. | ||
if (childNodes[forgoChildIndex] && | ||
childNodes[forgoChildIndex].nodeType === TEXT_NODE_TYPE) { | ||
internalRender(stringOfPrimitiveNode(forgoChild), childNodes[forgoChildIndex], [], fullRerender, boundary); | ||
} | ||
// But otherwise, don't pass a replacement node. Just insert instead. | ||
else { | ||
parentElement.appendChild(node); | ||
const { node } = internalRender(stringOfPrimitiveNode(forgoChild), undefined, [], fullRerender, boundary); | ||
if (childNodes.length > forgoChildIndex) { | ||
parentElement.insertBefore(node, childNodes[forgoChildIndex]); | ||
} | ||
else { | ||
parentElement.appendChild(node); | ||
} | ||
} | ||
@@ -483,3 +485,5 @@ } | ||
function mount(forgoNode, container) { | ||
let parentElement = isString(container) ? env.document.querySelector(container) : container; | ||
let parentElement = isString(container) | ||
? env.document.querySelector(container) | ||
: container; | ||
if (parentElement) { | ||
@@ -540,3 +544,3 @@ const { node } = internalRender(forgoNode, undefined, [], true); | ||
function stringOfPrimitiveNode(node) { | ||
return typeof node === "undefined" ? "undefined" : node.toString(); | ||
return typeof node === "undefined" || node === null ? "" : node.toString(); | ||
} | ||
@@ -548,3 +552,5 @@ /* | ||
function isForgoElement(node) { | ||
return (typeof node !== "undefined" && node.__is_forgo_element__ === true); | ||
return (typeof node !== "undefined" && | ||
node !== null && | ||
node.__is_forgo_element__ === true); | ||
} | ||
@@ -571,2 +577,10 @@ /* | ||
exports.setForgoState = setForgoState; | ||
/* | ||
Throw if component is a non-component | ||
*/ | ||
function assertIsComponent(component) { | ||
if (!component.render) { | ||
throw new Error("component must have a render() method."); | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
export function jsxs(type, props, key) { | ||
return { type, props, key, __is_forgo_element__: true }; | ||
} | ||
export function jsx(type, props, key) { | ||
return { type, props, key, __is_forgo_element__: true }; | ||
} |
{ | ||
"name": "forgo", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"main": "./dist", | ||
@@ -5,0 +5,0 @@ "author": "Jeswin Kumar<jeswinpk@agilehead.com>", |
123
src/index.ts
@@ -0,1 +1,7 @@ | ||
declare global { | ||
interface ChildNode { | ||
__forgo?: NodeAttachedState; | ||
} | ||
} | ||
/* | ||
@@ -84,3 +90,5 @@ A type that wraps a reference. | ||
| object | ||
| null | ||
| BigInt | ||
| undefined | ||
| ForgoElement<string | ForgoComponentCtor<any>, any>; | ||
@@ -142,3 +150,3 @@ | ||
const isString = (val: unknown): val is string => typeof val === 'string' | ||
const isString = (val: unknown): val is string => typeof val === "string"; | ||
@@ -189,3 +197,3 @@ export function setCustomEnv(value: any) { | ||
forgoNode as ForgoElement<ForgoComponentCtor<any>, any>, | ||
node, | ||
node as Required<ChildNode>, | ||
pendingAttachStates, | ||
@@ -218,3 +226,3 @@ fullRerender, | ||
const textNode = env.document.createTextNode(text); | ||
if (node) { | ||
@@ -281,7 +289,7 @@ // We have to get oldStates before attachProps; | ||
} | ||
// We have to get oldStates before attachProps; | ||
// coz attachProps will overwrite with new states. | ||
const oldComponentStates = getForgoState(node)?.components; | ||
attachProps(forgoElement, nodeToBindTo, pendingAttachStates); | ||
@@ -355,3 +363,3 @@ | ||
forgoElement: ForgoElement<ForgoComponentCtor<TProps>, TProps>, | ||
node: ChildNode | undefined, | ||
node: Required<ChildNode> | undefined, | ||
pendingAttachStates: NodeAttachedComponentState<any>[], | ||
@@ -427,4 +435,7 @@ fullRerender: boolean, | ||
const args: ForgoRenderArgs = { element: { componentIndex } }; | ||
const ctor = forgoElement.type; | ||
const component = ctor(forgoElement.props); | ||
assertIsComponent(component); | ||
boundary = component.error ? component : boundary; | ||
@@ -473,2 +484,4 @@ | ||
const component = ctor(forgoElement.props); | ||
assertIsComponent(component); | ||
boundary = component.error ? component : boundary; | ||
@@ -534,7 +547,5 @@ | ||
// Children will not be an array if single item | ||
const forgoChildren = (forgoChildrenObj !== undefined | ||
? Array.isArray(forgoChildrenObj) | ||
? forgoChildrenObj | ||
: [forgoChildrenObj] | ||
: []) as ForgoNode[]; | ||
const forgoChildren = Array.isArray(forgoChildrenObj) | ||
? forgoChildrenObj | ||
: [forgoChildrenObj]; | ||
@@ -553,30 +564,32 @@ let forgoChildIndex = 0; | ||
if (!isForgoElement(forgoChild)) { | ||
// If the first node is a text node, we could pass that along. | ||
// No need to replace here, callee does that. | ||
if ( | ||
childNodes[forgoChildIndex] && | ||
childNodes[forgoChildIndex].nodeType === TEXT_NODE_TYPE | ||
) { | ||
internalRender( | ||
stringOfPrimitiveNode(forgoChild), | ||
childNodes[forgoChildIndex], | ||
[], | ||
fullRerender, | ||
boundary | ||
); | ||
} | ||
// But otherwise, don't pass a replacement node. Just insert instead. | ||
else { | ||
const { node } = internalRender( | ||
stringOfPrimitiveNode(forgoChild), | ||
undefined, | ||
[], | ||
fullRerender, | ||
boundary | ||
); | ||
if (forgoChild !== undefined && forgoChild !== null) { | ||
// If the first node is a text node, we could pass that along. | ||
// No need to replace here, callee does that. | ||
if ( | ||
childNodes[forgoChildIndex] && | ||
childNodes[forgoChildIndex].nodeType === TEXT_NODE_TYPE | ||
) { | ||
internalRender( | ||
stringOfPrimitiveNode(forgoChild), | ||
childNodes[forgoChildIndex], | ||
[], | ||
fullRerender, | ||
boundary | ||
); | ||
} | ||
// But otherwise, don't pass a replacement node. Just insert instead. | ||
else { | ||
const { node } = internalRender( | ||
stringOfPrimitiveNode(forgoChild), | ||
undefined, | ||
[], | ||
fullRerender, | ||
boundary | ||
); | ||
if (childNodes.length > forgoChildIndex) { | ||
parentElement.insertBefore(node, childNodes[forgoChildIndex]); | ||
} else { | ||
parentElement.appendChild(node); | ||
if (childNodes.length > forgoChildIndex) { | ||
parentElement.insertBefore(node, childNodes[forgoChildIndex]); | ||
} else { | ||
parentElement.appendChild(node); | ||
} | ||
} | ||
@@ -841,4 +854,9 @@ } | ||
*/ | ||
export function mount(forgoNode: ForgoNode, container: HTMLElement | string | null) { | ||
let parentElement = isString(container) ? env.document.querySelector(container) : container; | ||
export function mount( | ||
forgoNode: ForgoNode, | ||
container: HTMLElement | string | null | ||
) { | ||
let parentElement = isString(container) | ||
? env.document.querySelector(container) | ||
: container; | ||
@@ -916,4 +934,4 @@ if (parentElement) { | ||
*/ | ||
function stringOfPrimitiveNode(node: ForgoNode) { | ||
return typeof node === "undefined" ? "undefined" : node.toString(); | ||
function stringOfPrimitiveNode(node: ForgoNode): string { | ||
return typeof node === "undefined" || node === null ? "" : node.toString(); | ||
} | ||
@@ -927,3 +945,5 @@ | ||
return ( | ||
typeof node !== "undefined" && (node as any).__is_forgo_element__ === true | ||
typeof node !== "undefined" && | ||
node !== null && | ||
(node as any).__is_forgo_element__ === true | ||
); | ||
@@ -936,3 +956,3 @@ } | ||
export function getForgoState(node: ChildNode): NodeAttachedState | undefined { | ||
return (node as any).__forgo; | ||
return node.__forgo; | ||
} | ||
@@ -943,4 +963,4 @@ | ||
*/ | ||
function getExistingForgoState(node: ChildNode): NodeAttachedState { | ||
return (node as any).__forgo; | ||
function getExistingForgoState(node: Required<ChildNode>): NodeAttachedState { | ||
return node.__forgo; | ||
} | ||
@@ -952,3 +972,12 @@ | ||
export function setForgoState(node: ChildNode, state: NodeAttachedState): void { | ||
(node as any).__forgo = state; | ||
node.__forgo = state; | ||
} | ||
/* | ||
Throw if component is a non-component | ||
*/ | ||
function assertIsComponent(component: ForgoComponent<any>) { | ||
if (!component.render) { | ||
throw new Error("component must have a render() method."); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87024
1527