Socket
Socket
Sign inDemoInstall

@zag-js/core

Package Overview
Dependencies
3
Maintainers
1
Versions
873
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0-dev-20220424123138 to 0.0.0-dev-20220425073521

22

dist/guard-utils.d.ts
import { StateMachine as S } from "./types";
declare function or<TContext, TEvent extends S.EventObject>(...conditions: Array<string | S.GuardHelper<TContext, TEvent> | S.GuardExpression<TContext, TEvent>>): S.GuardHelper<TContext, TEvent>;
declare function and<TContext, TEvent extends S.EventObject>(...conditions: Array<string | S.GuardHelper<TContext, TEvent> | S.GuardExpression<TContext, TEvent>>): S.GuardHelper<TContext, TEvent>;
declare function not<TContext, TEvent extends S.EventObject>(condition: string | S.GuardHelper<TContext, TEvent> | S.GuardExpression<TContext, TEvent>): S.GuardHelper<TContext, TEvent>;
declare function or<TContext, TEvent extends S.EventObject>(...conditions: Array<S.Guard<TContext, TEvent>>): S.GuardHelper<TContext, TEvent>;
declare function and<TContext, TEvent extends S.EventObject>(...conditions: Array<S.Guard<TContext, TEvent>>): S.GuardHelper<TContext, TEvent>;
declare function not<TContext, TEvent extends S.EventObject>(condition: S.Guard<TContext, TEvent>): S.GuardHelper<TContext, TEvent>;
export declare const guards: {

@@ -10,13 +10,9 @@ or: typeof or;

};
export declare function isGuardHelper(value: unknown): value is {
predicate: Function;
};
export declare const TruthyGuard: () => boolean;
/**
* Guards or conditions can be specified as:
* - a string (reference to `options.guards`)
* - a function that returns a number (in ms)
*/
export declare function determineGuardFn<TContext, TEvent extends S.EventObject>(guard: S.Guard<TContext, TEvent> | undefined, guardMap: S.GuardMap<TContext, TEvent> | undefined): (context: TContext, event: TEvent) => boolean;
export declare function choose<TContext, TState extends S.StateSchema, TEvent extends S.EventObject = S.AnyEventObject>(actions: Array<{
guard?: S.Guard<TContext, TEvent>;
actions: S.PureActions<TContext, TState, TEvent>;
}>): S.ChooseHelper<TContext, TState, TEvent>;
export declare function determineGuardFn<TContext, TEvent extends S.EventObject>(guard: S.Guard<TContext, TEvent> | undefined, guardMap: S.GuardMap<TContext, TEvent>): (context: TContext, event: TEvent) => boolean;
export declare function determineActionsFn<TContext, TState extends S.StateSchema, TEvent extends S.EventObject>(values: S.Actions<TContext, TState, TEvent> | undefined, guardMap: S.GuardMap<TContext, TEvent>): (context: TContext, event: TEvent) => string | S.ExpressionWithMeta<TContext, TState, TEvent, void> | S.Action<TContext, TState, TEvent>[];
export {};
//# sourceMappingURL=guard-utils.d.ts.map
export { proxy, ref, snapshot, subscribe } from "valtio/vanilla";
export { choose } from "./action-utils";
export { guards } from "./guard-utils";
export { guards, choose } from "./guard-utils";
export * from "./machine";

@@ -5,0 +4,0 @@ export { mergeProps } from "./merge-props";

@@ -97,45 +97,33 @@ var __defProp = Object.defineProperty;

