jaxs 0.3.2 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/jaxs.d.ts +648 -0
- package/dist/jaxs.js +810 -1071
- package/dist/jaxs.umd.cjs +1 -0
- package/package.json +41 -34
- package/.env +0 -1
- package/README.md +0 -15
- package/bun.lockb +0 -0
- package/bundle.ts +0 -5
- package/bunfig.toml +0 -0
- package/cypress/e2e/add-remove-nested-children.cy.js +0 -39
- package/cypress/e2e/add-remove-root-children.cy.js +0 -37
- package/cypress/e2e/svg-renders.cy.js +0 -10
- package/cypress/jaxs-apps/add-remove-nested-children.html +0 -12
- package/cypress/jaxs-apps/add-remove-nested-children.jsx +0 -85
- package/cypress/jaxs-apps/add-remove-root-children.html +0 -15
- package/cypress/jaxs-apps/add-remove-root-children.jsx +0 -54
- package/cypress/jaxs-apps/dist/add-remove-nested-children.afcab974.js +0 -1022
- package/cypress/jaxs-apps/dist/add-remove-nested-children.afcab974.js.map +0 -1
- package/cypress/jaxs-apps/dist/add-remove-nested-children.html +0 -12
- package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js +0 -1665
- package/cypress/jaxs-apps/dist/add-remove-root-children.3bb9b3f5.js.map +0 -1
- package/cypress/jaxs-apps/dist/add-remove-root-children.fbb4ec9b.js +0 -1011
- package/cypress/jaxs-apps/dist/add-remove-root-children.fbb4ec9b.js.map +0 -1
- package/cypress/jaxs-apps/dist/add-remove-root-children.html +0 -15
- package/cypress/jaxs-apps/dist/svg.04290504.js +0 -644
- package/cypress/jaxs-apps/dist/svg.04290504.js.map +0 -1
- package/cypress/jaxs-apps/dist/svg.html +0 -11
- package/cypress/jaxs-apps/svg.html +0 -11
- package/cypress/jaxs-apps/svg.jsx +0 -15
- package/cypress/support/commands.js +0 -25
- package/cypress/support/e2e.js +0 -20
- package/cypress.config.js +0 -10
- package/src/app.ts +0 -84
- package/src/debugging.js +0 -5
- package/src/jaxs.ts +0 -7
- package/src/jsx.js +0 -27
- package/src/messageBus.ts +0 -70
- package/src/navigation/findHref.js +0 -10
- package/src/navigation/routeState.js +0 -15
- package/src/navigation/setupHistory.js +0 -38
- package/src/navigation/setupNavigation.js +0 -25
- package/src/navigation.ts +0 -2
- package/src/rendering/change/compile.ts +0 -1
- package/src/rendering/change/instructions/attributes.ts +0 -81
- package/src/rendering/change/instructions/children.ts +0 -127
- package/src/rendering/change/instructions/element.ts +0 -49
- package/src/rendering/change/instructions/events.ts +0 -51
- package/src/rendering/change/instructions/generate.ts +0 -122
- package/src/rendering/change/instructions/idMap.js +0 -55
- package/src/rendering/change/instructions/node.ts +0 -55
- package/src/rendering/change/instructions/text.ts +0 -10
- package/src/rendering/change.ts +0 -139
- package/src/rendering/dom/attributesAndEvents.ts +0 -33
- package/src/rendering/dom/create.ts +0 -66
- package/src/rendering/dom/svg.ts +0 -18
- package/src/rendering/templates/bound.js +0 -56
- package/src/rendering/templates/children.ts +0 -99
- package/src/rendering/templates/root.ts +0 -55
- package/src/rendering/templates/tag.ts +0 -92
- package/src/rendering/templates/text.ts +0 -17
- package/src/state/equality.js +0 -36
- package/src/state/stores.js +0 -63
- package/src/state/testingTypes.js +0 -6
- package/src/state.js +0 -89
- package/src/types.ts +0 -160
- package/src/views/conditionals.jsx +0 -18
- package/src/views/link.jsx +0 -5
- package/src/views.js +0 -7
- package/tsconfig.json +0 -26
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Attributes,
|
|
3
|
-
DomEventPublisher,
|
|
4
|
-
EventAttributes,
|
|
5
|
-
EventMaps,
|
|
6
|
-
ExpandedElement,
|
|
7
|
-
InputElement,
|
|
8
|
-
RenderKit,
|
|
9
|
-
} from '../../types';
|
|
10
|
-
|
|
11
|
-
export const setAttributesOnElement = (
|
|
12
|
-
element: Element,
|
|
13
|
-
attributes: Attributes,
|
|
14
|
-
) => {
|
|
15
|
-
for (const key in attributes) {
|
|
16
|
-
if (key === '__self') continue;
|
|
17
|
-
if (key === 'value') {
|
|
18
|
-
(element as InputElement).value = attributes[key];
|
|
19
|
-
} else {
|
|
20
|
-
element.setAttribute(key, attributes[key]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const setEventsOnElement = (
|
|
26
|
-
element: ExpandedElement,
|
|
27
|
-
events: EventAttributes,
|
|
28
|
-
publish: DomEventPublisher,
|
|
29
|
-
) => {
|
|
30
|
-
const eventMaps = {} as EventMaps;
|
|
31
|
-
|
|
32
|
-
for (const domEvent in events) {
|
|
33
|
-
const eventName = events[domEvent];
|
|
34
|
-
const listener = (event: Event) => publish(eventName, event);
|
|
35
|
-
element.addEventListener(domEvent, listener);
|
|
36
|
-
|
|
37
|
-
eventMaps[domEvent] = {
|
|
38
|
-
domEvent: domEvent,
|
|
39
|
-
busEvent: eventName,
|
|
40
|
-
listener: listener,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
element.eventMaps = eventMaps;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const createNode = (type: string, document: Document) => {
|
|
48
|
-
return document.createElement(type);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const createTextNode = (value: string, document: Document) => {
|
|
52
|
-
return document.createTextNode(value);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const createDecoratedNode = (
|
|
56
|
-
type: string,
|
|
57
|
-
attributes: Attributes,
|
|
58
|
-
events: EventAttributes,
|
|
59
|
-
renderKit: RenderKit,
|
|
60
|
-
) => {
|
|
61
|
-
// deno-lint-ignore no-explicit-any
|
|
62
|
-
const dom = createNode(type, renderKit.document) as any as ExpandedElement;
|
|
63
|
-
setAttributesOnElement(dom, attributes);
|
|
64
|
-
setEventsOnElement(dom, events, renderKit.publish);
|
|
65
|
-
return dom;
|
|
66
|
-
};
|
package/src/rendering/dom/svg.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { RenderKit, Attributes, ExpandedElement } from "../../types"
|
|
2
|
-
|
|
3
|
-
export const namespace = 'http://www.w3.org/2000/svg'
|
|
4
|
-
export const isSvgTag = (tagType: string) => tagType === 'svg'
|
|
5
|
-
export const isSvg = (element: SVGElement) => element.namespaceURI === namespace
|
|
6
|
-
|
|
7
|
-
export const createSvgNode = (type: string, attributes: Attributes, renderKit: RenderKit) => {
|
|
8
|
-
const { document } = renderKit
|
|
9
|
-
const node = document.createElementNS(namespace, type)
|
|
10
|
-
|
|
11
|
-
for (const key in attributes) {
|
|
12
|
-
if (key === '__self' || key === 'xmlns') continue;
|
|
13
|
-
// adding namespace in as first argument makes it not really render!
|
|
14
|
-
node.setAttributeNS(null, key, attributes[key]);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return node as unknown as ExpandedElement
|
|
18
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { eventName } from '../../state'
|
|
2
|
-
import { change } from '../change'
|
|
3
|
-
|
|
4
|
-
const passThroughViewModel = (state) => state
|
|
5
|
-
export class Bound {
|
|
6
|
-
constructor (TemplateClass, viewModel, subscriptions, attributes) {
|
|
7
|
-
this.TemplateClass = TemplateClass
|
|
8
|
-
this.viewModel = viewModel || passThroughViewModel
|
|
9
|
-
this.attributes = attributes || {}
|
|
10
|
-
this.subscriptions = subscriptions
|
|
11
|
-
this.dom = []
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
render (renderKit) {
|
|
15
|
-
this.parentElement = renderKit.parent
|
|
16
|
-
this.renderKit = renderKit
|
|
17
|
-
this.subscribeForRerender()
|
|
18
|
-
this.dom = this._render(renderKit)
|
|
19
|
-
return this.dom
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
_render (renderKit) {
|
|
23
|
-
const props = {
|
|
24
|
-
...this.attributes,
|
|
25
|
-
...this.viewModel(renderKit.state.value())
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const template = this.TemplateClass(props)
|
|
29
|
-
|
|
30
|
-
const dom = !template ? [] : template.render(renderKit)
|
|
31
|
-
return dom
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
rerender () {
|
|
35
|
-
if (!this.parentElement) {
|
|
36
|
-
this.parentElement = this.dom[0] && this.dom[0].parentElement
|
|
37
|
-
}
|
|
38
|
-
const newDom = this._render(this.renderKit)
|
|
39
|
-
change(this.dom, newDom, this.parentElement)
|
|
40
|
-
|
|
41
|
-
if (this.parentElement) {
|
|
42
|
-
this.dom = Array.from(this.parentElement.childNodes)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
subscribeForRerender () {
|
|
47
|
-
this.subscriptions.forEach((storeName) => {
|
|
48
|
-
this.renderKit.subscribe(eventName(storeName), () => this.rerender())
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const bind = ({ Template, viewModel, subscriptions }) => {
|
|
54
|
-
subscriptions = subscriptions || []
|
|
55
|
-
return (attributes) => new Bound(Template, viewModel, subscriptions, attributes)
|
|
56
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Dom,
|
|
3
|
-
DomCollection,
|
|
4
|
-
RenderKit,
|
|
5
|
-
Template,
|
|
6
|
-
TextValue,
|
|
7
|
-
} from '../../types';
|
|
8
|
-
import { TextTemplate } from './text';
|
|
9
|
-
|
|
10
|
-
export const ensureArray = (children: Template | Template[]): Template[] => {
|
|
11
|
-
if (Array.isArray(children)) {
|
|
12
|
-
return children.flat();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (!children) {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return [children];
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/* three options for children
|
|
23
|
-
1. there is no view
|
|
24
|
-
2. view is an array, recurse
|
|
25
|
-
3. view is a renderable thing
|
|
26
|
-
*/
|
|
27
|
-
const recursiveRender = (
|
|
28
|
-
children: Template[],
|
|
29
|
-
renderKit: RenderKit,
|
|
30
|
-
rendered = [] as DomCollection,
|
|
31
|
-
): DomCollection => children.reduce(renderReducer(renderKit), rendered).flat();
|
|
32
|
-
|
|
33
|
-
const renderReducer =
|
|
34
|
-
(renderKit: RenderKit) =>
|
|
35
|
-
(aggregate: DomCollection, view: Template): DomCollection => {
|
|
36
|
-
if (!view) return aggregate;
|
|
37
|
-
|
|
38
|
-
if (Array.isArray(view)) {
|
|
39
|
-
const dom = recursiveRender(view, renderKit, aggregate) as DomCollection;
|
|
40
|
-
return dom;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
view.render(renderKit).forEach((template) => aggregate.push(template));
|
|
44
|
-
|
|
45
|
-
return aggregate;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const replaceTextNodes = (child: TextValue | Template) => {
|
|
49
|
-
if (isTextNode(child)) {
|
|
50
|
-
return textNode(child as TextValue);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return child;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const isTextNode = (child: TextValue | Template) => {
|
|
57
|
-
return typeof child === 'string' || typeof child === 'number';
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const textNode = (content: TextValue) => {
|
|
61
|
-
return new TextTemplate(content);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const withSvgFlag = (isSvg: boolean) => (template: Template) => {
|
|
65
|
-
template && (template.isSvg = template.isSvg || isSvg)
|
|
66
|
-
return template
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export class Children implements Template {
|
|
70
|
-
collection: Template[];
|
|
71
|
-
parentElement: Element | undefined;
|
|
72
|
-
isSvg: boolean;
|
|
73
|
-
|
|
74
|
-
constructor(jsxChildren: Template[], isSvg = false) {
|
|
75
|
-
this.collection = ensureArray(jsxChildren)
|
|
76
|
-
this.collection = this.collection.map(replaceTextNodes) as Template[];
|
|
77
|
-
this.collection = this.collection.flat() as Template[];
|
|
78
|
-
this.collection = this.collection.map(withSvgFlag(isSvg))
|
|
79
|
-
this.isSvg = isSvg;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
render(renderKit: RenderKit, parentElement: Element | undefined) {
|
|
83
|
-
this.parentElement = parentElement;
|
|
84
|
-
const dom = this.generateDom(renderKit);
|
|
85
|
-
this.attachToParent(dom);
|
|
86
|
-
return dom;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
generateDom(renderKit: RenderKit) {
|
|
90
|
-
return recursiveRender(this.collection, renderKit);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
attachToParent(dom: DomCollection) {
|
|
94
|
-
if (this.parentElement === undefined) return;
|
|
95
|
-
|
|
96
|
-
const parent = this.parentElement as Element;
|
|
97
|
-
dom.forEach((node: Dom) => parent.appendChild(node));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DomCollection,
|
|
3
|
-
RenderKit,
|
|
4
|
-
Template,
|
|
5
|
-
} from '../../types';
|
|
6
|
-
|
|
7
|
-
export class Root {
|
|
8
|
-
template: Template;
|
|
9
|
-
selector: string;
|
|
10
|
-
renderKit: RenderKit;
|
|
11
|
-
dom: DomCollection;
|
|
12
|
-
parentElement: Element | null;
|
|
13
|
-
|
|
14
|
-
constructor(template: Template, selector: string, renderKit: RenderKit) {
|
|
15
|
-
this.template = template;
|
|
16
|
-
this.selector = selector;
|
|
17
|
-
this.renderKit = renderKit;
|
|
18
|
-
this.dom = [];
|
|
19
|
-
this.parentElement = null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
renderAndAttach(renderKit: RenderKit) {
|
|
23
|
-
this.parentElement = this.getParentElement();
|
|
24
|
-
this.dom = this.render({...renderKit, parent: this.parentElement});
|
|
25
|
-
|
|
26
|
-
if (this.parentElement) {
|
|
27
|
-
this.attach();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
render(renderKit: RenderKit) {
|
|
32
|
-
return this.template.render(renderKit);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
attach() {
|
|
36
|
-
this.parentElement && (this.parentElement.innerHTML = '');
|
|
37
|
-
this.dom.forEach((element) => {
|
|
38
|
-
this.parentElement && this.parentElement.appendChild(element);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
getParentElement() {
|
|
43
|
-
return this.renderKit.document.querySelector(this.selector);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const render = (
|
|
48
|
-
template: Template,
|
|
49
|
-
selector: string,
|
|
50
|
-
renderKit: RenderKit,
|
|
51
|
-
) => {
|
|
52
|
-
const root = new Root(template, selector, renderKit);
|
|
53
|
-
root.renderAndAttach(renderKit);
|
|
54
|
-
return root;
|
|
55
|
-
};
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Attributes,
|
|
3
|
-
DomCollection,
|
|
4
|
-
EventAttributes,
|
|
5
|
-
RenderKit,
|
|
6
|
-
Template,
|
|
7
|
-
} from '../../types';
|
|
8
|
-
|
|
9
|
-
import { createDecoratedNode } from '../dom/create';
|
|
10
|
-
import { separateAttrsAndEvents } from '../dom/attributesAndEvents';
|
|
11
|
-
import { isSvgTag, createSvgNode } from '../dom/svg'
|
|
12
|
-
import { Children } from './children';
|
|
13
|
-
|
|
14
|
-
export class Tag implements Template {
|
|
15
|
-
type: string;
|
|
16
|
-
events: EventAttributes;
|
|
17
|
-
attributes: Attributes;
|
|
18
|
-
children: Children;
|
|
19
|
-
isSvg: boolean;
|
|
20
|
-
|
|
21
|
-
constructor(
|
|
22
|
-
tagType: string,
|
|
23
|
-
combinedAttributes: Attributes,
|
|
24
|
-
children: Template[],
|
|
25
|
-
isSvg = false
|
|
26
|
-
) {
|
|
27
|
-
this.type = tagType;
|
|
28
|
-
|
|
29
|
-
const { events, attributes } = separateAttrsAndEvents(combinedAttributes);
|
|
30
|
-
this.events = events;
|
|
31
|
-
this.attributes = attributes;
|
|
32
|
-
|
|
33
|
-
this.isSvg = isSvg || isSvgTag(this.type);
|
|
34
|
-
this.children = new Children(children, this.isSvg);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
render(renderKit: RenderKit): DomCollection {
|
|
38
|
-
const dom = this.generateDom(renderKit);
|
|
39
|
-
if (!dom) return [];
|
|
40
|
-
|
|
41
|
-
this.children.render(renderKit, dom);
|
|
42
|
-
return [dom];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
generateDom(renderKit: RenderKit) {
|
|
46
|
-
if (this.isSvg) {
|
|
47
|
-
return this.generateSvnDom(renderKit)
|
|
48
|
-
} else {
|
|
49
|
-
return this.generateHtmlDom(renderKit)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
generateHtmlDom(renderKit: RenderKit) {
|
|
54
|
-
const node = createDecoratedNode(
|
|
55
|
-
this.type,
|
|
56
|
-
this.attributes,
|
|
57
|
-
this.events,
|
|
58
|
-
renderKit,
|
|
59
|
-
);
|
|
60
|
-
node.__jsx = this.key();
|
|
61
|
-
return node;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
generateSvnDom(renderKit: RenderKit) {
|
|
65
|
-
const node = createSvgNode(
|
|
66
|
-
this.type,
|
|
67
|
-
this.attributes,
|
|
68
|
-
renderKit,
|
|
69
|
-
);
|
|
70
|
-
node.__jsx = this.key();
|
|
71
|
-
return node;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
key() {
|
|
75
|
-
return this.attributes.key || this.source() || this.createKey();
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
source() {
|
|
79
|
-
if (this.attributes.__source) {
|
|
80
|
-
const { fileName, lineNumber, columnNumber } = this.attributes.__source;
|
|
81
|
-
return `${fileName}:${lineNumber}:${columnNumber}`;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
createKey() {
|
|
86
|
-
const id = this.attributes.id ? `#${this.attributes.id}` : '';
|
|
87
|
-
const type = this.attributes.type ? `[type=${this.attributes.type}]` : '';
|
|
88
|
-
const name = this.attributes.name ? `[name=${this.attributes.name}]` : '';
|
|
89
|
-
|
|
90
|
-
return `${this.type}${id}${type}${name}`;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { JsxId, RenderKit, Template, TextValue } from '../../types';
|
|
2
|
-
import { createTextNode } from '../dom/create';
|
|
3
|
-
|
|
4
|
-
export class TextTemplate implements Template {
|
|
5
|
-
value: string;
|
|
6
|
-
|
|
7
|
-
constructor(content: TextValue) {
|
|
8
|
-
this.value = content.toString();
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
render(renderKit: RenderKit) {
|
|
12
|
-
const textNode = createTextNode(this.value, renderKit.document);
|
|
13
|
-
if (!textNode) return [];
|
|
14
|
-
(textNode as unknown as JsxId).__jsx = 'TextNode';
|
|
15
|
-
return [textNode];
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/state/equality.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { isObject, isArray } from './testingTypes'
|
|
2
|
-
|
|
3
|
-
export const areElementEqual = (oldValue, newValue) => oldValue === newValue
|
|
4
|
-
|
|
5
|
-
const keyLengthSame = (oldValue, newValue) =>
|
|
6
|
-
Object.keys(oldValue).length === Object.keys(newValue).length
|
|
7
|
-
|
|
8
|
-
export const areObjectsEqual = (oldValue, newValue) => {
|
|
9
|
-
if (!(isObject(oldValue) && isObject(newValue))) return false
|
|
10
|
-
if (!keyLengthSame(oldValue, newValue)) return false
|
|
11
|
-
|
|
12
|
-
Object.keys(oldValue).every((key) => {
|
|
13
|
-
const oldInnerValue = oldValue[key]
|
|
14
|
-
const newInnerValue = newValue[key]
|
|
15
|
-
|
|
16
|
-
return areEqual(oldInnerValue, newInnerValue)
|
|
17
|
-
})
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const areArraysEqual = (oldValue, newValue) => {
|
|
21
|
-
if (!(isArray(oldValue) && isArray(newValue))) return false
|
|
22
|
-
if (oldValue.length !== newValue.length) return false
|
|
23
|
-
|
|
24
|
-
oldValue.every((oldInnerValue, index) => {
|
|
25
|
-
const newInnerValue = newValue[index]
|
|
26
|
-
|
|
27
|
-
return areEqual(oldInnerValue, newInnerValue)
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const areEqual = (oldValue, newValue) => {
|
|
32
|
-
if (isObject(oldValue)) return areObjectsEqual(oldValue, newValue)
|
|
33
|
-
if (isArray(oldValue)) return areArraysEqual(oldValue, newValue)
|
|
34
|
-
|
|
35
|
-
return areElementEqual(oldValue, newValue)
|
|
36
|
-
}
|
package/src/state/stores.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { areElementEqual, areArraysEqual, areObjectsEqual } from './equality'
|
|
2
|
-
|
|
3
|
-
export class GeneralStore {
|
|
4
|
-
nullEvent = {}
|
|
5
|
-
|
|
6
|
-
constructor ({ name, value, parent }) {
|
|
7
|
-
this.name = name
|
|
8
|
-
this.value = value
|
|
9
|
-
this.parent = parent
|
|
10
|
-
this.initialState = value
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
update (newValue) {
|
|
14
|
-
if (this.isEqual(newValue)) return
|
|
15
|
-
|
|
16
|
-
this.value = newValue
|
|
17
|
-
return this.parent.publish(this.event())
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
reset () {
|
|
21
|
-
this.update(this.initialState)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
isEqual (newValue) {
|
|
25
|
-
return areElementEqual(this.value, newValue)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
event () {
|
|
29
|
-
return {
|
|
30
|
-
name: this.name,
|
|
31
|
-
value: this.value
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class BooleanStore extends GeneralStore {
|
|
37
|
-
toggle () {
|
|
38
|
-
this.update(!this.value)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class NumericStore extends GeneralStore {
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class StringStore extends GeneralStore {
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export class ListStore extends GeneralStore {
|
|
49
|
-
isEqual (newValue) {
|
|
50
|
-
return areArraysEqual(this.value, newValue)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
push (newValue) {
|
|
54
|
-
const value = [...this.value, newValue]
|
|
55
|
-
this.update(value)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class RecordStore extends GeneralStore {
|
|
60
|
-
isEqual (newValue) {
|
|
61
|
-
return areObjectsEqual(this.value, newValue)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export const isBoolean = (value) => typeof (value) === 'boolean'
|
|
2
|
-
export const isNumber = (value) => typeof (value) === 'number'
|
|
3
|
-
export const isString = (value) => typeof (value) === 'string'
|
|
4
|
-
export const isArray = (value) => Array.isArray(value)
|
|
5
|
-
export const isObject = (value) =>
|
|
6
|
-
value !== null && !isArray(value) && typeof (value) === 'object'
|
package/src/state.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isArray,
|
|
3
|
-
isObject,
|
|
4
|
-
isBoolean,
|
|
5
|
-
isNumber,
|
|
6
|
-
isString
|
|
7
|
-
} from './state/testingTypes'
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
GeneralStore,
|
|
11
|
-
ListStore,
|
|
12
|
-
NumericStore,
|
|
13
|
-
BooleanStore,
|
|
14
|
-
StringStore,
|
|
15
|
-
RecordStore
|
|
16
|
-
} from './state/stores'
|
|
17
|
-
|
|
18
|
-
const eventPrefix = 'state-change'
|
|
19
|
-
export const eventName = (name) => `${eventPrefix}:${name}`
|
|
20
|
-
|
|
21
|
-
export class State {
|
|
22
|
-
constructor (publish) {
|
|
23
|
-
this.publisher = publish
|
|
24
|
-
this.stores = {}
|
|
25
|
-
this.events = []
|
|
26
|
-
this.transacting = false
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
create (name, value) {
|
|
30
|
-
const StoreClass = this.storeTypeFor(value)
|
|
31
|
-
const store = new StoreClass({ name, value, parent: this })
|
|
32
|
-
this.addStore(name, store)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
add (store) {
|
|
36
|
-
const name = store.name
|
|
37
|
-
this.addStore(name, store)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getStore (name) {
|
|
41
|
-
return this.stores[name]
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
addStore (name, store) {
|
|
45
|
-
this.stores[name] = store
|
|
46
|
-
this[name] = store
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
storeTypeFor (value) {
|
|
50
|
-
if (isArray(value)) return ListStore
|
|
51
|
-
if (isObject(value)) return RecordStore
|
|
52
|
-
if (isNumber(value)) return NumericStore
|
|
53
|
-
if (isBoolean(value)) return BooleanStore
|
|
54
|
-
if (isString(value)) return StringStore
|
|
55
|
-
|
|
56
|
-
return GeneralStore
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
publish (event) {
|
|
60
|
-
this.events.push(event)
|
|
61
|
-
if (!this.transacting) this.publishAll()
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
publishAll () {
|
|
65
|
-
const publishedStores = []
|
|
66
|
-
this.events.reverse().forEach((event) => {
|
|
67
|
-
const { name, value } = event
|
|
68
|
-
if (!publishedStores.includes(name)) {
|
|
69
|
-
publishedStores.push(name)
|
|
70
|
-
this.publisher(`${eventPrefix}:${name}`, value)
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
this.events = []
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
transaction (setter) {
|
|
77
|
-
this.transacting = true
|
|
78
|
-
setter(this)
|
|
79
|
-
this.transacting = false
|
|
80
|
-
this.publishAll()
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
value () {
|
|
84
|
-
return Object.keys(this.stores).reduce((valueObject, key) => {
|
|
85
|
-
valueObject[key] = this.stores[key].value
|
|
86
|
-
return valueObject
|
|
87
|
-
}, {})
|
|
88
|
-
}
|
|
89
|
-
}
|