rumious 2.0.1 → 2.0.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.
@@ -1,31 +0,0 @@
1
- import { RumiousRenderContext } from './context.js';
2
- import { RumiousTemplate } from '../types/index.js';
3
-
4
- export function render(
5
- content: RumiousTemplate,
6
- container:HTMLElement,
7
- context:RumiousRenderContext
8
- ):HTMLElement{
9
- context.onRendered = [];
10
- let result = content(container,context);
11
-
12
- for (var i = 0; i < context.onRendered.length; i++) {
13
- context.onRendered[i]();
14
- }
15
- return result;
16
- }
17
-
18
- export function renderFrag(
19
- content: RumiousTemplate,
20
- context: RumiousRenderContext
21
- ): HTMLElement {
22
- let container = document.createDocumentFragment();
23
- context.onRendered = [];
24
- let result = content(container, context);
25
-
26
- for (var i = 0; i < context.onRendered.length; i++) {
27
- context.onRendered[i]();
28
- }
29
-
30
- return result;
31
- }
@@ -1,101 +0,0 @@
1
- import { RumiousTemplate } from '../types/index.js';
2
- import { RumiousRenderContext } from './context.js';
3
- import { render, renderFrag } from './render.js';
4
-
5
- export interface RumiousViewControlTarget {
6
- element: HTMLElement,
7
- context: RumiousRenderContext
8
- }
9
-
10
- export class RumiousViewControl {
11
- private targets: RumiousViewControlTarget[] = [];
12
-
13
- constructor() {}
14
-
15
- addTarget(target: RumiousViewControlTarget) {
16
- this.targets.push(target);
17
- }
18
-
19
-
20
- setView(template: RumiousTemplate) {
21
-
22
- const targets = this.targets;
23
-
24
- if (targets.length === 0) {
25
- throw new Error(`RumiousRenderError: No target assigned to ViewControl`);
26
- }
27
-
28
- for (let i = 0; i < targets.length; i++) {
29
- render(template, targets[i].element, targets[i].context);
30
- }
31
- }
32
-
33
- removeChild(index: number) {
34
- for (let i = 0; i < this.targets.length; i++) {
35
- let parent = this.targets[i].element.parentElement;
36
- if (!parent) return;
37
- let element = parent.children[index];
38
- if (element) parent.removeChild(element);
39
- }
40
- }
41
-
42
- addChild(
43
- template: RumiousTemplate,
44
- prepend: boolean = false
45
- ) {
46
- const targets = this.targets;
47
- if (targets.length === 0) {
48
- throw new Error(`RumiousRenderError: No target assigned to ViewControl`);
49
- }
50
-
51
- for (let i = 0; i < targets.length; i++) {
52
- let templ = renderFrag(template, targets[i].context);
53
- if (!prepend) targets[i].element.appendChild(templ);
54
- else targets[i].element.prepend(templ);
55
- }
56
- }
57
-
58
-
59
- each(callback: (target: RumiousViewControlTarget) => any) {
60
- for (let target of this.targets) {
61
- callback(target);
62
- }
63
- }
64
-
65
- emptyAll() {
66
- const targets = this.targets;
67
- for (let i = 0; i < targets.length; i++) {
68
- targets[i].element.textContent = '';
69
- }
70
- }
71
-
72
- empty(target: HTMLElement) {
73
- const targets = this.targets;
74
- for (let i = 0; i < targets.length; i++) {
75
- if (targets[i].element === target) {
76
- target.textContent = '';
77
- return;
78
- }
79
- }
80
- }
81
-
82
- updateChild(index: number, template: RumiousTemplate) {
83
- for (let i = 0; i < this.targets.length; i++) {
84
- const { element, context } = this.targets[i];
85
- const parent = element.parentElement;
86
- if (!parent) continue;
87
-
88
- const oldChild = parent.children[index];
89
- const newNode = renderFrag(template, context);
90
-
91
- if (oldChild) {
92
- parent.replaceChild(newNode, oldChild);
93
- }
94
- }
95
- }
96
-
97
- }
98
-
99
- export function createViewControl(): RumiousViewControl {
100
- return new RumiousViewControl();
101
- }
@@ -1,2 +0,0 @@
1
- export * from './state.js';
2
- export * from './list.js';
package/src/state/list.ts DELETED
@@ -1,96 +0,0 @@
1
- import { RumiousState } from './state.js';
2
- import { RumiousReactor } from './reactor.js';
3
-
4
- export class RumiousListState < T > extends RumiousState < T[] > {
5
- constructor(
6
- value: T[] = [],
7
- reactor ? : RumiousReactor < RumiousState < T[] >>
8
- ) {
9
- super(value, reactor);
10
- }
11
-
12
- append(value: T) {
13
- this.value.push(value);
14
- this.reactor?.notify({
15
- type: 'append',
16
- state: this,
17
- key:this.value.length -1 ,
18
- value
19
- });
20
- }
21
-
22
- prepend(value: T) {
23
- this.value.unshift(value);
24
- this.reactor?.notify({
25
- type: 'prepend',
26
- state: this,
27
- key:0,
28
- value
29
- });
30
- }
31
-
32
- insert(pos: number, value: T) {
33
- this.value.splice(pos, 0, value);
34
- this.reactor?.notify({
35
- type: 'insert',
36
- state: this,
37
- value,
38
- key: pos
39
- });
40
- }
41
-
42
- updateAt(pos: number, value: T) {
43
- this.value[pos] = value;
44
- this.reactor?.notify({
45
- type: 'update',
46
- state: this,
47
- value,
48
- key: pos
49
- });
50
- }
51
-
52
- remove(pos: number) {
53
- let currentValue = this.value[pos];
54
- this.value.splice(pos, 1);
55
- this.reactor?.notify({
56
- type: 'remove',
57
- state: this,
58
- value: currentValue,
59
- key: pos
60
- });
61
- }
62
-
63
- clear() {
64
- this.value.length = 0;
65
- this.reactor?.notify({
66
- type: 'set',
67
- state: this,
68
- value: []
69
- });
70
- }
71
-
72
- reverse() {
73
- this.value.reverse();
74
- this.reactor?.notify({
75
- type: 'set',
76
- state: this,
77
- value: this.value
78
- });
79
- }
80
-
81
- filter(predicate: (item: T, index: number, array: T[]) => boolean) {
82
- this.value = this.value.filter(predicate);
83
- this.reactor?.notify({
84
- type: 'set',
85
- state: this,
86
- value: this.value
87
- });
88
- }
89
-
90
- }
91
-
92
- export function createListState<T>(
93
- values:T[]
94
- ):RumiousListState<T>{
95
- return new RumiousListState(values);
96
- }
@@ -1,65 +0,0 @@
1
- import { RumiousStateBind, RumiousChangeCommit } from '../types/index.js';
2
-
3
- export class RumiousReactor < T = any > {
4
- private bindings: RumiousStateBind < T > [] = [];
5
- private internal: RumiousStateBind < T > [] = [];
6
- public isUIBatch = true;
7
- private scheduled = false;
8
- private queuedCommits: RumiousChangeCommit < T > [] = [];
9
-
10
-
11
- constructor(public target: T) {}
12
-
13
- addBinding(binding: RumiousStateBind < T > ): void {
14
- this.bindings.push(binding);
15
- }
16
-
17
- removeBinding(binding: RumiousStateBind < T > ): void {
18
- this.bindings = this.bindings.filter(b => b !== binding);
19
- }
20
-
21
- addInternalBinding(binding: RumiousStateBind < T > ): void {
22
- this.internal.push(binding);
23
- }
24
-
25
- removeInternalBinding(binding: RumiousStateBind < T > ): void {
26
- this.internal = this.internal.filter(b => b !== binding);
27
- }
28
-
29
- notify(commit: RumiousChangeCommit < T > ): void {
30
- for (const binding of this.bindings) {
31
- binding(commit);
32
- }
33
-
34
- if (this.isUIBatch) {
35
- this.scheduleInternalUpdate(commit);
36
- } else {
37
- for (const binding of this.internal) {
38
- binding(commit);
39
- }
40
- }
41
- }
42
-
43
-
44
- private scheduleInternalUpdate(commit: RumiousChangeCommit < T > ) {
45
- this.queuedCommits.push(commit);
46
-
47
- if (!this.scheduled) {
48
- this.scheduled = true;
49
- queueMicrotask(() => {
50
- this.flushInternal();
51
- });
52
- }
53
- }
54
-
55
- private flushInternal() {
56
- const lastCommit = this.queuedCommits[this.queuedCommits.length - 1];
57
-
58
- for (const binding of this.internal) {
59
- binding(lastCommit); // chỉ gửi commit cuối cùng
60
- }
61
-
62
- this.queuedCommits = [];
63
- this.scheduled = false;
64
- }
65
- }
@@ -1,68 +0,0 @@
1
- import { RumiousReactor } from './reactor.js';
2
- import { RumiousStateBind } from '../types/index.js';
3
-
4
- export class RumiousState < T > {
5
-
6
- constructor(
7
- public value: T,
8
- public reactor ? : RumiousReactor < RumiousState < T >>
9
- ) {
10
-
11
- if (!this.reactor) {
12
- this.reactor = new RumiousReactor < RumiousState < T >> (this);
13
- }
14
- }
15
-
16
- set(value: T) {
17
- this.value = value;
18
- this.reactor?.notify({
19
- type: 'set',
20
- value: value,
21
- state: this
22
- });
23
- }
24
-
25
- get(): T {
26
- return this.value;
27
- }
28
-
29
- slientUpdate(value: T) {
30
- this.value = value;
31
- }
32
-
33
- update(updater: (value: T) => T) {
34
- this.set(updater(this.value));
35
- }
36
-
37
- toJSON(): string {
38
- return JSON.stringify(this.value);
39
- }
40
-
41
- trigger() {
42
- this.reactor?.notify({
43
- type: 'set',
44
- value: this.value,
45
- state: this
46
- });
47
- }
48
- }
49
-
50
- export function createState < T > (value: T): RumiousState < T > {
51
- return new RumiousState < T > (value);
52
- }
53
-
54
- type StateBindFor < M > = RumiousStateBind < RumiousState < M >> ;
55
-
56
- export function watch < M > (
57
- state: RumiousState < M > ,
58
- callback: StateBindFor < M >
59
- ) {
60
- if (state.reactor) state.reactor.addBinding(callback);
61
- }
62
-
63
- export function unwatch < M > (
64
- state: RumiousState < M > ,
65
- callback: StateBindFor < M >
66
- ) {
67
- if (state.reactor) state.reactor.removeBinding(callback);
68
- }
@@ -1,6 +0,0 @@
1
- import type { RumiousComponent } from '../component/index.js';
2
-
3
- export interface RumiousComponentConstructor<T = any> {
4
- new(): RumiousComponent<T>;
5
- tagName: string | 'rumious-component';
6
- }
@@ -1,3 +0,0 @@
1
- export * from './template.js';
2
- export * from './component.js';
3
- export * from './state.js';
@@ -1,16 +0,0 @@
1
- export type RumiousChangeCommitType =
2
- | 'set'
3
- | 'append'
4
- | 'prepend'
5
- | 'insert'
6
- | 'update'
7
- | 'remove'
8
-
9
- export interface RumiousChangeCommit<T> {
10
- state:T;
11
- type:RumiousChangeCommitType;
12
- value:any;
13
- key?:number;
14
- }
15
-
16
- export type RumiousStateBind < T = any > = (commit:RumiousChangeCommit<T>) => void;
@@ -1,7 +0,0 @@
1
- import type { RumiousRenderContext } from '../render/index.js';
2
-
3
- export interface RumiousTemplate {
4
- (root: HTMLElement | DocumentFragment, context: RumiousRenderContext): HTMLElement;
5
- __isTemplate: true;
6
- }
7
-
@@ -1,5 +0,0 @@
1
- import {RumiousTemplate} from '../types/index.js';
2
-
3
- export function isTemplate(fn: unknown): fn is RumiousTemplate {
4
- return typeof fn === 'function' && (fn as any).__isTemplate === true;
5
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "esnext",
4
- "module": "esnext",
5
- "outDir": "./dist",
6
- "esModuleInterop": true,
7
- "sourceMap":false,
8
- "allowSyntheticDefaultImports": true,
9
- "moduleResolution": "node",
10
- "resolveJsonModule": true,
11
- "skipLibCheck": true,
12
- "declaration": true,
13
- "strict": true,
14
- "forceConsistentCasingInFileNames": true,
15
- "paths": {
16
- "rumious-compiler": ["../compiler/dist/index.d.ts"]
17
- }
18
- },
19
- "include": ["./src", "./src/global.d.ts"]
20
- }