// src/action-utils.ts
function choose(actions) {
return {
predicate: (guardMap) => (ctx, event) => {
var _a;
return (_a = actions.find((def) => {
var _a2;
def.guard = def.guard || (() => true);
if (isString(def.guard)) {
return !!((_a2 = guardMap[def.guard]) == null ? void 0 : _a2.call(guardMap, ctx, event));
}
if (isGuardHelper(def.guard)) {
return def.guard.predicate(guardMap != null ? guardMap : {})(ctx, event);
}
return def.guard(ctx, event);
})) == null ? void 0 : _a.actions;
}
};
// src/utils.ts
function toEvent(event) {
const obj = isString(event) ? { type: event } : event;
return obj;
}
function toArray(value) {
if (!value)
return [];
return isArray(value) ? value : [value];
}
function isGuardHelper(value) {
return isObject(value) && value.predicate != null;
}
function determineActionsFn(values, guardMap) {
return (context, event) => {
if (isGuardHelper(values))
return values.predicate(guardMap != null ? guardMap : {})(context, event);
return values;
// src/guard-utils.ts
var Truthy = () => true;
function exec(guardMap, ctx, event) {
return (guard) => {
var _a;
if (isString(guard)) {
return !!((_a = guardMap[guard]) == null ? void 0 : _a.call(guardMap, ctx, event));
}
if (isFunction(guard)) {
return guard(ctx, event);
}
return guard.predicate(guardMap)(ctx, event);
};
}
// src/guard-utils.ts
function or(...conditions) {
return {
predicate: (guards2) => (ctx, event) => conditions.map((condition) => {
var _a;
if (isString(condition)) {
return !!((_a = guards2[condition]) == null ? void 0 : _a.call(guards2, ctx, event));
}
if (isFunction(condition)) {
return condition(ctx, event);
}
return condition.predicate(guards2)(ctx, event);
}).some(Boolean)
predicate: (guardMap) => (ctx, event) => conditions.map(exec(guardMap, ctx, event)).some(Boolean)
};

@@ -145,12 +133,3 @@ }

return {
predicate: (guards2) => (ctx, event) => conditions.map((condition) => {
var _a;
if (isString(condition)) {
return !!((_a = guards2[condition]) == null ? void 0 : _a.call(guards2, ctx, event));
}
if (isFunction(condition)) {
return condition(ctx, event);
}
return condition.predicate(guards2)(ctx, event);
}).every(Boolean)
predicate: (guardMap) => (ctx, event) => conditions.map(exec(guardMap, ctx, event)).every(Boolean)
};

@@ -161,10 +140,3 @@ }

predicate: (guardMap) => (ctx, event) => {
var _a;
if (isString(condition)) {
return !((_a = guardMap[condition]) == null ? void 0 : _a.call(guardMap, ctx, event));
}
if (isFunction(condition)) {
return !condition(ctx, event);
}
return !condition.predicate(guardMap)(ctx, event);
return !exec(guardMap, ctx, event)(condition);
}

@@ -174,15 +146,23 @@ };

var guards = { or, and, not };
function isGuardHelper2(value) {
return isObject(value) && value.predicate != null;
function choose(actions) {
return {
predicate: (guardMap) => (ctx, event) => {
var _a;
return (_a = actions.find((def) => {
var _a2;
const guard = (_a2 = def.guard) != null ? _a2 : Truthy;
return exec(guardMap, ctx, event)(guard);
})) == null ? void 0 : _a.actions;
}
};
}
var TruthyGuard = () => true;
function determineGuardFn(guard, guardMap) {
guard = guard != null ? guard : TruthyGuard;
guard = guard != null ? guard : Truthy;
return (context, event) => {
if (isString(guard)) {
const value = guardMap == null ? void 0 : guardMap[guard];
const value = guardMap[guard];
return isFunction(value) ? value(context, event) : value;
}
if (isGuardHelper2(guard)) {
return guard.predicate(guardMap != null ? guardMap : {})(context, event);
if (isGuardHelper(guard)) {
return guard.predicate(guardMap)(context, event);
}

@@ -192,2 +172,10 @@ return guard == null ? void 0 : guard(context, event);

}
function determineActionsFn(values, guardMap) {
return (context, event) => {
if (isGuardHelper(values)) {
return values.predicate(guardMap)(context, event);
}
return values;
};
}

@@ -249,6 +237,3 @@ // src/machine.ts

const valueOrFn = delaysMap == null ? void 0 : delaysMap[delay];
if (valueOrFn == null) {
const msg = `[machine] Cannot determine delay for ${delay}. It doesn't exist in options.delays`;
throw new Error(msg);
}
invariant(valueOrFn == null, `[@zag-js/core > determine-delay] Cannot determine delay for \`${delay}\`. It doesn't exist in \`options.delays\``);
return isFunction(valueOrFn) ? valueOrFn(context, event) : valueOrFn;

@@ -260,13 +245,2 @@ }

// src/utils.ts
function toEvent(event) {
const obj = isString(event) ? { type: event } : event;
return obj;
}
function toArray(value) {
if (!value)
return [];
return isArray(value) ? value : [value];
}
// src/transition-utils.ts

@@ -340,8 +314,8 @@ function toTarget(target) {

}, this.sync);
this.removeEventListener = (0, import_utils9.subscribeKey)(this.state, "event", (value) => {
this.removeEventListener = (0, import_utils9.subscribeKey)(this.state, "event", (event2) => {
if (this.config.onEvent) {
this.executeActions(this.config.onEvent, value);
this.executeActions(this.config.onEvent, event2);
}
for (const listener of this.eventListeners) {
listener(value);
listener(event2);
}

@@ -386,4 +360,4 @@ });

this.detachComputed();
this.executeActions(this.config.exit, toEvent("machine.stop" /* Stop */));
this.status = "Stopped" /* Stopped */;
this.executeActions(this.config.exit, toEvent("machine.stop" /* Stop */));
return this;

@@ -428,3 +402,3 @@ };

if (!child) {
invariant(`[machine/send-child] Cannot send '${event.type}' event to unknown child`);
invariant(`[@zag-js/core] Cannot send '${event.type}' event to unknown child`);
}

@@ -435,3 +409,3 @@ child.send(event);

if (!this.children.has(id)) {
invariant("[machine/stop-child] Cannot stop unknown child");
invariant(`[@zag-js/core > stop-child] Cannot stop unknown child ${id}`);
}

@@ -570,3 +544,3 @@ this.children.get(id).stop();

const fn = isString(action) ? (_a = this.actionMap) == null ? void 0 : _a[action] : action;
warn(isString(action) && !fn, `[machine/exec-action] No implementation found for action: \`${action}\``);
warn(isString(action) && !fn, `[@zag-js/core > execute-actions] No implementation found for action: \`${action}\``);
fn == null ? void 0 : fn(this.state.context, event, this.meta);

@@ -580,3 +554,3 @@ }

if (!fn) {
warn(`[machine/exec-activity] No implementation found for activity: \`${activity}\``);
warn(`[@zag-js/core > execute-activity] No implementation found for activity: \`${activity}\``);
continue;

@@ -698,3 +672,3 @@ }

if (!this.parent) {
invariant("[machine/send-parent] Cannot send event to an unknown parent");
invariant("[@zag-js/core > send-parent] Cannot send event to an unknown parent");
}

@@ -713,3 +687,3 @@ const event = toEvent(evt);

if (!stateNode && !this.config.on) {
const msg = this.status === "Stopped" /* Stopped */ ? "[machine/transition] Cannot transition a stopped machine" : "[machine/transition] State does not have a definition";
const msg = this.status === "Stopped" /* Stopped */ ? "[@zag-js/core > transition] Cannot transition a stopped machine" : `[@zag-js/core > transition] State does not have a definition for \`state\`: ${state}, \`event\`: ${event.type}`;
warn(msg);

@@ -716,0 +690,0 @@ return;

@@ -13,4 +13,4 @@ import type { StateMachine as S } from "./types";

export declare function toTarget<TContext, TState extends S.StateSchema, TEvent extends S.EventObject>(target: S.Transition<TContext, TState, TEvent>): S.TransitionDefinition<TContext, TState, TEvent>;
export declare function determineTransitionFn<TContext, TState extends S.StateSchema, TEvent extends S.EventObject>(transitions?: S.Transitions<TContext, TState, TEvent>, guardMap?: S.GuardMap<TContext, TEvent>): (context: TContext, event: TEvent) => S.TransitionDefinition<TContext, TState, TEvent>;
export declare function determineTransitionFn<TContext, TState extends S.StateSchema, TEvent extends S.EventObject>(transitions: S.Transitions<TContext, TState, TEvent> | undefined, guardMap: S.GuardMap<TContext, TEvent>): (context: TContext, event: TEvent) => S.TransitionDefinition<TContext, TState, TEvent>;
export declare function toTransition<TContext, TState extends S.StateSchema, TEvent extends S.EventObject>(transition: S.Transitions<TContext, TState, TEvent> | undefined, current?: TState["value"] | null): S.TransitionDefinition<TContext, TState, TEvent> | S.TransitionDefinition<TContext, TState, TEvent>[];
//# sourceMappingURL=transition-utils.d.ts.map
import type { StateMachine as S } from "./types";
export declare function toEvent<T extends S.EventObject>(event: S.Event<T>): T;
export declare function toArray<T>(value: T | T[] | undefined): T[];
export declare function isGuardHelper(value: any): value is {
predicate: Function;
};
//# sourceMappingURL=utils.d.ts.map
{
"name": "@zag-js/core",
"version": "0.0.0-dev-20220424123138",
"version": "0.0.0-dev-20220425073521",
"description": "A minimal implementation of xstate fsm for UI machines",

@@ -40,3 +40,3 @@ "keywords": [

"dependencies": {
"@zag-js/utils": "^0.0.0-dev-20220424123138",
"@zag-js/utils": "^0.0.0-dev-20220425073521",
"klona": "^2.0.5",

@@ -43,0 +43,0 @@ "valtio": "^1.5.2"

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc