Skip to content

Commit 374701e

Browse files
committed
refactor: encapsulate minijs properties/methods
1 parent 9de0652 commit 374701e

File tree

6 files changed

+127
-104
lines changed

6 files changed

+127
-104
lines changed

lib/entity.js

+18-20
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Lexer } from './generators/lexer'
22
import { Events } from './entity/events'
33
import { Attributes } from './entity/attributes'
44
import { State } from './state'
5+
import { Mini } from './main'
56

67
const IGNORED_IDS = ['$', 'this']
78

89
export class Entity {
910
constructor(el, dynamicScripts = []) {
11+
this.base = new Mini()
1012
this.element = el
1113
this.tagName = el.tagName
1214
this.dynamicScripts = dynamicScripts
@@ -17,9 +19,9 @@ export class Entity {
1719
this.state = {}
1820
this.events = new Events(this)
1921
this.attributes = new Attributes(this)
20-
MiniJS.state.addEntity(this)
22+
this.base.state.addEntity(this)
2123

22-
if (MiniJS.debug) this.element.dataset.entityId = this.id
24+
if (this.base.debug) this.element.dataset.entityId = this.id
2325

2426
this.attributes.evaluateParent()
2527
}
@@ -144,14 +146,14 @@ export class Entity {
144146
this.setAsParent()
145147

146148
if (window[this.id] == null) {
147-
window[this.id] = MiniJS.state.create({}, this.id)
149+
window[this.id] = this.base.state.create({}, this.id)
148150
}
149151

150-
MiniJS.state.addVariable(this.id, this.id)
152+
this.base.state.addVariable(this.id, this.id)
151153

152154
if (variable !== 'el') {
153155
const [_, varName] = variable.split('.')
154-
MiniJS.state.addEntityVariable(this.id, varName, this.id)
156+
this.base.state.addEntityVariable(this.id, varName, this.id)
155157
}
156158
} else if (State.isParentState(variable)) {
157159
if (!this.parent) this.parent = this.getParent()
@@ -161,26 +163,22 @@ export class Entity {
161163
if (this.parent == null) return
162164

163165
if (window[this.parent.id] == null) {
164-
window[this.parent.id] = MiniJS.state.create({}, this.parent.id)
166+
window[this.parent.id] = this.base.state.create({}, this.parent.id)
165167
}
166168

167-
MiniJS.state.addVariable(this.parent.id, this.id)
169+
this.base.state.addVariable(this.parent.id, this.id)
168170

169171
if (variable !== 'parent') {
170172
const [_, varName] = variable.split('.')
171-
MiniJS.state.addEntityVariable(this.parent.id, varName, this.id)
173+
this.base.state.addEntityVariable(this.parent.id, varName, this.id)
172174
}
173175
} else if (typeof window[variable] === 'function') {
174176
this.variables.splice(this.variables.indexOf(variable), 1)
175177
} else {
176178
try {
177179
const [identifier] = variable.split('.')
178-
179-
window[identifier] = variable.startsWith('$')
180-
? MiniJS.tryFromLocal(identifier)
181-
: window[identifier]
182-
183-
MiniJS.state.addVariable(identifier, this.id)
180+
window[identifier] = this.base.state.getState(identifier)
181+
this.base.state.addVariable(identifier, this.id)
184182
} catch (error) {
185183
console.error('Failed to initialize variable:', variable, error)
186184
}
@@ -194,7 +192,7 @@ export class Entity {
194192

195193
if (parentNode == null) return { id: 'EntityDocument' }
196194

197-
const entities = Array.from(MiniJS.state.entities.values())
195+
const entities = Array.from(this.base.state.entities.values())
198196
const entity = entities.find(
199197
(e) => e.uuid == parentNode.dataset['mini.uuid']
200198
)
@@ -209,7 +207,7 @@ export class Entity {
209207
async init() {
210208
const isScript = this.element.tagName === 'SCRIPT'
211209

212-
if (!isScript) await MiniJS.observer.waitForScripts(this.dynamicScripts)
210+
if (!isScript) await this.base.observer.waitForScripts(this.dynamicScripts)
213211

214212
this.getVariables()
215213
this.events.apply()
@@ -258,20 +256,20 @@ export class Entity {
258256

259257
dispose() {
260258
const elements = [this.element, ...this.element.querySelectorAll('*')]
261-
const entities = Array.from(MiniJS.state.entities.values())
259+
const entities = Array.from(this.base.state.entities.values())
262260
const variables = []
263261

264262
// Remove event bindings
265263
for (const element of elements) {
266264
if (element.nodeType !== 1) continue
267265

268-
const entity = MiniJS.state.getEntityByElement(element, entities)
266+
const entity = this.base.state.getEntityByElement(element, entities)
269267

270268
if (!entity) continue
271269

272270
variables.push(...entity.variables)
273271
entity.events.dispose()
274-
MiniJS.state.removeEntity(entity)
272+
this.base.state.removeEntity(entity)
275273
}
276274

277275
// Clean up unused variables
@@ -283,6 +281,6 @@ export class Entity {
283281
(variable) => !usedVariables.includes(variable)
284282
)
285283

286-
MiniJS.state.disposeVariables(this.id, unusedVariables)
284+
this.base.state.disposeVariables(this.id, unusedVariables)
287285
}
288286
}

lib/entity/events.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Interpreter } from '../generators/interpreter'
22
import { camelToKebabCase } from '../helpers/strings'
3+
import { Mini } from '../main'
34
import { State } from '../state'
45
import { EventsExtensions } from './events-extensions'
56

@@ -75,7 +76,8 @@ export class Events {
7576
}
7677

7778
static applyEvents() {
78-
const entities = Array.from(MiniJS.state.entities.values())
79+
const mini = new Mini()
80+
const entities = Array.from(mini.state.entities.values())
7981
entities.forEach((entity) => {
8082
entity.events.apply()
8183
})
@@ -417,26 +419,29 @@ export class Events {
417419
}
418420

419421
async evaluate(attr) {
420-
const expr = this.base.element.getAttribute(attr)
422+
const entity = this.base
423+
const mini = entity.base
424+
425+
const expr = entity.element.getAttribute(attr)
421426
if (!expr) return
422427

423-
const elVariables = this.base.variables
428+
const elVariables = entity.variables
424429
.filter((v) => v.startsWith('el.') && v !== 'el')
425430
.map((v) => v.replace('el.', ''))
426-
const variables = this.base.variables.filter((v) => !v.startsWith('el.'))
431+
const variables = entity.variables.filter((v) => !v.startsWith('el.'))
427432

428-
MiniJS.state.attachVariableHelpers(variables)
429-
MiniJS.state.attachVariableHelpers(elVariables, this.base.id)
433+
mini.state.attachVariableHelpers(variables)
434+
mini.state.attachVariableHelpers(elVariables, entity.id)
430435

431436
try {
432437
await this._interpret(expr)
433438

434-
MiniJS.state.attachVariableHelpers(variables)
435-
MiniJS.state.attachVariableHelpers(elVariables, this.base.id)
439+
mini.state.attachVariableHelpers(variables)
440+
mini.state.attachVariableHelpers(elVariables, entity.id)
436441
} catch (error) {
437-
if (!this.base.isExists()) return
442+
if (!entity.isExists()) return
438443
console.error(
439-
`Failed to evaluate ${attr} for ${this.base.id}:\n\nCode:\n${expr}\n\n`,
444+
`Failed to evaluate ${attr} for ${entity.id}:\n\nCode:\n${expr}\n\n`,
440445
error
441446
)
442447
}

lib/generators/interpreter.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Mini } from '../main.js'
12
import { Lexer } from './lexer.js'
23

34
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
@@ -37,10 +38,11 @@ export class Interpreter extends Lexer {
3738

3839
async interpret(context, _scope = {}) {
3940
const code = super.output()
41+
const mini = new Mini()
4042

4143
const scope = {
4244
...DEFAULT_SCOPE,
43-
proxyWindow: MiniJS.window,
45+
proxyWindow: mini.state.window,
4446
..._scope,
4547
}
4648

@@ -61,8 +63,13 @@ export class ClassInterpreter extends Lexer {
6163

6264
async interpret(context, _scope = {}) {
6365
const classNames = super.conditional()
66+
const mini = new Mini()
6467

65-
const scope = { ...DEFAULT_SCOPE, proxyWindow: MiniJS.window, ..._scope }
68+
const scope = {
69+
...DEFAULT_SCOPE,
70+
proxyWindow: mini.state.window,
71+
..._scope,
72+
}
6673

6774
let newClassNames = [...this._baseClasses]
6875

lib/generators/observer.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Entity } from '../entity'
22
import { Events } from '../entity/events'
3+
import { Mini } from '../main'
34

45
const MutationObserver =
56
window.MutationObserver || window.WebKitMutationObserver
@@ -35,7 +36,7 @@ export class Observer {
3536
static SCRIPT_ID = 'mini.scriptId'
3637

3738
constructor(state) {
38-
this.state = state
39+
this.base = new Mini()
3940
this.observer = null
4041
this.dynamicScripts = new Map()
4142
}
@@ -47,7 +48,7 @@ export class Observer {
4748
record.type === 'attributes' &&
4849
record.attributeName.startsWith(':')
4950
) {
50-
const entity = this.state.getEntityByElement(record.target)
51+
const entity = this.base.state.getEntityByElement(record.target)
5152
if (!entity) return
5253

5354
const attr = record.attributeName
@@ -58,7 +59,7 @@ export class Observer {
5859

5960
record.removedNodes.forEach((node) => {
6061
if (node.nodeType !== 1) return
61-
const entity = this.state.getEntityByElement(node)
62+
const entity = this.base.state.getEntityByElement(node)
6263
entity?.dispose()
6364
})
6465

@@ -102,7 +103,7 @@ export class Observer {
102103
script.dataset[Observer.SCRIPT_ID] =
103104
script.dataset[Observer.SCRIPT_ID] ?? this.generateUUID()
104105

105-
script.textContent += `\nMiniJS.observer.resolveScript()`
106+
script.textContent += `\nMiniJS.resolveScript()`
106107
if (!this.dynamicScripts.get(script.dataset[Observer.SCRIPT_ID]))
107108
this.dynamicScripts.set(script.dataset[Observer.SCRIPT_ID], promise)
108109

@@ -127,7 +128,7 @@ export class Observer {
127128

128129
delete script.dataset['mini.scriptId']
129130
script.textContent = script.textContent.replace(
130-
'\nMiniJS.observer.resolveScript()',
131+
'\nMiniJS.resolveScript()',
131132
''
132133
)
133134
}

0 commit comments

Comments
 (0)