ueca-react 1.0.7 → 2.0.0

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.

Potentially problematic release.


This version of ueca-react might be problematic. Click here for more details.

Files changed (39) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +109 -119
  3. package/dist/index.d.ts +266 -4
  4. package/dist/ueca-react.js +1453 -0
  5. package/docs/Arrays and Reactivity in UECA-React.md +158 -0
  6. package/docs/Automatic onChange Events in UECA-React.md +142 -0
  7. package/docs/Automatic onChanging Events in UECA-React.md +157 -0
  8. package/docs/Automatic onPropChange and onPropChanging Events in UECA-React.md +112 -0
  9. package/docs/Component Extension in UECA-React.md +275 -0
  10. package/docs/Component IDs in UECA-React.md +181 -0
  11. package/docs/{component-intergation-model.md → Component Integration Model in UECA-React.md } +4 -3
  12. package/docs/{component-mental-model.md → Component Mental Model in UECA-React.md } +4 -3
  13. package/docs/Introduction to UECA-React Components.md +190 -0
  14. package/docs/Introduction to UECA-React.md +24 -0
  15. package/docs/Lifecycle Hooks in UECA-React.md +237 -0
  16. package/docs/Message Bus in UECA-React.md +260 -0
  17. package/docs/Model Caching in UECA-React.md +144 -0
  18. package/docs/Property Bindings in UECA-React.md +191 -0
  19. package/docs/State Management in UECA-React.md +128 -0
  20. package/docs/Technology of UECA-React.md +45 -0
  21. package/docs/Tracing in UECA-React.md +110 -0
  22. package/docs/code-template.md +53 -27
  23. package/docs/index.md +31 -11
  24. package/package.json +68 -72
  25. package/dist/componentModel.d.ts +0 -127
  26. package/dist/componentModel.js +0 -772
  27. package/dist/dynamicContent.d.ts +0 -22
  28. package/dist/dynamicContent.js +0 -80
  29. package/dist/index.js +0 -29
  30. package/dist/messageBus.d.ts +0 -46
  31. package/dist/messageBus.js +0 -141
  32. package/dist/utils.d.ts +0 -8
  33. package/dist/utils.js +0 -52
  34. package/docs/base-concepts.md +0 -192
  35. package/docs/bindings-overview.md +0 -164
  36. package/docs/general-code-structure.md +0 -177
  37. package/docs/introduction.md +0 -56
  38. package/docs/message-bus.md +0 -177
  39. package/docs/technology.md +0 -45
