@zag-js/core
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -44,9 +44,9 @@ var __defProp = Object.defineProperty; | ||
mergeProps: () => mergeProps, | ||
proxy: () => import_vanilla3.proxy, | ||
ref: () => import_vanilla3.ref, | ||
snapshot: () => import_vanilla3.snapshot, | ||
subscribe: () => import_vanilla3.subscribe | ||
proxy: () => import_vanilla4.proxy, | ||
ref: () => import_vanilla4.ref, | ||
snapshot: () => import_vanilla4.snapshot, | ||
subscribe: () => import_vanilla4.subscribe | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_vanilla3 = require("valtio/vanilla"); | ||
var import_vanilla4 = require("valtio/vanilla"); | ||
@@ -99,2 +99,3 @@ // ../utilities/core/dist/index.mjs | ||
// src/utils.ts | ||
var import_vanilla = require("valtio/vanilla"); | ||
function toEvent(event) { | ||
@@ -112,2 +113,12 @@ const obj = isString(event) ? { type: event } : event; | ||
} | ||
function subscribeKey(obj, key, fn, sync) { | ||
let prev = Reflect.get((0, import_vanilla.snapshot)(obj), key); | ||
return (0, import_vanilla.subscribe)(obj, () => { | ||
const __obj = (0, import_vanilla.snapshot)(obj); | ||
if (!Object.is(prev, __obj[key])) { | ||
fn(__obj[key]); | ||
prev = Reflect.get(__obj, key); | ||
} | ||
}, sync); | ||
} | ||
@@ -182,14 +193,16 @@ // src/guard-utils.ts | ||
var import_json = require("klona/json"); | ||
var import_utils9 = require("valtio/utils"); | ||
var import_vanilla2 = require("valtio/vanilla"); | ||
var import_vanilla3 = require("valtio/vanilla"); | ||
// src/create-proxy.ts | ||
var import_vanilla = require("valtio/vanilla"); | ||
var import_vanilla2 = require("valtio/vanilla"); | ||
var import_utils4 = require("valtio/utils"); | ||
function createProxy(config) { | ||
var _a; | ||
const state = (0, import_vanilla.proxy)({ | ||
var _a, _b; | ||
const computedContext = (_a = config.computed) != null ? _a : cast({}); | ||
const initialContext = (_b = config.context) != null ? _b : cast({}); | ||
const state = (0, import_vanilla2.proxy)({ | ||
value: "", | ||
previousValue: "", | ||
event: cast({}), | ||
context: (_a = config.context) != null ? _a : cast({}), | ||
context: (0, import_utils4.proxyWithComputed)(initialContext, computedContext), | ||
done: false, | ||
@@ -207,4 +220,4 @@ tags: [], | ||
get nextEvents() { | ||
var _a2, _b, _c, _d; | ||
const stateEvents = (_c = (_b = (_a2 = config.states) == null ? void 0 : _a2[this.value]) == null ? void 0 : _b["on"]) != null ? _c : {}; | ||
var _a2, _b2, _c, _d; | ||
const stateEvents = (_c = (_b2 = (_a2 = config.states) == null ? void 0 : _a2[this.value]) == null ? void 0 : _b2["on"]) != null ? _c : {}; | ||
const globalEvents = (_d = config == null ? void 0 : config.on) != null ? _d : {}; | ||
@@ -280,2 +293,3 @@ return Object.keys(__spreadValues(__spreadValues({}, stateEvents), globalEvents)); | ||
this.type = "machine" /* Machine */; | ||
this.deriving = false; | ||
this.activityEvents = /* @__PURE__ */ new Map(); | ||
@@ -308,3 +322,3 @@ this.delayedEvents = /* @__PURE__ */ new Map(); | ||
this.performStateChangeEffects(info.target, info, event); | ||
this.removeStateListener = (0, import_vanilla2.subscribe)(this.state, () => { | ||
this.removeStateListener = (0, import_vanilla3.subscribe)(this.state, () => { | ||
this.stateListeners.forEach((listener) => { | ||
@@ -314,3 +328,3 @@ listener(this.stateSnapshot); | ||
}, this.sync); | ||
this.removeEventListener = (0, import_utils9.subscribeKey)(this.state, "event", (event2) => { | ||
this.removeEventListener = subscribeKey(this.state, "event", (event2) => { | ||
if (this.config.onEvent) { | ||
@@ -331,3 +345,3 @@ this.executeActions(this.config.onEvent, event2); | ||
for (const [key, fn] of Object.entries((_a = this.config.watch) != null ? _a : {})) { | ||
this.contextWatchers.add((0, import_utils9.subscribeKey)(this.state.context, key, () => { | ||
this.contextWatchers.add(subscribeKey(this.state.context, key, () => { | ||
this.executeActions(fn, this.state.event); | ||
@@ -337,11 +351,2 @@ })); | ||
}; | ||
this.setupComputed = () => { | ||
var _a; | ||
const computed = cast((_a = this.config.computed) != null ? _a : {}); | ||
const deriveFns = Object.fromEntries(Object.entries(computed).map(([key, fn]) => [key, (get) => fn(get(this.state.context))])); | ||
(0, import_utils9.derive)(deriveFns, { proxy: this.state.context, sync: this.sync || this.options.hookSync }); | ||
}; | ||
this.detachComputed = () => { | ||
(0, import_utils9.underive)(this.state.context); | ||
}; | ||
this.stop = () => { | ||
@@ -352,5 +357,2 @@ if (this.status === "Stopped" /* Stopped */) | ||
this.setEvent("machine.stop" /* Stop */); | ||
if (this.config.context) { | ||
this.setContext(this.config.context); | ||
} | ||
this.stopStateListeners(); | ||
@@ -362,3 +364,2 @@ this.stopChildren(); | ||
this.stopEventListeners(); | ||
this.detachComputed(); | ||
this.executeActions(this.config.exit, toEvent("machine.stop" /* Stop */)); | ||
@@ -436,3 +437,3 @@ this.status = "Stopped" /* Stopped */; | ||
}).start(); | ||
return cast((0, import_vanilla2.ref)(actor)); | ||
return cast((0, import_vanilla3.ref)(actor)); | ||
}; | ||
@@ -465,3 +466,2 @@ this.addActivityCleanup = (state, cleanup) => { | ||
this.withContext = (context) => { | ||
this.detachComputed(); | ||
const newContext = __spreadValues(__spreadValues({}, this.config.context), context); | ||
@@ -608,3 +608,3 @@ return new Machine(__spreadProps(__spreadValues({}, this.config), { context: newContext }), this.options); | ||
this.setEvent = (event) => { | ||
this.state.event = (0, import_vanilla2.ref)(toEvent(event)); | ||
this.state.event = (0, import_vanilla3.ref)(toEvent(event)); | ||
}; | ||
@@ -734,3 +734,2 @@ this.performExitEffects = (current, event) => { | ||
this.state = createProxy((0, import_json.klona)(config)); | ||
this.setupComputed(); | ||
const event = toEvent("machine.created" /* Created */); | ||
@@ -740,3 +739,3 @@ this.executeActions(config == null ? void 0 : config.created, event); | ||
get stateSnapshot() { | ||
return cast((0, import_vanilla2.snapshot)(this.state)); | ||
return cast((0, import_vanilla3.snapshot)(this.state)); | ||
} | ||
@@ -743,0 +742,0 @@ get contextSnapshot() { |
@@ -9,2 +9,3 @@ import { Dict, MachineStatus, MachineType, StateMachine as S, Writable } from "./types"; | ||
type: MachineType; | ||
private deriving; | ||
private activityEvents; | ||
@@ -31,4 +32,2 @@ private delayedEvents; | ||
private setupContextWatchers; | ||
private setupComputed; | ||
private detachComputed; | ||
stop: () => this; | ||
@@ -35,0 +34,0 @@ private stopEventListeners; |
@@ -7,2 +7,3 @@ import type { StateMachine as S } from "./types"; | ||
}; | ||
export declare function subscribeKey<T extends object, K extends keyof T>(obj: T, key: K, fn: (value: T[K]) => void, sync?: boolean): () => void; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@zag-js/core", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A minimal implementation of xstate fsm for UI machines", | ||
@@ -40,3 +40,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@zag-js/utils": "0.1.0", | ||
"@zag-js/utils": "0.1.1", | ||
"klona": "^2.0.5", | ||
@@ -43,0 +43,0 @@ "valtio": "^1.6.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
222093
1991
+ Added@zag-js/utils@0.1.1(transitive)
- Removed@zag-js/utils@0.1.0(transitive)
Updated@zag-js/utils@0.1.1