@expressive/mvc 0.73.0 → 0.78.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.
- package/README.md +137 -0
- package/dist/component.d.ts +87 -0
- package/dist/component.js +111 -0
- package/dist/component.js.map +1 -0
- package/dist/context.d.ts +56 -0
- package/dist/context.js +201 -0
- package/dist/context.js.map +1 -0
- package/dist/field/def.d.ts +17 -0
- package/dist/field/def.js +29 -0
- package/dist/field/def.js.map +1 -0
- package/dist/field/get.d.ts +57 -0
- package/dist/field/get.js +90 -0
- package/dist/field/get.js.map +1 -0
- package/dist/field/hot.d.ts +16 -0
- package/dist/field/hot.js +98 -0
- package/dist/field/hot.js.map +1 -0
- package/dist/field/ref.d.ts +78 -0
- package/dist/field/ref.js +85 -0
- package/dist/field/ref.js.map +1 -0
- package/dist/field/set.d.ts +59 -0
- package/dist/field/set.js +84 -0
- package/dist/field/set.js.map +1 -0
- package/dist/index.d.ts +10 -502
- package/dist/index.js +11 -872
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +90 -0
- package/dist/jsx-runtime.js +57 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/observable.d.ts +58 -0
- package/dist/observable.js +184 -0
- package/dist/observable.js.map +1 -0
- package/dist/state.d.ts +311 -0
- package/dist/state.js +424 -0
- package/dist/state.js.map +1 -0
- package/package.json +35 -18
- package/dist/esm/context.d.ts +0 -62
- package/dist/esm/context.js +0 -158
- package/dist/esm/index.d.ts +0 -8
- package/dist/esm/index.js +0 -9
- package/dist/esm/instruction/get.d.ts +0 -36
- package/dist/esm/instruction/get.js +0 -77
- package/dist/esm/instruction/ref.d.ts +0 -71
- package/dist/esm/instruction/ref.js +0 -68
- package/dist/esm/instruction/set.d.ts +0 -69
- package/dist/esm/instruction/set.js +0 -118
- package/dist/esm/instruction/use.d.ts +0 -23
- package/dist/esm/instruction/use.js +0 -56
- package/dist/esm/observable.d.ts +0 -42
- package/dist/esm/observable.js +0 -121
- package/dist/esm/state.d.ts +0 -234
- package/dist/esm/state.js +0 -293
- package/dist/index.js.map +0 -1
package/dist/state.js
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { event, listener, observer, pending, touch, watch } from "./observable.js";
|
|
2
|
+
import { Context } from "./context.js";
|
|
3
|
+
|
|
4
|
+
//#region src/state.ts
|
|
5
|
+
const define = Object.defineProperty;
|
|
6
|
+
/** Register for all active states via string identifiers (usually unique). */
|
|
7
|
+
const ID = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
/** Internal state assigned to states. */
|
|
9
|
+
const STORE = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
/** External lifecycle listeners for any given State class. */
|
|
11
|
+
const SETUP = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
/** Parent-child relationships. */
|
|
13
|
+
const PARENT = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
/** Reference bound instance methods, or their getter, to real one. */
|
|
15
|
+
const UNBIND = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
/** List of methods defined by a given type. */
|
|
17
|
+
const METHODS = /* @__PURE__ */ new WeakMap();
|
|
18
|
+
/** List of reactive getters defined by a given type. */
|
|
19
|
+
const GETTERS = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
/** Stale flags for compute closures awaiting refresh on next access. */
|
|
21
|
+
const STALE = /* @__PURE__ */ new WeakSet();
|
|
22
|
+
var State = class State {
|
|
23
|
+
constructor(...args) {
|
|
24
|
+
define(this, "is", { value: this });
|
|
25
|
+
init(this, args, this.new);
|
|
26
|
+
}
|
|
27
|
+
get(arg1, arg2, arg3) {
|
|
28
|
+
const self = this.is;
|
|
29
|
+
if (arg1 === void 0) return values(self);
|
|
30
|
+
if (State.is(arg1)) return Context.get(self).get(arg1, arg2, arg3, self);
|
|
31
|
+
if (typeof arg1 == "function") return watch(self, unbind(arg1));
|
|
32
|
+
if (typeof arg2 == "function") return callback(self, arg2, arg1);
|
|
33
|
+
if (arg1 === null) return observer(self) === null;
|
|
34
|
+
return access(self, arg1, arg2);
|
|
35
|
+
}
|
|
36
|
+
set(arg1, arg2) {
|
|
37
|
+
const self = this.is;
|
|
38
|
+
if (typeof arg1 == "function") return listener(self, (key) => {
|
|
39
|
+
if (typeof key == "string" || typeof key == "number" || typeof key == "symbol") return arg1.call(self, key, self);
|
|
40
|
+
});
|
|
41
|
+
if (typeof arg2 == "function") return callback(self, arg2, arg1);
|
|
42
|
+
if (arg1 && typeof arg1 == "object") assign(self, arg1, arg2 === true);
|
|
43
|
+
else if (arg2) apply(self, arg1, arg2);
|
|
44
|
+
else event(self, arg1);
|
|
45
|
+
return pending(self);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Iterate over managed properties in this instance of State.
|
|
49
|
+
* Yeilds the key and current value for each property.
|
|
50
|
+
*/
|
|
51
|
+
[Symbol.iterator]() {
|
|
52
|
+
return Object.entries(STORE.get(this.is))[Symbol.iterator]();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create and activate a new instance of this state.
|
|
56
|
+
*
|
|
57
|
+
* **Important** - Unlike `new this(...)` - this method also activates state.
|
|
58
|
+
*
|
|
59
|
+
* @param args - arguments sent to constructor
|
|
60
|
+
*/
|
|
61
|
+
static new(...args) {
|
|
62
|
+
const instance = new this(...args);
|
|
63
|
+
event(instance);
|
|
64
|
+
return instance;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Static equivalent of `x instanceof this`.
|
|
68
|
+
* Determines if provided class is a subtype of this one.
|
|
69
|
+
* If so, language server will make available all static
|
|
70
|
+
* methods and properties of this class.
|
|
71
|
+
*/
|
|
72
|
+
static is(maybe) {
|
|
73
|
+
return maybe === this || typeof maybe == "function" && maybe.prototype instanceof this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Register a lifecycle handler for this State and its subclasses.
|
|
77
|
+
*
|
|
78
|
+
* A bare function is per-instance setup run in the `prepare` phase (sugar for
|
|
79
|
+
* `{ before }`); if it returns a function, that runs when the instance is
|
|
80
|
+
* destroyed. Pass a {@link State.On} object to hook by cadence - `type`
|
|
81
|
+
* (per-class, at bootstrap), `before` (per-instance, before `new()`), and
|
|
82
|
+
* `after` (per-instance, at the `new()` slot).
|
|
83
|
+
*
|
|
84
|
+
* @returns Function to remove the handler.
|
|
85
|
+
*/
|
|
86
|
+
static on(handler) {
|
|
87
|
+
let setup = SETUP.get(this);
|
|
88
|
+
if (!setup) SETUP.set(this, setup = /* @__PURE__ */ new Set());
|
|
89
|
+
setup.add(handler);
|
|
90
|
+
return () => setup.delete(handler);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
define(State.prototype, "toString", { value() {
|
|
94
|
+
return ID.get(this.is);
|
|
95
|
+
} });
|
|
96
|
+
define(State, "toString", { value() {
|
|
97
|
+
return this.name;
|
|
98
|
+
} });
|
|
99
|
+
/** Register a user OnEvent callback, preserving `this` and `source`. */
|
|
100
|
+
function callback(self, cb, select) {
|
|
101
|
+
return listener(self, (key) => cb.call(self, key, self), select);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Apply state arguemnts, run callbacks and observe properties.
|
|
105
|
+
* Accumulate and handle cleanup events.
|
|
106
|
+
**/
|
|
107
|
+
function init(state, ...args) {
|
|
108
|
+
const T = state.constructor;
|
|
109
|
+
if (T === State) throw new Error("Cannot create base State.");
|
|
110
|
+
const { before, after } = bootstrap(T);
|
|
111
|
+
ID.set(state, `${T}-${uid()}`);
|
|
112
|
+
STORE.set(state, {});
|
|
113
|
+
function observe() {
|
|
114
|
+
for (const key in state) {
|
|
115
|
+
const desc = Object.getOwnPropertyDescriptor(state, key);
|
|
116
|
+
if ("value" in desc) apply(state, key, desc, true);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function register() {
|
|
120
|
+
if (Context.get(state) === Context.root) return Context.root.add(state);
|
|
121
|
+
}
|
|
122
|
+
listener(state, () => {
|
|
123
|
+
parent(state, null);
|
|
124
|
+
const queue = [
|
|
125
|
+
...before,
|
|
126
|
+
observe,
|
|
127
|
+
...args,
|
|
128
|
+
...after,
|
|
129
|
+
register
|
|
130
|
+
];
|
|
131
|
+
for (let i = 0; i < queue.length; i++) {
|
|
132
|
+
const arg = queue[i];
|
|
133
|
+
const out = typeof arg == "function" ? arg.call(state, state) : arg;
|
|
134
|
+
if (out instanceof Promise) out.catch((err) => {
|
|
135
|
+
console.error(`Async error in constructor for ${state}:`);
|
|
136
|
+
console.error(err);
|
|
137
|
+
});
|
|
138
|
+
else if (Array.isArray(out)) queue.splice(i + 1, 0, ...out);
|
|
139
|
+
else if (typeof out == "function") listener(state, out, null);
|
|
140
|
+
else if (typeof out == "object") assign(state, out, true);
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Apply instructions and inherited event listeners. Ensure class metadata is ready.
|
|
147
|
+
*
|
|
148
|
+
* @param T - State class to bootstrap.
|
|
149
|
+
* @return Array of init functions to run on instance, in order of class hierarchy.
|
|
150
|
+
*/
|
|
151
|
+
function bootstrap(T) {
|
|
152
|
+
const chain = [];
|
|
153
|
+
const before = /* @__PURE__ */ new Set();
|
|
154
|
+
const after = /* @__PURE__ */ new Set();
|
|
155
|
+
const onType = /* @__PURE__ */ new Set();
|
|
156
|
+
let keys = /* @__PURE__ */ new Map();
|
|
157
|
+
let getters = /* @__PURE__ */ new Map();
|
|
158
|
+
do {
|
|
159
|
+
chain.unshift(T);
|
|
160
|
+
T = Object.getPrototypeOf(T);
|
|
161
|
+
} while (T.name);
|
|
162
|
+
for (const type of chain) {
|
|
163
|
+
for (const handler of SETUP.get(type) || []) if (typeof handler == "function") before.add(handler);
|
|
164
|
+
else {
|
|
165
|
+
if (handler.before) before.add(handler.before);
|
|
166
|
+
if (handler.after) after.add(handler.after);
|
|
167
|
+
if (handler.type) onType.add(handler.type);
|
|
168
|
+
}
|
|
169
|
+
if (type === State) continue;
|
|
170
|
+
if (METHODS.has(type)) {
|
|
171
|
+
keys = METHODS.get(type);
|
|
172
|
+
getters = GETTERS.get(type);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
for (const setupType of onType) setupType(type);
|
|
176
|
+
METHODS.set(type, keys = new Map(keys));
|
|
177
|
+
GETTERS.set(type, getters = new Map(getters));
|
|
178
|
+
for (const [key, desc] of Object.entries(Object.getOwnPropertyDescriptors(type.prototype))) {
|
|
179
|
+
if (key == "constructor" || !desc.configurable) continue;
|
|
180
|
+
if (typeof desc.get == "function") {
|
|
181
|
+
if (desc.configurable && typeof desc.set != "function") getters.set(key, desc.get);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const { value } = desc;
|
|
185
|
+
if (typeof value !== "function") continue;
|
|
186
|
+
function bind(original) {
|
|
187
|
+
const { is } = this;
|
|
188
|
+
if (is.hasOwnProperty(key) && !original) return value;
|
|
189
|
+
const fn = original || value;
|
|
190
|
+
const bound = fn.bind(is);
|
|
191
|
+
UNBIND.set(bound, fn);
|
|
192
|
+
define(is, key, {
|
|
193
|
+
value: bound,
|
|
194
|
+
writable: true,
|
|
195
|
+
configurable: true
|
|
196
|
+
});
|
|
197
|
+
return bound;
|
|
198
|
+
}
|
|
199
|
+
UNBIND.set(bind, value);
|
|
200
|
+
keys.set(key, bind);
|
|
201
|
+
define(type.prototype, key, {
|
|
202
|
+
get: bind,
|
|
203
|
+
set: bind
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (getters.size) before.add((self) => getters.forEach(compute, self));
|
|
208
|
+
return {
|
|
209
|
+
before,
|
|
210
|
+
after
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Install a reactive computed property on a state instance, derived from a prototype getter.
|
|
215
|
+
*
|
|
216
|
+
* The getter is invoked with the tracking proxy as `this`; reads of managed properties
|
|
217
|
+
* through it create subscriptions. Result is cached and emits a keyed event when stale.
|
|
218
|
+
*/
|
|
219
|
+
function compute(getter, key) {
|
|
220
|
+
const store = STORE.get(this);
|
|
221
|
+
let reset;
|
|
222
|
+
let isAsync;
|
|
223
|
+
let proxy;
|
|
224
|
+
const connect = () => {
|
|
225
|
+
reset = watch(this, (current) => {
|
|
226
|
+
proxy = current;
|
|
227
|
+
if (!(key in store) || STALE.delete(run)) run(!reset);
|
|
228
|
+
return (didUpdate) => {
|
|
229
|
+
if (didUpdate) {
|
|
230
|
+
STALE.add(run);
|
|
231
|
+
event(this, key, true);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}, false);
|
|
235
|
+
};
|
|
236
|
+
const run = (initial) => {
|
|
237
|
+
let next;
|
|
238
|
+
try {
|
|
239
|
+
next = getter.call(proxy);
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.warn(`An exception was thrown while ${initial ? "initializing" : "refreshing"} ${this}.${key}.`);
|
|
242
|
+
if (initial) throw err;
|
|
243
|
+
console.error(err);
|
|
244
|
+
}
|
|
245
|
+
update(this, key, next, !isAsync);
|
|
246
|
+
};
|
|
247
|
+
apply(this, key, {
|
|
248
|
+
enumerable: true,
|
|
249
|
+
set: false,
|
|
250
|
+
get: () => {
|
|
251
|
+
if (!proxy) {
|
|
252
|
+
if (observer(this) === null) return store[key];
|
|
253
|
+
connect();
|
|
254
|
+
isAsync = true;
|
|
255
|
+
}
|
|
256
|
+
if (STALE.delete(run)) run();
|
|
257
|
+
return access(this, key, !proxy);
|
|
258
|
+
}
|
|
259
|
+
}, true);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Define or update a managed property using a descriptor config.
|
|
263
|
+
* If the property already is managed, config will only accept value.
|
|
264
|
+
* If the property does not exist, it will be created and made reactive.
|
|
265
|
+
*
|
|
266
|
+
* @param state - State to apply property to.
|
|
267
|
+
* @param key - Property to define or update.
|
|
268
|
+
* @param config - Descriptor config with value, get, set, enumerable, and/or destroy.
|
|
269
|
+
* @param silent - If an update does occur, listeners will not be refreshed automatically.
|
|
270
|
+
*/
|
|
271
|
+
function apply(state, key, config, silent) {
|
|
272
|
+
const desc = Object.getOwnPropertyDescriptor(state, key);
|
|
273
|
+
if (desc && "get" in desc) {
|
|
274
|
+
if (Object.keys(config).some((k) => k != "value")) throw new Error(`Property ${key} on ${state} is already defined.`);
|
|
275
|
+
if ("value" in config) update(state, key, config.value, silent);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const adopt = config.value instanceof State && child(state);
|
|
279
|
+
function set(value, silent) {
|
|
280
|
+
if (!update(state, key, value, silent)) return;
|
|
281
|
+
if (adopt) adopt(value);
|
|
282
|
+
}
|
|
283
|
+
define(state, key, {
|
|
284
|
+
configurable: true,
|
|
285
|
+
enumerable: config.enumerable !== false,
|
|
286
|
+
get() {
|
|
287
|
+
const value = typeof config.get == "function" ? config.get(this) : access(state, key, config.get);
|
|
288
|
+
return touch(this, key, value);
|
|
289
|
+
},
|
|
290
|
+
set(next, silent) {
|
|
291
|
+
if (config.set === false) throw new Error(`${state}.${String(key)} is read-only.`);
|
|
292
|
+
if (typeof config.set == "function") try {
|
|
293
|
+
const output = config.set(next, STORE.get(state)[key]);
|
|
294
|
+
if (output !== void 0) next = output;
|
|
295
|
+
} catch (err) {
|
|
296
|
+
if (err === false) return;
|
|
297
|
+
if (err === true) {
|
|
298
|
+
set(next, true);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
throw err;
|
|
302
|
+
}
|
|
303
|
+
set(next, silent);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
if ("value" in config) set(config.value, silent);
|
|
307
|
+
}
|
|
308
|
+
function child(state) {
|
|
309
|
+
let cleanup;
|
|
310
|
+
const ctx = Context.get(state);
|
|
311
|
+
function reset() {
|
|
312
|
+
if (cleanup) cleanup();
|
|
313
|
+
cleanup = void 0;
|
|
314
|
+
}
|
|
315
|
+
listener(state, reset, null);
|
|
316
|
+
return (value) => {
|
|
317
|
+
reset();
|
|
318
|
+
if (!(value instanceof State)) return;
|
|
319
|
+
const remove = ctx.add(value);
|
|
320
|
+
if (parent(value) === void 0) {
|
|
321
|
+
parent(value, state);
|
|
322
|
+
cleanup = () => {
|
|
323
|
+
cancel();
|
|
324
|
+
remove();
|
|
325
|
+
event(value, null);
|
|
326
|
+
};
|
|
327
|
+
const cancel = listener(state, cleanup, null);
|
|
328
|
+
} else cleanup = remove;
|
|
329
|
+
event(value);
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
/** Currently accumulating export. Stores real values of placeholder properties such as ref() or child states. */
|
|
333
|
+
let EXPORT;
|
|
334
|
+
function values(state) {
|
|
335
|
+
const values = {};
|
|
336
|
+
let notRecursive;
|
|
337
|
+
if (!EXPORT) {
|
|
338
|
+
notRecursive = true;
|
|
339
|
+
EXPORT = new Map([[state, values]]);
|
|
340
|
+
}
|
|
341
|
+
for (let [key, value] of state) {
|
|
342
|
+
if (EXPORT.has(value)) value = EXPORT.get(value);
|
|
343
|
+
else if (value && typeof value == "object" && "get" in value && typeof value.get === "function") EXPORT.set(value, value = value.get());
|
|
344
|
+
values[key] = value;
|
|
345
|
+
}
|
|
346
|
+
if (notRecursive) EXPORT = void 0;
|
|
347
|
+
return Object.freeze(values);
|
|
348
|
+
}
|
|
349
|
+
function access(state, property, required) {
|
|
350
|
+
const store = STORE.get(state);
|
|
351
|
+
if (property in store || required === false) {
|
|
352
|
+
const value = store[property];
|
|
353
|
+
if (value !== void 0 || !required) return value;
|
|
354
|
+
}
|
|
355
|
+
if (METHODS.get(state.constructor).has(property)) return unbind(state[property]);
|
|
356
|
+
if (observer(state) === null) return void 0;
|
|
357
|
+
const error = /* @__PURE__ */ new Error(`${state}.${property} is not yet available.`);
|
|
358
|
+
const promise = new Promise((resolve, reject) => {
|
|
359
|
+
listener(state, (key) => {
|
|
360
|
+
if (key === property) {
|
|
361
|
+
resolve(store[key]);
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
if (key === null) reject(/* @__PURE__ */ new Error(`${state} is destroyed.`));
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
throw Object.assign(promise, {
|
|
368
|
+
toString: () => String(error),
|
|
369
|
+
name: "Suspense",
|
|
370
|
+
message: error.message,
|
|
371
|
+
stack: error.stack
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function assign(state, data, silent) {
|
|
375
|
+
if (!silent) event(state);
|
|
376
|
+
const methods = METHODS.get(state.constructor);
|
|
377
|
+
for (const key in data) {
|
|
378
|
+
const bind = methods.get(key);
|
|
379
|
+
if (bind) bind.call(state, data[key]);
|
|
380
|
+
else if (key in state && key !== "is") {
|
|
381
|
+
const desc = Object.getOwnPropertyDescriptor(state, key);
|
|
382
|
+
const set = desc && desc.set;
|
|
383
|
+
if (set) set.call(state, data[key], silent);
|
|
384
|
+
else state[key] = data[key];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Update a property on a state instance and notify listeners.
|
|
390
|
+
*
|
|
391
|
+
* This is used internally to update properties, but can also be used to update properties which are not managed by state, or to update values without triggering setters.
|
|
392
|
+
*
|
|
393
|
+
* If `silent` is true, the update will not dispatch events and will return `false` instead of throwing if state is destroyed.
|
|
394
|
+
*/
|
|
395
|
+
function update(state, key, value, silent) {
|
|
396
|
+
if (observer(state) === null) {
|
|
397
|
+
if (silent) return false;
|
|
398
|
+
throw new Error(`Tried to update ${state}.${String(key)} but state is destroyed.`);
|
|
399
|
+
}
|
|
400
|
+
const store = STORE.get(state);
|
|
401
|
+
if (key in store && value === store[key]) return false;
|
|
402
|
+
store[key] = value;
|
|
403
|
+
if (!silent) event(state, key);
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
/** Random alphanumberic of length 6; always starts with a letter. */
|
|
407
|
+
function uid() {
|
|
408
|
+
return (.278 + Math.random() * .722).toString(36).substring(2, 8).toUpperCase();
|
|
409
|
+
}
|
|
410
|
+
function unbind(fn) {
|
|
411
|
+
return UNBIND.get(fn) || fn;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Get or set a parent on given state. May only be assigned once; ignores new value.
|
|
415
|
+
* Returns parent of child, or `undefined` if not assigned yet.
|
|
416
|
+
*/
|
|
417
|
+
function parent(child, value) {
|
|
418
|
+
if (arguments.length > 1 && !PARENT.has(child)) PARENT.set(child, value);
|
|
419
|
+
return PARENT.get(child);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
//#endregion
|
|
423
|
+
export { STORE, State, access, apply, event, parent, uid, unbind, update };
|
|
424
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","names":[],"sources":["../src/state.ts"],"sourcesContent":["import { Context } from './context';\nimport {\n event,\n listener,\n Observer,\n observer,\n pending,\n touch,\n watch\n} from './observable';\n\nconst define = Object.defineProperty;\n\n/** Register for all active states via string identifiers (usually unique). */\nconst ID = new WeakMap<State, string>();\n\n/** Internal state assigned to states. */\nconst STORE = new WeakMap<State, Record<string | number | symbol, unknown>>();\n\n/** External lifecycle listeners for any given State class. */\nconst SETUP = new WeakMap<State.Extends, Set<State.Init<any> | State.On<any>>>();\n\n/** Parent-child relationships. */\nconst PARENT = new WeakMap<State, State | null>();\n\n/** Reference bound instance methods, or their getter, to real one. */\nconst UNBIND = new WeakMap<any, any>();\n\n/** List of methods defined by a given type. */\nconst METHODS = new WeakMap<Function, Map<string, (value: any) => void>>();\n\n/** List of reactive getters defined by a given type. */\nconst GETTERS = new WeakMap<Function, Map<string, () => unknown>>();\n\n/** Stale flags for compute closures awaiting refresh on next access. */\nconst STALE = new WeakSet<() => void>();\n\ndeclare namespace State {\n /** Any type of State, using own class constructor as its identifier. */\n type Extends<T extends State = State> = (abstract new (...args: any[]) => T) &\n typeof State;\n\n /** A State constructor which may be instanciated. */\n type Type<T extends State = State> = (new (...args: State.Args<T>) => T) &\n Omit<typeof State, never>;\n\n /** State constructor arguments */\n type Args<T extends State = any> = (Args<T> | Init<T> | Assign<T> | void)[];\n\n /**\n * State constructor callback - runs during activation, in argument order,\n * before the `new()` lifecycle hook (so it may configure state `new()`\n * will observe). Returned function will run when state is destroyed.\n */\n type Init<T extends State = State> = (\n this: T,\n thisArg: T\n ) => Promise<void> | (() => void) | Args<T> | Assign<T> | void;\n\n /**\n * Lifecycle handlers for `State.on`, keyed by when they run. A bare `Init`\n * function passed to `on` is sugar for `{ before }`.\n */\n interface On<T extends State = State> {\n /**\n * Per-class setup, run once when the class is first bootstrapped - before\n * its members are classified and bound. Receives the class, so a handler\n * may inspect or reshape the prototype first. A handler registered on a\n * base class runs for each subclass too.\n */\n type?(type: State.Extends<T>): void;\n\n /**\n * Per-instance setup, run in the `prepare` phase before `observe` and the\n * `new()` hook. Equivalent to passing a bare function to `on`. May return a\n * cleanup, constructor args, or an assign overlay.\n */\n before?(this: T, thisArg: T): void | (() => void) | Promise<void> | Args<T> | Assign<T>;\n\n /**\n * Per-instance setup, run at the `new()` slot - after own values are\n * observed and constructor args applied. May return a cleanup function.\n */\n after?(this: T, self: T): void | (() => void);\n }\n\n /** Object overlay to override values and methods on a state. */\n type Assign<T> = Record<string, unknown> & {\n [K in Field<T>]?: T[K] extends (...args: infer A) => infer R\n ? (this: T, ...args: A) => R\n : T[K];\n };\n\n /** Subset of `keyof T` not defined by base State. **/\n type Field<T> = Exclude<keyof T, keyof State>;\n\n /** Any valid key for state, including but not limited to Field<T>. */\n type Event<T = State> = Field<T> | number | symbol | (string & {});\n\n /** Any event signal dispatched to listeners, including lifecycle meta events. */\n type Signal<T = State> = Event<T> | true | false | null;\n\n /** Export/Import compatible value for a given property in a State. */\n type Export<R> = R extends State\n ? Values<R>\n : R extends { get(): infer T }\n ? T\n : R;\n\n /**\n * Values from current state of given state.\n * Differs from `Values` as values here will drill\n * into \"real\" values held by exotics like ref.Object.\n */\n type Values<T> = { [P in Field<T>]: Export<T[P]> };\n\n /** Object comperable to data found in T. */\n type Partial<T> = { [P in Field<T>]?: Export<T[P]> };\n\n /** Value for a property applied by a state. */\n type Value<T extends State, K extends State.Event<T>> = K extends keyof T\n ? Export<T[K]>\n : unknown;\n\n type Setter<T> = (value: T, previous: T) => T | void;\n\n /** Descriptor config for a managed property. */\n type Apply<T = any> = {\n value?: T;\n get?: ((source: State) => T) | boolean;\n set?: Setter<T> | boolean;\n enumerable?: boolean;\n // destroy?: () => void;\n };\n\n /** Descriptor config for set() overload - enforces type when key is a known field. */\n type Define<T, K> = K extends Field<T> ? Apply<T[K] | Export<T[K]>> : Apply;\n\n type OnEvent<T extends State> = (\n this: T,\n key: Signal<T>,\n source: T\n ) => void | (() => void) | null;\n\n /** Exotic value, where actual value is contained within. */\n type Ref<T = any> = {\n (next: T): void;\n current: T | null;\n };\n\n /**\n * A callback function which is subscribed to parent and updates when accessed properties change.\n *\n * @param current - Current state of this state. This is a proxy which detects properties which\n * where accessed, and thus depended upon to stay current.\n *\n * @param update - Set of properties which have changed, and events fired, since last update.\n *\n * @returns A callback function which will be called when this effect is stale.\n */\n type Effect<T> = (\n this: T,\n current: T,\n update: readonly State.Event<T>[] | undefined\n ) => EffectCallback | Promise<void> | null | void;\n\n /**\n * A callback function returned by effect. Will be called when effect is stale.\n *\n * @param update - `true` if update is pending, `false` effect has been cancelled, `null` if state is destroyed.\n */\n type EffectCallback = (update: boolean | null) => void;\n\n /**\n * A list of keys updated, or events fired, since last update.\n * This may be awaited to get full list (same array) when update is settled.\n */\n type Updated<T extends State> = readonly Event<T>[] &\n PromiseLike<readonly Event<T>[]>;\n}\n\nabstract class State {\n /**\n * Loopback to instance of this state. This is useful when in a subscribed context,\n * to keep write access to `this` after a destructure. You can use it to read variables silently as well.\n **/\n declare is: this;\n\n constructor(...args: State.Args) {\n define(this, 'is', { value: this });\n init(this, args, this.new);\n }\n\n /**\n * Optional lifecycle hook called during State initialization.\n * Can return a cleanup function to run when state is destroyed.\n *\n * It is recommended you protect this method.\n */\n protected new?(): void | (() => void);\n\n /**\n * Pull current values from state. Flattens all states and exotic values recursively.\n *\n * @returns Object with all values from this state.\n **/\n get(): State.Values<this>;\n\n /**\n * Run a function to run automatically when accessed values change.\n *\n * @param effect Function to run, and whenever accessed values change.\n * If effect returns a function, it will be called when a change occurs (syncronously),\n * effect is cancelled, or parent state is destroyed.\n * @returns Function to cancel listener.\n */\n get(effect: State.Effect<this>): () => void;\n\n /**\n * Get value of a property. Will fetch underlying value from exotic values like ref.Object.\n *\n * Any value which implements `get()` (with no arguments) will be treated as such.\n *\n * @param key - Property to get value of.\n * @param required - If true, will throw an error if property is not available.\n * @returns Value of property.\n */\n get<T extends State.Event<this>>(\n key: T,\n required?: boolean\n ): State.Value<this, T>;\n\n /**\n * Check if state is destroyed.\n *\n * @param status - `null` to check if state is destroyed.\n * @returns `true` if state is destroyed, `false` otherwise.\n */\n get(status: null): boolean;\n\n /**\n * Callback when state is to be destroyed.\n *\n * @param callback - Function to call when state is destroyed.\n * @returns Function to cancel listener.\n */\n get(status: null, callback: () => void): () => void;\n\n /** Fetch State of type from context. Throws if not found. */\n get<T extends State>(type: State.Type<T>, required?: true): T;\n\n /** Fetch a State from context. Undefined if not found. */\n get<T extends State>(type: State.Type<T>, required: boolean): T | undefined;\n\n /**\n * Subscribe to State becoming available in context.\n *\n * Will search both up and downstream by default.\n * Normally you can ignore this because State you expect is rarely both.\n *\n * Specify downstream if you only want to watch for State in children, which can be useful if you expect multiple of the same State in different branches of the tree.\n *\n * @param type - Type of State to watch for.\n * @param callback - Function to call when State is found. Will be called immediately if State is already available.\n * @param downstream - If true, will only watch for State in children, if false will only for parents.\n * @returns Function to cancel listener.\n */\n get<T extends State>(\n type: State.Type<T>,\n callback: Context.Expect<T>,\n downstream?: boolean\n ): () => void;\n\n get(\n arg1?: State.Effect<this> | State.Type | string | null,\n arg2?: boolean | Context.Expect | (() => void),\n arg3?: boolean\n ) {\n const self = this.is;\n\n if (arg1 === undefined) return values(self);\n if (State.is(arg1)) return Context.get(self).get(arg1, arg2, arg3, self);\n if (typeof arg1 == 'function') return watch(self, unbind(arg1));\n if (typeof arg2 == 'function') return callback(self, arg2, arg1);\n if (arg1 === null) return observer(self) === null;\n return access(self, arg1, arg2);\n }\n\n /**\n * Get update in progress.\n *\n * @returns Promise which resolves object with updated values, `undefined` if there no update is pending.\n **/\n set(): State.Updated<this>;\n\n /**\n * Merge argument with current state, updating one or more properties at once.\n * Properties which are not managed by this state will be ignored.\n *\n * @param assign - Object with properties to update.\n * @param silent - If true, listeners will not be notified. If state is destroyed, will squash update without throwing.\n * @returns Array of keys updated, syncronously contains keys updated immediately and may be resolved (to itself) when all updates are settled.\n */\n set(assign?: State.Assign<this>, silent?: boolean): State.Updated<this>;\n\n /**\n * Call a function when update occurs.\n *\n * Given function is called for every assignment (which changes value) or explicit `set`.\n *\n * To run logic on final value only, callback may return a function. The same\n * function for one or more events will be called only once when update is settled.\n *\n * Return `null` from callback to stop listening.\n *\n * @param callback - Function to call when update occurs.\n * @returns Function to remove listener. Will return `true` if removed, `false` if inactive already.\n */\n set(callback: State.OnEvent<this>): () => boolean;\n\n /**\n * Push an update. This will not change the value of associated property.\n *\n * Useful where a property value internally has changed, but the object is the same.\n * For example: An array has pushed a new value, or a nested property is updated.\n *\n * You can also use this to dispatch arbitrary events.\n * Symbols are recommended as non-property events, however you can use any string.\n * If doing so, be sure to avoid collisions using property names. An easy way to do this is\n * to prefix an event with \"!\" and/or use dash-case. e.g. `set(\"!event\")` or `set(\"my-event\")`.\n *\n * @param key - Property or event to dispatch.\n * @returns Promise resolves an array of keys updated.\n */\n set(key: State.Event<this>): State.Updated<this>;\n\n /**\n * Declare an end to updates. This event is final and will freeze state.\n * This event can be watched for as well, to run cleanup logic and internally will remove all listeners.\n *\n * @param status - `null` to end updates.\n */\n set(status: null): void;\n\n /**\n * Register a callback for a specific property or event.\n *\n * Callback receives the key, current value, and source instance.\n *\n * @param event - Property or event to watch. If `null`, will callback on destroy.\n * @param callback - Function to call when event occurs.\n * @returns Function to remove listener.\n */\n set<K extends State.Event<this>>(\n event: K | null,\n callback: State.OnEvent<this>\n ): () => boolean;\n\n /**\n * Define or update a managed property using a descriptor config.\n * If the property already is managed, config will only accept value.\n * If the property does not exist, it will be created and made reactive.\n *\n * @param key - Property to define or update.\n * @param config - Descriptor config with value, get, set, enumerable, and/or destroy.\n * @returns Promise resolves an array of keys updated.\n */\n set<K extends State.Event<this>>(\n key: K,\n config: State.Define<this, K>\n ): State.Updated<this>;\n\n set(\n arg1?: State.OnEvent<this> | State.Assign<this> | State.Event<this> | null,\n arg2?: unknown\n ) {\n const self = this.is;\n\n if (typeof arg1 == 'function')\n return listener(self, (key) => {\n if (\n typeof key == 'string' ||\n typeof key == 'number' ||\n typeof key == 'symbol'\n )\n return arg1.call(self, key, self);\n });\n\n if (typeof arg2 == 'function')\n return callback(self, arg2, arg1 as State.Signal);\n\n if (arg1 && typeof arg1 == 'object') assign(self, arg1, arg2 === true);\n else if (arg2) apply(self, arg1 as string, arg2);\n else event(self, arg1);\n\n return pending(self) as State.Updated<this>;\n }\n\n /**\n * Iterate over managed properties in this instance of State.\n * Yeilds the key and current value for each property.\n */\n [Symbol.iterator](): Iterator<[string, unknown]> {\n return Object.entries(STORE.get(this.is)!)[Symbol.iterator]();\n }\n\n /**\n * Create and activate a new instance of this state.\n *\n * **Important** - Unlike `new this(...)` - this method also activates state.\n *\n * @param args - arguments sent to constructor\n */\n static new<T extends State>(this: State.Type<T>, ...args: State.Args<T>): T {\n const instance = new this(...args);\n event(instance);\n return instance;\n }\n\n /**\n * Static equivalent of `x instanceof this`.\n * Determines if provided class is a subtype of this one.\n * If so, language server will make available all static\n * methods and properties of this class.\n */\n static is<T extends State.Extends>(this: T, maybe: unknown): maybe is T {\n return (\n maybe === this ||\n (typeof maybe == 'function' && maybe.prototype instanceof this)\n );\n }\n\n /**\n * Register a lifecycle handler for this State and its subclasses.\n *\n * A bare function is per-instance setup run in the `prepare` phase (sugar for\n * `{ before }`); if it returns a function, that runs when the instance is\n * destroyed. Pass a {@link State.On} object to hook by cadence - `type`\n * (per-class, at bootstrap), `before` (per-instance, before `new()`), and\n * `after` (per-instance, at the `new()` slot).\n *\n * @returns Function to remove the handler.\n */\n static on<T extends State>(\n this: State.Extends<T>,\n handler: State.Init<T> | State.On<T>\n ) {\n let setup = SETUP.get(this);\n\n if (!setup) SETUP.set(this, (setup = new Set()));\n\n setup.add(handler);\n\n return () => setup.delete(handler);\n }\n}\n\ndefine(State.prototype, 'toString', {\n value() {\n return ID.get(this.is);\n }\n});\n\ndefine(State, 'toString', {\n value() {\n return this.name;\n }\n});\n\n/** Register a user OnEvent callback, preserving `this` and `source`. */\nfunction callback<T extends State>(\n self: T,\n cb: Function,\n select?: Observer.Signal | Set<Observer.Signal>\n) {\n return listener(self, (key) => cb.call(self, key, self), select);\n}\n\n/**\n * Apply state arguemnts, run callbacks and observe properties.\n * Accumulate and handle cleanup events.\n **/\nfunction init(state: State, ...args: State.Args) {\n const T = state.constructor as State.Extends;\n\n if (T === State) throw new Error('Cannot create base State.');\n\n const { before, after } = bootstrap(T);\n\n ID.set(state, `${T}-${uid()}`);\n STORE.set(state, {});\n\n function observe() {\n for (const key in state) {\n const desc = Object.getOwnPropertyDescriptor(state, key)!;\n if ('value' in desc) apply(state, key, desc, true);\n }\n }\n\n function register() {\n if (Context.get(state) === Context.root) return Context.root.add(state);\n }\n\n listener(state, () => {\n parent(state, null);\n\n const queue = [...before, observe, ...args, ...after, register];\n\n for (let i = 0; i < queue.length; i++) {\n const arg = queue[i];\n const out = typeof arg == 'function' ? arg.call(state, state) : arg;\n\n if (out instanceof Promise)\n out.catch((err) => {\n console.error(`Async error in constructor for ${state}:`);\n console.error(err);\n });\n else if (Array.isArray(out)) queue.splice(i + 1, 0, ...out);\n else if (typeof out == 'function') listener(state, out, null);\n else if (typeof out == 'object') assign(state, out, true);\n }\n\n return null;\n });\n}\n\n/**\n * Apply instructions and inherited event listeners. Ensure class metadata is ready.\n *\n * @param T - State class to bootstrap.\n * @return Array of init functions to run on instance, in order of class hierarchy.\n */\nfunction bootstrap(T: State.Extends) {\n const chain = [] as State.Extends[];\n const before = new Set<State.Init>();\n const after = new Set<State.Init>();\n const onType = new Set<NonNullable<State.On['type']>>();\n let keys = new Map<string, (value: any) => void>();\n let getters = new Map<string, () => unknown>();\n\n do {\n chain.unshift(T);\n T = Object.getPrototypeOf(T);\n } while (T.name);\n\n for (const type of chain) {\n for (const handler of SETUP.get(type) || [])\n if (typeof handler == 'function') before.add(handler);\n else {\n if (handler.before) before.add(handler.before);\n if (handler.after) after.add(handler.after);\n if (handler.type) onType.add(handler.type);\n }\n\n if (type === State) continue;\n\n if (METHODS.has(type)) {\n keys = METHODS.get(type)!;\n getters = GETTERS.get(type)!;\n continue;\n }\n\n for (const setupType of onType) setupType(type);\n\n METHODS.set(type, (keys = new Map(keys)));\n GETTERS.set(type, (getters = new Map(getters)));\n\n for (const [key, desc] of Object.entries(\n Object.getOwnPropertyDescriptors(type.prototype)\n )) {\n if (key == 'constructor' || !desc.configurable) continue;\n\n if (typeof desc.get == 'function') {\n if (desc.configurable && typeof desc.set != 'function')\n getters.set(key, desc.get);\n\n continue;\n }\n\n const { value } = desc;\n\n if (typeof value !== 'function') continue;\n\n function bind(this: State, original?: Function) {\n const { is } = this;\n\n if (is.hasOwnProperty(key) && !original) return value as Function;\n\n const fn = original || value;\n const bound = fn.bind(is);\n\n UNBIND.set(bound, fn);\n define(is, key, { value: bound, writable: true, configurable: true });\n\n return bound;\n }\n\n UNBIND.set(bind, value);\n\n keys.set(key, bind);\n define(type.prototype, key, { get: bind, set: bind });\n }\n }\n\n if (getters.size)\n before.add((self) => getters.forEach(compute, self));\n\n return { before, after };\n}\n\n/**\n * Install a reactive computed property on a state instance, derived from a prototype getter.\n *\n * The getter is invoked with the tracking proxy as `this`; reads of managed properties\n * through it create subscriptions. Result is cached and emits a keyed event when stale.\n */\nfunction compute(this: State, getter: () => unknown, key: string) {\n const store = STORE.get(this)!;\n let reset: (() => void) | undefined;\n let isAsync: boolean;\n let proxy: any;\n\n const connect = () => {\n reset = watch(\n this,\n (current) => {\n proxy = current;\n\n if (!(key in store) || STALE.delete(run)) run(!reset);\n\n return (didUpdate) => {\n if (didUpdate) {\n STALE.add(run);\n event(this, key, true);\n }\n };\n },\n false\n );\n };\n\n const run = (initial?: boolean) => {\n let next: unknown;\n\n try {\n next = getter.call(proxy);\n } catch (err) {\n console.warn(\n `An exception was thrown while ${initial ? 'initializing' : 'refreshing'\n } ${this}.${key}.`\n );\n\n if (initial) throw err;\n\n console.error(err);\n }\n\n update(this, key, next, !isAsync);\n };\n\n apply(\n this,\n key,\n {\n enumerable: true,\n set: false,\n get: () => {\n if (!proxy) {\n if (observer(this) === null) return store[key];\n\n connect();\n isAsync = true;\n }\n\n if (STALE.delete(run)) run();\n\n return access(this, key, !proxy);\n }\n },\n true\n );\n}\n\n/**\n * Define or update a managed property using a descriptor config.\n * If the property already is managed, config will only accept value.\n * If the property does not exist, it will be created and made reactive.\n *\n * @param state - State to apply property to.\n * @param key - Property to define or update.\n * @param config - Descriptor config with value, get, set, enumerable, and/or destroy.\n * @param silent - If an update does occur, listeners will not be refreshed automatically.\n */\nfunction apply(\n state: State,\n key: string,\n config: State.Apply,\n silent?: boolean\n) {\n const desc = Object.getOwnPropertyDescriptor(state, key);\n\n if (desc && 'get' in desc) {\n if (Object.keys(config).some((k) => k != 'value'))\n throw new Error(`Property ${key} on ${state} is already defined.`);\n if ('value' in config) update(state, key, config.value, silent);\n return;\n }\n\n const adopt = config.value instanceof State && child(state);\n\n function set(value: unknown, silent?: boolean) {\n if (!update(state, key, value, silent)) return;\n if (adopt) adopt(value);\n }\n\n define(state, key, {\n configurable: true,\n enumerable: config.enumerable !== false,\n get(this: State) {\n const value =\n typeof config.get == 'function'\n ? config.get(this)\n : access(state, key, config.get);\n\n return touch(this, key, value);\n },\n set(next: any, silent?: boolean) {\n if (config.set === false)\n throw new Error(`${state}.${String(key)} is read-only.`);\n\n if (typeof config.set == 'function')\n try {\n const output = config.set(next, STORE.get(state)![key]);\n if (output !== undefined) next = output;\n } catch (err: unknown) {\n if (err === false) return;\n if (err === true) {\n set(next, true);\n return;\n }\n throw err;\n }\n\n set(next, silent);\n }\n });\n\n if ('value' in config) set(config.value, silent);\n}\n\nfunction child(state: State) {\n let cleanup: (() => void) | undefined;\n const ctx = Context.get(state);\n\n function reset() {\n if (cleanup) cleanup();\n cleanup = undefined;\n }\n\n listener(state, reset, null);\n\n return (value: unknown) => {\n reset();\n\n if (!(value instanceof State)) return;\n\n const remove = ctx.add(value);\n\n if (parent(value) === undefined) {\n parent(value, state);\n cleanup = () => {\n cancel();\n remove();\n event(value, null);\n };\n const cancel = listener(state, cleanup, null);\n } else {\n cleanup = remove;\n }\n\n event(value);\n };\n}\n\n/** Currently accumulating export. Stores real values of placeholder properties such as ref() or child states. */\nlet EXPORT: Map<any, any> | undefined;\n\nfunction values<T extends State>(state: T): State.Values<T> {\n const values = {} as any;\n let notRecursive;\n\n if (!EXPORT) {\n notRecursive = true;\n EXPORT = new Map([[state, values]]);\n }\n\n for (let [key, value] of state) {\n if (EXPORT.has(value)) value = EXPORT.get(value);\n else if (\n value &&\n typeof value == 'object' &&\n 'get' in value &&\n typeof value.get === 'function'\n )\n EXPORT.set(value, (value = value.get()));\n\n values[key] = value;\n }\n\n if (notRecursive) EXPORT = undefined;\n\n return Object.freeze(values);\n}\n\nfunction access(state: State, property: string, required?: boolean) {\n const store = STORE.get(state)!;\n\n if (property in store || required === false) {\n const value = store[property];\n\n if (value !== undefined || !required) return value;\n }\n\n if (METHODS.get(state.constructor)!.has(property))\n return unbind((state as any)[property]);\n\n if (observer(state) === null) return undefined;\n\n const error = new Error(`${state}.${property} is not yet available.`);\n const promise = new Promise<any>((resolve, reject) => {\n listener(state, (key) => {\n if (key === property) {\n resolve(store[key]);\n return null;\n }\n\n if (key === null) {\n reject(new Error(`${state} is destroyed.`));\n }\n });\n });\n\n throw Object.assign(promise, {\n toString: () => String(error),\n name: 'Suspense',\n message: error.message,\n stack: error.stack\n });\n}\n\nfunction assign(state: State, data: State.Assign<State>, silent?: boolean) {\n if (!silent) event(state);\n\n const methods = METHODS.get(state.constructor)!;\n\n for (const key in data) {\n const bind = methods.get(key);\n\n if (bind) bind.call(state, data[key]);\n else if (key in state && key !== 'is') {\n const desc = Object.getOwnPropertyDescriptor(state, key)!;\n const set = desc && (desc.set as (value: any, silent?: boolean) => void);\n\n if (set) {\n set.call(state, data[key], silent);\n } else {\n (state as any)[key] = data[key];\n }\n }\n }\n}\n\n/**\n * Update a property on a state instance and notify listeners.\n *\n * This is used internally to update properties, but can also be used to update properties which are not managed by state, or to update values without triggering setters.\n *\n * If `silent` is true, the update will not dispatch events and will return `false` instead of throwing if state is destroyed.\n */\nfunction update<T>(\n state: State,\n key: State.Event<T>,\n value: T,\n silent?: boolean\n) {\n if (observer(state) === null) {\n if (silent) return false;\n throw new Error(\n `Tried to update ${state}.${String(key)} but state is destroyed.`\n );\n }\n\n const store = STORE.get(state)!;\n\n if (key in store && value === store[key]) return false;\n\n store[key] = value;\n\n if (!silent) event(state, key);\n\n return true;\n}\n\n/** Random alphanumberic of length 6; always starts with a letter. */\nfunction uid() {\n return (0.278 + Math.random() * 0.722)\n .toString(36)\n .substring(2, 8)\n .toUpperCase();\n}\n\nfunction unbind<T extends Function>(fn: T): T;\nfunction unbind<T extends Function>(fn?: T): T | undefined;\nfunction unbind(fn?: Function) {\n return UNBIND.get(fn) || fn;\n}\n\n/**\n * Get or set a parent on given state. May only be assigned once; ignores new value.\n * Returns parent of child, or `undefined` if not assigned yet.\n */\nfunction parent(child: State, value?: State | null) {\n if (arguments.length > 1 && !PARENT.has(child)) PARENT.set(child, value!);\n return PARENT.get(child);\n}\n\nexport { event, unbind, State, parent, STORE, uid, access, update, apply };\n"],"mappings":";;;;AAWA,MAAM,SAAS,OAAO;;AAGtB,MAAM,qBAAK,IAAI,SAAwB;;AAGvC,MAAM,wBAAQ,IAAI,SAA2D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MAAM,OAAO,QAAQ,IAAI,IAAI;AAE7B,MAAI,KAAM,MAAK,KAAK,OAAO,KAAK,KAAK;WAC5B,OAAO,SAAS,QAAQ,MAAM;GACrC,MAAM,OAAO,OAAO,yBAAyB,OAAO,IAAI;GACxD,MAAM,MAAM,QAAS,KAAK;AAE1B,OAAI,IACF,KAAI,KAAK,OAAO,KAAK,MAAM,OAAO;OAElC,CAAC,MAAc,OAAO,KAAK;;;;;;;;;;;AAanC,SAAS,OACP,OACA,KACA,OACA,QACA;AACA,KAAI,SAAS,MAAM,KAAK,MAAM;AAC5B,MAAI,OAAQ,QAAO;AACnB,QAAM,IAAI,MACR,mBAAmB,MAAM,GAAG,OAAO,IAAI,CAAC,0BACzC;;CAGH,MAAM,QAAQ,MAAM,IAAI,MAAM;AAE9B,KAAI,OAAO,SAAS,UAAU,MAAM,KAAM,QAAO;AAEjD,OAAM,OAAO;AAEb,KAAI,CAAC,OAAQ,OAAM,OAAO,IAAI;AAE9B,QAAO;;;AAIT,SAAS,MAAM;AACb,SAAQ,OAAQ,KAAK,QAAQ,GAAG,MAC7B,SAAS,GAAG,CACZ,UAAU,GAAG,EAAE,CACf,aAAa;;AAKlB,SAAS,OAAO,IAAe;AAC7B,QAAO,OAAO,IAAI,GAAG,IAAI;;;;;;AAO3B,SAAS,OAAO,OAAc,OAAsB;AAClD,KAAI,UAAU,SAAS,KAAK,CAAC,OAAO,IAAI,MAAM,CAAE,QAAO,IAAI,OAAO,MAAO;AACzE,QAAO,OAAO,IAAI,MAAM"}
|
package/package.json
CHANGED
|
@@ -1,37 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expressive/mvc",
|
|
3
|
+
"version": "0.78.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"keywords": [
|
|
6
|
+
"expressive",
|
|
7
|
+
"mvc",
|
|
8
|
+
"state"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
3
11
|
"author": {
|
|
4
12
|
"name": "Gabe Klein",
|
|
5
13
|
"email": "gabe@termtm.com",
|
|
6
14
|
"url": "https://github.com/gabeklein"
|
|
7
15
|
},
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"access": "public"
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/gabeklein/expressive-mvc.git"
|
|
12
19
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"mvc"
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
16
22
|
],
|
|
17
23
|
"type": "module",
|
|
18
24
|
"main": "dist/index.js",
|
|
19
|
-
"module": "dist/esm/index.js",
|
|
20
25
|
"types": "dist/index.d.ts",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./jsx-runtime": {
|
|
32
|
+
"types": "./dist/jsx-runtime.d.ts",
|
|
33
|
+
"default": "./dist/jsx-runtime.js"
|
|
34
|
+
},
|
|
35
|
+
"./jsx-dev-runtime": {
|
|
36
|
+
"types": "./dist/jsx-dev-runtime.d.ts",
|
|
37
|
+
"default": "./dist/jsx-dev-runtime.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
24
46
|
"scripts": {
|
|
25
47
|
"build": "tsdown",
|
|
26
|
-
"test": "tsc --noEmit &&
|
|
27
|
-
"prebuild": "rm -rf dist",
|
|
28
|
-
"preversion": "npm run test && npm run build"
|
|
48
|
+
"test": "tsc --noEmit && bun test --coverage --only-failures"
|
|
29
49
|
},
|
|
30
50
|
"devDependencies": {
|
|
31
51
|
"@types/node": "^18.19.0",
|
|
32
|
-
"jest": "^27.5.1",
|
|
33
52
|
"tsdown": "^0.20.1"
|
|
34
|
-
}
|
|
35
|
-
"version": "0.73.0",
|
|
36
|
-
"gitHead": "c5a755934bc66896216eb508f00e6fa7e3efec3b"
|
|
53
|
+
}
|
|
37
54
|
}
|
package/dist/esm/context.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { State } from "./state.js";
|
|
2
|
-
|
|
3
|
-
//#region src/context.d.ts
|
|
4
|
-
declare namespace Context {
|
|
5
|
-
type Multiple<T extends State> = {
|
|
6
|
-
[key: string | number]: State.Type<T> | T;
|
|
7
|
-
};
|
|
8
|
-
type Accept<T extends State = State> = T | State.Type<T> | Multiple<T>;
|
|
9
|
-
type Expect<T extends State = State> = (state: T) => (() => void) | void;
|
|
10
|
-
}
|
|
11
|
-
interface Context {
|
|
12
|
-
[key: symbol]: State | Context.Expect | null | undefined;
|
|
13
|
-
}
|
|
14
|
-
declare class Context {
|
|
15
|
-
/**
|
|
16
|
-
* Get the context for a specified State. If a callback is provided, it will be run when
|
|
17
|
-
* the context becomes available.
|
|
18
|
-
*/
|
|
19
|
-
static get<T extends State>(on: State, callback: (got: Context) => void): void;
|
|
20
|
-
/**
|
|
21
|
-
* Get the context for a specified State. Returns undefined if none are found.
|
|
22
|
-
*/
|
|
23
|
-
static get<T extends State>(on: State): Context | undefined;
|
|
24
|
-
id: string;
|
|
25
|
-
protected inputs: Record<string | number, State | State.Extends>;
|
|
26
|
-
protected cleanup: Set<() => void>;
|
|
27
|
-
constructor(inputs?: Context.Accept);
|
|
28
|
-
/** Find specified type registered to a parent context. Throws if none are found. */
|
|
29
|
-
get<T extends State>(Type: State.Extends<T>, require: true): T;
|
|
30
|
-
/** Find specified type registered to a parent context. Returns undefined if none are found. */
|
|
31
|
-
get<T extends State>(Type: State.Extends<T>, require?: boolean): T | undefined;
|
|
32
|
-
/** Run callback when a specified type is registered in context downstream. */
|
|
33
|
-
get<T extends State>(Type: State.Extends<T>, callback: (state: T) => void): () => void;
|
|
34
|
-
/**
|
|
35
|
-
* Adds a State to this context.
|
|
36
|
-
*/
|
|
37
|
-
protected add<T extends State>(input: T | State.Type<T>, implicit?: boolean): T;
|
|
38
|
-
/**
|
|
39
|
-
* Register one or more States to this context.
|
|
40
|
-
*
|
|
41
|
-
* Context will add or remove States as needed to keep with provided input.
|
|
42
|
-
*
|
|
43
|
-
* @param inputs State, State class, or map of States / State classes to register.
|
|
44
|
-
* @param forEach Optional callback to run for each State registered.
|
|
45
|
-
*/
|
|
46
|
-
use<T extends State>(inputs: Context.Accept<T>, forEach?: Context.Expect<T>): void;
|
|
47
|
-
/**
|
|
48
|
-
* Create a child context, optionally registering one or more States to it.
|
|
49
|
-
*
|
|
50
|
-
* @param inputs State, State class, or map of States / State classes to register.
|
|
51
|
-
*/
|
|
52
|
-
push(inputs?: Context.Accept): this;
|
|
53
|
-
/**
|
|
54
|
-
* Remove all States from this context.
|
|
55
|
-
*
|
|
56
|
-
* Will also run any cleanup callbacks registered when States were added.
|
|
57
|
-
*/
|
|
58
|
-
pop(): void;
|
|
59
|
-
}
|
|
60
|
-
//#endregion
|
|
61
|
-
export { Context };
|
|
62
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5kLnRzIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb250ZXh0LmQudHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgU3RhdGUgfSBmcm9tICcuL3N0YXRlJztcbmRlY2xhcmUgbmFtZXNwYWNlIENvbnRleHQge1xuICAgIHR5cGUgTXVsdGlwbGU8VCBleHRlbmRzIFN0YXRlPiA9IHtcbiAgICAgICAgW2tleTogc3RyaW5nIHwgbnVtYmVyXTogU3RhdGUuVHlwZTxUPiB8IFQ7XG4gICAgfTtcbiAgICB0eXBlIEFjY2VwdDxUIGV4dGVuZHMgU3RhdGUgPSBTdGF0ZT4gPSBUI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