marko 6.0.31 → 6.0.33

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.
@@ -80,6 +80,7 @@ export interface Template {
80
80
  render(input?: Input): RenderedTemplate;
81
81
  }
82
82
  export interface MountedTemplate {
83
+ value: unknown;
83
84
  update(input: unknown): void;
84
85
  destroy(): void;
85
86
  }
package/dist/debug/dom.js CHANGED
@@ -2303,6 +2303,7 @@ function mount(input = {}, reference, position) {
2303
2303
  nextSibling = reference.nextSibling;
2304
2304
  break;
2305
2305
  }
2306
+ let curValue;
2306
2307
  const args = this.___params;
2307
2308
  const effects = prepareEffects(() => {
2308
2309
  branch = createBranch(
@@ -2311,6 +2312,9 @@ function mount(input = {}, reference, position) {
2311
2312
  void 0,
2312
2313
  parentNode
2313
2314
  );
2315
+ branch["#TagVariable" /* TagVariable */] = (newValue) => {
2316
+ curValue = newValue;
2317
+ };
2314
2318
  this.___setup?.(branch);
2315
2319
  args?.(branch, input);
2316
2320
  });
@@ -2322,6 +2326,12 @@ function mount(input = {}, reference, position) {
2322
2326
  );
2323
2327
  runEffects(effects);
2324
2328
  return {
2329
+ get value() {
2330
+ return curValue;
2331
+ },
2332
+ set value(newValue) {
2333
+ tagVarSignalChange(branch, newValue);
2334
+ },
2325
2335
  update(newInput) {
2326
2336
  if (args) {
2327
2337
  runEffects(
@@ -2210,6 +2210,7 @@ function mount(input = {}, reference, position) {
2210
2210
  nextSibling = reference.nextSibling;
2211
2211
  break;
2212
2212
  }
2213
+ let curValue;
2213
2214
  const args = this.___params;
2214
2215
  const effects = prepareEffects(() => {
2215
2216
  branch = createBranch(
@@ -2218,6 +2219,9 @@ function mount(input = {}, reference, position) {
2218
2219
  void 0,
2219
2220
  parentNode
2220
2221
  );
2222
+ branch["#TagVariable" /* TagVariable */] = (newValue) => {
2223
+ curValue = newValue;
2224
+ };
2221
2225
  this.___setup?.(branch);
2222
2226
  args?.(branch, input);
2223
2227
  });
@@ -2229,6 +2233,12 @@ function mount(input = {}, reference, position) {
2229
2233
  );
2230
2234
  runEffects(effects);
2231
2235
  return {
2236
+ get value() {
2237
+ return curValue;
2238
+ },
2239
+ set value(newValue) {
2240
+ tagVarSignalChange(branch, newValue);
2241
+ },
2232
2242
  update(newInput) {
2233
2243
  if (args) {
2234
2244
  runEffects(
@@ -1,3 +1,3 @@
1
- import type { Scope, Template } from "../common/types";
2
- import type { Signal } from "./signals";
1
+ import { type Scope, type Template } from "../common/types";
2
+ import { type Signal } from "./signals";
3
3
  export declare const createTemplate: (id: string, template: string | 0, walks?: string | 0, setup?: ((scope: Scope) => void) | 0, inputSignal?: Signal<unknown>) => Template;
package/dist/dom.js CHANGED
@@ -1509,13 +1509,15 @@ function mount(input = {}, reference, position) {
1509
1509
  parentNode = reference.parentNode, nextSibling = reference.nextSibling;
1510
1510
  break;
1511
1511
  }
1512
- let args = this.m, effects = prepareEffects(() => {
1512
+ let curValue, args = this.m, effects = prepareEffects(() => {
1513
1513
  branch = createBranch(
1514
1514
  $global,
1515
1515
  this,
1516
1516
  void 0,
1517
1517
  parentNode
1518
- ), this.D?.(branch), args?.(branch, input);
1518
+ ), branch.e = (newValue) => {
1519
+ curValue = newValue;
1520
+ }, this.D?.(branch), args?.(branch, input);
1519
1521
  });
1520
1522
  return insertChildNodes(
1521
1523
  parentNode,
@@ -1523,6 +1525,12 @@ function mount(input = {}, reference, position) {
1523
1525
  branch.h,
1524
1526
  branch.j
1525
1527
  ), runEffects(effects), {
1528
+ get value() {
1529
+ return curValue;
1530
+ },
1531
+ set value(newValue) {
1532
+ tagVarSignalChange(branch, newValue);
1533
+ },
1526
1534
  update(newInput) {
1527
1535
  args && runEffects(
1528
1536
  prepareEffects(() => {
package/dist/dom.mjs CHANGED
@@ -1419,13 +1419,15 @@ function mount(input = {}, reference, position) {
1419
1419
  parentNode = reference.parentNode, nextSibling = reference.nextSibling;
1420
1420
  break;
1421
1421
  }
1422
- let args = this.m, effects = prepareEffects(() => {
1422
+ let curValue, args = this.m, effects = prepareEffects(() => {
1423
1423
  branch = createBranch(
1424
1424
  $global,
1425
1425
  this,
1426
1426
  void 0,
1427
1427
  parentNode
1428
- ), this.D?.(branch), args?.(branch, input);
1428
+ ), branch.e = (newValue) => {
1429
+ curValue = newValue;
1430
+ }, this.D?.(branch), args?.(branch, input);
1429
1431
  });
1430
1432
  return insertChildNodes(
1431
1433
  parentNode,
@@ -1433,6 +1435,12 @@ function mount(input = {}, reference, position) {
1433
1435
  branch.h,
1434
1436
  branch.j
1435
1437
  ), runEffects(effects), {
1438
+ get value() {
1439
+ return curValue;
1440
+ },
1441
+ set value(newValue) {
1442
+ tagVarSignalChange(branch, newValue);
1443
+ },
1436
1444
  update(newInput) {
1437
1445
  args && runEffects(
1438
1446
  prepareEffects(() => {
@@ -538,6 +538,36 @@ function forEachIdentifier(node, cb) {
538
538
  break;
539
539
  }
540
540
  }
541
+ function forEachIdentifierPath(nodePath, cb) {
542
+ if (nodePath.isIdentifier()) {
543
+ cb(nodePath);
544
+ } else if (nodePath.isObjectPattern()) {
545
+ for (const prop of nodePath.get("properties")) {
546
+ if (prop.isObjectProperty()) {
547
+ const value = prop.get("value");
548
+ if (value.isAssignmentPattern()) {
549
+ forEachIdentifierPath(value.get("left"), cb);
550
+ } else {
551
+ forEachIdentifierPath(value, cb);
552
+ }
553
+ } else if (prop.isRestElement()) {
554
+ forEachIdentifierPath(prop.get("argument"), cb);
555
+ }
556
+ }
557
+ } else if (nodePath.isArrayPattern()) {
558
+ for (const el of nodePath.get("elements")) {
559
+ if (el) {
560
+ if (el.isRestElement()) {
561
+ forEachIdentifierPath(el.get("argument"), cb);
562
+ } else if (el.isAssignmentPattern()) {
563
+ forEachIdentifierPath(el.get("left"), cb);
564
+ } else {
565
+ forEachIdentifierPath(el, cb);
566
+ }
567
+ }
568
+ }
569
+ }
570
+ }
541
571
 
542
572
  // src/translator/util/generate-uid.ts
543
573
  var import_compiler3 = require("@marko/compiler");
@@ -3313,13 +3343,17 @@ function getTranslatedExtraArgs(signal) {
3313
3343
  }
3314
3344
  return emptyExtraArgs;
3315
3345
  }
3316
- function subscribe(provider, subscriber) {
3317
- if (Array.isArray(provider)) {
3318
- provider.forEach((p) => subscribe(p, subscriber));
3319
- return;
3346
+ function subscribe(references, subscriber) {
3347
+ if (references) {
3348
+ forEach(references, (binding) => {
3349
+ const source = binding.property === void 0 && binding.upstreamAlias || binding;
3350
+ const providerSignal = getSignal(subscriber.section, source);
3351
+ providerSignal.intersection = push(
3352
+ providerSignal.intersection,
3353
+ subscriber
3354
+ );
3355
+ });
3320
3356
  }
3321
- const providerSignal = getSignal(subscriber.section, provider);
3322
- providerSignal.intersection = push(providerSignal.intersection, subscriber);
3323
3357
  }
3324
3358
  function generateSignalName(referencedBindings) {
3325
3359
  let name2;
@@ -3880,10 +3914,7 @@ function replaceAssignedNode(node) {
3880
3914
  case "UpdateExpression": {
3881
3915
  const { extra } = node.argument;
3882
3916
  if (isAssignedBindingExtra(extra)) {
3883
- const { buildAssignment } = getSignal(
3884
- extra.assignment.section,
3885
- extra.assignment
3886
- );
3917
+ const buildAssignment = getBuildAssignment(extra);
3887
3918
  if (buildAssignment) {
3888
3919
  const replacement = buildAssignment(
3889
3920
  extra.section,
@@ -3906,10 +3937,7 @@ function replaceAssignedNode(node) {
3906
3937
  case "Identifier": {
3907
3938
  const { extra } = node.left;
3908
3939
  if (isAssignedBindingExtra(extra)) {
3909
- const { buildAssignment } = getSignal(
3910
- extra.assignment.section,
3911
- extra.assignment
3912
- );
3940
+ const buildAssignment = getBuildAssignment(extra);
3913
3941
  if (buildAssignment) {
3914
3942
  return buildAssignment(
3915
3943
  extra.section,
@@ -3933,15 +3961,12 @@ function replaceAssignedNode(node) {
3933
3961
  forEachIdentifier(node.left, (id) => {
3934
3962
  const { extra } = id;
3935
3963
  if (isAssignedBindingExtra(extra)) {
3936
- const signal = getSignal(
3937
- extra.assignment.section,
3938
- extra.assignment
3939
- );
3940
- if (signal?.buildAssignment) {
3964
+ const buildAssignment = getBuildAssignment(extra);
3965
+ if (buildAssignment) {
3941
3966
  id.name = generateUid(id.name);
3942
3967
  (params ||= []).push(import_compiler20.types.identifier(id.name));
3943
3968
  (assignments ||= []).push(
3944
- signal.buildAssignment(extra.section, import_compiler20.types.identifier(id.name))
3969
+ buildAssignment(extra.section, import_compiler20.types.identifier(id.name))
3945
3970
  );
3946
3971
  }
3947
3972
  }
@@ -3970,6 +3995,15 @@ function replaceAssignedNode(node) {
3970
3995
  break;
3971
3996
  }
3972
3997
  }
3998
+ function getBuildAssignment(extra) {
3999
+ const { assignmentTo, assignment } = extra;
4000
+ if (assignmentTo) {
4001
+ return (_section, value) => {
4002
+ return import_compiler20.types.callExpression(import_compiler20.types.identifier(assignmentTo.name), [value]);
4003
+ };
4004
+ }
4005
+ return getSignal(assignment.section, assignment).buildAssignment;
4006
+ }
3973
4007
  var registeredFnsForProgram = /* @__PURE__ */ new WeakMap();
3974
4008
  function replaceRegisteredFunctionNode2(node) {
3975
4009
  switch (node.type) {
@@ -4527,9 +4561,23 @@ function trackReferencesForBinding(babelBinding, binding) {
4527
4561
  function trackAssignment(assignment, binding) {
4528
4562
  const section = getOrCreateSection(assignment);
4529
4563
  setReferencesScope(assignment);
4530
- forEachIdentifier(assignment.node, (id) => {
4531
- if (id.name === binding.name) {
4532
- const extra = id.extra ??= {};
4564
+ forEachIdentifierPath(assignment, (id) => {
4565
+ if (id.node.name === binding.name) {
4566
+ const extra = id.node.extra ??= {};
4567
+ if (binding.upstreamAlias && binding.property !== void 0) {
4568
+ const changePropName = binding.property + "Change";
4569
+ const changeBinding = binding.upstreamAlias.propertyAliases.get(changePropName) || createBinding(
4570
+ generateUid(changePropName),
4571
+ 5 /* derived */,
4572
+ binding.section,
4573
+ binding.upstreamAlias,
4574
+ changePropName,
4575
+ id.node.loc,
4576
+ true
4577
+ );
4578
+ extra.assignmentTo = changeBinding;
4579
+ addReadToExpression(id, changeBinding);
4580
+ }
4533
4581
  binding.assignmentSections = sectionUtil.add(
4534
4582
  binding.assignmentSections,
4535
4583
  section
@@ -4676,21 +4724,7 @@ function trackReference(referencePath, binding) {
4676
4724
  prop
4677
4725
  );
4678
4726
  }
4679
- const fnRoot = getFnRoot(root);
4680
- const exprRoot = getExprRoot(fnRoot || root);
4681
- const { section } = addReadToExpression(exprRoot, reference, root.node);
4682
- if (fnRoot) {
4683
- const readsByFn = getReadsByFunction();
4684
- const fnExtra = fnRoot.node.extra ??= {};
4685
- fnExtra.section = section;
4686
- readsByFn.set(
4687
- fnExtra,
4688
- push(readsByFn.get(fnExtra), {
4689
- binding: reference,
4690
- node: root.node
4691
- })
4692
- );
4693
- }
4727
+ addReadToExpression(root, reference);
4694
4728
  }
4695
4729
  var [getMergedReferences] = createProgramState(
4696
4730
  () => /* @__PURE__ */ new Map()
@@ -5138,15 +5172,24 @@ var [getReadsByExpression] = createProgramState(
5138
5172
  var [getReadsByFunction] = createProgramState(
5139
5173
  () => /* @__PURE__ */ new Map()
5140
5174
  );
5141
- function addReadToExpression(path5, binding, node) {
5142
- const exprExtra = path5.node.extra ??= {};
5175
+ function addReadToExpression(root, binding) {
5176
+ const { node } = root;
5177
+ const fnRoot = getFnRoot(root);
5178
+ const exprRoot = getExprRoot(fnRoot || root);
5179
+ const exprExtra = exprRoot.node.extra ??= {};
5143
5180
  const readsByExpression = getReadsByExpression();
5144
- exprExtra.section = getOrCreateSection(path5);
5181
+ const section = exprExtra.section = getOrCreateSection(exprRoot);
5182
+ const read = { binding, node };
5145
5183
  readsByExpression.set(
5146
5184
  exprExtra,
5147
- push(readsByExpression.get(exprExtra), { binding, node })
5185
+ push(readsByExpression.get(exprExtra), read)
5148
5186
  );
5149
- return exprExtra;
5187
+ if (fnRoot) {
5188
+ const readsByFn = getReadsByFunction();
5189
+ const fnExtra = fnRoot.node.extra ??= {};
5190
+ fnExtra.section = section;
5191
+ readsByFn.set(fnExtra, push(readsByFn.get(fnExtra), read));
5192
+ }
5150
5193
  }
5151
5194
  function dropReferences(node) {
5152
5195
  if (Array.isArray(node)) {
@@ -5557,10 +5600,36 @@ function translateVar(tag, initialValue, kind = "const") {
5557
5600
  if (!tagVar) {
5558
5601
  return;
5559
5602
  }
5603
+ forEachIdentifierPath(tag.get("var"), (id) => {
5604
+ const binding = id.node.extra?.binding;
5605
+ if (!binding || !binding.upstreamAlias || !binding.assignmentSections || id.node === tagVar) {
5606
+ return;
5607
+ }
5608
+ const changeName = binding.name + "Change";
5609
+ const changeBinding = binding.upstreamAlias.propertyAliases.get(changeName);
5610
+ if (changeBinding && changeName !== changeBinding.name) {
5611
+ getDestructurePattern(id)?.pushContainer(
5612
+ "properties",
5613
+ import_compiler27.types.objectProperty(
5614
+ import_compiler27.types.identifier(changeName),
5615
+ import_compiler27.types.identifier(changeBinding.name)
5616
+ )
5617
+ );
5618
+ }
5619
+ });
5560
5620
  tag.insertBefore(
5561
5621
  import_compiler27.types.variableDeclaration(kind, [import_compiler27.types.variableDeclarator(tagVar, initialValue)])
5562
5622
  );
5563
5623
  }
5624
+ function getDestructurePattern(id) {
5625
+ let cur = id;
5626
+ while (cur) {
5627
+ if (cur.node.type === "ObjectPattern") {
5628
+ return cur;
5629
+ }
5630
+ cur = cur.parentPath;
5631
+ }
5632
+ }
5564
5633
 
5565
5634
  // src/translator/core/const.ts
5566
5635
  var const_default = {
@@ -1,2 +1,3 @@
1
1
  import type { types as t } from "@marko/compiler";
2
2
  export declare function forEachIdentifier(node: t.Node, cb: (identifier: t.Identifier) => void): void;
3
+ export declare function forEachIdentifierPath(nodePath: t.NodePath<any>, cb: (identifier: t.NodePath<t.Identifier>) => void): void;
@@ -51,6 +51,7 @@ declare module "@marko/compiler/dist/types" {
51
51
  referencedBindings?: ReferencedBindings;
52
52
  binding?: Binding;
53
53
  assignment?: Binding;
54
+ assignmentTo?: Binding;
54
55
  read?: {
55
56
  binding: Binding;
56
57
  props: Opt<string>;
@@ -86,7 +87,6 @@ export declare function createSources(state: Sources["state"], input: Sources["i
86
87
  export declare function compareSources(a: Sources, b: Sources): number;
87
88
  export declare function mergeSources(a: undefined | Sources, b: undefined | Sources): Sources | undefined;
88
89
  export declare const bindingUtil: Sorted<Binding>;
89
- export declare function addReadToExpression(path: t.NodePath, binding: Binding, node?: t.Identifier | t.MemberExpression): ReferencedExtra;
90
90
  export declare function dropReferences(node: t.Node | t.Node[]): void;
91
91
  export declare function getCanonicalBinding(binding?: Binding): Binding | undefined;
92
92
  export declare function getAllTagReferenceNodes(tag: t.MarkoTag, referenceNodes?: t.Node[]): t.Node[];
@@ -35,7 +35,7 @@ export declare function getHoistFunctionIdentifier(hoistedBinding: Binding): t.I
35
35
  export declare function getSignal(section: Section, referencedBindings: ReferencedBindings, name?: string): Signal;
36
36
  export declare function initValue(binding: Binding, runtimeHelper?: "value" | "state"): Signal;
37
37
  export declare function getSignalFn(signal: Signal): t.Expression;
38
- export declare function subscribe(provider: ReferencedBindings, subscriber: Signal): void;
38
+ export declare function subscribe(references: ReferencedBindings, subscriber: Signal): void;
39
39
  export declare function replaceNullishAndEmptyFunctionsWith0(args: (t.Expression | undefined | false)[]): t.Expression[];
40
40
  export declare function addStatement(type: "render" | "effect", targetSection: Section, referencedBindings: ReferencedBindings, statement: t.Statement | t.Statement[], usedReferences?: ReferencedBindings[] | false): void;
41
41
  export declare function addValue(targetSection: Section, referencedBindings: ReferencedBindings, signal: Signal["values"][number]["signal"], value: t.Expression): void;
package/index.d.ts CHANGED
@@ -46,7 +46,8 @@ declare global {
46
46
  };
47
47
 
48
48
  /** The result of calling `template.mount`. */
49
- export type MountedTemplate<Input = unknown> = {
49
+ export type MountedTemplate<Input = unknown, Return = unknown> = {
50
+ value: Return;
50
51
  update(input: Marko.TemplateInput<Input>): void;
51
52
  destroy(): void;
52
53
  };
@@ -95,7 +96,7 @@ declare global {
95
96
  input: Marko.TemplateInput<Input>,
96
97
  reference: Node,
97
98
  position?: "afterbegin" | "afterend" | "beforebegin" | "beforeend",
98
- ): Marko.MountedTemplate<typeof input>;
99
+ ): Marko.MountedTemplate<typeof input, Return>;
99
100
  /** @marko-overload-end */
100
101
  }
101
102
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marko",
3
- "version": "6.0.31",
3
+ "version": "6.0.33",
4
4
  "description": "Optimized runtime for Marko templates.",
5
5
  "keywords": [
6
6
  "api",