@@ -1,772 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.$ = exports.blankView = exports.ComponentModelTrap = void 0;
37
- exports.useComponentModelTrap = useComponentModelTrap;
38
- exports.newComponentModelTrapRegistry = newComponentModelTrapRegistry;
39
- exports.useComponent = useComponent;
40
- exports.mergeStruct = mergeStruct;
41
- exports.bind = bind;
42
- exports.bindProp = bindProp;
43
- exports.componentModelDebug = componentModelDebug;
44
- exports.getFC = getFC;
45
- const React = __importStar(require("react"));
46
- const react_1 = require("react");
47
- const lodash_1 = require("lodash");
48
- const mobx_1 = require("mobx");
49
- const mobx_react_1 = require("mobx-react");
50
- const messageBus_1 = require("./messageBus");
51
- const $name = "$";
52
- const $ = Symbol($name);
53
- exports.$ = $;
54
- const blankView = () => null;
55
- exports.blankView = blankView;
56
- function newComponent(struct, params) {
57
- const fullStruct = mergeStruct(struct, {}, () => model);
58
- const propBindings = [];
59
- if (params) {
60
- for (const p in fullStruct.props) {
61
- if (Reflect.has(params, p)) {
62
- if (isBinding(fullStruct.props[p])) {
63
- propBindings.push(p);
64
- }
65
- else {
66
- fullStruct.props[p] = params[p];
67
- }
68
- }
69
- }
70
- }
71
- const model = createProxy(fullStruct);
72
- model[$].__messages = struct.messages;
73
- model[$].__propBindings = propBindings;
74
- model[$].__initialize = _initialize;
75
- model[$].__unmount = _unmount;
76
- model[$].__updateInitializedModelProperties = _updateInitializedModelProperties;
77
- model.disableOnChange();
78
- for (const p in params) {
79
- if (!Reflect.has(fullStruct.props, p) && (["init", "mount", "unmount"].indexOf(p) == -1)) {
80
- if (typeof model[p] === "function" && typeof params[p] === "function") {
81
- const fn = model[p];
82
- const paramVal = params[p];
83
- if (p.startsWith("onChanging") && p !== "onChanging") {
84
- model[p] = (newValue, oldValue) => paramVal(fn(newValue, oldValue), oldValue);
85
- }
86
- else if (p.startsWith("onChange") && p !== "onChange") {
87
- model[p] = (value) => { fn(value); paramVal(value); };
88
- }
89
- else if (p === "onPropChanging") {
90
- model[p] = (prop, newValue, oldValue) => paramVal(prop, fn(prop, newValue, oldValue), oldValue);
91
- }
92
- else if (p === "onPropChange") {
93
- model[p] = (prop, value) => { fn(prop, value); paramVal(prop, value); };
94
- }
95
- else {
96
- model[p] = params[p];
97
- }
98
- }
99
- else {
100
- model[p] = params[p];
101
- }
102
- }
103
- }
104
- if (params === null || params === void 0 ? void 0 : params.init) {
105
- const mainInit = fullStruct.init;
106
- fullStruct.init = () => {
107
- params.init(model);
108
- mainInit === null || mainInit === void 0 ? void 0 : mainInit(model);
109
- };
110
- }
111
- if (params === null || params === void 0 ? void 0 : params.mount) {
112
- const mainMount = fullStruct.mount;
113
- fullStruct.mount = () => {
114
- mainMount === null || mainMount === void 0 ? void 0 : mainMount(model);
115
- params.mount(model);
116
- };
117
- }
118
- if (params === null || params === void 0 ? void 0 : params.unmount) {
119
- const mainUnmount = fullStruct.unmount;
120
- fullStruct.unmount = () => {
121
- mainUnmount === null || mainUnmount === void 0 ? void 0 : mainUnmount(model);
122
- params.unmount(model);
123
- };
124
- }
125
- model.enableOnChange();
126
- model[$].View = (props) => {
127
- var _a, _b;
128
- (0, react_1.useEffect)(_remount, []);
129
- if ((_a = props === null || props === void 0 ? void 0 : props.render) !== null && _a !== void 0 ? _a : true) {
130
- return (_b = fullStruct.View) === null || _b === void 0 ? void 0 : _b.call(fullStruct, props);
131
- }
132
- else {
133
- return null;
134
- }
135
- };
136
- model[$].BaseView = fullStruct.BaseView;
137
- model[$].init = fullStruct.init;
138
- model[$].mount = fullStruct.mount;
139
- model[$].unmount = fullStruct.unmount;
140
- model[$].__mounted = false;
141
- model[$].firstTimeRendering = true;
142
- componentModelDebug(`create model=#${model.birthMark()} path=${model.htmlId()}`);
143
- return model;
144
- function _initializeBindings() {
145
- if (model[$].__autoRunDisposers) {
146
- _disposeAutoruns();
147
- }
148
- else {
149
- model[$].__autoRunDisposers = [];
150
- }
151
- for (const p in params) {
152
- if (isBinding(params[p])) {
153
- model[$].__autoRunDisposers.push((0, mobx_1.autorun)(() => {
154
- try {
155
- model[p] = params[p][0]();
156
- }
157
- catch (e) {
158
- model[p] = undefined;
159
- componentModelDebug(e.message);
160
- }
161
- }));
162
- }
163
- }
164
- for (const p of model[$].__propBindings) {
165
- if (isBinding(params[p])) {
166
- if (isReadWriteBinding(params[p])) {
167
- model[$].__autoRunDisposers.push((0, mobx_1.autorun)(() => { params[p][1](model[p]); }));
168
- }
169
- }
170
- else {
171
- model[p] = params[p];
172
- }
173
- }
174
- }
175
- function _disposeAutoruns() {
176
- if (model[$].__autoRunDisposers) {
177
- model[$].__autoRunDisposers.map((d) => d());
178
- model[$].__autoRunDisposers = [];
179
- }
180
- }
181
- function _updateInitializedModelProperties(params) {
182
- for (const p in params) {
183
- if (Reflect.has(model, p)) {
184
- if (!isBinding(params[p]) && typeof model[p] !== "function") {
185
- model[p] = params[p];
186
- }
187
- }
188
- }
189
- }
190
- function _initialize() {
191
- var _a, _b, _c, _d;
192
- if (!model[$].firstTimeRendering)
193
- return;
194
- componentModelDebug(`init model=#${model.birthMark()} path=${model.htmlId()}`);
195
- _initializeBindings();
196
- (_b = (_a = model[$]).init) === null || _b === void 0 ? void 0 : _b.call(_a, model);
197
- componentModelDebug(`mount model=#${model.birthMark()} path=${model.htmlId()}`);
198
- model[$].__mounted = true;
199
- (_d = (_c = model[$]).mount) === null || _d === void 0 ? void 0 : _d.call(_c, model);
200
- model[$].firstTimeRendering = false;
201
- }
202
- function _unmount() {
203
- var _a, _b;
204
- if (!model[$].__mounted)
205
- return;
206
- componentModelDebug(`unmount model=#${model.birthMark()} path=${model.htmlId()}`);
207
- model[$].__mounted = false;
208
- _disposeAutoruns();
209
- (_b = (_a = model[$]).unmount) === null || _b === void 0 ? void 0 : _b.call(_a, model);
210
- }
211
- function _remount() {
212
- var _a, _b;
213
- if (!model[$].firstTimeRendering && !model[$].__mounted) {
214
- _updateInitializedModelProperties(params);
215
- componentModelDebug(`remount model=#${model.birthMark()} path=${model.htmlId()}`);
216
- _initializeBindings();
217
- (_b = (_a = model[$]).mount) === null || _b === void 0 ? void 0 : _b.call(_a, model);
218
- model[$].__mounted = true;
219
- return _unmount;
220
- }
221
- }
222
- }
223
- function useComponent(struct, params) {
224
- if (params) {
225
- let calledFormJSX = !Object.isExtensible(params);
226
- if (calledFormJSX) {
227
- params = Object.assign({}, params);
228
- }
229
- for (const p in struct.props) {
230
- if (Reflect.has(params, p)) {
231
- if (typeof params[p] === "function") {
232
- const fn = params[p];
233
- params[p] = bind(fn, undefined);
234
- }
235
- }
236
- }
237
- }
238
- const model = useComponentModel(() => newComponent(struct, params), params);
239
- return model;
240
- }
241
- function useComponentModel(modelConstructor, params) {
242
- const ownerScopeContext = useComponentModelTrap();
243
- const paramOwner = params === null || params === void 0 ? void 0 : params.owner;
244
- const paramOwnerChildModel = paramOwner && params.id && paramOwner[params.id];
245
- let isNewModel = false;
246
- const [modelR, setModelR] = (0, react_1.useState)(() => {
247
- if (paramOwnerChildModel)
248
- return paramOwnerChildModel;
249
- if (!paramOwner && (ownerScopeContext === null || ownerScopeContext === void 0 ? void 0 : ownerScopeContext.cached)) {
250
- const ch = ownerScopeContext === null || ownerScopeContext === void 0 ? void 0 : ownerScopeContext.nextCachedModel();
251
- if (ch)
252
- return ch;
253
- }
254
- const m = _createModel(paramOwner);
255
- if (!paramOwner && ownerScopeContext) {
256
- ownerScopeContext.cacheModel(m);
257
- }
258
- isNewModel = true;
259
- return m;
260
- });
261
- let model = modelR;
262
- if (!isNewModel && paramOwner && !paramOwnerChildModel) {
263
- const newModel = _createModel(paramOwner);
264
- isNewModel = true;
265
- setModelR(newModel);
266
- componentModelDebug(`reset owned newModel=#${newModel.birthMark()} oldModel=#${model.birthMark()} path=${newModel.fullId()}`);
267
- model = newModel;
268
- }
269
- else if (!model[$].firstTimeRendering && !isNewModel && ownerScopeContext && !ownerScopeContext.cached) {
270
- const newModel = _createModel(undefined);
271
- isNewModel = true;
272
- setModelR(newModel);
273
- ownerScopeContext.cacheModel(newModel);
274
- componentModelDebug(`reset newModel=#${newModel.birthMark()} oldModel=#${model.birthMark()} path=${newModel.fullId()}`);
275
- model = newModel;
276
- }
277
- else if (isNewModel) {
278
- componentModelDebug(`set${paramOwner ? " owned" : ""} model=#${model.birthMark()} path=${model.htmlId()}`);
279
- }
280
- model[$].bus = (0, messageBus_1.useMessaging)(model[$].__messages);
281
- (0, react_1.useEffect)(() => {
282
- if (model[$].firstTimeRendering) {
283
- model[$].__initialize();
284
- return () => model[$].__unmount();
285
- }
286
- }, [modelR]);
287
- if (!model[$].firstTimeRendering) {
288
- componentModelDebug(`update props model=#${model.birthMark()} path=${model.htmlId()}`);
289
- model[$].__updateInitializedModelProperties(params);
290
- }
291
- return model;
292
- function _createModel(owner) {
293
- let newModel;
294
- if ((0, lodash_1.isFunction)(modelConstructor)) {
295
- newModel = modelConstructor();
296
- }
297
- else {
298
- newModel = modelConstructor;
299
- }
300
- if (owner) {
301
- if (isComponentModel(owner)) {
302
- newModel[$].__owner = owner;
303
- }
304
- if (params.id) {
305
- owner[params.id] = newModel;
306
- }
307
- }
308
- else {
309
- if (!newModel.fullId()) {
310
- componentModelDebug("Warning: Specify a default id in the struct of the component model hook");
311
- }
312
- }
313
- return newModel;
314
- }
315
- }
316
- function mergeStruct(struct, extStruct, model) {
317
- var _a;
318
- if (!extStruct) {
319
- extStruct = {};
320
- }
321
- const mergedStruct = {
322
- props: Object.assign(Object.assign({}, struct.props), extStruct.props),
323
- children: Object.assign(Object.assign({}, struct.children), extStruct.children),
324
- methods: Object.assign(Object.assign({}, struct.methods), extStruct.methods),
325
- events: Object.assign(Object.assign({}, struct.events), extStruct.events),
326
- messages: Object.assign(Object.assign({}, struct.messages), extStruct.messages),
327
- View: (extStruct.View && extStruct.View !== blankView) ? extStruct.View : struct.View ? struct.View : blankView,
328
- BaseView: (_a = struct.View) !== null && _a !== void 0 ? _a : blankView
329
- };
330
- if (extStruct === null || extStruct === void 0 ? void 0 : extStruct.init) {
331
- mergedStruct.init = () => {
332
- var _a;
333
- (_a = struct.init) === null || _a === void 0 ? void 0 : _a.call(struct, model());
334
- extStruct.init(model());
335
- };
336
- }
337
- else {
338
- mergedStruct.init = struct.init;
339
- }
340
- if (extStruct === null || extStruct === void 0 ? void 0 : extStruct.mount) {
341
- mergedStruct.mount = () => {
342
- var _a;
343
- extStruct.mount(model());
344
- (_a = struct.mount) === null || _a === void 0 ? void 0 : _a.call(struct, model());
345
- };
346
- }
347
- else {
348
- mergedStruct.mount = struct.mount;
349
- }
350
- if (extStruct === null || extStruct === void 0 ? void 0 : extStruct.unmount) {
351
- mergedStruct.unmount = () => {
352
- var _a;
353
- extStruct.unmount(model());
354
- (_a = struct.unmount) === null || _a === void 0 ? void 0 : _a.call(struct, model());
355
- };
356
- }
357
- else {
358
- mergedStruct.unmount = struct.unmount;
359
- }
360
- for (const p in mergedStruct.props) {
361
- if (typeof mergedStruct.props[p] === "function") {
362
- const fn = mergedStruct.props[p];
363
- mergedStruct.props[p] = bind(fn, undefined);
364
- }
365
- }
366
- const invalidProps = (0, lodash_1.intersection)(Object.keys(mergedStruct.props), Object.keys(mergedStruct.children));
367
- invalidProps.concat((0, lodash_1.intersection)(Object.keys(mergedStruct.props), Object.keys(mergedStruct.methods)));
368
- invalidProps.concat((0, lodash_1.intersection)(Object.keys(mergedStruct.props), Object.keys(mergedStruct.events)));
369
- invalidProps.concat((0, lodash_1.intersection)(Object.keys(mergedStruct.children), Object.keys(mergedStruct.methods)));
370
- invalidProps.concat((0, lodash_1.intersection)(Object.keys(mergedStruct.children), Object.keys(mergedStruct.events)));
371
- invalidProps.concat((0, lodash_1.intersection)(Object.keys(mergedStruct.methods), Object.keys(mergedStruct.events)));
372
- if (invalidProps.length > 0) {
373
- throw new Error(`The component structure contains not unique members: ${invalidProps}`);
374
- }
375
- return mergedStruct;
376
- }
377
- function createProxy(struct) {
378
- const mobxState = {};
379
- let disableOnChangeCount = 0;
380
- const changeDisabled = () => disableOnChangeCount > 0;
381
- const settersInProgress = [];
382
- const propHandler = {
383
- get: (target, prop) => {
384
- if ((0, lodash_1.isSymbol)(prop) && prop === $ || prop === $name) {
385
- return target[$name];
386
- }
387
- if (Reflect.has(target[$name], prop)) {
388
- return prepareReturnValue(target[$name], prop);
389
- }
390
- if (Reflect.has(mobxState, prop)) {
391
- if (_isView(target[prop])) {
392
- throw Error("Observable View is not allowed");
393
- }
394
- let newVal = mobxState[prop].value;
395
- let oldVal = mobxState[prop].value;
396
- if (isBinding(target[prop])) {
397
- try {
398
- newVal = target[prop][0]();
399
- }
400
- catch (e) {
401
- newVal = undefined;
402
- componentModelDebug(e.message);
403
- }
404
- }
405
- else {
406
- const onGetProp = proxy[`onGet${capitalizeFirstLetter(prop)}`];
407
- if (onGetProp) {
408
- newVal = onGetProp();
409
- }
410
- }
411
- if (!_isEqual(newVal, oldVal)) {
412
- _assignObservableProperty(prop, newVal);
413
- if (!changeDisabled()) {
414
- if (!settersInProgress.find(x => x === prop)) {
415
- proxy.onPropChange && proxy.onPropChange(prop);
416
- const onChangeProp = proxy[`onChange${capitalizeFirstLetter(prop)}`];
417
- onChangeProp && onChangeProp(newVal);
418
- }
419
- }
420
- }
421
- return mobxState[prop].value;
422
- }
423
- return prepareReturnValue(target, prop);
424
- },
425
- set: (target, prop, value) => {
426
- if ((0, lodash_1.isSymbol)(prop) && prop === $ || prop === $name) {
427
- return false;
428
- }
429
- if (Reflect.has(target[$name], prop)) {
430
- return false;
431
- }
432
- if (isBinding(value)) {
433
- target[prop] = value;
434
- mobxState[prop] = { value: undefined };
435
- return true;
436
- }
437
- let oldValue = target[prop];
438
- if (Reflect.has(mobxState, prop)) {
439
- oldValue = mobxState[prop].value;
440
- }
441
- if (_isEqual(value, oldValue)) {
442
- return true;
443
- }
444
- settersInProgress.push(prop);
445
- try {
446
- if (!changeDisabled()) {
447
- if (proxy.onPropChanging) {
448
- const res = proxy.onPropChanging(prop, value, oldValue);
449
- if (_isEqual(res, oldValue)) {
450
- return true;
451
- }
452
- value = res;
453
- }
454
- const onChangingProp = proxy[`onChanging${capitalizeFirstLetter(prop)}`];
455
- if (onChangingProp) {
456
- const res = onChangingProp(value, oldValue);
457
- if (_isEqual(res, oldValue)) {
458
- return true;
459
- }
460
- value = res;
461
- }
462
- }
463
- if (Reflect.has(mobxState, prop)) {
464
- if (_isView(target[prop])) {
465
- throw Error("Observable View is not allowed");
466
- }
467
- if (isReadWriteBinding(target[prop])) {
468
- (0, mobx_1.runInAction)(() => target[prop][1](value));
469
- }
470
- _assignObservableProperty(prop, value);
471
- }
472
- else {
473
- target[prop] = value;
474
- }
475
- if (!changeDisabled()) {
476
- proxy.onPropChange && proxy.onPropChange(prop);
477
- const onChangeProp = proxy[`onChange${capitalizeFirstLetter(prop)}`];
478
- onChangeProp && onChangeProp(value);
479
- }
480
- }
481
- finally {
482
- settersInProgress.pop();
483
- }
484
- return true;
485
- }
486
- };
487
- disableOnChangeCount++;
488
- const target = _createProxyTarget(struct);
489
- const proxy = new Proxy(target, propHandler);
490
- const privateMembers = target[$name];
491
- privateMembers.__deleteMember = (name) => {
492
- let deleted = false;
493
- if (Reflect.has(target, name)) {
494
- delete target[name];
495
- deleted = true;
496
- }
497
- if (Reflect.has(mobxState, name)) {
498
- delete mobxState[name];
499
- deleted = true;
500
- }
501
- return deleted;
502
- };
503
- privateMembers.disableOnChange = () => disableOnChangeCount++;
504
- privateMembers.enableOnChange = () => {
505
- if (disableOnChangeCount === 0) {
506
- throw new Error("enableOnChange() requires disableOnChange() pair");
507
- }
508
- --disableOnChangeCount;
509
- };
510
- privateMembers.changeNotifyDisabled = changeDisabled;
511
- const birthMark = Math.round(Math.random() * 1000000).toString();
512
- privateMembers.birthMark = () => birthMark;
513
- privateMembers.fullId = () => {
514
- var _a;
515
- let s = "";
516
- if (privateMembers.__owner) {
517
- s = privateMembers.__owner.fullId() + ".";
518
- }
519
- return s + ((_a = proxy.id) !== null && _a !== void 0 ? _a : "<empty>");
520
- };
521
- privateMembers.htmlId = () => {
522
- const id = privateMembers.fullId();
523
- return (id === "<empty>") ? undefined : window.componentModelHashHtmlId ? _getHashCode(id) : id;
524
- };
525
- privateMembers.calledFromJSX = () => !!privateMembers.__calledFromJSX;
526
- for (const p in struct.children) {
527
- proxy[p][$].__owner = proxy;
528
- }
529
- disableOnChangeCount--;
530
- return proxy;
531
- function _createProxyTarget(struct) {
532
- let targetExt = Object.assign(Object.assign(Object.assign(Object.assign({}, struct.props), struct.children), struct.events), struct.methods);
533
- for (const p in struct.children) {
534
- targetExt[p].disableOnChange();
535
- targetExt[p].id = p;
536
- targetExt[p].enableOnChange();
537
- }
538
- for (const p in struct.props) {
539
- let propVal = struct.props[p];
540
- if (isBinding(propVal)) {
541
- mobxState[p] = { value: undefined };
542
- }
543
- else {
544
- mobxState[p] = undefined;
545
- _assignObservableProperty(p, propVal);
546
- targetExt[p] = undefined;
547
- }
548
- }
549
- targetExt[$name] = {
550
- __owner: undefined,
551
- __deleteMember: undefined,
552
- __calledFromJSX: undefined,
553
- __messages: undefined,
554
- __propBindings: undefined,
555
- __mounted: undefined,
556
- __initialize: undefined,
557
- __unmount: undefined,
558
- __updateInitializedModelProperties: undefined,
559
- __autoRunDisposers: undefined,
560
- bus: undefined,
561
- View: undefined,
562
- BaseView: undefined,
563
- firstTimeRendering: undefined,
564
- disableOnChange: undefined,
565
- enableOnChange: undefined,
566
- changeNotifyDisabled: undefined,
567
- fullId: undefined,
568
- htmlId: undefined,
569
- birthMark: undefined,
570
- calledFromJSX: undefined,
571
- init: undefined,
572
- mount: undefined,
573
- unmount: undefined
574
- };
575
- return targetExt;
576
- }
577
- function _isView(value) {
578
- return value &&
579
- typeof value === "object" && Object.keys(value).length === 1 && typeof value.__viewObserver === "function";
580
- }
581
- function prepareReturnValue(obj, prop) {
582
- if (_isView(obj[prop])) {
583
- return obj[prop].__viewObserver;
584
- }
585
- if (typeof obj[prop] === "function" && prop.endsWith("View")) {
586
- const Obs = (0, mobx_react_1.observer)(obj[prop]);
587
- obj[prop] = {
588
- __viewObserver: (props => React.createElement(Obs, Object.assign({}, props, { key: proxy.birthMark() })))
589
- };
590
- return obj[prop].__viewObserver;
591
- }
592
- return obj[prop];
593
- }
594
- function _getHashCode(s) {
595
- const charCodes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
596
- let result = "";
597
- const hash = [...s].reduce((hash, c) => (Math.imul(31, hash) + c.charCodeAt(0)) | 0, 0);
598
- for (let i = 0; i < 32; i += 6) {
599
- result += charCodes.charAt((hash >>> i) & 0x3F);
600
- }
601
- return result;
602
- }
603
- function _isReactElement(value) {
604
- return (0, lodash_1.isSymbol)(value === null || value === void 0 ? void 0 : value.$$typeof) && value.$$typeof.description === "react.element";
605
- }
606
- function _isEqual(value, other) {
607
- if (_isReactElement(value) || _isReactElement(other)) {
608
- return value === other;
609
- }
610
- else {
611
- return (0, lodash_1.isEqual)(value, other);
612
- }
613
- }
614
- function _assignObservableProperty(prop, value) {
615
- (0, mobx_1.runInAction)(() => {
616
- const initialization = !mobxState[prop];
617
- if (isComponentModel(value) && !(0, mobx_1.isObservable)(value)) {
618
- if (initialization) {
619
- mobxState[prop] = { value: undefined };
620
- mobx_1.observable.ref(mobxState[prop], "value");
621
- }
622
- mobxState[prop].value = value;
623
- }
624
- else {
625
- if ((0, lodash_1.isArray)(value)) {
626
- if (initialization) {
627
- mobxState[prop] = (0, mobx_1.observable)({ value: undefined });
628
- }
629
- mobxState[prop].value = mobx_1.observable.array(value, { deep: false });
630
- }
631
- else {
632
- const isView = prop.endsWith("View") || prop === "children";
633
- if (isView || _isReactElement(value)) {
634
- if (!isView && !initialization && !_isReactElement(mobxState[prop].value) && (0, mobx_1.isObservable)(mobxState[prop])) {
635
- componentModelDebug("Warning! A react element replaces a value. There may be issues with rendering.\r\n" +
636
- `Option #1(recommended): Rename property "${prop}" to "${prop}View".\r\n` +
637
- `Option #2: Initialize property "${prop}" with some JSX`);
638
- }
639
- if (initialization) {
640
- mobxState[prop] = { value: undefined };
641
- mobx_1.observable.ref(mobxState[prop], "value");
642
- }
643
- mobxState[prop].value = value;
644
- }
645
- else {
646
- if (initialization) {
647
- mobxState[prop] = (0, mobx_1.observable)({ value: undefined });
648
- }
649
- mobxState[prop].value = value;
650
- }
651
- }
652
- }
653
- });
654
- }
655
- }
656
- function isComponentModel(obj) {
657
- return obj && typeof obj === "object" && Reflect.has(obj, $name);
658
- }
659
- function isBinding(value) {
660
- return value && typeof value === "object" && Object.keys(value).length === 2
661
- && typeof value[0] === "function" && (typeof value[1] === "function" || typeof value[1] === "undefined");
662
- }
663
- function isReadBinding(value) {
664
- return isBinding(value) && typeof value[1] === "undefined";
665
- }
666
- function isReadWriteBinding(value) {
667
- return isBinding(value) && typeof value[1] === "function";
668
- }
669
- function capitalizeFirstLetter(name) {
670
- return name.replace(/^./, name[0].toUpperCase());
671
- }
672
- function bind(get, set) {
673
- const bond = [get, set];
674
- return bond;
675
- }
676
- function bindProp(obj, prop) {
677
- const bond = bind(() => {
678
- let o;
679
- try {
680
- o = obj();
681
- }
682
- catch (e) {
683
- componentModelDebug(e.message);
684
- }
685
- if (!o) {
686
- componentModelDebug(`Attempt to get bound property "${prop.toString()}" of undefined`);
687
- }
688
- return o ? o[prop] : undefined;
689
- }, (value) => {
690
- let o;
691
- try {
692
- o = obj();
693
- }
694
- catch (e) {
695
- componentModelDebug(e);
696
- }
697
- if (o) {
698
- (0, mobx_1.runInAction)(() => { o[prop] = value; });
699
- }
700
- else {
701
- componentModelDebug(`Attempt to set bound property "${prop.toString()}" of undefined`);
702
- }
703
- });
704
- return bond;
705
- }
706
- function newComponentModelTrapRegistry(cache) {
707
- const trap = {
708
- _hookCallsCount: -1,
709
- _newModels: [],
710
- models: cache !== null && cache !== void 0 ? cache : [],
711
- cached: !!cache,
712
- cacheModel: (model) => trap._newModels.push(model),
713
- nextCachedModel: () => {
714
- if (trap.cached) {
715
- if (trap._hookCallsCount === trap.models.length - 1) {
716
- componentModelDebug("ComponentModelTrap cache pointer reached the top");
717
- return;
718
- }
719
- trap._hookCallsCount++;
720
- return trap.models[trap._hookCallsCount];
721
- }
722
- componentModelDebug("Attempt to read uncached ComponentModelTrap");
723
- },
724
- updateCache: (model) => {
725
- if (!trap._newModels.length)
726
- return;
727
- let cnt = 0;
728
- trap._newModels.forEach((x) => {
729
- if (x.id && (!x[$].__owner || x[$].__owner === model)) {
730
- if (!x[$].__owner) {
731
- x[$].__owner = model;
732
- }
733
- if (model[x.id]) {
734
- componentModelDebug(`The child "${x.id}" already exists in model=#${model.birthMark()} path=${model.htmlId()}. This model won't be available by name.`);
735
- }
736
- else {
737
- model[x.id] = x;
738
- cnt++;
739
- }
740
- }
741
- });
742
- cnt && componentModelDebug(`link ${cnt} children by name to model=#${model.birthMark()} path=${model.htmlId()}`);
743
- if (trap._newModels.length) {
744
- trap.models = [...trap.models, ...trap._newModels];
745
- componentModelDebug(`cache new ${trap._newModels.length} children models in model=#${model.birthMark()} path=${model.htmlId()}`);
746
- trap._newModels = [];
747
- }
748
- }
749
- };
750
- return trap;
751
- }
752
- const ComponentModelTrap = (0, react_1.createContext)(undefined);
753
- exports.ComponentModelTrap = ComponentModelTrap;
754
- function useComponentModelTrap() {
755
- return (0, react_1.useContext)(ComponentModelTrap);
756
- }
757
- function getFC(modelHook) {
758
- return (params) => {
759
- const hookName = modelHook.name;
760
- if (!(params === null || params === void 0 ? void 0 : params.id)) {
761
- params = Object.assign(Object.assign({}, params), { id: hookName.startsWith("use") ? hookName.substring(3) : hookName });
762
- }
763
- const model = modelHook(params);
764
- model[$].__calledFromJSX = true;
765
- return model.View();
766
- };
767
- }
768
- window.componentModelDebug = false;
769
- window.componentModelHashHtmlId = false;
770
- function componentModelDebug(text) {
771
- window.componentModelDebug && console.debug(text);
772
- }