123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { __assign } from './_virtual/_tslib.js';
- import { symbolObservable, toInvokeSource, mapContext, isMachine } from './utils.js';
- import { provide } from './serviceScope.js';
- function createNullActor(id) {
- var _a;
- return _a = {
- id: id,
- send: function () {
- return void 0;
- },
- subscribe: function () {
- return {
- unsubscribe: function () {
- return void 0;
- }
- };
- },
- getSnapshot: function () {
- return undefined;
- },
- toJSON: function () {
- return {
- id: id
- };
- }
- }, _a[symbolObservable] = function () {
- return this;
- }, _a;
- }
- /**
- * Creates a deferred actor that is able to be invoked given the provided
- * invocation information in its `.meta` value.
- *
- * @param invokeDefinition The meta information needed to invoke the actor.
- */
- function createInvocableActor(invokeDefinition, machine, context, _event) {
- var _a;
- var invokeSrc = toInvokeSource(invokeDefinition.src);
- var serviceCreator = (_a = machine === null || machine === void 0 ? void 0 : machine.options.services) === null || _a === void 0 ? void 0 : _a[invokeSrc.type];
- var resolvedData = invokeDefinition.data ? mapContext(invokeDefinition.data, context, _event) : undefined;
- var tempActor = serviceCreator ? createDeferredActor(serviceCreator, invokeDefinition.id, resolvedData) : createNullActor(invokeDefinition.id); // @ts-ignore
- tempActor.meta = invokeDefinition;
- return tempActor;
- }
- function createDeferredActor(entity, id, data) {
- var tempActor = createNullActor(id); // @ts-ignore
- tempActor.deferred = true;
- if (isMachine(entity)) {
- // "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here
- var initialState_1 = tempActor.state = provide(undefined, function () {
- return (data ? entity.withContext(data) : entity).initialState;
- });
- tempActor.getSnapshot = function () {
- return initialState_1;
- };
- }
- return tempActor;
- }
- function isActor(item) {
- try {
- return typeof item.send === 'function';
- } catch (e) {
- return false;
- }
- }
- function isSpawnedActor(item) {
- return isActor(item) && 'id' in item;
- } // TODO: refactor the return type, this could be written in a better way but it's best to avoid unneccessary breaking changes now
- function toActorRef(actorRefLike) {
- var _a;
- return __assign((_a = {
- subscribe: function () {
- return {
- unsubscribe: function () {
- return void 0;
- }
- };
- },
- id: 'anonymous',
- getSnapshot: function () {
- return undefined;
- }
- }, _a[symbolObservable] = function () {
- return this;
- }, _a), actorRefLike);
- }
- export { createDeferredActor, createInvocableActor, createNullActor, isActor, isSpawnedActor, toActorRef };
|