Skip to content

Commit

Permalink
::bug:: => types were not being imported correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
spuckhafte committed Mar 27, 2023
1 parent 77937af commit 5a6f6cf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 113 deletions.
55 changes: 6 additions & 49 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,39 @@
import { Attributes, Events, Func, Init, Prop, States, Subscribers } from "./tsimp.types";
declare class TSimp {
/**Initialize the element by defining its type, parent(query-selector), classes and id*/
import { Attributes, Events, Func, Init, Prop, States, Subscribers } from "../types";
declare class Criya {
init: Init;
/**Define the text, html or value(attribute) of the element */
prop: Prop;
/**Add some events that the element will listen to like click, input etc */
events: Events;
/**Define the attributes of the element */
attr: Attributes;
/**The actual physical element present in the dom */
domElement: HTMLElement | undefined;
/**The state object, contains all the states and their current values */
states: States;
pseudoStates: States;
/**List of all the subscribers and the states they are subscribed to */
subscribers: Subscribers<TSimp>;
subscribers: Subscribers<Criya>;
private onmount;
private onunmount;
private onsubscribed;
private onnewsubscriber;
private effects;
private renderCondition;
constructor(init: Init, prop?: Prop, events?: Events, attr?: Attributes);
/**Converts the virtual element into a physical element */
render(): void;
/**Append the element to the DOM */
mount(): this;
/**Remove the element from the DOM */
unMount(): void;
private _directMount;
/**Check if this element is in the DOM */
isMount(): boolean;
/**Combines render and mount methods and returns the element */
make(): this;
/**Make an element subscribe to the other so that it can access its states as pseudo-states.
* @param subscriber - the element which will access the states by subscribing to other.
* @param main - the element that'll share its states.
* @param forStates - States of the `main` element to be shared, leave the array empty to trigger all.
*/
static subscribe(subscriber: TSimp, main: TSimp, forStates: string[]): void;
/**States are internal variables that when change automatically update their special references in some specific properties, i.e., `html, text, css, value, class, id`
* @param stateName - name of the state
* @param initialValue - initial value of the state
* @returns Two functions in an array, one to get state (non reactive) another to set state
*/
static subscribe(subscriber: Criya, main: Criya, forStates: string[]): void;
state<T>(stateName: string, initialValue: T): [(() => T), ((newVal: T | Func<T, T>) => void)];
/**
* Effects are functions that get called when some states or pseudoStates (dependencies) change
* @param func - this function will get called when the dependencies change
* @param dependencyArray - add states that will affect the effect, examples:
* - `['$count$', '%color%']`
* - `['f']`
* - `['e']`
* @param onFirst - `default: true`, by default every effect runs on its first render whether the deps change or not.
* */
effect(func: CallableFunction, dependencyArray: string[], onFirst?: boolean): void;
/**Define a condition for when an element should be in the DOM
* @param condition - function or a text condition that'll return boolean signifying mount or not, eg:
* - Function - `putIf(() => state() > 2)`
* - Text - `putIf('$state$ > 2')`
* - Conditions can include pseudo-states also
* @param stick - if true, the element will be in its position after remounting. Bydefault: `false`
* @returns a [getter and setter] (just like `.state` does) for the "sticky" state
*/
putIf(condition: ((() => boolean) | string), stick?: boolean): [() => boolean, (newVal: boolean | Func<boolean, boolean>) => void];
/**Called when the element is added to the dom */
onMount(func: ((didMount?: boolean) => void)): void;
/**Called when the element is removed from the dom */
onUnmount(func: CallableFunction): void;
/**Called on the element to which the subscriber is subscribing when subscription is added */
onNewSubscriber(func: CallableFunction): void;
/**Called on the subscriber element when subscription is added */
onSubscribed(func: CallableFunction): void;
private getState;
private getPState;
private formatString;
private stateExtracter;
}
declare const subscribe: typeof TSimp.subscribe;
declare const subscribe: typeof Criya.subscribe;
export { subscribe };
export default TSimp;
export default Criya;
68 changes: 12 additions & 56 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ const regex = {
pStateExp: /%[a-zA-Z0-9-]+%/g,
both: /\$[a-zA-Z0-9-]+\$ | %[a-zA-Z0-9-]+%/g
};
class TSimp {
class Criya {
constructor(init, prop, events, attr) {
this.init = init; // { type, parent, class("class1 class2"), id }
this.prop = prop; // { text, html, css, value }
this.events = events; // { event: e => e.target.value }
this.attr = attr; // { attributes (eg. title, type) }
this.states = {}; // { "nameOfState": valueOfState }
this.pseudoStates = {}; // --dito--
this.subscribers = []; // [ { subscriber: El, states: [] } ]
this.effects = []; // [ { func(), deps[], ranOnce:false, onFirst:boolean, currentStates[] } ]
this.renderCondition = () => true; // by default no condition
this.init = init;
this.prop = prop;
this.events = events;
this.attr = attr;
this.states = {};
this.pseudoStates = {};
this.subscribers = [];
this.effects = [];
this.renderCondition = () => true;
}
// dom methods:
/**Converts the virtual element into a physical element */
render() {
this.domElement = this.domElement ? this.domElement : document.createElement(this.init.type);
if (this.init.class) {
Expand All @@ -33,7 +31,6 @@ class TSimp {
}
if (this.events) {
Object.keys(this.events).forEach(event => {
// @ts-ignore
this.domElement.addEventListener(event, this.events[event]);
});
}
Expand All @@ -50,7 +47,6 @@ class TSimp {
const css = this.prop.css;
if (this.prop.css) {
for (let property of Object.keys(this.prop.css)) {
//@ts-ignore
this.domElement.style[property] = this.formatString(css[property]);
}
}
Expand Down Expand Up @@ -105,7 +101,6 @@ class TSimp {
});
}
}
/**Append the element to the DOM */
mount() {
if (this.renderCondition() && this.isMount())
return this;
Expand Down Expand Up @@ -137,7 +132,6 @@ class TSimp {
this._directMount(parent);
return this;
}
/**Remove the element from the DOM */
unMount() {
if (this.onunmount)
this.onunmount();
Expand All @@ -151,7 +145,6 @@ class TSimp {
if (this.domElement)
parent.appendChild(this.domElement);
}
/**Check if this element is in the DOM */
isMount() {
let parent = document.querySelector(this.init.parent);
if (!parent)
Expand All @@ -160,17 +153,10 @@ class TSimp {
throw Error('No DOMElement attached :(');
return Array.from(parent.children).indexOf(this.domElement) > -1;
}
/**Combines render and mount methods and returns the element */
make() {
this.render();
return this.mount();
}
// out-of-the-box-feature methods
/**Make an element subscribe to the other so that it can access its states as pseudo-states.
* @param subscriber - the element which will access the states by subscribing to other.
* @param main - the element that'll share its states.
* @param forStates - States of the `main` element to be shared, leave the array empty to trigger all.
*/
static subscribe(subscriber, main, forStates) {
forStates = forStates.length == 0
? Object.keys(main.states)
Expand All @@ -184,16 +170,10 @@ class TSimp {
if (subscriber.onsubscribed)
subscriber.onsubscribed();
}
/**States are internal variables that when change automatically update their special references in some specific properties, i.e., `html, text, css, value, class, id`
* @param stateName - name of the state
* @param initialValue - initial value of the state
* @returns Two functions in an array, one to get state (non reactive) another to set state
*/
state(stateName, initialValue) {
this.states[stateName] = initialValue;
const setState = (newVal) => {
let stateValue;
//@ts-ignore
if (typeof newVal == 'function')
stateValue = newVal(this.getState(stateName));
else
Expand All @@ -209,15 +189,6 @@ class TSimp {
const stateGetter = () => this.getState(stateName);
return [stateGetter, setState];
}
/**
* Effects are functions that get called when some states or pseudoStates (dependencies) change
* @param func - this function will get called when the dependencies change
* @param dependencyArray - add states that will affect the effect, examples:
* - `['$count$', '%color%']`
* - `['f']`
* - `['e']`
* @param onFirst - `default: true`, by default every effect runs on its first render whether the deps change or not.
* */
effect(func, dependencyArray, onFirst = true) {
if (dependencyArray[0] == 'f' || dependencyArray[0] == 'e') {
this.effects.push({
Expand All @@ -235,14 +206,6 @@ class TSimp {
ranOnce: false, onFirst, currentStates
});
}
/**Define a condition for when an element should be in the DOM
* @param condition - function or a text condition that'll return boolean signifying mount or not, eg:
* - Function - `putIf(() => state() > 2)`
* - Text - `putIf('$state$ > 2')`
* - Conditions can include pseudo-states also
* @param stick - if true, the element will be in its position after remounting. Bydefault: `false`
* @returns a [getter and setter] (just like `.state` does) for the "sticky" state
*/
putIf(condition, stick = false) {
if (typeof condition == 'string') {
this.renderCondition = () => eval(this.formatString(condition));
Expand All @@ -251,24 +214,18 @@ class TSimp {
this.renderCondition = condition;
return this.state('__stick__', stick);
}
// inbuilt events
/**Called when the element is added to the dom */
onMount(func) {
this.onmount = func;
}
/**Called when the element is removed from the dom */
onUnmount(func) {
this.onunmount = func;
}
/**Called on the element to which the subscriber is subscribing when subscription is added */
onNewSubscriber(func) {
this.onnewsubscriber = func;
}
/**Called on the subscriber element when subscription is added */
onSubscribed(func) {
this.onsubscribed = func;
}
// util methods
getState(stateName) {
return this.states[stateName];
}
Expand All @@ -280,7 +237,6 @@ class TSimp {
if (!checkForOperation(text))
return this.stateExtracter(text);
const operations = text.match(regex.stateOperateExp);
//@ts-ignore - operations is not null, cause we are already checking for it (look up)
for (let rawOperation of operations) {
let operation = rawOperation.replace(/{{|}}/g, '');
operation = this.stateExtracter(operation);
Expand Down Expand Up @@ -326,9 +282,9 @@ class TSimp {
return text;
}
}
const subscribe = TSimp.subscribe;
const subscribe = Criya.subscribe;
export { subscribe };
export default TSimp;
export default Criya;
function checkForOperation(text) {
return regex.stateOperateExp.test(text);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "criya",
"version": "1.3.9",
"version": "1.4.0",
"description": "",
"main": "./dist/index.js",
"repository": {
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Attributes, Effects, Events, Func, Init, Prop, States, Subscribers } from "./tsimp.types";
import { Attributes, Effects, Events, Func, Init, Prop, States, Subscribers } from "../types";

const regex = {
stateOperateExp: /{{[a-zA-Z0-9$%+\-*/()\[\]<>?:="'^.! ]+}}/g,
Expand All @@ -7,7 +7,7 @@ const regex = {
both: /\$[a-zA-Z0-9-]+\$ | %[a-zA-Z0-9-]+%/g
}

class TSimp {
class Criya {

/**Initialize the element by defining its type, parent(query-selector), classes and id*/
init:Init;
Expand All @@ -22,7 +22,7 @@ class TSimp {
/**The state object, contains all the states and their current values */
states:States; pseudoStates:States;
/**List of all the subscribers and the states they are subscribed to */
subscribers:Subscribers<TSimp>;
subscribers:Subscribers<Criya>;

private onmount:CallableFunction|undefined; private onunmount:CallableFunction|undefined;
private onsubscribed: CallableFunction|undefined; private onnewsubscriber: CallableFunction|undefined;
Expand Down Expand Up @@ -195,7 +195,7 @@ class TSimp {
* @param main - the element that'll share its states.
* @param forStates - States of the `main` element to be shared, leave the array empty to trigger all.
*/
static subscribe(subscriber:TSimp, main:TSimp, forStates:string[]) {
static subscribe(subscriber:Criya, main:Criya, forStates:string[]) {
forStates = forStates.length == 0
? Object.keys(main.states)
: forStates.filter(state => main.states[state] != undefined);
Expand Down Expand Up @@ -354,10 +354,10 @@ class TSimp {
}
}

const subscribe = TSimp.subscribe;
const subscribe = Criya.subscribe;

export { subscribe };
export default TSimp;
export default Criya;

function checkForOperation(text:string) {
return regex.stateOperateExp.test(text);
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
"removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
Expand Down
File renamed without changes.

0 comments on commit 5a6f6cf

Please sign in to comment.