ks-fwork 3.0.2 → 3.0.4
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/ks-fwork.js +60 -62
- package/package.json +1 -1
- package/src/app.js +0 -5
- package/src/mount-dom.js +1 -1
package/dist/ks-fwork.js
CHANGED
|
@@ -238,40 +238,6 @@ function removeFragmentNode(vNode) {
|
|
|
238
238
|
children.forEach(destroyDOM);
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
class Dispatcher {
|
|
242
|
-
#handlersByCommand = new Map();
|
|
243
|
-
#afterHandlers = [];
|
|
244
|
-
subscribe(commandName, handler) {
|
|
245
|
-
if (!this.#handlersByCommand.has(commandName)) {
|
|
246
|
-
this.#handlersByCommand.set(commandName, []);
|
|
247
|
-
}
|
|
248
|
-
const handlers = this.#handlersByCommand.get(commandName);
|
|
249
|
-
if (handlers.includes(handler)) {
|
|
250
|
-
return () => {}
|
|
251
|
-
}
|
|
252
|
-
handlers.push(handler);
|
|
253
|
-
return () => {
|
|
254
|
-
const index = handlers.indexOf(handler);
|
|
255
|
-
handlers.splice(index, 1);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
afterEveryCommand(handler) {
|
|
259
|
-
this.#afterHandlers.push(handler);
|
|
260
|
-
return () => {
|
|
261
|
-
const index = this.#afterHandlers.indexOf(handler);
|
|
262
|
-
this.#afterHandlers.splice(index, 1);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
dispatch(commandName, payload) {
|
|
266
|
-
if (this.#handlersByCommand.has(commandName)) {
|
|
267
|
-
this.#handlersByCommand.get(commandName).forEach((handler) => handler(payload));
|
|
268
|
-
} else {
|
|
269
|
-
console.warn(`No handlers for command: ${commandName}`);
|
|
270
|
-
}
|
|
271
|
-
this.#afterHandlers.forEach((handler) => handler());
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
241
|
function setAttributes(el, attrs) {
|
|
276
242
|
const { class: className, style, ...otherAttrs } = attrs;
|
|
277
243
|
if (className) {
|
|
@@ -321,7 +287,7 @@ function extractPropsAndEvents(vNode) {
|
|
|
321
287
|
return { props, events }
|
|
322
288
|
}
|
|
323
289
|
|
|
324
|
-
function mountDOM(vNode, parentEl, index, hostComponent = null) {
|
|
290
|
+
function mountDOM(vNode, parentEl, index = undefined, hostComponent = null) {
|
|
325
291
|
switch (vNode.type) {
|
|
326
292
|
case DOM_TYPES.TEXT: {
|
|
327
293
|
createTextNode(vNode, parentEl, index);
|
|
@@ -393,6 +359,31 @@ function insert(el, parentEl, index) {
|
|
|
393
359
|
}
|
|
394
360
|
}
|
|
395
361
|
|
|
362
|
+
function createApp(RootComponent, props = {}) {
|
|
363
|
+
let parentEl = null;
|
|
364
|
+
let rootNode = null;
|
|
365
|
+
let isMounted = false;
|
|
366
|
+
function reset() {
|
|
367
|
+
parentEl = null;
|
|
368
|
+
rootNode = null;
|
|
369
|
+
isMounted = false;
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
mount(_parentEl) {
|
|
373
|
+
if (isMounted) throw new Error('The application is already mounted');
|
|
374
|
+
parentEl = _parentEl;
|
|
375
|
+
rootNode = h(RootComponent, props);
|
|
376
|
+
mountDOM(rootNode, parentEl);
|
|
377
|
+
isMounted = true;
|
|
378
|
+
},
|
|
379
|
+
unmount() {
|
|
380
|
+
if (!isMounted) throw new Error('The application is not mounted');
|
|
381
|
+
destroyDOM(rootNode);
|
|
382
|
+
reset();
|
|
383
|
+
},
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
396
387
|
function areNodesEqual(nodeOne, nodeTwo) {
|
|
397
388
|
if (nodeOne.type !== nodeTwo.type) return false;
|
|
398
389
|
if (nodeOne.type === DOM_TYPES.ELEMENT) {
|
|
@@ -572,33 +563,6 @@ function patchChildren(oldVNode, newVNode, hostComponent) {
|
|
|
572
563
|
}
|
|
573
564
|
}
|
|
574
565
|
|
|
575
|
-
function createApp(RootComponent, props = {}) {
|
|
576
|
-
let parentEl = null;
|
|
577
|
-
let rootNode = null;
|
|
578
|
-
let isMounted = false;
|
|
579
|
-
const dispatcher = new Dispatcher();
|
|
580
|
-
[dispatcher.afterEveryCommand(renderApp)];
|
|
581
|
-
function reset() {
|
|
582
|
-
parentEl = null;
|
|
583
|
-
rootNode = null;
|
|
584
|
-
isMounted = false;
|
|
585
|
-
}
|
|
586
|
-
return {
|
|
587
|
-
mount(_parentEl) {
|
|
588
|
-
if (isMounted) throw new Error('The application is already mounted');
|
|
589
|
-
parentEl = _parentEl;
|
|
590
|
-
rootNode = h(RootComponent, props);
|
|
591
|
-
mountDOM(rootNode, parentEl);
|
|
592
|
-
isMounted = true;
|
|
593
|
-
},
|
|
594
|
-
unmount() {
|
|
595
|
-
if (!isMounted) throw new Error('The application is not mounted');
|
|
596
|
-
destroyDOM(rootNode);
|
|
597
|
-
reset();
|
|
598
|
-
},
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
566
|
function getDefaultExportFromCjs (x) {
|
|
603
567
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
604
568
|
}
|
|
@@ -642,6 +606,40 @@ function requireFastDeepEqual () {
|
|
|
642
606
|
var fastDeepEqualExports = requireFastDeepEqual();
|
|
643
607
|
var equal = /*@__PURE__*/getDefaultExportFromCjs(fastDeepEqualExports);
|
|
644
608
|
|
|
609
|
+
class Dispatcher {
|
|
610
|
+
#handlersByCommand = new Map();
|
|
611
|
+
#afterHandlers = [];
|
|
612
|
+
subscribe(commandName, handler) {
|
|
613
|
+
if (!this.#handlersByCommand.has(commandName)) {
|
|
614
|
+
this.#handlersByCommand.set(commandName, []);
|
|
615
|
+
}
|
|
616
|
+
const handlers = this.#handlersByCommand.get(commandName);
|
|
617
|
+
if (handlers.includes(handler)) {
|
|
618
|
+
return () => {}
|
|
619
|
+
}
|
|
620
|
+
handlers.push(handler);
|
|
621
|
+
return () => {
|
|
622
|
+
const index = handlers.indexOf(handler);
|
|
623
|
+
handlers.splice(index, 1);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
afterEveryCommand(handler) {
|
|
627
|
+
this.#afterHandlers.push(handler);
|
|
628
|
+
return () => {
|
|
629
|
+
const index = this.#afterHandlers.indexOf(handler);
|
|
630
|
+
this.#afterHandlers.splice(index, 1);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
dispatch(commandName, payload) {
|
|
634
|
+
if (this.#handlersByCommand.has(commandName)) {
|
|
635
|
+
this.#handlersByCommand.get(commandName).forEach((handler) => handler(payload));
|
|
636
|
+
} else {
|
|
637
|
+
console.warn(`No handlers for command: ${commandName}`);
|
|
638
|
+
}
|
|
639
|
+
this.#afterHandlers.forEach((handler) => handler());
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
645
643
|
function defineComponent({ render, state, ...methods }) {
|
|
646
644
|
class Component {
|
|
647
645
|
#isMounted = false;
|
package/package.json
CHANGED
package/src/app.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { destroyDOM } from "./destroy-dom.js";
|
|
2
|
-
import { Dispatcher } from "./dispatcher.js";
|
|
3
2
|
import { mountDOM } from "./mount-dom.js";
|
|
4
|
-
import { patchDOM } from "./patch-dom.js";
|
|
5
3
|
import { h } from "./h.js";
|
|
6
4
|
|
|
7
5
|
export function createApp(RootComponent, props = {}) {
|
|
@@ -9,9 +7,6 @@ export function createApp(RootComponent, props = {}) {
|
|
|
9
7
|
let rootNode = null;
|
|
10
8
|
let isMounted = false;
|
|
11
9
|
|
|
12
|
-
const dispatcher = new Dispatcher();
|
|
13
|
-
const unsubscribers = [dispatcher.afterEveryCommand(renderApp)];
|
|
14
|
-
|
|
15
10
|
function reset() {
|
|
16
11
|
parentEl = null;
|
|
17
12
|
rootNode = null;
|
package/src/mount-dom.js
CHANGED
|
@@ -4,7 +4,7 @@ import { addEventListeners } from "./events.js";
|
|
|
4
4
|
import { extractPropsAndEvents } from "./utils/props.js";
|
|
5
5
|
|
|
6
6
|
// Recursively mount a virtual node (and its subtree) into a real parent DOM element.
|
|
7
|
-
export function mountDOM(vNode, parentEl, index, hostComponent = null) {
|
|
7
|
+
export function mountDOM(vNode, parentEl, index = undefined, hostComponent = null) {
|
|
8
8
|
switch (vNode.type) {
|
|
9
9
|
case DOM_TYPES.TEXT: {
|
|
10
10
|
createTextNode(vNode, parentEl, index);
|